File tree Expand file tree Collapse file tree 2 files changed +46
-0
lines changed Expand file tree Collapse file tree 2 files changed +46
-0
lines changed Original file line number Diff line number Diff 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
Original file line number Diff line number Diff line change 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+ }
You can’t perform that action at this time.
0 commit comments