|
3 | 3 | namespace PHPStan\Type\Constant;
|
4 | 4 |
|
5 | 5 | use Nette\Utils\Strings;
|
| 6 | +use PHPStan\Internal\CombinationsHelper; |
6 | 7 | use PHPStan\Php\PhpVersion;
|
7 | 8 | use PHPStan\PhpDocParser\Ast\ConstExpr\ConstExprIntegerNode;
|
8 | 9 | use PHPStan\PhpDocParser\Ast\ConstExpr\ConstExprStringNode;
|
|
13 | 14 | use PHPStan\PhpDocParser\Ast\Type\TypeNode;
|
14 | 15 | use PHPStan\Reflection\ClassMemberAccessAnswerer;
|
15 | 16 | use PHPStan\Reflection\InaccessibleMethod;
|
| 17 | +use PHPStan\Reflection\InitializerExprTypeResolver; |
16 | 18 | use PHPStan\Reflection\ParametersAcceptor;
|
17 | 19 | use PHPStan\Reflection\TrivialParametersAcceptor;
|
18 | 20 | use PHPStan\ShouldNotHappenException;
|
@@ -1606,6 +1608,44 @@ public static function isValidIdentifier(string $value): bool
|
1606 | 1608 | return $result !== null;
|
1607 | 1609 | }
|
1608 | 1610 |
|
| 1611 | + public function getFiniteTypes(): array |
| 1612 | + { |
| 1613 | + $arraysArraysForCombinations = []; |
| 1614 | + $count = 0; |
| 1615 | + foreach ($this->getAllArrays() as $array) { |
| 1616 | + $values = $array->getValueTypes(); |
| 1617 | + $arraysForCombinations = []; |
| 1618 | + $combinationCount = 1; |
| 1619 | + foreach ($values as $valueType) { |
| 1620 | + if ($valueType->getFiniteTypes() === []) { |
| 1621 | + return []; |
| 1622 | + } |
| 1623 | + $arraysForCombinations[] = $valueType->getFiniteTypes(); |
| 1624 | + $combinationCount *= count($valueType->getFiniteTypes()); |
| 1625 | + } |
| 1626 | + $arraysArraysForCombinations[] = $arraysForCombinations; |
| 1627 | + $count += $combinationCount; |
| 1628 | + } |
| 1629 | + |
| 1630 | + if ($count > InitializerExprTypeResolver::CALCULATE_SCALARS_LIMIT) { |
| 1631 | + return []; |
| 1632 | + } |
| 1633 | + |
| 1634 | + $finiteTypes = []; |
| 1635 | + foreach ($arraysArraysForCombinations as $arraysForCombinations) { |
| 1636 | + $combinations = CombinationsHelper::combinations($arraysForCombinations); |
| 1637 | + foreach ($combinations as $combination) { |
| 1638 | + $builder = ConstantArrayTypeBuilder::createEmpty(); |
| 1639 | + foreach ($combination as $i => $v) { |
| 1640 | + $builder->setOffsetValueType($this->keyTypes[$i], $v); |
| 1641 | + } |
| 1642 | + $finiteTypes[] = $builder->getArray(); |
| 1643 | + } |
| 1644 | + } |
| 1645 | + |
| 1646 | + return $finiteTypes; |
| 1647 | + } |
| 1648 | + |
1609 | 1649 | /**
|
1610 | 1650 | * @param mixed[] $properties
|
1611 | 1651 | */
|
|
0 commit comments