Skip to content

Commit 65357eb

Browse files
committed
Add possibility to define array return type for Repository->findAll
The RepositoryFindAllDynamicReturnTypeExtension does now check where the findAll method is defined. If it is not overwritten but defined in the native Repository class, the default behavior, returning a QueryResultInterface, will be used. If the findAll method is overwritten, the declaration from that overwrite will be used. The Repository.stub has been adjusted to now also support and array as return type for findAll.
1 parent 3d45a0a commit 65357eb

File tree

3 files changed

+33
-4
lines changed

3 files changed

+33
-4
lines changed

src/Type/RepositoryFindAllDynamicReturnTypeExtension.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,10 @@
1010
use PHPStan\Type\Generic\GenericObjectType;
1111
use PHPStan\Type\ObjectType;
1212
use PHPStan\Type\Type;
13+
use PHPStan\Type\TypeWithClassName;
1314
use TYPO3\CMS\Core\Utility\ClassNamingUtility;
1415
use TYPO3\CMS\Extbase\Persistence\QueryResultInterface;
16+
use TYPO3\CMS\Extbase\Persistence\Repository;
1517

1618
class RepositoryFindAllDynamicReturnTypeExtension implements DynamicMethodReturnTypeExtension
1719
{
@@ -36,7 +38,7 @@ public function getTypeFromMethodCall(
3638
{
3739
$variableType = $scope->getType($methodCall->var);
3840

39-
if (!($variableType instanceof ObjectType) || !is_subclass_of($variableType->getClassName(), $this->getClass())) {
41+
if ($methodReflection->getDeclaringClass()->getName() !== Repository::class || !$variableType instanceof TypeWithClassName) {
4042
return ParametersAcceptorSelector::selectSingle($methodReflection->getVariants())->getReturnType();
4143
}
4244

stubs/Repository.stub

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ class Repository
3030
public function update($modifiedObject);
3131

3232
/**
33-
* @phpstan-return QueryResultInterface<TEntityClass>
33+
* @phpstan-return QueryResultInterface<TEntityClass>|list<TEntityClass>
3434
*/
3535
public function findAll();
3636

tests/Unit/Type/data/repository-stub-files.php

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,16 +59,43 @@ public function myTests(): void
5959

6060
}
6161

62-
/** @phpstan-ignore-next-line */
62+
class MyModelWithoutExtends extends \TYPO3\CMS\Extbase\DomainObject\AbstractEntity
63+
{
64+
65+
}
66+
67+
/** @phpstan-ignore-next-line */
6368
class MyModelWithoutExtendsRepository extends \TYPO3\CMS\Extbase\Persistence\Repository
6469
{
6570

6671
public function myTests(): void
6772
{
6873
assertType(
69-
'TYPO3\CMS\Extbase\Persistence\QueryResultInterface<TYPO3\CMS\Extbase\DomainObject\DomainObjectInterface>',
74+
'TYPO3\CMS\Extbase\Persistence\QueryResultInterface<RepositoryStubFiles\My\Test\Extension\Domain\Model\MyModelWithoutExtends>',
7075
$this->findAll()
7176
);
7277
}
7378

7479
}
80+
81+
/** @extends \TYPO3\CMS\Extbase\Persistence\Repository<\RepositoryStubFiles\My\Test\Extension\Domain\Model\MyModel> */
82+
class FindAllTestRepository extends \TYPO3\CMS\Extbase\Persistence\Repository
83+
{
84+
85+
public function myTests(): void
86+
{
87+
assertType(
88+
'array<int, RepositoryStubFiles\My\Test\Extension\Domain\Model\MyModel>',
89+
$this->findAll()
90+
);
91+
}
92+
93+
/**
94+
* @return list<\RepositoryStubFiles\My\Test\Extension\Domain\Model\MyModel>
95+
*/
96+
public function findAll(): array
97+
{
98+
return [];
99+
}
100+
101+
}

0 commit comments

Comments
 (0)