Skip to content

Commit c1e7147

Browse files
committed
Fix another case and add more tests
1 parent 58ada25 commit c1e7147

File tree

3 files changed

+70
-4
lines changed

3 files changed

+70
-4
lines changed

src/Type/QueryResultToArrayDynamicReturnTypeExtension.php

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,9 +40,13 @@ public function getTypeFromMethodCall(
4040
Scope $scope
4141
): Type
4242
{
43-
$resultType = $this->getGenericTypes(
44-
$scope->getType($methodCall->var)
45-
)[0] ?? null;
43+
$resultType = $scope->getType($methodCall->var);
44+
45+
if (!($resultType instanceof ObjectType)) {
46+
$resultType = $this->getGenericTypes(
47+
$scope->getType($methodCall->var)
48+
)[0] ?? null;
49+
}
4650

4751
if ($resultType instanceof GenericObjectType) {
4852
$modelType = $resultType->getTypes();

tests/Unit/Type/QueryResultToArrayDynamicReturnTypeExtension/data/query-result-to-array.php

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77

88
use TYPO3\CMS\Extbase\DomainObject\AbstractEntity;
99
use TYPO3\CMS\Extbase\Mvc\Controller\ActionController;
10+
use TYPO3\CMS\Extbase\Persistence\Generic\QueryResult;
11+
use TYPO3\CMS\Extbase\Persistence\QueryResultInterface;
1012
use TYPO3\CMS\Extbase\Persistence\Repository;
1113
use function PHPStan\Testing\assertType;
1214

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

1921
}
2022

23+
/**
24+
* @extends Repository<FrontendUserGroup>
25+
*/
26+
class FrontendUserCustomFindAllGroupRepository extends Repository
27+
{
28+
29+
/**
30+
* @return QueryResultInterface<FrontendUserGroup>
31+
*/
32+
public function findAll(): QueryResultInterface // phpcs:ignore SlevomatCodingStandard.TypeHints.ReturnTypeHint.MissingAnyTypeHint
33+
{
34+
$queryResult = null; // phpcs:ignore SlevomatCodingStandard.Variables.UselessVariable.UselessVariable
35+
/** @var QueryResult<FrontendUserGroup> $queryResult */
36+
return $queryResult;
37+
}
38+
39+
}
40+
2141
class FrontendUserGroup extends AbstractEntity
2242
{
2343

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

32-
public function __construct(FrontendUserGroupRepository $myRepository)
52+
/** @var FrontendUserCustomFindAllGroupRepository */
53+
private $myCustomFindAllRepository;
54+
55+
public function __construct(
56+
FrontendUserGroupRepository $myRepository,
57+
FrontendUserCustomFindAllGroupRepository $myCustomFindAllRepository
58+
)
3359
{
3460
$this->myRepository = $myRepository;
61+
$this->myCustomFindAllRepository = $myCustomFindAllRepository;
3562
}
3663

3764
public function showAction(): void
@@ -47,6 +74,18 @@ public function showAction(): void
4774
'array<int, SaschaEgerer\PhpstanTypo3\Tests\Unit\Type\QueryResultToArrayDynamicReturnTypeExtension\FrontendUserGroup>',
4875
$myObjects
4976
);
77+
78+
assertType(
79+
'array<int, SaschaEgerer\PhpstanTypo3\Tests\Unit\Type\QueryResultToArrayDynamicReturnTypeExtension\FrontendUserGroup>',
80+
$this->myCustomFindAllRepository->findAll()->toArray()
81+
);
82+
83+
$queryResult = $this->myCustomFindAllRepository->findAll();
84+
$myObjects = $queryResult->toArray();
85+
assertType(
86+
'array<int, SaschaEgerer\PhpstanTypo3\Tests\Unit\Type\QueryResultToArrayDynamicReturnTypeExtension\FrontendUserGroup>',
87+
$myObjects
88+
);
5089
}
5190

5291
}

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

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,3 +99,26 @@ public function findAll(): array
9999
}
100100

101101
}
102+
103+
/** @extends \TYPO3\CMS\Extbase\Persistence\Repository<\RepositoryStubFiles\My\Test\Extension\Domain\Model\MyModel> */
104+
class FindAllWithoutReturnTestRepository extends \TYPO3\CMS\Extbase\Persistence\Repository
105+
{
106+
107+
public function myTests(): void
108+
{
109+
assertType(
110+
'array<int, RepositoryStubFiles\My\Test\Extension\Domain\Model\MyModel>|TYPO3\CMS\Extbase\Persistence\QueryResultInterface<RepositoryStubFiles\My\Test\Extension\Domain\Model\MyModel>',
111+
$this->findAll()
112+
);
113+
}
114+
115+
public function findAll() // phpcs:ignore SlevomatCodingStandard.TypeHints.ReturnTypeHint.MissingAnyTypeHint
116+
{
117+
$foo = null; // phpcs:ignore SlevomatCodingStandard.Variables.UselessVariable.UselessVariable
118+
/**
119+
* @var array<int, \RepositoryStubFiles\My\Test\Extension\Domain\Model\MyModel>|\TYPO3\CMS\Extbase\Persistence\QueryResultInterface<\RepositoryStubFiles\My\Test\Extension\Domain\Model\MyModel> $foo
120+
*/
121+
return $foo;
122+
}
123+
124+
}

0 commit comments

Comments
 (0)