2020use PHPStan \Reflection \MethodReflection ;
2121use PHPStan \Type \Accessory \AccessoryArrayListType ;
2222use PHPStan \Type \ArrayType ;
23+ use PHPStan \Type \Constant \ConstantArrayType ;
2324use PHPStan \Type \DynamicMethodReturnTypeExtension ;
2425use PHPStan \Type \IntegerType ;
2526use PHPStan \Type \IntersectionType ;
@@ -49,11 +50,11 @@ public function getTypeFromMethodCall(MethodReflection $methodReflection, Method
4950 $ methodName = $ methodReflection ->getName ();
5051
5152 if ($ methodName === 'find ' ) {
52- return $ this ->getTypeFromFind ($ methodReflection , $ methodCall , $ scope );
53+ return $ this ->getTypeFromFind ($ methodCall , $ scope );
5354 }
5455
5556 if ($ methodName === 'findAll ' ) {
56- return $ this ->getTypeFromFindAll ($ methodReflection , $ methodCall , $ scope );
57+ return $ this ->getTypeFromFindAll ($ methodCall , $ scope );
5758 }
5859
5960 $ classReflection = $ this ->getClassReflection ($ methodCall , $ scope );
@@ -69,23 +70,23 @@ private function getClassReflection(MethodCall $methodCall, Scope $scope): Class
6970 return current ($ classTypes );
7071 }
7172
72- private function getTypeFromFind (MethodReflection $ methodReflection , MethodCall $ methodCall , Scope $ scope ): Type
73+ private function getTypeFromFind (MethodCall $ methodCall , Scope $ scope ): Type
7374 {
7475 $ args = $ methodCall ->getArgs ();
7576
7677 if (! isset ($ args [0 ])) {
77- return $ this ->getTypeFromFindAll ($ methodReflection , $ methodCall , $ scope );
78+ return $ this ->getTypeFromFindAll ($ methodCall , $ scope );
7879 }
7980
8081 return TypeTraverser::map (
8182 $ 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 {
8384 if ($ idType instanceof UnionType || $ idType instanceof IntersectionType) {
8485 return $ traverse ($ idType );
8586 }
8687
87- if ($ idType ->isNull ()->yes ()) {
88- return $ this -> getTypeFromFindAll ( $ methodReflection , $ methodCall , $ scope );
88+ if ($ idType ->isArray ()-> yes () && ! $ idType -> isIterableAtLeastOnce ()->yes ()) {
89+ return new ConstantArrayType ([], [] );
8990 }
9091
9192 if ($ idType ->isInteger ()->yes () || $ idType ->isString ()->yes ()) {
@@ -94,12 +95,12 @@ function (Type $idType, callable $traverse) use ($methodReflection, $methodCall,
9495 return TypeCombinator::addNull ($ this ->modelFetchedReturnTypeHelper ->getFetchedReturnType ($ classReflection , $ methodCall , $ scope ));
9596 }
9697
97- return $ this ->getTypeFromFindAll ($ methodReflection , $ methodCall , $ scope );
98+ return $ this ->getTypeFromFindAll ($ methodCall , $ scope );
9899 }
99100 );
100101 }
101102
102- private function getTypeFromFindAll (MethodReflection $ methodReflection , MethodCall $ methodCall , Scope $ scope ): Type
103+ private function getTypeFromFindAll (MethodCall $ methodCall , Scope $ scope ): Type
103104 {
104105 $ classReflection = $ this ->getClassReflection ($ methodCall , $ scope );
105106
0 commit comments