|
4 | 4 |
|
5 | 5 | use PhpParser\Node\Expr\MethodCall; |
6 | 6 | use PHPStan\Analyser\Scope; |
| 7 | +use PHPStan\PhpDoc\Tag\ExtendsTag; |
7 | 8 | use PHPStan\Reflection\MethodReflection; |
8 | 9 | use PHPStan\Reflection\ParametersAcceptorSelector; |
9 | 10 | use PHPStan\Type\DynamicMethodReturnTypeExtension; |
| 11 | +use PHPStan\Type\ErrorType; |
10 | 12 | use PHPStan\Type\Generic\GenericObjectType; |
| 13 | +use PHPStan\Type\Generic\TemplateType; |
11 | 14 | use PHPStan\Type\ObjectType; |
12 | 15 | use PHPStan\Type\Type; |
| 16 | +use PHPStan\Type\TypeTraverser; |
13 | 17 | use PHPStan\Type\TypeWithClassName; |
14 | 18 | use SaschaEgerer\PhpstanTypo3\Helpers\Typo3ClassNamingUtilityTrait; |
| 19 | +use TYPO3\CMS\Extbase\DomainObject\DomainObjectInterface; |
15 | 20 | use TYPO3\CMS\Extbase\Persistence\QueryResultInterface; |
16 | 21 | use TYPO3\CMS\Extbase\Persistence\Repository; |
17 | 22 |
|
@@ -39,18 +44,73 @@ public function getTypeFromMethodCall( |
39 | 44 | ): Type |
40 | 45 | { |
41 | 46 | $variableType = $scope->getType($methodCall->var); |
| 47 | + $methodReturnType = ParametersAcceptorSelector::selectSingle($methodReflection->getVariants())->getReturnType(); |
| 48 | + if (!$variableType instanceof TypeWithClassName) { |
| 49 | + return $methodReturnType; |
| 50 | + } |
42 | 51 |
|
43 | | - if (!$variableType instanceof TypeWithClassName |
44 | | - || $methodReflection->getDeclaringClass()->getName() !== Repository::class) { |
45 | | - return ParametersAcceptorSelector::selectSingle($methodReflection->getVariants())->getReturnType(); |
| 52 | + $methodReturnTypeGeneric = $this->getGenericTypes($methodReturnType)[0] ?? null; |
| 53 | + if ( |
| 54 | + $methodReturnTypeGeneric instanceof GenericObjectType && |
| 55 | + ($methodReturnTypeGeneric->getTypes()[0] ?? null) instanceof ObjectType && |
| 56 | + $methodReturnTypeGeneric->getTypes()[0]->getClassName() !== DomainObjectInterface::class |
| 57 | + ) { |
| 58 | + if ($methodReflection->getDeclaringClass()->getName() !== Repository::class) { |
| 59 | + return $methodReturnType; |
| 60 | + } |
| 61 | + return $methodReturnTypeGeneric; |
46 | 62 | } |
47 | 63 |
|
48 | 64 | /** @var class-string $className */ |
49 | 65 | $className = $variableType->getClassName(); |
50 | 66 |
|
| 67 | + // if we have a custom findAll method... |
| 68 | + if ($methodReflection->getDeclaringClass()->getName() !== Repository::class) { |
| 69 | + if ($methodReturnType->getIterableValueType() instanceof ObjectType) { |
| 70 | + return $methodReturnType; |
| 71 | + } |
| 72 | + |
| 73 | + if ($variableType->getClassReflection() !== null) { |
| 74 | + $repositoryExtendsTags = $variableType->getClassReflection()->getExtendsTags()[Repository::class] ?? null; |
| 75 | + if ($repositoryExtendsTags instanceof ExtendsTag && $repositoryExtendsTags->getType() instanceof GenericObjectType) { |
| 76 | + return new GenericObjectType(QueryResultInterface::class, [$repositoryExtendsTags->getType()->getTypes()[0] ?? new ErrorType()]); |
| 77 | + } |
| 78 | + } |
| 79 | + /** @var class-string $className */ |
| 80 | + $className = $methodReflection->getDeclaringClass()->getName(); |
| 81 | + } |
| 82 | + |
51 | 83 | $modelName = $this->translateRepositoryNameToModelName($className); |
52 | 84 |
|
53 | 85 | return new GenericObjectType(QueryResultInterface::class, [new ObjectType($modelName)]); |
54 | 86 | } |
55 | 87 |
|
| 88 | + /** |
| 89 | + * @return GenericObjectType[] |
| 90 | + */ |
| 91 | + private function getGenericTypes(Type $baseType): array |
| 92 | + { |
| 93 | + $genericObjectTypes = []; |
| 94 | + TypeTraverser::map($baseType, static function (Type $type, callable $traverse) use (&$genericObjectTypes): Type { |
| 95 | + if ($type instanceof GenericObjectType) { |
| 96 | + $resolvedType = TypeTraverser::map($type, static function (Type $type, callable $traverse): Type { |
| 97 | + if ($type instanceof TemplateType) { |
| 98 | + return $traverse($type->getBound()); |
| 99 | + } |
| 100 | + return $traverse($type); |
| 101 | + }); |
| 102 | + if (!$resolvedType instanceof GenericObjectType) { |
| 103 | + throw new \PHPStan\ShouldNotHappenException(); |
| 104 | + } |
| 105 | + $genericObjectTypes[] = $resolvedType; |
| 106 | + $traverse($type); |
| 107 | + return $type; |
| 108 | + } |
| 109 | + $traverse($type); |
| 110 | + return $type; |
| 111 | + }); |
| 112 | + |
| 113 | + return $genericObjectTypes; |
| 114 | + } |
| 115 | + |
56 | 116 | } |
0 commit comments