Skip to content

Commit 843be53

Browse files
authored
Faster analysis with a big const array in a class
1 parent 692d510 commit 843be53

File tree

3 files changed

+2695
-0
lines changed

3 files changed

+2695
-0
lines changed

src/Type/TypeCombinator.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -897,6 +897,10 @@ private static function optimizeConstantArrays(array $types): array
897897
$keyType = self::union(...$keyTypes);
898898
$valueType = self::union(...$valueTypes);
899899

900+
if ($valueType instanceof UnionType && count($valueType->getTypes()) > ConstantArrayTypeBuilder::ARRAY_COUNT_LIMIT) {
901+
$valueType = $valueType->generalize(GeneralizePrecision::lessSpecific());
902+
}
903+
900904
$arrayType = new ArrayType($keyType, $valueType);
901905
if ($eachIsList) {
902906
$arrayType = self::intersect($arrayType, new AccessoryArrayListType());

tests/PHPStan/Analyser/AnalyserIntegrationTest.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1510,6 +1510,16 @@ public function testBug12627(): void
15101510
$this->assertNoErrors($errors);
15111511
}
15121512

1513+
public function testBug12159(): void
1514+
{
1515+
if (PHP_VERSION_ID < 80300) {
1516+
$this->markTestSkipped('Test requires PHP 8.3.');
1517+
}
1518+
1519+
$errors = $this->runAnalyse(__DIR__ . '/data/bug-12159.php');
1520+
$this->assertNoErrors($errors);
1521+
}
1522+
15131523
/**
15141524
* @param string[]|null $allAnalysedFiles
15151525
* @return Error[]

0 commit comments

Comments
 (0)