From e5e2ba2c721aebd507d338d77996e45ca84567ba Mon Sep 17 00:00:00 2001 From: Abdul Malik Ikhsan Date: Thu, 19 Dec 2024 12:02:15 +0700 Subject: [PATCH] Revert "Ensure reindex in deep stmts before refactor() (#6614)" (#6615) This reverts commit 89441c419b0a8b9817b89b963af1dcfff804d3f7. --- .../LazyContainerFactory.php | 2 -- .../PHPStan/Scope/PHPStanNodeScopeResolver.php | 8 ++++---- .../ReIndexNodeAttributesTraverser.php | 16 ---------------- src/Rector/AbstractRector.php | 8 ++------ .../Issues/IndexedStmt/Fixture/fixture.php.inc | 14 +++++--------- .../Source/ChangeLastIndex1Rector.php | 18 +++++++----------- 6 files changed, 18 insertions(+), 48 deletions(-) delete mode 100644 src/PhpParser/NodeTraverser/ReIndexNodeAttributesTraverser.php diff --git a/src/DependencyInjection/LazyContainerFactory.php b/src/DependencyInjection/LazyContainerFactory.php index 039ef176e56..3f7231cb054 100644 --- a/src/DependencyInjection/LazyContainerFactory.php +++ b/src/DependencyInjection/LazyContainerFactory.php @@ -120,7 +120,6 @@ use Rector\PhpParser\Comparing\NodeComparator; use Rector\PhpParser\Node\NodeFactory; use Rector\PhpParser\NodeTraverser\RectorNodeTraverser; -use Rector\PhpParser\NodeTraverser\ReIndexNodeAttributesTraverser; use Rector\PHPStanStaticTypeMapper\Contract\TypeMapperInterface; use Rector\PHPStanStaticTypeMapper\PHPStanStaticTypeMapper; use Rector\PHPStanStaticTypeMapper\TypeMapper\AccessoryLiteralStringTypeMapper; @@ -550,7 +549,6 @@ static function (AbstractRector $rector, Container $container): void { $container->get(CurrentFileProvider::class), $container->get(CreatedByRuleDecorator::class), $container->get(ChangedNodeScopeRefresher::class), - $container->get(ReIndexNodeAttributesTraverser::class) ); } ); diff --git a/src/NodeTypeResolver/PHPStan/Scope/PHPStanNodeScopeResolver.php b/src/NodeTypeResolver/PHPStan/Scope/PHPStanNodeScopeResolver.php index 2ab197bb35a..a2b424bdb0e 100644 --- a/src/NodeTypeResolver/PHPStan/Scope/PHPStanNodeScopeResolver.php +++ b/src/NodeTypeResolver/PHPStan/Scope/PHPStanNodeScopeResolver.php @@ -105,7 +105,7 @@ use Rector\NodeTypeResolver\Node\AttributeKey; use Rector\NodeTypeResolver\PHPStan\Scope\Contract\NodeVisitor\ScopeResolverNodeVisitorInterface; use Rector\PhpParser\Node\CustomNode\FileWithoutNamespace; -use Rector\PhpParser\NodeTraverser\ReIndexNodeAttributesTraverser; +use Rector\PHPStan\NodeVisitor\ReIndexNodeAttributeVisitor; use Rector\PHPStan\NodeVisitor\UnreachableStatementNodeVisitor; use Rector\PHPStan\NodeVisitor\WrappedNodeRestoringNodeVisitor; use Rector\Util\Reflection\PrivatesAccessor; @@ -134,8 +134,7 @@ public function __construct( private ScopeFactory $scopeFactory, private PrivatesAccessor $privatesAccessor, private NodeNameResolver $nodeNameResolver, - private ClassAnalyzer $classAnalyzer, - private ReIndexNodeAttributesTraverser $reIndexNodeAttributesTraverser + private ClassAnalyzer $classAnalyzer ) { $this->nodeTraverser = new NodeTraverser(); @@ -164,7 +163,8 @@ public function processNodes( // due to PHPStan doing printing internally on process nodes // using reindex via NodeVisitor on purpose to ensure reindex happen even on deep node changed if ($formerMutatingScope instanceof MutatingScope) { - $stmts = $this->reIndexNodeAttributesTraverser->traverse($stmts); + $nodeTraverser = new NodeTraverser(new ReIndexNodeAttributeVisitor()); + $stmts = $nodeTraverser->traverse($stmts); } $this->nodeTraverser->traverse($stmts); diff --git a/src/PhpParser/NodeTraverser/ReIndexNodeAttributesTraverser.php b/src/PhpParser/NodeTraverser/ReIndexNodeAttributesTraverser.php deleted file mode 100644 index 577552d5ec7..00000000000 --- a/src/PhpParser/NodeTraverser/ReIndexNodeAttributesTraverser.php +++ /dev/null @@ -1,16 +0,0 @@ -nodeNameResolver = $nodeNameResolver; $this->nodeTypeResolver = $nodeTypeResolver; @@ -104,7 +101,6 @@ public function autowire( $this->currentFileProvider = $currentFileProvider; $this->createdByRuleDecorator = $createdByRuleDecorator; $this->changedNodeScopeRefresher = $changedNodeScopeRefresher; - $this->reIndexNodeAttributesTraverser = $reIndexNodeAttributesTraverser; } /** @@ -143,7 +139,7 @@ final public function enterNode(Node $node): int|Node|null // ensure origNode pulled before refactor to avoid changed during refactor, ref https://3v4l.org/YMEGN $originalNode = $node->getAttribute(AttributeKey::ORIGINAL_NODE) ?? $node; - $this->reIndexNodeAttributesTraverser->traverse([$node]); + NodeAttributeReIndexer::reIndexNodeAttributes($node); $refactoredNode = $this->refactor($node); diff --git a/tests/Issues/IndexedStmt/Fixture/fixture.php.inc b/tests/Issues/IndexedStmt/Fixture/fixture.php.inc index b4c945a5e61..ad2d646c37e 100644 --- a/tests/Issues/IndexedStmt/Fixture/fixture.php.inc +++ b/tests/Issues/IndexedStmt/Fixture/fixture.php.inc @@ -6,11 +6,9 @@ final class Fixture { public function run() { - if (rand(0, 1)) { - 'with index 0'; - 'with index 1'; - 'with index 2'; - } + 'with index 0'; + 'with index 1'; + 'with index 2'; } } @@ -24,10 +22,8 @@ final class Fixture { public function run() { - if (rand(0, 1)) { - 'with index 0'; - 'final index'; - } + 'with index 0'; + 'final index'; } } diff --git a/tests/Issues/IndexedStmt/Source/ChangeLastIndex1Rector.php b/tests/Issues/IndexedStmt/Source/ChangeLastIndex1Rector.php index 1d8e46daa65..437b5b92630 100644 --- a/tests/Issues/IndexedStmt/Source/ChangeLastIndex1Rector.php +++ b/tests/Issues/IndexedStmt/Source/ChangeLastIndex1Rector.php @@ -6,9 +6,9 @@ use PhpParser\Node; use PhpParser\Node\Scalar\String_; -use PhpParser\Node\Stmt\ClassMethod; use PhpParser\Node\Stmt\Expression; -use PhpParser\Node\Stmt\If_; +use PhpParser\NodeVisitor; +use Rector\Contract\PhpParser\Node\StmtsAwareInterface; use Rector\NodeTypeResolver\Node\AttributeKey; use Rector\Rector\AbstractRector; use Symplify\RuleDocGenerator\ValueObject\RuleDefinition; @@ -22,11 +22,11 @@ public function getRuleDefinition(): RuleDefinition public function getNodeTypes(): array { - return [ClassMethod::class]; + return [StmtsAwareInterface::class]; } /** - * @param ClassMethod $node + * @param StmtsAwareInterface $node */ public function refactor(Node $node) { @@ -35,13 +35,9 @@ public function refactor(Node $node) } foreach ($node->stmts as $stmt) { - if ($stmt instanceof If_) { - foreach ($stmt->stmts as $childStmt) { - if ($childStmt->getAttribute(AttributeKey::STMT_KEY) === 1 && $childStmt instanceof Expression && $childStmt->expr instanceof String_ && $childStmt->expr->value === 'with index 2') { - $childStmt->expr->value = 'final index'; - return $node; - } - } + if ($stmt->getAttribute(AttributeKey::STMT_KEY) === 1 && $stmt instanceof Expression && $stmt->expr instanceof String_ && $stmt->expr->value === 'with index 2') { + $stmt->expr->value = 'final index'; + return $node; } }