44
55namespace Rector \TypeDeclarationDocblocks \Rector \ClassMethod ;
66
7+ use PhpParser \Node ;
78use PhpParser \Node \Stmt \Class_ ;
8- use PhpParser \Node \Stmt \Property ;
99use PHPStan \PhpDocParser \Ast \PhpDoc \VarTagValueNode ;
1010use PHPStan \PhpDocParser \Ast \Type \ArrayTypeNode ;
1111use PHPStan \PhpDocParser \Ast \Type \GenericTypeNode ;
12- use PHPStan \Type \Type ;
13- use PhpParser \Node ;
14- use PhpParser \Node \Expr \PropertyFetch ;
15- use PhpParser \Node \Stmt \ClassMethod ;
16- use PhpParser \Node \Stmt \Return_ ;
17- use PHPStan \Type \ArrayType ;
18- use PHPStan \Type \MixedType ;
19- use PHPStan \Type \UnionType ;
2012use Rector \BetterPhpDocParser \PhpDocInfo \PhpDocInfoFactory ;
2113use Rector \Rector \AbstractRector ;
2214use Rector \TypeDeclarationDocblocks \NodeDocblockTypeDecorator ;
15+ use Rector \TypeDeclarationDocblocks \NodeFinder \GetterClassMethodPropertyFinder ;
2316use Rector \TypeDeclarationDocblocks \TagNodeAnalyzer \UsefulArrayTagNodeAnalyzer ;
2417use Symplify \RuleDocGenerator \ValueObject \CodeSample \CodeSample ;
2518use Symplify \RuleDocGenerator \ValueObject \RuleDefinition ;
@@ -32,7 +25,8 @@ final class DocblockGetterReturnArrayFromPropertyDocblockVarRector extends Abstr
3225 public function __construct (
3326 private readonly PhpDocInfoFactory $ phpDocInfoFactory ,
3427 private readonly UsefulArrayTagNodeAnalyzer $ usefulArrayTagNodeAnalyzer ,
35- private readonly NodeDocblockTypeDecorator $ nodeDocblockTypeDecorator
28+ private readonly NodeDocblockTypeDecorator $ nodeDocblockTypeDecorator ,
29+ private readonly GetterClassMethodPropertyFinder $ getterClassMethodPropertyFinder ,
3630 ) {
3731 }
3832
@@ -108,13 +102,12 @@ public function refactor(Node $node): ?Node
108102 continue ;
109103 }
110104
111- // @todo add promoted proeprty
112- $ property = $ this ->matchReturnLocalPropertyFetch ($ classMethod , $ node );
113- if (! $ property instanceof Property) {
105+ $ propertyOrParam = $ this ->getterClassMethodPropertyFinder ->find ($ classMethod , $ node );
106+ if (! $ propertyOrParam instanceof Node) {
114107 continue ;
115108 }
116109
117- $ propertyDocInfo = $ this ->phpDocInfoFactory ->createFromNodeOrEmpty ($ property );
110+ $ propertyDocInfo = $ this ->phpDocInfoFactory ->createFromNodeOrEmpty ($ propertyOrParam );
118111
119112 $ varTagValueNode = $ propertyDocInfo ->getVarTagValueNode ();
120113
@@ -145,49 +138,49 @@ public function refactor(Node $node): ?Node
145138 return $ node ;
146139 }
147140
148- private function matchReturnLocalPropertyFetch (ClassMethod $ classMethod , Class_ $ class ): ?Property
149- {
150- // we need exactly one statement of return
151- if ($ classMethod ->stmts === null || count ($ classMethod ->stmts ) !== 1 ) {
152- return null ;
153- }
154-
155- $ onlyStmt = $ classMethod ->stmts [0 ];
156- if (! $ onlyStmt instanceof Return_) {
157- return null ;
158- }
159-
160- if (! $ onlyStmt ->expr instanceof PropertyFetch) {
161- return null ;
162- }
163-
164- $ propertyFetch = $ onlyStmt ->expr ;
165- if (! $ this ->isName ($ propertyFetch ->var , 'this ' )) {
166- return null ;
167- }
168-
169- $ propertyName = $ this ->getName ($ propertyFetch ->name );
170- if (! is_string ($ propertyName )) {
171- return null ;
172- }
173-
174- return $ class ->getProperty ($ propertyName );
175- }
176-
177- private function isUsefulType (Type $ type ): bool
178- {
179- if ($ type instanceof UnionType) {
180- return false ;
181- }
182-
183- if (! $ type instanceof ArrayType) {
184- return true ;
185- }
186-
187- if (! $ type ->getKeyType () instanceof MixedType) {
188- return true ;
189- }
190-
191- return ! $ type ->getItemType () instanceof MixedType;
192- }
141+ // private function matchReturnLocalPropertyFetch(ClassMethod $classMethod, Class_ $class): ?Property
142+ // {
143+ // // we need exactly one statement of return
144+ // if ($classMethod->stmts === null || count($classMethod->stmts) !== 1) {
145+ // return null;
146+ // }
147+ //
148+ // $onlyStmt = $classMethod->stmts[0];
149+ // if (! $onlyStmt instanceof Return_) {
150+ // return null;
151+ // }
152+ //
153+ // if (! $onlyStmt->expr instanceof PropertyFetch) {
154+ // return null;
155+ // }
156+ //
157+ // $propertyFetch = $onlyStmt->expr;
158+ // if (! $this->isName($propertyFetch->var, 'this')) {
159+ // return null;
160+ // }
161+ //
162+ // $propertyName = $this->getName($propertyFetch->name);
163+ // if (! is_string($propertyName)) {
164+ // return null;
165+ // }
166+ //
167+ // return $class->getProperty($propertyName);
168+ // }
169+
170+ // private function isUsefulType(Type $type): bool
171+ // {
172+ // if ($type instanceof UnionType) {
173+ // return false;
174+ // }
175+ //
176+ // if (! $type instanceof ArrayType) {
177+ // return true;
178+ // }
179+ //
180+ // if (! $type->getKeyType() instanceof MixedType) {
181+ // return true;
182+ // }
183+ //
184+ // return ! $type->getItemType() instanceof MixedType;
185+ // }
193186}
0 commit comments