77// e.g. to move PhpDocInfo to the particular rule itself
88use PhpParser \Node ;
99use PhpParser \Node \ContainsStmts ;
10+ use PhpParser \Node \Expr ;
11+ use PhpParser \Node \Expr \Array_ ;
12+ use PhpParser \Node \Expr \BinaryOp \Identical ;
1013use PhpParser \Node \Expr \MethodCall ;
1114use PhpParser \Node \Expr \PropertyFetch ;
1215use PHPStan \Type \ObjectType ;
16+ use Rector \PhpParser \Node \Value \ValueResolver ;
1317use Rector \Rector \AbstractRector ;
1418use Symplify \RuleDocGenerator \ValueObject \RuleDefinition ;
1519
1620final class MakeUseOfContaintsStmtsRector extends AbstractRector
1721{
22+ public function __construct (
23+ private ValueResolver $ valueResolver
24+ ) {
25+ }
26+
1827 public function getRuleDefinition (): RuleDefinition
1928 {
2029 // @see https://github.com/nikic/PHP-Parser/pull/1113
@@ -23,14 +32,30 @@ public function getRuleDefinition(): RuleDefinition
2332
2433 public function getNodeTypes (): array
2534 {
26- return [PropertyFetch::class];
35+ return [Identical::class, PropertyFetch::class];
2736 }
2837
2938 /**
30- * @param PropertyFetch $node
39+ * @param PropertyFetch|Identical $node
3140 */
32- public function refactor (Node $ node ): ? Node
41+ public function refactor (Node $ node ): MethodCall | Identical | null
3342 {
43+ if ($ node instanceof Identical) {
44+ if (! $ this ->valueResolver ->isNull ($ node ->right )) {
45+ return null ;
46+ }
47+
48+ if ($ this ->isStmtsPropertyFetch ($ node ->left )) {
49+ /** @var PropertyFetch $propertyFetch */
50+ $ propertyFetch = $ node ->left ;
51+
52+ $ node ->left = new MethodCall ($ propertyFetch ->var , 'getStmts ' );
53+ $ node ->right = new Array_ ([]);
54+
55+ return $ node ;
56+ }
57+ }
58+
3459 if (! $ this ->isName ($ node ->name , 'stmts ' )) {
3560 return null ;
3661 }
@@ -41,4 +66,13 @@ public function refactor(Node $node): ?Node
4166
4267 return new MethodCall ($ node ->var , 'getStmts ' );
4368 }
69+
70+ private function isStmtsPropertyFetch (Expr $ expr ): bool
71+ {
72+ if (! $ expr instanceof PropertyFetch) {
73+ return false ;
74+ }
75+
76+ return $ this ->isName ($ expr ->name , 'stmts ' );
77+ }
4478}
0 commit comments