|
22 | 22 | use PHPStan\Type\ArrayType;
|
23 | 23 | use PHPStan\Type\DynamicMethodReturnTypeExtension;
|
24 | 24 | use PHPStan\Type\IntegerType;
|
| 25 | +use PHPStan\Type\IntersectionType; |
25 | 26 | use PHPStan\Type\Type;
|
26 | 27 | use PHPStan\Type\TypeCombinator;
|
| 28 | +use PHPStan\Type\TypeTraverser; |
| 29 | +use PHPStan\Type\UnionType; |
27 | 30 |
|
28 | 31 | final class ModelFindReturnTypeExtension implements DynamicMethodReturnTypeExtension
|
29 | 32 | {
|
@@ -74,19 +77,26 @@ private function getTypeFromFind(MethodReflection $methodReflection, MethodCall
|
74 | 77 | return $this->getTypeFromFindAll($methodReflection, $methodCall, $scope);
|
75 | 78 | }
|
76 | 79 |
|
77 |
| - $idType = $scope->getType($args[0]->value); |
| 80 | + return TypeTraverser::map( |
| 81 | + $scope->getType($args[0]->value), |
| 82 | + function (Type $idType, callable $traverse) use ($methodReflection, $methodCall, $scope): Type { |
| 83 | + if ($idType instanceof UnionType || $idType instanceof IntersectionType) { |
| 84 | + return $traverse($idType); |
| 85 | + } |
78 | 86 |
|
79 |
| - if ($idType->isNull()->yes()) { |
80 |
| - return $this->getTypeFromFindAll($methodReflection, $methodCall, $scope); |
81 |
| - } |
| 87 | + if ($idType->isNull()->yes()) { |
| 88 | + return $this->getTypeFromFindAll($methodReflection, $methodCall, $scope); |
| 89 | + } |
82 | 90 |
|
83 |
| - if ($idType->isInteger()->yes() || $idType->isString()->yes()) { |
84 |
| - $classReflection = $this->getClassReflection($methodCall, $scope); |
| 91 | + if ($idType->isInteger()->yes() || $idType->isString()->yes()) { |
| 92 | + $classReflection = $this->getClassReflection($methodCall, $scope); |
85 | 93 |
|
86 |
| - return TypeCombinator::addNull($this->modelFetchedReturnTypeHelper->getFetchedReturnType($classReflection, $scope)); |
87 |
| - } |
| 94 | + return TypeCombinator::addNull($this->modelFetchedReturnTypeHelper->getFetchedReturnType($classReflection, $scope)); |
| 95 | + } |
88 | 96 |
|
89 |
| - return $this->getTypeFromFindAll($methodReflection, $methodCall, $scope); |
| 97 | + return $this->getTypeFromFindAll($methodReflection, $methodCall, $scope); |
| 98 | + } |
| 99 | + ); |
90 | 100 | }
|
91 | 101 |
|
92 | 102 | private function getTypeFromFindAll(MethodReflection $methodReflection, MethodCall $methodCall, Scope $scope): Type
|
|
0 commit comments