Skip to content

Commit

Permalink
fix spaceship operator for large constant unions
Browse files Browse the repository at this point in the history
  • Loading branch information
schlndh authored and ondrejmirtes committed Jan 21, 2024
1 parent 4e1bfac commit b60d12c
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 5 deletions.
6 changes: 1 addition & 5 deletions src/Reflection/InitializerExprTypeResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -746,17 +746,13 @@ public function getSpaceshipType(Expr $left, Expr $right, callable $getTypeCallb

$leftTypesCount = count($leftTypes);
$rightTypesCount = count($rightTypes);
if ($leftTypesCount > 0 && $rightTypesCount > 0) {
if ($leftTypesCount > 0 && $rightTypesCount > 0 && $leftTypesCount * $rightTypesCount <= self::CALCULATE_SCALARS_LIMIT) {
$resultTypes = [];
$generalize = $leftTypesCount * $rightTypesCount > self::CALCULATE_SCALARS_LIMIT;
foreach ($leftTypes as $leftType) {
foreach ($rightTypes as $rightType) {
$leftValue = $leftType->getValue();
$rightValue = $rightType->getValue();
$resultType = $this->getTypeFromValue($leftValue <=> $rightValue);
if ($generalize) {
$resultType = $resultType->generalize(GeneralizePrecision::lessSpecific());
}
$resultTypes[] = $resultType;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,4 +60,9 @@ public function testBug8179(): void
$this->analyse([__DIR__ . '/data/bug-8179.php'], []);
}

public function testBugSpaceship(): void
{
$this->analyse([__DIR__ . '/data/bug-spaceship.php'], []);
}

}
6 changes: 6 additions & 0 deletions tests/PHPStan/Rules/Functions/data/bug-spaceship.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?php declare(strict_types=1);

namespace BugSpaceship;

$arr = range(1, 16);
usort($arr, static fn (int $a, int $b): int => $a <=> $b);

0 comments on commit b60d12c

Please sign in to comment.