mirror of
https://github.com/rectorphp/rector.git
synced 2025-02-25 12:14:02 +01:00
AbstractRector: add remove node option
This commit is contained in:
parent
e79ca7b059
commit
4b2a37b4fe
@ -12,6 +12,11 @@ use Rector\NodeChanger\PropertyAdder;
|
||||
|
||||
abstract class AbstractRector extends NodeVisitorAbstract implements RectorInterface
|
||||
{
|
||||
/**
|
||||
* @var bool
|
||||
*/
|
||||
protected $removeNode = false;
|
||||
|
||||
/**
|
||||
* @var ExpressionAdder
|
||||
*/
|
||||
@ -62,12 +67,29 @@ abstract class AbstractRector extends NodeVisitorAbstract implements RectorInter
|
||||
return $newNode;
|
||||
}
|
||||
|
||||
if (is_int($newNode)) {
|
||||
return $newNode;
|
||||
}
|
||||
|
||||
return NodeTraverser::DONT_TRAVERSE_CHILDREN;
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return null|int|Node
|
||||
*/
|
||||
public function leaveNode(Node $node)
|
||||
{
|
||||
if ($this->removeNode) {
|
||||
$this->removeNode = false;
|
||||
return NodeTraverser::REMOVE_NODE;
|
||||
}
|
||||
|
||||
return $node;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Node[] $nodes
|
||||
* @return Node[]
|
||||
|
@ -6,6 +6,8 @@ use PhpParser\Node;
|
||||
use PhpParser\Node\Stmt\Nop;
|
||||
use PhpParser\Node\Stmt\Return_;
|
||||
use PhpParser\Node\Expr\Variable;
|
||||
use PhpParser\NodeTraverser;
|
||||
use Rector\Node\Attribute;
|
||||
use Rector\Rector\AbstractRector;
|
||||
|
||||
final class FluentReplaceRector extends AbstractRector
|
||||
@ -25,8 +27,9 @@ final class FluentReplaceRector extends AbstractRector
|
||||
return $returnExpr->name === 'this';
|
||||
}
|
||||
|
||||
public function refactor(Node $node): Node
|
||||
public function refactor(Node $node): ?Node
|
||||
{
|
||||
return new Nop;
|
||||
$this->removeNode = true;
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user