Skip to content

Commit

Permalink
Add possibility to define array return type for Repository->findAll
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
sascha-egerer committed Feb 8, 2022
1 parent 3d45a0a commit 65357eb
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 4 deletions.
4 changes: 3 additions & 1 deletion src/Type/RepositoryFindAllDynamicReturnTypeExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand All @@ -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();
}

Expand Down
2 changes: 1 addition & 1 deletion stubs/Repository.stub
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ class Repository
public function update($modifiedObject);

/**
* @phpstan-return QueryResultInterface<TEntityClass>
* @phpstan-return QueryResultInterface<TEntityClass>|list<TEntityClass>
*/
public function findAll();

Expand Down
31 changes: 29 additions & 2 deletions tests/Unit/Type/data/repository-stub-files.php
Original file line number Diff line number Diff line change
Expand Up @@ -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\DomainObject\DomainObjectInterface>',
'TYPO3\CMS\Extbase\Persistence\QueryResultInterface<RepositoryStubFiles\My\Test\Extension\Domain\Model\MyModelWithoutExtends>',
$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<int, RepositoryStubFiles\My\Test\Extension\Domain\Model\MyModel>',
$this->findAll()
);
}

/**
* @return list<\RepositoryStubFiles\My\Test\Extension\Domain\Model\MyModel>
*/
public function findAll(): array
{
return [];
}

}

0 comments on commit 65357eb

Please sign in to comment.