Updated Rector to commit 0eb6000cfce746d9cf714722047a7d824d1f2dac

0eb6000cfc [DeadCode] Skip used in next For_/Foreach_ on RemoveDeadIfForeachForRector (#5154)
This commit is contained in:
Tomas Votruba 2023-10-11 04:48:30 +00:00
parent 882ff036c6
commit 251ad8aeef
2 changed files with 78 additions and 24 deletions

View File

@ -7,11 +7,13 @@ use PhpParser\Node;
use PhpParser\Node\Expr;
use PhpParser\Node\Expr\Assign;
use PhpParser\Node\Expr\CallLike;
use PhpParser\Node\Expr\Variable;
use PhpParser\Node\Stmt\Else_;
use PhpParser\Node\Stmt\For_;
use PhpParser\Node\Stmt\Foreach_;
use PhpParser\Node\Stmt\If_;
use PhpParser\NodeTraverser;
use Rector\Core\Contract\PhpParser\Node\StmtsAwareInterface;
use Rector\Core\NodeManipulator\StmtsManipulator;
use Rector\Core\PhpParser\Node\BetterNodeFinder;
use Rector\Core\Rector\AbstractRector;
use Rector\EarlyReturn\NodeTransformer\ConditionInverter;
@ -32,10 +34,20 @@ final class RemoveDeadIfForeachForRector extends AbstractRector
* @var \Rector\Core\PhpParser\Node\BetterNodeFinder
*/
private $betterNodeFinder;
public function __construct(ConditionInverter $conditionInverter, BetterNodeFinder $betterNodeFinder)
/**
* @readonly
* @var \Rector\Core\NodeManipulator\StmtsManipulator
*/
private $stmtsManipulator;
/**
* @var bool
*/
private $hasChanged = \false;
public function __construct(ConditionInverter $conditionInverter, BetterNodeFinder $betterNodeFinder, StmtsManipulator $stmtsManipulator)
{
$this->conditionInverter = $conditionInverter;
$this->betterNodeFinder = $betterNodeFinder;
$this->stmtsManipulator = $stmtsManipulator;
}
public function getRuleDefinition() : RuleDefinition
{
@ -70,39 +82,81 @@ CODE_SAMPLE
*/
public function getNodeTypes() : array
{
return [For_::class, If_::class, Foreach_::class];
return [StmtsAwareInterface::class];
}
/**
* @param For_|If_|Foreach_ $node
* @param StmtsAwareInterface $node
* @return \PhpParser\Node|null|int
*/
public function refactor(Node $node)
{
if ($node instanceof If_) {
if ($node->stmts !== []) {
return null;
if ($node->stmts === null) {
return null;
}
$this->hasChanged = \false;
foreach ($node->stmts as $key => $stmt) {
if (!$stmt instanceof If_ && !$stmt instanceof For_ && !$stmt instanceof Foreach_) {
continue;
}
if ($node->elseifs !== []) {
return null;
if ($stmt->stmts !== []) {
continue;
}
// useless if ()
if (!$node->else instanceof Else_) {
if ($this->hasNodeSideEffect($node->cond)) {
return null;
}
return NodeTraverser::REMOVE_NODE;
if ($stmt instanceof If_) {
$this->processIf($stmt, $key, $node);
continue;
}
$node->cond = $this->conditionInverter->createInvertedCondition($node->cond);
$node->stmts = $node->else->stmts;
$node->else = null;
$this->processForForeach($stmt, $key, $node);
}
if ($this->hasChanged) {
return $node;
}
// nothing to "for"
if ($node->stmts === []) {
return NodeTraverser::REMOVE_NODE;
}
return null;
}
private function processIf(If_ $if, int $key, StmtsAwareInterface $stmtsAware) : void
{
if ($if->elseifs !== []) {
return;
}
// useless if ()
if (!$if->else instanceof Else_) {
if ($this->hasNodeSideEffect($if->cond)) {
return;
}
unset($stmtsAware->stmts[$key]);
$this->hasChanged = \true;
return;
}
$if->cond = $this->conditionInverter->createInvertedCondition($if->cond);
$if->stmts = $if->else->stmts;
$if->else = null;
$this->hasChanged = \true;
}
/**
* @param \PhpParser\Node\Stmt\For_|\PhpParser\Node\Stmt\Foreach_ $for
*/
private function processForForeach($for, int $key, StmtsAwareInterface $stmtsAware) : void
{
if ($for instanceof For_) {
$variables = $this->betterNodeFinder->findInstanceOf(\array_merge($for->init, $for->cond, $for->loop), Variable::class);
foreach ($variables as $variable) {
if ($this->stmtsManipulator->isVariableUsedInNextStmt($stmtsAware, $key + 1, (string) $this->getName($variable))) {
return;
}
}
unset($stmtsAware->stmts[$key]);
$this->hasChanged = \true;
return;
}
$exprs = \array_filter([$for->expr, $for->valueVar, $for->valueVar]);
$variables = $this->betterNodeFinder->findInstanceOf($exprs, Variable::class);
foreach ($variables as $variable) {
if ($this->stmtsManipulator->isVariableUsedInNextStmt($stmtsAware, $key + 1, (string) $this->getName($variable))) {
return;
}
}
unset($stmtsAware->stmts[$key]);
$this->hasChanged = \true;
}
private function hasNodeSideEffect(Expr $expr) : bool
{
return $this->betterNodeFinder->hasInstancesOf($expr, [CallLike::class, Assign::class]);

View File

@ -19,12 +19,12 @@ final class VersionResolver
* @api
* @var string
*/
public const PACKAGE_VERSION = '6d96068b0232141101c238752d0c5e80d5781f74';
public const PACKAGE_VERSION = '0eb6000cfce746d9cf714722047a7d824d1f2dac';
/**
* @api
* @var string
*/
public const RELEASE_DATE = '2023-10-10 18:22:13';
public const RELEASE_DATE = '2023-10-11 11:45:36';
/**
* @var int
*/