Skip to content

Commit b60d12c

Browse files
schlndhondrejmirtes
authored andcommitted
fix spaceship operator for large constant unions
1 parent 4e1bfac commit b60d12c

File tree

3 files changed

+12
-5
lines changed

3 files changed

+12
-5
lines changed

src/Reflection/InitializerExprTypeResolver.php

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -746,17 +746,13 @@ public function getSpaceshipType(Expr $left, Expr $right, callable $getTypeCallb
746746

747747
$leftTypesCount = count($leftTypes);
748748
$rightTypesCount = count($rightTypes);
749-
if ($leftTypesCount > 0 && $rightTypesCount > 0) {
749+
if ($leftTypesCount > 0 && $rightTypesCount > 0 && $leftTypesCount * $rightTypesCount <= self::CALCULATE_SCALARS_LIMIT) {
750750
$resultTypes = [];
751-
$generalize = $leftTypesCount * $rightTypesCount > self::CALCULATE_SCALARS_LIMIT;
752751
foreach ($leftTypes as $leftType) {
753752
foreach ($rightTypes as $rightType) {
754753
$leftValue = $leftType->getValue();
755754
$rightValue = $rightType->getValue();
756755
$resultType = $this->getTypeFromValue($leftValue <=> $rightValue);
757-
if ($generalize) {
758-
$resultType = $resultType->generalize(GeneralizePrecision::lessSpecific());
759-
}
760756
$resultTypes[] = $resultType;
761757
}
762758
}

tests/PHPStan/Rules/Functions/ArrowFunctionReturnTypeRuleTest.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,4 +60,9 @@ public function testBug8179(): void
6060
$this->analyse([__DIR__ . '/data/bug-8179.php'], []);
6161
}
6262

63+
public function testBugSpaceship(): void
64+
{
65+
$this->analyse([__DIR__ . '/data/bug-spaceship.php'], []);
66+
}
67+
6368
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<?php declare(strict_types=1);
2+
3+
namespace BugSpaceship;
4+
5+
$arr = range(1, 16);
6+
usort($arr, static fn (int $a, int $b): int => $a <=> $b);

0 commit comments

Comments
 (0)