Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix spaceship operator for large constant unions #2886

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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);