Skip to content

Commit ede3432

Browse files
committed
fixup! add MakeUseOfContaintsStmtsRector
1 parent 67fd8d5 commit ede3432

File tree

1 file changed

+37
-3
lines changed

1 file changed

+37
-3
lines changed

utils/Rector/MakeUseOfContaintsStmtsRector.php

Lines changed: 37 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,23 @@
77
// e.g. to move PhpDocInfo to the particular rule itself
88
use PhpParser\Node;
99
use PhpParser\Node\ContainsStmts;
10+
use PhpParser\Node\Expr;
11+
use PhpParser\Node\Expr\Array_;
12+
use PhpParser\Node\Expr\BinaryOp\Identical;
1013
use PhpParser\Node\Expr\MethodCall;
1114
use PhpParser\Node\Expr\PropertyFetch;
1215
use PHPStan\Type\ObjectType;
16+
use Rector\PhpParser\Node\Value\ValueResolver;
1317
use Rector\Rector\AbstractRector;
1418
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
1519

1620
final 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

Comments
 (0)