|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace Rector\Utils\Rector; |
| 6 | + |
| 7 | +// e.g. to move PhpDocInfo to the particular rule itself |
| 8 | +use PhpParser\Node; |
| 9 | +use PhpParser\Node\ContainsStmts; |
| 10 | +use PhpParser\Node\Expr; |
| 11 | +use PhpParser\Node\Expr\Array_; |
| 12 | +use PhpParser\Node\Expr\ArrayDimFetch; |
| 13 | +use PhpParser\Node\Expr\Assign; |
| 14 | +use PhpParser\Node\Expr\BinaryOp\Identical; |
| 15 | +use PhpParser\Node\Expr\MethodCall; |
| 16 | +use PhpParser\Node\Expr\PropertyFetch; |
| 17 | +use PhpParser\Node\Stmt\ClassMethod; |
| 18 | +use PhpParser\Node\Stmt\Foreach_; |
| 19 | +use PhpParser\Node\Stmt\Function_; |
| 20 | +use PhpParser\NodeVisitor; |
| 21 | +use PHPStan\Type\ObjectType; |
| 22 | +use Rector\PhpParser\Node\Value\ValueResolver; |
| 23 | +use Rector\Rector\AbstractRector; |
| 24 | +use Symplify\RuleDocGenerator\ValueObject\RuleDefinition; |
| 25 | + |
| 26 | +final class MakeUseOfContaintsStmtsRector extends AbstractRector |
| 27 | +{ |
| 28 | + public function __construct( |
| 29 | + private readonly ValueResolver $valueResolver |
| 30 | + ) { |
| 31 | + } |
| 32 | + |
| 33 | + public function getRuleDefinition(): RuleDefinition |
| 34 | + { |
| 35 | + // @see https://github.com/nikic/PHP-Parser/pull/1113 |
| 36 | + return new RuleDefinition('Move $node->stmts to $node->getStmts() for ContaintsStmts', []); |
| 37 | + } |
| 38 | + |
| 39 | + public function getNodeTypes(): array |
| 40 | + { |
| 41 | + return [ |
| 42 | + Identical::class, |
| 43 | + PropertyFetch::class, |
| 44 | + Function_::class, |
| 45 | + Assign::class, |
| 46 | + Foreach_::class, |
| 47 | + ClassMethod::class, |
| 48 | + ]; |
| 49 | + } |
| 50 | + |
| 51 | + /** |
| 52 | + * @param PropertyFetch|Identical|Function_|Assign|Foreach_|ClassMethod $node |
| 53 | + * @return MethodCall|Identical|null|NodeVisitor::DONT_TRAVERSE_CURRENT_AND_CHILDREN |
| 54 | + */ |
| 55 | + public function refactor(Node $node): MethodCall|Identical|null|int |
| 56 | + { |
| 57 | + if ($node instanceof ClassMethod) { |
| 58 | + if ($this->isName($node, 'getStmts')) { |
| 59 | + // skip getter |
| 60 | + return NodeVisitor::DONT_TRAVERSE_CURRENT_AND_CHILDREN; |
| 61 | + } |
| 62 | + |
| 63 | + return null; |
| 64 | + } |
| 65 | + |
| 66 | + if ($node instanceof Foreach_) { |
| 67 | + return $this->refactorForeach($node); |
| 68 | + } |
| 69 | + |
| 70 | + if ($node instanceof Function_) { |
| 71 | + return $this->refactorFunction($node); |
| 72 | + } |
| 73 | + |
| 74 | + if ($node instanceof Assign) { |
| 75 | + return $this->refactorAssign($node); |
| 76 | + } |
| 77 | + |
| 78 | + if ($node instanceof Identical) { |
| 79 | + return $this->refactorIdentical($node); |
| 80 | + } |
| 81 | + |
| 82 | + if (! $this->isName($node->name, 'stmts')) { |
| 83 | + return null; |
| 84 | + } |
| 85 | + |
| 86 | + if (! $this->isObjectType($node->var, new ObjectType(ContainsStmts::class))) { |
| 87 | + return null; |
| 88 | + } |
| 89 | + |
| 90 | + return new MethodCall($node->var, 'getStmts'); |
| 91 | + } |
| 92 | + |
| 93 | + private function isStmtsPropertyFetch(Expr $expr): bool |
| 94 | + { |
| 95 | + if (! $expr instanceof PropertyFetch) { |
| 96 | + return false; |
| 97 | + } |
| 98 | + |
| 99 | + return $this->isName($expr->name, 'stmts'); |
| 100 | + } |
| 101 | + |
| 102 | + private function refactorIdentical(Identical $identical): ?Identical |
| 103 | + { |
| 104 | + if (! $this->valueResolver->isNull($identical->right)) { |
| 105 | + return null; |
| 106 | + } |
| 107 | + |
| 108 | + if ($this->isStmtsPropertyFetch($identical->left)) { |
| 109 | + /** @var PropertyFetch $propertyFetch */ |
| 110 | + $propertyFetch = $identical->left; |
| 111 | + |
| 112 | + $identical->left = new MethodCall($propertyFetch->var, 'getStmts'); |
| 113 | + $identical->right = new Array_([]); |
| 114 | + |
| 115 | + return $identical; |
| 116 | + } |
| 117 | + |
| 118 | + return null; |
| 119 | + } |
| 120 | + |
| 121 | + /** |
| 122 | + * @return null|NodeVisitor::DONT_TRAVERSE_CURRENT_AND_CHILDREN |
| 123 | + */ |
| 124 | + private function refactorForeach(Foreach_ $foreach): ?int |
| 125 | + { |
| 126 | + if (! $this->isStmtsPropertyFetch($foreach->expr)) { |
| 127 | + return null; |
| 128 | + } |
| 129 | + // skip $node->stmts in foreach with key, as key is probably used on the $node->stmts |
| 130 | + if ($foreach->keyVar instanceof Expr) { |
| 131 | + return NodeVisitor::DONT_TRAVERSE_CURRENT_AND_CHILDREN; |
| 132 | + } |
| 133 | + |
| 134 | + return null; |
| 135 | + } |
| 136 | + |
| 137 | + private function refactorFunction(Function_ $function): ?int |
| 138 | + { |
| 139 | + // keep any unset($node->stmts[x])) calls |
| 140 | + if ($this->isName($function->name, 'unset')) { |
| 141 | + return NodeVisitor::DONT_TRAVERSE_CURRENT_AND_CHILDREN; |
| 142 | + } |
| 143 | + |
| 144 | + return null; |
| 145 | + } |
| 146 | + |
| 147 | + /** |
| 148 | + * @return null|NodeVisitor::DONT_TRAVERSE_CURRENT_AND_CHILDREN |
| 149 | + */ |
| 150 | + private function refactorAssign(Assign $assign): ?int |
| 151 | + { |
| 152 | + if ($assign->var instanceof ArrayDimFetch) { |
| 153 | + $arrayDimFetch = $assign->var; |
| 154 | + if ($this->isStmtsPropertyFetch($arrayDimFetch->var)) { |
| 155 | + // keep $node->stmts[x] = ... |
| 156 | + return NodeVisitor::DONT_TRAVERSE_CURRENT_AND_CHILDREN; |
| 157 | + } |
| 158 | + } |
| 159 | + |
| 160 | + if (! $this->isStmtsPropertyFetch($assign->var)) { |
| 161 | + return null; |
| 162 | + } |
| 163 | + |
| 164 | + // keep assign to $node->stmts property |
| 165 | + return NodeVisitor::DONT_TRAVERSE_CURRENT_AND_CHILDREN; |
| 166 | + } |
| 167 | +} |
0 commit comments