Skip to content

Commit

Permalink
Revert "Ensure reindex in deep stmts before refactor() (#6614)" (#6615)
Browse files Browse the repository at this point in the history
This reverts commit 89441c4.
  • Loading branch information
samsonasik authored Dec 19, 2024
1 parent 89441c4 commit e5e2ba2
Show file tree
Hide file tree
Showing 6 changed files with 18 additions and 48 deletions.
2 changes: 0 additions & 2 deletions src/DependencyInjection/LazyContainerFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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)
);
}
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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();

Expand Down Expand Up @@ -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);
Expand Down
16 changes: 0 additions & 16 deletions src/PhpParser/NodeTraverser/ReIndexNodeAttributesTraverser.php

This file was deleted.

8 changes: 2 additions & 6 deletions src/Rector/AbstractRector.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
use PHPStan\Type\ObjectType;
use PHPStan\Type\Type;
use Rector\Application\ChangedNodeScopeRefresher;
use Rector\Application\NodeAttributeReIndexer;
use Rector\Application\Provider\CurrentFileProvider;
use Rector\BetterPhpDocParser\PhpDocInfo\PhpDocInfo;
use Rector\ChangesReporting\ValueObject\RectorWithLineChange;
Expand All @@ -33,7 +34,6 @@
use Rector\PhpDocParser\NodeTraverser\SimpleCallableNodeTraverser;
use Rector\PhpParser\Comparing\NodeComparator;
use Rector\PhpParser\Node\NodeFactory;
use Rector\PhpParser\NodeTraverser\ReIndexNodeAttributesTraverser;
use Rector\Skipper\Skipper\Skipper;
use Rector\ValueObject\Application\File;

Expand Down Expand Up @@ -79,8 +79,6 @@ abstract class AbstractRector extends NodeVisitorAbstract implements RectorInter

private CreatedByRuleDecorator $createdByRuleDecorator;

private ReIndexNodeAttributesTraverser $reIndexNodeAttributesTraverser;

private ?int $toBeRemovedNodeId = null;

public function autowire(
Expand All @@ -93,7 +91,6 @@ public function autowire(
CurrentFileProvider $currentFileProvider,
CreatedByRuleDecorator $createdByRuleDecorator,
ChangedNodeScopeRefresher $changedNodeScopeRefresher,
ReIndexNodeAttributesTraverser $reIndexNodeAttributesTraverser
): void {
$this->nodeNameResolver = $nodeNameResolver;
$this->nodeTypeResolver = $nodeTypeResolver;
Expand All @@ -104,7 +101,6 @@ public function autowire(
$this->currentFileProvider = $currentFileProvider;
$this->createdByRuleDecorator = $createdByRuleDecorator;
$this->changedNodeScopeRefresher = $changedNodeScopeRefresher;
$this->reIndexNodeAttributesTraverser = $reIndexNodeAttributesTraverser;
}

/**
Expand Down Expand Up @@ -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);

Expand Down
14 changes: 5 additions & 9 deletions tests/Issues/IndexedStmt/Fixture/fixture.php.inc
Original file line number Diff line number Diff line change
Expand Up @@ -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';
}
}

Expand All @@ -24,10 +22,8 @@ final class Fixture
{
public function run()
{
if (rand(0, 1)) {
'with index 0';
'final index';
}
'with index 0';
'final index';
}
}

Expand Down
18 changes: 7 additions & 11 deletions tests/Issues/IndexedStmt/Source/ChangeLastIndex1Rector.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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)
{
Expand All @@ -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;
}
}

Expand Down

0 comments on commit e5e2ba2

Please sign in to comment.