AbstractRector: add remove node option

This commit is contained in:
TomasVotruba 2018-01-13 22:58:38 +01:00
parent e79ca7b059
commit 4b2a37b4fe
2 changed files with 27 additions and 2 deletions

View File

@ -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[]

View File

@ -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;
}
}