Skip to content

Commit

Permalink
Fix return type of repository findBy methods
Browse files Browse the repository at this point in the history
The FooRepository->findByProperty() function must return an
`QueryResultInterface<Foo>` type.

Tests have also been added for that case.
  • Loading branch information
sascha-egerer committed Feb 3, 2022
1 parent a809882 commit c2600b9
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 4 deletions.
12 changes: 11 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,10 @@
"autoload-dev": {
"psr-4": {
"SaschaEgerer\\PhpstanTypo3\\Tests\\": "tests/"
}
},
"files": [
"tests/Unit/Type/data/repository-stub-files.php"
]
},
"extra": {
"branch-alias": {
Expand All @@ -38,5 +41,12 @@
"phpstan": {
"includes": ["extension.neon"]
}
},
"config": {
"allow-plugins": {
"dealerdirect/phpcodesniffer-composer-installer": true,
"typo3/class-alias-loader": true,
"typo3/cms-composer-installers": true
}
}
}
6 changes: 3 additions & 3 deletions src/Reflection/RepositoryFindByMethodReflection.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@
use PHPStan\Reflection\MethodReflection;
use PHPStan\Reflection\ReflectionProvider;
use PHPStan\TrinaryLogic;
use PHPStan\Type\Generic\GenericObjectType;
use PHPStan\Type\Generic\TemplateTypeMap;
use PHPStan\Type\ObjectType;
use PHPStan\Type\Type;
use TYPO3\CMS\Core\Utility\ClassNamingUtility;
use TYPO3\CMS\Extbase\Persistence\QueryResultInterface;

Expand Down Expand Up @@ -98,9 +98,9 @@ public function isVariadic(): bool
return false;
}

public function getReturnType(): Type
public function getReturnType(): GenericObjectType
{
return new ObjectType(QueryResultInterface::class);
return new GenericObjectType(QueryResultInterface::class, [new ObjectType($this->getModelName())]);
}

/**
Expand Down
41 changes: 41 additions & 0 deletions tests/Unit/Type/data/repository-stub-files.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,47 @@ public function __construct()
'class-string<static(RepositoryStubFiles\My\Test\Extension\Domain\Repository\MyModelRepository)>',
$this->getRepositoryClassName()
);

assertType(
'TYPO3\CMS\Extbase\Persistence\QueryResultInterface<RepositoryStubFiles\My\Test\Extension\Domain\Model\MyModel>',
$this->findAll()
);

assertType(
'TYPO3\CMS\Extbase\Persistence\QueryResultInterface<RepositoryStubFiles\My\Test\Extension\Domain\Model\MyModel>',
$this->findByFoo()
);

assertType(
'RepositoryStubFiles\My\Test\Extension\Domain\Model\MyModel|null',
$this->findOneByFoo()
);

assertType(
'array<int, RepositoryStubFiles\My\Test\Extension\Domain\Model\MyModel>',
$this->findByFoo()->toArray()
);

assertType(
'int',
$this->countByFoo()
);

assertType(
'int',
$this->countByFoo('a')
);
}
}

class MyModelWithoutExtendsRepository extends \TYPO3\CMS\Extbase\Persistence\Repository
{
public function __construct()
{
assertType(
'TYPO3\CMS\Extbase\Persistence\QueryResultInterface<TYPO3\CMS\Extbase\DomainObject\DomainObjectInterface>',
$this->findAll()
);
}
}
}

0 comments on commit c2600b9

Please sign in to comment.