Skip to content

Commit 944673f

Browse files
authored
Fix slow enum cases union with lots of cases
1 parent 0f2366b commit 944673f

File tree

4 files changed

+9410
-2
lines changed

4 files changed

+9410
-2
lines changed

phpstan-baseline.neon

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1597,6 +1597,11 @@ parameters:
15971597
count: 4
15981598
path: src/Type/TypeCombinator.php
15991599

1600+
-
1601+
message: "#^Doing instanceof PHPStan\\\\Type\\\\Enum\\\\EnumCaseObjectType is error\\-prone and deprecated\\. Use Type\\:\\:getEnumCases\\(\\) instead\\.$#"
1602+
count: 1
1603+
path: src/Type/TypeCombinator.php
1604+
16001605
-
16011606
message: "#^Doing instanceof PHPStan\\\\Type\\\\FloatType is error\\-prone and deprecated\\. Use Type\\:\\:isFloat\\(\\) instead\\.$#"
16021607
count: 1

src/Type/TypeCombinator.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
use PHPStan\Type\Constant\ConstantFloatType;
1717
use PHPStan\Type\Constant\ConstantIntegerType;
1818
use PHPStan\Type\Constant\ConstantStringType;
19+
use PHPStan\Type\Enum\EnumCaseObjectType;
1920
use PHPStan\Type\Generic\GenericClassStringType;
2021
use PHPStan\Type\Generic\TemplateArrayType;
2122
use PHPStan\Type\Generic\TemplateBenevolentUnionType;
@@ -200,8 +201,7 @@ public static function union(Type ...$types): Type
200201
if ($types[$i] instanceof StringType && !$types[$i] instanceof ClassStringType) {
201202
$hasGenericScalarTypes[ConstantStringType::class] = true;
202203
}
203-
$enumCases = $types[$i]->getEnumCases();
204-
if (count($enumCases) === 1) {
204+
if ($types[$i] instanceof EnumCaseObjectType) {
205205
$enumCaseTypes[$types[$i]->describe(VerbosityLevel::cache())] = $types[$i];
206206

207207
unset($types[$i]);

tests/PHPStan/Analyser/AnalyserIntegrationTest.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1299,6 +1299,16 @@ public function testBug10538(): void
12991299
$this->assertNoErrors($errors);
13001300
}
13011301

1302+
public function testBug10772(): void
1303+
{
1304+
if (PHP_VERSION_ID < 80100) {
1305+
$this->markTestSkipped('Test requires PHP 8.1.');
1306+
}
1307+
1308+
$errors = $this->runAnalyse(__DIR__ . '/data/bug-10772.php');
1309+
$this->assertNoErrors($errors);
1310+
}
1311+
13021312
/**
13031313
* @param string[]|null $allAnalysedFiles
13041314
* @return Error[]

0 commit comments

Comments
 (0)