|
5 | 5 | use PhpParser\Node\Expr; |
6 | 6 | use PhpParser\Node\Expr\FuncCall; |
7 | 7 | use PhpParser\Node\Expr\Variable; |
| 8 | +use PhpParser\Node\Stmt; |
8 | 9 | use PHPStan\Analyser\Scope; |
9 | 10 | use PHPStan\Analyser\SpecifiedTypes; |
10 | 11 | use PHPStan\Analyser\TypeSpecifier; |
@@ -41,44 +42,45 @@ public function specifyTypes(FunctionReflection $functionReflection, FuncCall $n |
41 | 42 | } |
42 | 43 |
|
43 | 44 | $array = $args[0]->value; |
44 | | - $arrayArgType = $scope->getType($array); |
45 | | - $arrayTypes = $arrayArgType->getArrays(); |
46 | | - |
47 | | - if (count($arrayTypes) === 0) { |
| 45 | + $callable = $args[1]->value; |
| 46 | + if ($callable instanceof Expr\ArrowFunction) { |
| 47 | + $callableExpr = $callable->expr; |
| 48 | + } elseif ( |
| 49 | + $callable instanceof Expr\Closure && |
| 50 | + count($callable->stmts) === 1 && |
| 51 | + $callable->stmts[0] instanceof Stmt\Return_ |
| 52 | + ) { |
| 53 | + $callableExpr = $callable->stmts[0]->expr; |
| 54 | + } else { |
48 | 55 | return new SpecifiedTypes(); |
49 | 56 | } |
50 | 57 |
|
51 | | - $callable = $args[1]->value; |
| 58 | + $callableParams = $callable->params; |
| 59 | + $specifiedTypesInFuncCall = $this->typeSpecifier->specifyTypesInCondition($scope, $callableExpr, $context)->getSureTypes(); |
52 | 60 |
|
53 | | - if ($callable instanceof Expr\ArrowFunction) { |
| 61 | + if ( |
| 62 | + isset($callableParams[0]) && |
| 63 | + $callableParams[0]->var instanceof Variable && |
| 64 | + is_string($callableParams[0]->var->name) |
| 65 | + ) { |
| 66 | + $valueType = $this->fetchTypeByVariable($specifiedTypesInFuncCall, $callableParams[0]->var->name); |
| 67 | + } |
| 68 | + |
| 69 | + if ( |
| 70 | + isset($callableParams[1]) && |
| 71 | + $callableParams[1]->var instanceof Variable && |
| 72 | + is_string($callableParams[1]->var->name) |
| 73 | + ) { |
| 74 | + $keyType = $this->fetchTypeByVariable($specifiedTypesInFuncCall, $callableParams[1]->var->name); |
| 75 | + } |
54 | 76 |
|
55 | | - $callableParams = $callable->params; |
56 | | - $specifiedTypesInFuncCall = $this->typeSpecifier->specifyTypesInCondition($scope, $callable->expr, $context)->getSureTypes(); |
57 | | - |
58 | | - if ( |
59 | | - isset($callableParams[0]) && |
60 | | - $callableParams[0]->var instanceof Variable && |
61 | | - is_string($callableParams[0]->var->name) |
62 | | - ) { |
63 | | - $valueType = $this->fetchTypeByVariable($specifiedTypesInFuncCall, $callableParams[0]->var->name); |
64 | | - } |
65 | | - |
66 | | - if ( |
67 | | - isset($callableParams[1]) && |
68 | | - $callableParams[1]->var instanceof Variable && |
69 | | - is_string($callableParams[1]->var->name) |
70 | | - ) { |
71 | | - $keyType = $this->fetchTypeByVariable($specifiedTypesInFuncCall, $callableParams[1]->var->name); |
72 | | - } |
73 | | - |
74 | | - if (isset($keyType) || isset($valueType)) { |
75 | | - return $this->typeSpecifier->create( |
76 | | - $array, |
77 | | - new ArrayType($keyType ?? new MixedType(), $valueType ?? new MixedType()), |
78 | | - $context, |
79 | | - $scope, |
80 | | - ); |
81 | | - } |
| 77 | + if (isset($keyType) || isset($valueType)) { |
| 78 | + return $this->typeSpecifier->create( |
| 79 | + $array, |
| 80 | + new ArrayType($keyType ?? new MixedType(), $valueType ?? new MixedType()), |
| 81 | + $context, |
| 82 | + $scope, |
| 83 | + ); |
82 | 84 | } |
83 | 85 |
|
84 | 86 | return new SpecifiedTypes(); |
|
0 commit comments