6
6
use PHPStan \Analyser \Scope ;
7
7
use PHPStan \Reflection \MethodReflection ;
8
8
use PHPStan \Type \ArrayType ;
9
+ use PHPStan \Type \BooleanType ;
9
10
use PHPStan \Type \DynamicMethodReturnTypeExtension ;
10
11
use PHPStan \Type \MixedType ;
11
12
use PHPStan \Type \ObjectType ;
12
13
use PHPStan \Type \StringType ;
13
14
use PHPStan \Type \Type ;
15
+ use PHPStan \Type \Constant \ConstantStringType ;
16
+ use PHPStan \Type \ClassStringType ;
14
17
use function count ;
15
18
16
19
final class FormContainerValuesDynamicReturnTypeExtension implements DynamicMethodReturnTypeExtension
17
20
{
18
-
19
21
public function getClass (): string
20
22
{
21
23
return 'Nette\Forms\Container ' ;
@@ -28,20 +30,43 @@ public function isMethodSupported(MethodReflection $methodReflection): bool
28
30
29
31
public function getTypeFromMethodCall (MethodReflection $ methodReflection , MethodCall $ methodCall , Scope $ scope ): ?Type
30
32
{
31
- if (count ($ methodCall ->getArgs ()) === 0 ) {
33
+ $ args = $ methodCall ->getArgs ();
34
+
35
+ // No argument => default object
36
+ if (count ($ args ) === 0 ) {
32
37
return new ObjectType ('Nette\Utils\ArrayHash ' );
33
38
}
34
39
35
- $ arg = $ methodCall -> getArgs () [0 ]->value ;
40
+ $ arg = $ args [0 ]->value ;
36
41
$ scopedType = $ scope ->getType ($ arg );
37
- if ($ scopedType ->isTrue ()->yes ()) {
38
- return new ArrayType (new StringType (), new MixedType ());
42
+
43
+ if ($ scopedType instanceof BooleanType) {
44
+ if ($ scopedType ->isTrue ()->yes ()) {
45
+ return new ArrayType (new StringType (), new MixedType ());
46
+ }
47
+
48
+ // boolean false → object
49
+ if ($ scopedType ->isFalse ()->yes ()) {
50
+ return new ObjectType ('Nette\Utils\ArrayHash ' );
51
+ }
39
52
}
40
- if ($ scopedType ->isFalse ()->yes ()) {
41
- return new ObjectType ('Nette\Utils\ArrayHash ' );
53
+
54
+ if ($ scopedType instanceof ConstantStringType) {
55
+ $ value = $ scopedType ->getValue ();
56
+
57
+ if ($ scopedType ->isClassString ()->yes ()) {
58
+ return $ scopedType ->getClassStringObjectType ();
59
+ }
60
+
61
+ if ($ value === 'array ' ) {
62
+ return new ArrayType (new StringType (), new MixedType ());
63
+ }
64
+
65
+ if ($ value === 'object ' ) {
66
+ return new ObjectType ('Nette\Utils\ArrayHash ' );
67
+ }
42
68
}
43
69
44
- return null ;
70
+ return new ObjectType ( ' Nette\Utils\ArrayHash ' ) ;
45
71
}
46
-
47
72
}
0 commit comments