20
20
use PHPStan \Reflection \MethodReflection ;
21
21
use PHPStan \Type \Accessory \AccessoryArrayListType ;
22
22
use PHPStan \Type \ArrayType ;
23
+ use PHPStan \Type \Constant \ConstantArrayType ;
23
24
use PHPStan \Type \DynamicMethodReturnTypeExtension ;
24
25
use PHPStan \Type \IntegerType ;
25
26
use PHPStan \Type \IntersectionType ;
@@ -49,11 +50,11 @@ public function getTypeFromMethodCall(MethodReflection $methodReflection, Method
49
50
$ methodName = $ methodReflection ->getName ();
50
51
51
52
if ($ methodName === 'find ' ) {
52
- return $ this ->getTypeFromFind ($ methodReflection , $ methodCall , $ scope );
53
+ return $ this ->getTypeFromFind ($ methodCall , $ scope );
53
54
}
54
55
55
56
if ($ methodName === 'findAll ' ) {
56
- return $ this ->getTypeFromFindAll ($ methodReflection , $ methodCall , $ scope );
57
+ return $ this ->getTypeFromFindAll ($ methodCall , $ scope );
57
58
}
58
59
59
60
$ classReflection = $ this ->getClassReflection ($ methodCall , $ scope );
@@ -69,23 +70,23 @@ private function getClassReflection(MethodCall $methodCall, Scope $scope): Class
69
70
return current ($ classTypes );
70
71
}
71
72
72
- private function getTypeFromFind (MethodReflection $ methodReflection , MethodCall $ methodCall , Scope $ scope ): Type
73
+ private function getTypeFromFind (MethodCall $ methodCall , Scope $ scope ): Type
73
74
{
74
75
$ args = $ methodCall ->getArgs ();
75
76
76
77
if (! isset ($ args [0 ])) {
77
- return $ this ->getTypeFromFindAll ($ methodReflection , $ methodCall , $ scope );
78
+ return $ this ->getTypeFromFindAll ($ methodCall , $ scope );
78
79
}
79
80
80
81
return TypeTraverser::map (
81
82
$ scope ->getType ($ args [0 ]->value ),
82
- function (Type $ idType , callable $ traverse ) use ($ methodReflection , $ methodCall , $ scope ): Type {
83
+ function (Type $ idType , callable $ traverse ) use ($ methodCall , $ scope ): Type {
83
84
if ($ idType instanceof UnionType || $ idType instanceof IntersectionType) {
84
85
return $ traverse ($ idType );
85
86
}
86
87
87
- if ($ idType ->isNull ()->yes ()) {
88
- return $ this -> getTypeFromFindAll ( $ methodReflection , $ methodCall , $ scope );
88
+ if ($ idType ->isArray ()-> yes () && ! $ idType -> isIterableAtLeastOnce ()->yes ()) {
89
+ return new ConstantArrayType ([], [] );
89
90
}
90
91
91
92
if ($ idType ->isInteger ()->yes () || $ idType ->isString ()->yes ()) {
@@ -94,12 +95,12 @@ function (Type $idType, callable $traverse) use ($methodReflection, $methodCall,
94
95
return TypeCombinator::addNull ($ this ->modelFetchedReturnTypeHelper ->getFetchedReturnType ($ classReflection , $ methodCall , $ scope ));
95
96
}
96
97
97
- return $ this ->getTypeFromFindAll ($ methodReflection , $ methodCall , $ scope );
98
+ return $ this ->getTypeFromFindAll ($ methodCall , $ scope );
98
99
}
99
100
);
100
101
}
101
102
102
- private function getTypeFromFindAll (MethodReflection $ methodReflection , MethodCall $ methodCall , Scope $ scope ): Type
103
+ private function getTypeFromFindAll (MethodCall $ methodCall , Scope $ scope ): Type
103
104
{
104
105
$ classReflection = $ this ->getClassReflection ($ methodCall , $ scope );
105
106
0 commit comments