Skip to content

Commit d12d4d6

Browse files
authored
optimize AbstractImmutableNodeTraverser, remove deprecated constants (#7718)
* static fixes * optimize AbstractImmutableNodeTraverser, remove deprecated constants * misc
1 parent 147c266 commit d12d4d6

File tree

3 files changed

+26
-71
lines changed

3 files changed

+26
-71
lines changed

phpstan.neon

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,12 @@ parameters:
4848
narrow_return: true
4949

5050
ignoreErrors:
51+
# required generics on interface array, not helpful
5152
-
53+
message: '#Method (.*?)\:\:__construct\(\) has parameter#'
54+
identifier: missingType.generics
55+
-
56+
message: '#(.*?) with generic interface (.*?)Interface does not specify its types#'
5257
identifier: missingType.generics
5358

5459
# phpstan class instance
@@ -135,7 +140,6 @@ parameters:
135140

136141
# internal reflection
137142
- '#Instead of "new ClassReflection\(\)" use ReflectionProvider service or "\(new PHPStan\\Reflection\\ClassReflection\(<desired_type>\)\)" for static reflection to work#'
138-
139143
- '#Callable callable\(PHPStan\\Type\\Type\)\: PHPStan\\Type\\Type invoked with 2 parameters, 1 required#'
140144

141145
# known value
@@ -181,10 +185,15 @@ parameters:
181185
# fixture class
182186
- '#Class "Rector\\Tests\\Issues\\ScopeNotAvailable\\Variable\\ArrayItemForeachValueRector" is missing @see annotation with test case class reference#'
183187

188+
# classes are part of *.php.inc fixture
184189
-
185190
message: '#Class (.*?) not found#'
186191
paths:
187-
- rules-tests/*/config/*
192+
- rules-tests/Arguments/Rector/ClassMethod/ReplaceArgumentDefaultValueRector/config
193+
- rules-tests/TypeDeclaration/Rector/ClassMethod/AddReturnTypeDeclarationRector/config
194+
- rules-tests/Renaming/Rector/Name/RenameClassRector/config
195+
- rules-tests/Renaming/Rector/MethodCall/RenameMethodRector/config
196+
- rules-tests/Arguments/Rector/ClassMethod/ArgumentAdderRector/config
188197

189198
-
190199
message: '#Function "(function_exists|dump_node)\(\)" cannot be used/left in the code#'
@@ -334,12 +343,6 @@ parameters:
334343
identifier: arrayValues.list
335344
path: rules/CodingStyle/Application/UseImportsAdder.php
336345

337-
-
338-
message: '#^Register "Rector\\Php74\\Rector\\Double\\RealToFloatTypeCastRector" service to "php74\.php" config set$#'
339-
identifier: rector.upgradeDowngradeRegisteredInSet
340-
count: 1
341-
path: rules/Php74/Rector/Double/RealToFloatTypeCastRector.php
342-
343346
-
344347
message: '#Offset float\|int\|string might not exist on string#'
345348
path: rules/Php70/EregToPcreTransformer.php
@@ -387,14 +390,11 @@ parameters:
387390
-
388391
paths:
389392
- rules/TypeDeclaration/Rector/StmtsAwareInterface/IncreaseDeclareStrictTypesRector.php
390-
- rules/TypeDeclaration/Rector/StmtsAwareInterface/DeclareStrictTypesRector.php
391393
identifier: rector.noOnlyNullReturnInRefactor
392394

393395
-
394396
identifier: rector.noIntegerRefactorReturn
395397
paths:
396-
- rules/Transform/Rector/ArrayDimFetch/ArrayDimFetchToMethodCallRector.php
397-
398398
# valid, as use REMOVE_NODE
399399
- rules/DeadCode/Rector/Expression/RemoveDeadStmtRector.php
400400
- rules/DeadCode/Rector/If_/RemoveDeadInstanceOfRector.php

src/PhpParser/NodeTraverser/AbstractImmutableNodeTraverser.php

Lines changed: 15 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -10,29 +10,10 @@
1010
use PhpParser\Node\Stmt;
1111
use PhpParser\NodeTraverserInterface;
1212
use PhpParser\NodeVisitor;
13+
use Rector\Exception\ShouldNotHappenException;
1314

1415
abstract class AbstractImmutableNodeTraverser implements NodeTraverserInterface
1516
{
16-
/**
17-
* @deprecated Use NodeVisitor::DONT_TRAVERSE_CHILDREN instead.
18-
*/
19-
public const DONT_TRAVERSE_CHILDREN = NodeVisitor::DONT_TRAVERSE_CHILDREN;
20-
21-
/**
22-
* @deprecated Use NodeVisitor::STOP_TRAVERSAL instead.
23-
*/
24-
public const STOP_TRAVERSAL = NodeVisitor::STOP_TRAVERSAL;
25-
26-
/**
27-
* @deprecated Use NodeVisitor::REMOVE_NODE instead.
28-
*/
29-
public const REMOVE_NODE = NodeVisitor::REMOVE_NODE;
30-
31-
/**
32-
* @deprecated Use NodeVisitor::DONT_TRAVERSE_CURRENT_AND_CHILDREN instead.
33-
*/
34-
public const DONT_TRAVERSE_CURRENT_AND_CHILDREN = NodeVisitor::DONT_TRAVERSE_CURRENT_AND_CHILDREN;
35-
3617
/**
3718
* @var list<NodeVisitor> Visitors
3819
*/
@@ -63,15 +44,9 @@ public function addVisitor(NodeVisitor $visitor): void
6344
$this->visitors[] = $visitor;
6445
}
6546

66-
/**
67-
* Removes an added visitor.
68-
*/
6947
public function removeVisitor(NodeVisitor $visitor): void
7048
{
71-
$index = array_search($visitor, $this->visitors, true);
72-
if ($index !== false) {
73-
array_splice($this->visitors, $index, 1, []);
74-
}
49+
throw new ShouldNotHappenException('The immutable node traverser does not support removing visitors.');
7550
}
7651

7752
/**
@@ -106,11 +81,6 @@ public function traverse(array $nodes): array
10681
*/
10782
abstract public function getVisitorsForNode(Node $node): array;
10883

109-
/**
110-
* Recursively traverse a node.
111-
*
112-
* @param Node $node Node to traverse.
113-
*/
11484
protected function traverseNode(Node $node): void
11585
{
11686
foreach ($node->getSubNodeNames() as $name) {
@@ -189,10 +159,7 @@ protected function traverseNode(Node $node): void
189159
}
190160

191161
/**
192-
* Recursively traverse array (usually of nodes).
193-
*
194-
* @param Node[] $nodes Array to traverse
195-
*
162+
* @param Node[] $nodes
196163
* @return array Result of traversal (may be original array or changed one)
197164
*/
198165
protected function traverseArray(array $nodes): array
@@ -285,16 +252,20 @@ protected function traverseArray(array $nodes): array
285252

286253
private function ensureReplacementReasonable(Node $old, Node $new): void
287254
{
288-
if ($old instanceof Stmt && $new instanceof Expr) {
289-
throw new LogicException(
290-
sprintf('Trying to replace statement (%s) ', $old->getType()) . sprintf(
291-
'with expression (%s). Are you missing a ',
292-
$new->getType()
293-
) . 'Stmt_Expression wrapper?'
294-
);
255+
if ($old instanceof Stmt) {
256+
if ($new instanceof Expr) {
257+
throw new LogicException(
258+
sprintf('Trying to replace statement (%s) ', $old->getType()) . sprintf(
259+
'with expression (%s). Are you missing a ',
260+
$new->getType()
261+
) . 'Stmt_Expression wrapper?'
262+
);
263+
}
264+
265+
return;
295266
}
296267

297-
if ($old instanceof Expr && $new instanceof Stmt) {
268+
if ($new instanceof Stmt) {
298269
throw new LogicException(
299270
sprintf('Trying to replace expression (%s) ', $old->getType()) . sprintf(
300271
'with statement (%s)',

tests/PhpParser/NodeTraverser/RectorNodeTraverserTest.php

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -76,20 +76,4 @@ public function testGetVisitorsForNodeWhenAllVisitorsMatch(): void
7676

7777
$this->assertEquals([$this->ruleUsingClassRector, $this->ruleUsingClassLikeRector], $visitors);
7878
}
79-
80-
public function testGetVisitorsForNodeUsesCachedValue(): void
81-
{
82-
$class = new Class_('test');
83-
$this->rectorNodeTraverser->addVisitor($this->ruleUsingClassRector);
84-
$this->rectorNodeTraverser->addVisitor($this->ruleUsingClassLikeRector);
85-
86-
$visitors = $this->rectorNodeTraverser->getVisitorsForNode($class);
87-
88-
$this->assertEquals([$this->ruleUsingClassRector, $this->ruleUsingClassLikeRector], $visitors);
89-
90-
$this->rectorNodeTraverser->removeVisitor($this->ruleUsingClassRector);
91-
$visitors = $this->rectorNodeTraverser->getVisitorsForNode($class);
92-
93-
$this->assertEquals([$this->ruleUsingClassRector, $this->ruleUsingClassLikeRector], $visitors);
94-
}
9579
}

0 commit comments

Comments
 (0)