Skip to content

Commit

Permalink
Fix another case and add more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
sascha-egerer committed Oct 26, 2022
1 parent 58ada25 commit c1e7147
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 4 deletions.
10 changes: 7 additions & 3 deletions src/Type/QueryResultToArrayDynamicReturnTypeExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,13 @@ public function getTypeFromMethodCall(
Scope $scope
): Type
{
$resultType = $this->getGenericTypes(
$scope->getType($methodCall->var)
)[0] ?? null;
$resultType = $scope->getType($methodCall->var);

if (!($resultType instanceof ObjectType)) {
$resultType = $this->getGenericTypes(
$scope->getType($methodCall->var)
)[0] ?? null;
}

if ($resultType instanceof GenericObjectType) {
$modelType = $resultType->getTypes();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@

use TYPO3\CMS\Extbase\DomainObject\AbstractEntity;
use TYPO3\CMS\Extbase\Mvc\Controller\ActionController;
use TYPO3\CMS\Extbase\Persistence\Generic\QueryResult;
use TYPO3\CMS\Extbase\Persistence\QueryResultInterface;
use TYPO3\CMS\Extbase\Persistence\Repository;
use function PHPStan\Testing\assertType;

Expand All @@ -18,6 +20,24 @@ class FrontendUserGroupRepository extends Repository

}

/**
* @extends Repository<FrontendUserGroup>
*/
class FrontendUserCustomFindAllGroupRepository extends Repository
{

/**
* @return QueryResultInterface<FrontendUserGroup>
*/
public function findAll(): QueryResultInterface // phpcs:ignore SlevomatCodingStandard.TypeHints.ReturnTypeHint.MissingAnyTypeHint
{
$queryResult = null; // phpcs:ignore SlevomatCodingStandard.Variables.UselessVariable.UselessVariable
/** @var QueryResult<FrontendUserGroup> $queryResult */
return $queryResult;
}

}

class FrontendUserGroup extends AbstractEntity
{

Expand All @@ -29,9 +49,16 @@ class MyController extends ActionController
/** @var FrontendUserGroupRepository */
private $myRepository;

public function __construct(FrontendUserGroupRepository $myRepository)
/** @var FrontendUserCustomFindAllGroupRepository */
private $myCustomFindAllRepository;

public function __construct(
FrontendUserGroupRepository $myRepository,
FrontendUserCustomFindAllGroupRepository $myCustomFindAllRepository
)
{
$this->myRepository = $myRepository;
$this->myCustomFindAllRepository = $myCustomFindAllRepository;
}

public function showAction(): void
Expand All @@ -47,6 +74,18 @@ public function showAction(): void
'array<int, SaschaEgerer\PhpstanTypo3\Tests\Unit\Type\QueryResultToArrayDynamicReturnTypeExtension\FrontendUserGroup>',
$myObjects
);

assertType(
'array<int, SaschaEgerer\PhpstanTypo3\Tests\Unit\Type\QueryResultToArrayDynamicReturnTypeExtension\FrontendUserGroup>',
$this->myCustomFindAllRepository->findAll()->toArray()
);

$queryResult = $this->myCustomFindAllRepository->findAll();
$myObjects = $queryResult->toArray();
assertType(
'array<int, SaschaEgerer\PhpstanTypo3\Tests\Unit\Type\QueryResultToArrayDynamicReturnTypeExtension\FrontendUserGroup>',
$myObjects
);
}

}
23 changes: 23 additions & 0 deletions tests/Unit/Type/data/repository-stub-files.php
Original file line number Diff line number Diff line change
Expand Up @@ -99,3 +99,26 @@ public function findAll(): array
}

}

/** @extends \TYPO3\CMS\Extbase\Persistence\Repository<\RepositoryStubFiles\My\Test\Extension\Domain\Model\MyModel> */
class FindAllWithoutReturnTestRepository extends \TYPO3\CMS\Extbase\Persistence\Repository
{

public function myTests(): void
{
assertType(
'array<int, RepositoryStubFiles\My\Test\Extension\Domain\Model\MyModel>|TYPO3\CMS\Extbase\Persistence\QueryResultInterface<RepositoryStubFiles\My\Test\Extension\Domain\Model\MyModel>',
$this->findAll()
);
}

public function findAll() // phpcs:ignore SlevomatCodingStandard.TypeHints.ReturnTypeHint.MissingAnyTypeHint
{
$foo = null; // phpcs:ignore SlevomatCodingStandard.Variables.UselessVariable.UselessVariable
/**
* @var array<int, \RepositoryStubFiles\My\Test\Extension\Domain\Model\MyModel>|\TYPO3\CMS\Extbase\Persistence\QueryResultInterface<\RepositoryStubFiles\My\Test\Extension\Domain\Model\MyModel> $foo
*/
return $foo;
}

}

0 comments on commit c1e7147

Please sign in to comment.