diff --git a/src/Type/RepositoryFindAllDynamicReturnTypeExtension.php b/src/Type/RepositoryFindAllDynamicReturnTypeExtension.php index 2e476d8..3db5847 100644 --- a/src/Type/RepositoryFindAllDynamicReturnTypeExtension.php +++ b/src/Type/RepositoryFindAllDynamicReturnTypeExtension.php @@ -10,8 +10,10 @@ use PHPStan\Type\Generic\GenericObjectType; use PHPStan\Type\ObjectType; use PHPStan\Type\Type; +use PHPStan\Type\TypeWithClassName; use TYPO3\CMS\Core\Utility\ClassNamingUtility; use TYPO3\CMS\Extbase\Persistence\QueryResultInterface; +use TYPO3\CMS\Extbase\Persistence\Repository; class RepositoryFindAllDynamicReturnTypeExtension implements DynamicMethodReturnTypeExtension { @@ -36,7 +38,7 @@ public function getTypeFromMethodCall( { $variableType = $scope->getType($methodCall->var); - if (!($variableType instanceof ObjectType) || !is_subclass_of($variableType->getClassName(), $this->getClass())) { + if ($methodReflection->getDeclaringClass()->getName() !== Repository::class || !$variableType instanceof TypeWithClassName) { return ParametersAcceptorSelector::selectSingle($methodReflection->getVariants())->getReturnType(); } diff --git a/stubs/Repository.stub b/stubs/Repository.stub index 2a8d66c..e837963 100644 --- a/stubs/Repository.stub +++ b/stubs/Repository.stub @@ -30,7 +30,7 @@ class Repository public function update($modifiedObject); /** - * @phpstan-return QueryResultInterface + * @phpstan-return QueryResultInterface|list */ public function findAll(); diff --git a/tests/Unit/Type/data/repository-stub-files.php b/tests/Unit/Type/data/repository-stub-files.php index 11d9a9b..44b41db 100644 --- a/tests/Unit/Type/data/repository-stub-files.php +++ b/tests/Unit/Type/data/repository-stub-files.php @@ -59,16 +59,43 @@ public function myTests(): void } - /** @phpstan-ignore-next-line */ +class MyModelWithoutExtends extends \TYPO3\CMS\Extbase\DomainObject\AbstractEntity +{ + +} + +/** @phpstan-ignore-next-line */ class MyModelWithoutExtendsRepository extends \TYPO3\CMS\Extbase\Persistence\Repository { public function myTests(): void { assertType( - 'TYPO3\CMS\Extbase\Persistence\QueryResultInterface', + 'TYPO3\CMS\Extbase\Persistence\QueryResultInterface', $this->findAll() ); } } + +/** @extends \TYPO3\CMS\Extbase\Persistence\Repository<\RepositoryStubFiles\My\Test\Extension\Domain\Model\MyModel> */ +class FindAllTestRepository extends \TYPO3\CMS\Extbase\Persistence\Repository +{ + + public function myTests(): void + { + assertType( + 'array', + $this->findAll() + ); + } + + /** + * @return list<\RepositoryStubFiles\My\Test\Extension\Domain\Model\MyModel> + */ + public function findAll(): array + { + return []; + } + +}