Updated Rector to commit 3e414f34039da79932322ed6e1f906454ab8c366

3e414f3403 [NodeTypeResolver Ensure reindex stmt_key on NodeAttributeReIndexer (#6611)
This commit is contained in:
Tomas Votruba 2024-12-18 22:27:49 +00:00
parent 7180d53b03
commit b0043cf604
7 changed files with 27 additions and 22 deletions

View File

@ -103,7 +103,7 @@ CODE_SAMPLE
$assignedVariableNamesByStmtPosition = $this->resolvedAssignedVariablesByStmtPosition($stmts);
$hasChanged = \false;
foreach ($assignedVariableNamesByStmtPosition as $stmtPosition => $variableName) {
if ($this->stmtsManipulator->isVariableUsedInNextStmt($stmts, $stmtPosition + 1, $variableName)) {
if ($this->stmtsManipulator->isVariableUsedInNextStmt($node, $stmtPosition + 1, $variableName)) {
continue;
}
/** @var Expression<Assign> $currentStmt */

View File

@ -48,7 +48,7 @@ final class RemoveDeadIfForeachForRector extends AbstractRector
return new RuleDefinition('Remove if, foreach and for that does not do anything', [new CodeSample(<<<'CODE_SAMPLE'
class SomeClass
{
public function run($value)
public function run($value, $differrentValue)
{
if ($value) {
}
@ -56,16 +56,16 @@ class SomeClass
foreach ($values as $value) {
}
return $value;
return $differentValue;
}
}
CODE_SAMPLE
, <<<'CODE_SAMPLE'
class SomeClass
{
public function run($value)
public function run($value, $differrentValue)
{
return $value;
return $differentValue;
}
}
CODE_SAMPLE
@ -129,11 +129,10 @@ CODE_SAMPLE
*/
private function processForForeach($for, int $key, StmtsAwareInterface $stmtsAware) : void
{
$stmts = (array) $stmtsAware->stmts;
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($stmts, $key + 1, (string) $this->getName($variable))) {
if ($this->stmtsManipulator->isVariableUsedInNextStmt($stmtsAware, $key + 1, (string) $this->getName($variable))) {
return;
}
}
@ -144,7 +143,7 @@ CODE_SAMPLE
$exprs = [$for->expr, $for->valueVar, $for->valueVar];
$variables = $this->betterNodeFinder->findInstanceOf($exprs, Variable::class);
foreach ($variables as $variable) {
if ($this->stmtsManipulator->isVariableUsedInNextStmt($stmts, $key + 1, (string) $this->getName($variable))) {
if ($this->stmtsManipulator->isVariableUsedInNextStmt($stmtsAware, $key + 1, (string) $this->getName($variable))) {
return;
}
}

View File

@ -102,11 +102,6 @@ CODE_SAMPLE
return null;
}
$hasChanged = \false;
// stmts variable defined to avoid unset overlap when used via array_slice() on
// StmtsManipulator::isVariableUsedInNextStmt()
// @see https://github.com/rectorphp/rector-src/pull/5968
// @see https://3v4l.org/eojhk
$stmts = (array) $constructClassMethod->stmts;
foreach ((array) $constructClassMethod->stmts as $key => $stmt) {
foreach ($params as $param) {
$paramName = $this->getName($param);
@ -114,7 +109,7 @@ CODE_SAMPLE
if (!$coalesce instanceof Coalesce) {
continue;
}
if ($this->stmtsManipulator->isVariableUsedInNextStmt($stmts, $key + 1, $paramName)) {
if ($this->stmtsManipulator->isVariableUsedInNextStmt($constructClassMethod, $key + 1, $paramName)) {
continue;
}
/** @var NullableType $currentParamType */

View File

@ -21,12 +21,17 @@ use PhpParser\Node\Stmt\If_;
use PhpParser\Node\Stmt\Switch_;
use PhpParser\Node\Stmt\TryCatch;
use Rector\Contract\PhpParser\Node\StmtsAwareInterface;
use Rector\NodeTypeResolver\Node\AttributeKey;
final class NodeAttributeReIndexer
{
public static function reIndexNodeAttributes(Node $node) : ?Node
{
if (($node instanceof StmtsAwareInterface || $node instanceof ClassLike || $node instanceof Declare_) && $node->stmts !== null) {
$node->stmts = \array_values($node->stmts);
// re-index stmt key under current node
foreach ($node->stmts as $key => $childStmt) {
$childStmt->setAttribute(AttributeKey::STMT_KEY, $key);
}
if ($node instanceof If_) {
$node->elseifs = \array_values($node->elseifs);
return $node;

View File

@ -19,12 +19,12 @@ final class VersionResolver
* @api
* @var string
*/
public const PACKAGE_VERSION = 'c5ac495242295f73aaa0dadad8cfebe901735422';
public const PACKAGE_VERSION = '3e414f34039da79932322ed6e1f906454ab8c366';
/**
* @api
* @var string
*/
public const RELEASE_DATE = '2024-12-18 18:43:20';
public const RELEASE_DATE = '2024-12-19 05:25:16';
/**
* @var int
*/

View File

@ -76,15 +76,20 @@ final class StmtsManipulator
});
return $stmts;
}
/**
* @param StmtsAwareInterface|Stmt[] $stmtsAware
*/
public function isVariableUsedInNextStmt($stmtsAware, int $jumpToKey, string $variableName) : bool
public function isVariableUsedInNextStmt(StmtsAwareInterface $stmtsAware, int $jumpToKey, string $variableName) : bool
{
if ($stmtsAware instanceof StmtsAwareInterface && $stmtsAware->stmts === null) {
if ($stmtsAware->stmts === null) {
return \false;
}
$stmts = \array_slice($stmtsAware instanceof StmtsAwareInterface ? $stmtsAware->stmts : $stmtsAware, $jumpToKey, null, \true);
$lastKey = \array_key_last($stmtsAware->stmts);
$stmts = [];
for ($key = $jumpToKey; $key <= $lastKey; ++$key) {
if (!isset($stmtsAware->stmts[$key])) {
// can be just removed
continue;
}
$stmts[] = $stmtsAware->stmts[$key];
}
if ($stmtsAware instanceof TryCatch) {
$stmts = \array_merge($stmts, $stmtsAware->catches);
if ($stmtsAware->finally instanceof Finally_) {

View File

@ -20,6 +20,7 @@ final class StmtKeyNodeVisitor extends NodeVisitorAbstract implements ScopeResol
if ($node->stmts === null) {
return null;
}
$node->stmts = \array_values($node->stmts);
// re-index stmt key under current node
foreach ($node->stmts as $key => $childStmt) {
$childStmt->setAttribute(AttributeKey::STMT_KEY, $key);