mirror of https://github.com/rectorphp/rector.git
Updated Rector to commit 6d96068b0232141101c238752d0c5e80d5781f74
6d96068b02
[DeadCode] Skip key used in next stmt on RemoveUnusedForeachKeyRector (#5153)
This commit is contained in:
parent
970b701227
commit
b70e1334d4
|
@ -4,8 +4,10 @@ declare (strict_types=1);
|
|||
namespace Rector\DeadCode\Rector\Foreach_;
|
||||
|
||||
use PhpParser\Node;
|
||||
use PhpParser\Node\Expr;
|
||||
use PhpParser\Node\Expr\Variable;
|
||||
use PhpParser\Node\Stmt\Foreach_;
|
||||
use Rector\Core\Contract\PhpParser\Node\StmtsAwareInterface;
|
||||
use Rector\Core\NodeManipulator\StmtsManipulator;
|
||||
use Rector\Core\PhpParser\Node\BetterNodeFinder;
|
||||
use Rector\Core\Rector\AbstractRector;
|
||||
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
|
||||
|
@ -20,9 +22,15 @@ final class RemoveUnusedForeachKeyRector extends AbstractRector
|
|||
* @var \Rector\Core\PhpParser\Node\BetterNodeFinder
|
||||
*/
|
||||
private $betterNodeFinder;
|
||||
public function __construct(BetterNodeFinder $betterNodeFinder)
|
||||
/**
|
||||
* @readonly
|
||||
* @var \Rector\Core\NodeManipulator\StmtsManipulator
|
||||
*/
|
||||
private $stmtsManipulator;
|
||||
public function __construct(BetterNodeFinder $betterNodeFinder, StmtsManipulator $stmtsManipulator)
|
||||
{
|
||||
$this->betterNodeFinder = $betterNodeFinder;
|
||||
$this->stmtsManipulator = $stmtsManipulator;
|
||||
}
|
||||
public function getRuleDefinition() : RuleDefinition
|
||||
{
|
||||
|
@ -45,24 +53,40 @@ CODE_SAMPLE
|
|||
*/
|
||||
public function getNodeTypes() : array
|
||||
{
|
||||
return [Foreach_::class];
|
||||
return [StmtsAwareInterface::class];
|
||||
}
|
||||
/**
|
||||
* @param Foreach_ $node
|
||||
* @param StmtsAwareInterface $node
|
||||
*/
|
||||
public function refactor(Node $node) : ?Node
|
||||
{
|
||||
if (!$node->keyVar instanceof Expr) {
|
||||
if ($node->stmts === null) {
|
||||
return null;
|
||||
}
|
||||
$keyVar = $node->keyVar;
|
||||
$isNodeUsed = (bool) $this->betterNodeFinder->findFirst($node->stmts, function (Node $node) use($keyVar) : bool {
|
||||
return $this->nodeComparator->areNodesEqual($node, $keyVar);
|
||||
});
|
||||
if ($isNodeUsed) {
|
||||
return null;
|
||||
$hasChanged = \false;
|
||||
foreach ($node->stmts as $key => $stmt) {
|
||||
if (!$stmt instanceof Foreach_) {
|
||||
continue;
|
||||
}
|
||||
if (!$stmt->keyVar instanceof Variable) {
|
||||
continue;
|
||||
}
|
||||
$keyVar = $stmt->keyVar;
|
||||
$isNodeUsed = (bool) $this->betterNodeFinder->findFirst($stmt->stmts, function (Node $node) use($keyVar) : bool {
|
||||
return $this->nodeComparator->areNodesEqual($node, $keyVar);
|
||||
});
|
||||
if ($isNodeUsed) {
|
||||
continue;
|
||||
}
|
||||
if ($this->stmtsManipulator->isVariableUsedInNextStmt($node, $key + 1, (string) $this->getName($keyVar))) {
|
||||
continue;
|
||||
}
|
||||
$stmt->keyVar = null;
|
||||
$hasChanged = \true;
|
||||
}
|
||||
$node->keyVar = null;
|
||||
return $node;
|
||||
if ($hasChanged) {
|
||||
return $node;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -19,12 +19,12 @@ final class VersionResolver
|
|||
* @api
|
||||
* @var string
|
||||
*/
|
||||
public const PACKAGE_VERSION = '6cb63f3752012f33f691844906e56d1045c8bfcf';
|
||||
public const PACKAGE_VERSION = '6d96068b0232141101c238752d0c5e80d5781f74';
|
||||
/**
|
||||
* @api
|
||||
* @var string
|
||||
*/
|
||||
public const RELEASE_DATE = '2023-10-10 17:42:30';
|
||||
public const RELEASE_DATE = '2023-10-10 18:22:13';
|
||||
/**
|
||||
* @var int
|
||||
*/
|
||||
|
|
Loading…
Reference in New Issue