Skip to content

Commit

Permalink
Fix slow enum cases union with lots of cases
Browse files Browse the repository at this point in the history
  • Loading branch information
staabm committed Mar 22, 2024
1 parent 0f2366b commit 944673f
Show file tree
Hide file tree
Showing 4 changed files with 9,410 additions and 2 deletions.
5 changes: 5 additions & 0 deletions phpstan-baseline.neon
Original file line number Diff line number Diff line change
Expand Up @@ -1597,6 +1597,11 @@ parameters:
count: 4
path: src/Type/TypeCombinator.php

-
message: "#^Doing instanceof PHPStan\\\\Type\\\\Enum\\\\EnumCaseObjectType is error\\-prone and deprecated\\. Use Type\\:\\:getEnumCases\\(\\) instead\\.$#"
count: 1
path: src/Type/TypeCombinator.php

-
message: "#^Doing instanceof PHPStan\\\\Type\\\\FloatType is error\\-prone and deprecated\\. Use Type\\:\\:isFloat\\(\\) instead\\.$#"
count: 1
Expand Down
4 changes: 2 additions & 2 deletions src/Type/TypeCombinator.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
use PHPStan\Type\Constant\ConstantFloatType;
use PHPStan\Type\Constant\ConstantIntegerType;
use PHPStan\Type\Constant\ConstantStringType;
use PHPStan\Type\Enum\EnumCaseObjectType;
use PHPStan\Type\Generic\GenericClassStringType;
use PHPStan\Type\Generic\TemplateArrayType;
use PHPStan\Type\Generic\TemplateBenevolentUnionType;
Expand Down Expand Up @@ -200,8 +201,7 @@ public static function union(Type ...$types): Type
if ($types[$i] instanceof StringType && !$types[$i] instanceof ClassStringType) {
$hasGenericScalarTypes[ConstantStringType::class] = true;
}
$enumCases = $types[$i]->getEnumCases();
if (count($enumCases) === 1) {
if ($types[$i] instanceof EnumCaseObjectType) {
$enumCaseTypes[$types[$i]->describe(VerbosityLevel::cache())] = $types[$i];

unset($types[$i]);
Expand Down
10 changes: 10 additions & 0 deletions tests/PHPStan/Analyser/AnalyserIntegrationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1299,6 +1299,16 @@ public function testBug10538(): void
$this->assertNoErrors($errors);
}

public function testBug10772(): void
{
if (PHP_VERSION_ID < 80100) {
$this->markTestSkipped('Test requires PHP 8.1.');
}

$errors = $this->runAnalyse(__DIR__ . '/data/bug-10772.php');
$this->assertNoErrors($errors);
}

/**
* @param string[]|null $allAnalysedFiles
* @return Error[]
Expand Down

0 comments on commit 944673f

Please sign in to comment.