|
5 | 5 | use PhpParser\Node\Expr\FuncCall;
|
6 | 6 | use PHPStan\Analyser\Scope;
|
7 | 7 | use PHPStan\Reflection\FunctionReflection;
|
8 |
| -use PHPStan\ShouldNotHappenException; |
| 8 | +use PHPStan\TrinaryLogic; |
9 | 9 | use PHPStan\Type\Accessory\AccessoryArrayListType;
|
10 | 10 | use PHPStan\Type\Accessory\NonEmptyArrayType;
|
11 | 11 | use PHPStan\Type\ArrayType;
|
12 | 12 | use PHPStan\Type\Constant\ConstantArrayType;
|
13 | 13 | use PHPStan\Type\Constant\ConstantArrayTypeBuilder;
|
14 | 14 | use PHPStan\Type\Constant\ConstantIntegerType;
|
| 15 | +use PHPStan\Type\Constant\ConstantStringType; |
15 | 16 | use PHPStan\Type\DynamicFunctionReturnTypeExtension;
|
16 | 17 | use PHPStan\Type\IntegerType;
|
17 | 18 | use PHPStan\Type\NeverType;
|
@@ -39,58 +40,53 @@ public function getTypeFromFunctionCall(FunctionReflection $functionReflection,
|
39 | 40 |
|
40 | 41 | $argTypes = [];
|
41 | 42 | $optionalArgTypes = [];
|
42 |
| - $allConstant = true; |
43 | 43 | foreach ($args as $arg) {
|
44 | 44 | $argType = $scope->getType($arg->value);
|
45 | 45 |
|
46 | 46 | if ($arg->unpack) {
|
47 |
| - if ($argType instanceof ConstantArrayType) { |
48 |
| - $argTypesFound = $argType->getValueTypes(); |
49 |
| - } else { |
50 |
| - $argTypesFound = [$argType->getIterableValueType()]; |
51 |
| - } |
52 |
| - |
53 |
| - foreach ($argTypesFound as $argTypeFound) { |
54 |
| - $argTypes[] = $argTypeFound; |
55 |
| - if ($argTypeFound instanceof ConstantArrayType) { |
56 |
| - continue; |
| 47 | + if ($argType->isConstantArray()->yes()) { |
| 48 | + foreach ($argType->getConstantArrays() as $constantArray) { |
| 49 | + foreach ($constantArray->getValueTypes() as $valueType) { |
| 50 | + $argTypes[] = $valueType; |
| 51 | + } |
57 | 52 | }
|
58 |
| - $allConstant = false; |
| 53 | + } else { |
| 54 | + $argTypes[] = $argType->getIterableValueType(); |
59 | 55 | }
|
60 | 56 |
|
61 | 57 | if (!$argType->isIterableAtLeastOnce()->yes()) {
|
62 | 58 | // unpacked params can be empty, making them optional
|
63 | 59 | $optionalArgTypesOffset = count($argTypes) - 1;
|
64 |
| - foreach (array_keys($argTypesFound) as $key) { |
| 60 | + foreach (array_keys($argTypes) as $key) { |
65 | 61 | $optionalArgTypes[] = $optionalArgTypesOffset + $key;
|
66 | 62 | }
|
67 | 63 | }
|
68 | 64 | } else {
|
69 | 65 | $argTypes[] = $argType;
|
70 |
| - if (!$argType instanceof ConstantArrayType) { |
71 |
| - $allConstant = false; |
72 |
| - } |
73 | 66 | }
|
74 | 67 | }
|
75 | 68 |
|
76 |
| - if ($allConstant) { |
| 69 | + $allConstant = TrinaryLogic::createYes()->lazyAnd( |
| 70 | + $argTypes, |
| 71 | + static fn (Type $argType) => $argType->isConstantArray(), |
| 72 | + ); |
| 73 | + |
| 74 | + if ($allConstant->yes()) { |
77 | 75 | $newArrayBuilder = ConstantArrayTypeBuilder::createEmpty();
|
78 | 76 | foreach ($argTypes as $argType) {
|
79 |
| - if (!$argType instanceof ConstantArrayType) { |
80 |
| - throw new ShouldNotHappenException(); |
| 77 | + /** @var array<int|string, ConstantIntegerType|ConstantStringType> $keyTypes */ |
| 78 | + $keyTypes = []; |
| 79 | + foreach ($argType->getConstantArrays() as $constantArray) { |
| 80 | + foreach ($constantArray->getKeyTypes() as $keyType) { |
| 81 | + $keyTypes[$keyType->getValue()] = $keyType; |
| 82 | + } |
81 | 83 | }
|
82 | 84 |
|
83 |
| - $keyTypes = $argType->getKeyTypes(); |
84 |
| - $valueTypes = $argType->getValueTypes(); |
85 |
| - $optionalKeys = $argType->getOptionalKeys(); |
86 |
| - |
87 |
| - foreach ($keyTypes as $k => $keyType) { |
88 |
| - $isOptional = in_array($k, $optionalKeys, true); |
89 |
| - |
| 85 | + foreach ($keyTypes as $keyType) { |
90 | 86 | $newArrayBuilder->setOffsetValueType(
|
91 | 87 | $keyType instanceof ConstantIntegerType ? null : $keyType,
|
92 |
| - $valueTypes[$k], |
93 |
| - $isOptional, |
| 88 | + $argType->getOffsetValueType($keyType), |
| 89 | + !$argType->hasOffsetValueType($keyType)->yes(), |
94 | 90 | );
|
95 | 91 | }
|
96 | 92 | }
|
|
0 commit comments