5
5
use PhpParser \Node \Expr \FuncCall ;
6
6
use PHPStan \Analyser \Scope ;
7
7
use PHPStan \Reflection \FunctionReflection ;
8
- use PHPStan \Reflection \ParametersAcceptorSelector ;
9
- use PHPStan \Type \Constant \ConstantIntegerType ;
10
8
use PHPStan \Type \DynamicFunctionReturnTypeExtension ;
11
9
use PHPStan \Type \NullType ;
12
10
use PHPStan \Type \Type ;
13
11
use PHPStan \Type \TypeCombinator ;
14
- use function count ;
15
12
16
13
class ArrayKeyFirstDynamicReturnTypeExtension implements DynamicFunctionReturnTypeExtension
17
14
{
@@ -21,10 +18,10 @@ public function isFunctionSupported(FunctionReflection $functionReflection): boo
21
18
return $ functionReflection ->getName () === 'array_key_first ' ;
22
19
}
23
20
24
- public function getTypeFromFunctionCall (FunctionReflection $ functionReflection , FuncCall $ functionCall , Scope $ scope ): Type
21
+ public function getTypeFromFunctionCall (FunctionReflection $ functionReflection , FuncCall $ functionCall , Scope $ scope ): ? Type
25
22
{
26
23
if (!isset ($ functionCall ->getArgs ()[0 ])) {
27
- return ParametersAcceptorSelector:: selectSingle ( $ functionReflection -> getVariants ())-> getReturnType () ;
24
+ return null ;
28
25
}
29
26
30
27
$ argType = $ scope ->getType ($ functionCall ->getArgs ()[0 ]->value );
@@ -33,30 +30,7 @@ public function getTypeFromFunctionCall(FunctionReflection $functionReflection,
33
30
return new NullType ();
34
31
}
35
32
36
- $ constantArrays = $ argType ->getConstantArrays ();
37
- if (count ($ constantArrays ) > 0 ) {
38
- $ keyTypes = [];
39
- foreach ($ constantArrays as $ constantArray ) {
40
- $ iterableAtLeastOnce = $ constantArray ->isIterableAtLeastOnce ();
41
- if (!$ iterableAtLeastOnce ->yes ()) {
42
- $ keyTypes [] = new NullType ();
43
- }
44
- if ($ iterableAtLeastOnce ->no ()) {
45
- continue ;
46
- }
47
-
48
- $ keyTypes [] = $ constantArray ->getFirstKeyType ();
49
- }
50
-
51
- return TypeCombinator::union (...$ keyTypes );
52
- }
53
-
54
- if ($ argType ->isList ()->yes ()) {
55
- $ keyType = new ConstantIntegerType (0 );
56
- } else {
57
- $ keyType = $ argType ->getIterableKeyType ();
58
- }
59
-
33
+ $ keyType = $ argType ->getFirstIterableKeyType ();
60
34
if ($ iterableAtLeastOnce ->yes ()) {
61
35
return $ keyType ;
62
36
}
0 commit comments