Skip to content

Commit e5e2ba2

Browse files
authored
Revert "Ensure reindex in deep stmts before refactor() (#6614)" (#6615)
This reverts commit 89441c4.
1 parent 89441c4 commit e5e2ba2

File tree

6 files changed

+18
-48
lines changed

6 files changed

+18
-48
lines changed

src/DependencyInjection/LazyContainerFactory.php

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,6 @@
120120
use Rector\PhpParser\Comparing\NodeComparator;
121121
use Rector\PhpParser\Node\NodeFactory;
122122
use Rector\PhpParser\NodeTraverser\RectorNodeTraverser;
123-
use Rector\PhpParser\NodeTraverser\ReIndexNodeAttributesTraverser;
124123
use Rector\PHPStanStaticTypeMapper\Contract\TypeMapperInterface;
125124
use Rector\PHPStanStaticTypeMapper\PHPStanStaticTypeMapper;
126125
use Rector\PHPStanStaticTypeMapper\TypeMapper\AccessoryLiteralStringTypeMapper;
@@ -550,7 +549,6 @@ static function (AbstractRector $rector, Container $container): void {
550549
$container->get(CurrentFileProvider::class),
551550
$container->get(CreatedByRuleDecorator::class),
552551
$container->get(ChangedNodeScopeRefresher::class),
553-
$container->get(ReIndexNodeAttributesTraverser::class)
554552
);
555553
}
556554
);

src/NodeTypeResolver/PHPStan/Scope/PHPStanNodeScopeResolver.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@
105105
use Rector\NodeTypeResolver\Node\AttributeKey;
106106
use Rector\NodeTypeResolver\PHPStan\Scope\Contract\NodeVisitor\ScopeResolverNodeVisitorInterface;
107107
use Rector\PhpParser\Node\CustomNode\FileWithoutNamespace;
108-
use Rector\PhpParser\NodeTraverser\ReIndexNodeAttributesTraverser;
108+
use Rector\PHPStan\NodeVisitor\ReIndexNodeAttributeVisitor;
109109
use Rector\PHPStan\NodeVisitor\UnreachableStatementNodeVisitor;
110110
use Rector\PHPStan\NodeVisitor\WrappedNodeRestoringNodeVisitor;
111111
use Rector\Util\Reflection\PrivatesAccessor;
@@ -134,8 +134,7 @@ public function __construct(
134134
private ScopeFactory $scopeFactory,
135135
private PrivatesAccessor $privatesAccessor,
136136
private NodeNameResolver $nodeNameResolver,
137-
private ClassAnalyzer $classAnalyzer,
138-
private ReIndexNodeAttributesTraverser $reIndexNodeAttributesTraverser
137+
private ClassAnalyzer $classAnalyzer
139138
) {
140139
$this->nodeTraverser = new NodeTraverser();
141140

@@ -164,7 +163,8 @@ public function processNodes(
164163
// due to PHPStan doing printing internally on process nodes
165164
// using reindex via NodeVisitor on purpose to ensure reindex happen even on deep node changed
166165
if ($formerMutatingScope instanceof MutatingScope) {
167-
$stmts = $this->reIndexNodeAttributesTraverser->traverse($stmts);
166+
$nodeTraverser = new NodeTraverser(new ReIndexNodeAttributeVisitor());
167+
$stmts = $nodeTraverser->traverse($stmts);
168168
}
169169

170170
$this->nodeTraverser->traverse($stmts);

src/PhpParser/NodeTraverser/ReIndexNodeAttributesTraverser.php

Lines changed: 0 additions & 16 deletions
This file was deleted.

src/Rector/AbstractRector.php

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
use PHPStan\Type\ObjectType;
2121
use PHPStan\Type\Type;
2222
use Rector\Application\ChangedNodeScopeRefresher;
23+
use Rector\Application\NodeAttributeReIndexer;
2324
use Rector\Application\Provider\CurrentFileProvider;
2425
use Rector\BetterPhpDocParser\PhpDocInfo\PhpDocInfo;
2526
use Rector\ChangesReporting\ValueObject\RectorWithLineChange;
@@ -33,7 +34,6 @@
3334
use Rector\PhpDocParser\NodeTraverser\SimpleCallableNodeTraverser;
3435
use Rector\PhpParser\Comparing\NodeComparator;
3536
use Rector\PhpParser\Node\NodeFactory;
36-
use Rector\PhpParser\NodeTraverser\ReIndexNodeAttributesTraverser;
3737
use Rector\Skipper\Skipper\Skipper;
3838
use Rector\ValueObject\Application\File;
3939

@@ -79,8 +79,6 @@ abstract class AbstractRector extends NodeVisitorAbstract implements RectorInter
7979

8080
private CreatedByRuleDecorator $createdByRuleDecorator;
8181

82-
private ReIndexNodeAttributesTraverser $reIndexNodeAttributesTraverser;
83-
8482
private ?int $toBeRemovedNodeId = null;
8583

8684
public function autowire(
@@ -93,7 +91,6 @@ public function autowire(
9391
CurrentFileProvider $currentFileProvider,
9492
CreatedByRuleDecorator $createdByRuleDecorator,
9593
ChangedNodeScopeRefresher $changedNodeScopeRefresher,
96-
ReIndexNodeAttributesTraverser $reIndexNodeAttributesTraverser
9794
): void {
9895
$this->nodeNameResolver = $nodeNameResolver;
9996
$this->nodeTypeResolver = $nodeTypeResolver;
@@ -104,7 +101,6 @@ public function autowire(
104101
$this->currentFileProvider = $currentFileProvider;
105102
$this->createdByRuleDecorator = $createdByRuleDecorator;
106103
$this->changedNodeScopeRefresher = $changedNodeScopeRefresher;
107-
$this->reIndexNodeAttributesTraverser = $reIndexNodeAttributesTraverser;
108104
}
109105

110106
/**
@@ -143,7 +139,7 @@ final public function enterNode(Node $node): int|Node|null
143139
// ensure origNode pulled before refactor to avoid changed during refactor, ref https://3v4l.org/YMEGN
144140
$originalNode = $node->getAttribute(AttributeKey::ORIGINAL_NODE) ?? $node;
145141

146-
$this->reIndexNodeAttributesTraverser->traverse([$node]);
142+
NodeAttributeReIndexer::reIndexNodeAttributes($node);
147143

148144
$refactoredNode = $this->refactor($node);
149145

tests/Issues/IndexedStmt/Fixture/fixture.php.inc

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,9 @@ final class Fixture
66
{
77
public function run()
88
{
9-
if (rand(0, 1)) {
10-
'with index 0';
11-
'with index 1';
12-
'with index 2';
13-
}
9+
'with index 0';
10+
'with index 1';
11+
'with index 2';
1412
}
1513
}
1614

@@ -24,10 +22,8 @@ final class Fixture
2422
{
2523
public function run()
2624
{
27-
if (rand(0, 1)) {
28-
'with index 0';
29-
'final index';
30-
}
25+
'with index 0';
26+
'final index';
3127
}
3228
}
3329

tests/Issues/IndexedStmt/Source/ChangeLastIndex1Rector.php

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@
66

77
use PhpParser\Node;
88
use PhpParser\Node\Scalar\String_;
9-
use PhpParser\Node\Stmt\ClassMethod;
109
use PhpParser\Node\Stmt\Expression;
11-
use PhpParser\Node\Stmt\If_;
10+
use PhpParser\NodeVisitor;
11+
use Rector\Contract\PhpParser\Node\StmtsAwareInterface;
1212
use Rector\NodeTypeResolver\Node\AttributeKey;
1313
use Rector\Rector\AbstractRector;
1414
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
@@ -22,11 +22,11 @@ public function getRuleDefinition(): RuleDefinition
2222

2323
public function getNodeTypes(): array
2424
{
25-
return [ClassMethod::class];
25+
return [StmtsAwareInterface::class];
2626
}
2727

2828
/**
29-
* @param ClassMethod $node
29+
* @param StmtsAwareInterface $node
3030
*/
3131
public function refactor(Node $node)
3232
{
@@ -35,13 +35,9 @@ public function refactor(Node $node)
3535
}
3636

3737
foreach ($node->stmts as $stmt) {
38-
if ($stmt instanceof If_) {
39-
foreach ($stmt->stmts as $childStmt) {
40-
if ($childStmt->getAttribute(AttributeKey::STMT_KEY) === 1 && $childStmt instanceof Expression && $childStmt->expr instanceof String_ && $childStmt->expr->value === 'with index 2') {
41-
$childStmt->expr->value = 'final index';
42-
return $node;
43-
}
44-
}
38+
if ($stmt->getAttribute(AttributeKey::STMT_KEY) === 1 && $stmt instanceof Expression && $stmt->expr instanceof String_ && $stmt->expr->value === 'with index 2') {
39+
$stmt->expr->value = 'final index';
40+
return $node;
4541
}
4642
}
4743

0 commit comments

Comments
 (0)