Skip to content

Commit ed21a99

Browse files
committed
Add support for Repository::findOneBy
Add support for the TYPO3 Repository findOneBy method
1 parent 091beaf commit ed21a99

File tree

2 files changed

+46
-0
lines changed

2 files changed

+46
-0
lines changed

extension.neon

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,3 +19,7 @@ services:
1919
class: SaschaEgerer\PhpstanTypo3\Type\ObjectStorageDynamicReturnTypeExtension
2020
tags:
2121
- phpstan.broker.dynamicMethodReturnTypeExtension
22+
-
23+
class: SaschaEgerer\PhpstanTypo3\Type\RepositoryDynamicReturnTypeExtension
24+
tags:
25+
- phpstan.broker.dynamicMethodReturnTypeExtension
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
<?php
2+
declare(strict_types=1);
3+
4+
namespace SaschaEgerer\PhpstanTypo3\Type;
5+
6+
use PhpParser\Node\Expr\MethodCall;
7+
use PHPStan\Analyser\Scope;
8+
use PHPStan\Type\DynamicMethodReturnTypeExtension;
9+
use PHPStan\Type\ObjectType;
10+
use PHPStan\Type\Type;
11+
use PHPStan\Reflection\MethodReflection;
12+
use TYPO3\CMS\Core\Utility\ClassNamingUtility;
13+
14+
class RepositoryDynamicReturnTypeExtension implements DynamicMethodReturnTypeExtension
15+
{
16+
public function getClass(): string
17+
{
18+
return \TYPO3\CMS\Extbase\Persistence\RepositoryInterface::class;
19+
}
20+
21+
public function isMethodSupported(
22+
MethodReflection $methodReflection
23+
): bool {
24+
return $methodReflection->getName() === 'findByUid';
25+
}
26+
27+
public function getTypeFromMethodCall(
28+
MethodReflection $methodReflection,
29+
MethodCall $methodCall,
30+
Scope $scope
31+
): Type {
32+
$variableType = $scope->getType($methodCall->var);
33+
34+
if (!($variableType instanceof ObjectType) || !is_subclass_of($variableType->getClassName(), $this->getClass())) {
35+
return $methodReflection->getReturnType();
36+
}
37+
38+
$modelName = ClassNamingUtility::translateRepositoryNameToModelName($variableType->getClassName());
39+
40+
return new ObjectType($modelName);
41+
}
42+
}

0 commit comments

Comments
 (0)