Skip to content

Commit 7bb5249

Browse files
committed
[cleanup] cleanup AbstractRector from DONT_TRAVERSE_* enums as no longer supported, all rules work without it now
1 parent 08246ce commit 7bb5249

File tree

7 files changed

+32
-72
lines changed

7 files changed

+32
-72
lines changed

phpstan.neon

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -398,3 +398,11 @@ parameters:
398398
- rules/DeadCode/Rector/If_/RemoveDeadInstanceOfRector.php
399399
- rules/DeadCode/Rector/ConstFetch/RemovePhpVersionIdCheckRector.php
400400
- rules/DeadCode/Rector/If_/UnwrapFutureCompatibleIfPhpVersionRector.php
401+
402+
# condition check, just to be sure
403+
- '#Method Rector\\Rector\\AbstractRector\:\:enterNode\(\) never returns 3 so it can be removed from the return type#'
404+
405+
# special case, working on a file-level
406+
-
407+
identifier: rector.noOnlyNullReturnInRefactor
408+
path: rules/TypeDeclaration/Rector/StmtsAwareInterface/DeclareStrictTypesRector.php

rules/Transform/Rector/ArrayDimFetch/ArrayDimFetchToMethodCallRector.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,7 @@ private function handleIsset(Isset_ $isset): Expr|int|null
122122
$issets[] = $var;
123123
}
124124

125+
// @todo fix this
125126
if ($exprs === []) {
126127
return NodeVisitor::DONT_TRAVERSE_CHILDREN;
127128
}

rules/TypeDeclaration/Rector/StmtsAwareInterface/DeclareStrictTypesRector.php

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
use PhpParser\Node;
88
use PhpParser\Node\Stmt;
99
use PhpParser\Node\Stmt\Nop;
10-
use PhpParser\NodeVisitor;
1110
use Rector\ChangesReporting\ValueObject\RectorWithLineChange;
1211
use Rector\Contract\Rector\HTMLAverseRectorInterface;
1312
use Rector\PhpParser\Enum\NodeGroup;
@@ -103,11 +102,9 @@ public function getNodeTypes(): array
103102
/**
104103
* @param StmtsAware $node
105104
*/
106-
public function refactor(Node $node): int
105+
public function refactor(Node $node): ?Node
107106
{
108-
// workaround, as Rector now only hooks to specific nodes, not arrays
109-
// avoid traversing, as we already handled in beforeTraverse()
110-
return NodeVisitor::DONT_TRAVERSE_CURRENT_AND_CHILDREN;
107+
return null;
111108
}
112109

113110
public function provideMinPhpVersion(): int

src/Contract/Rector/RectorInterface.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ public function getNodeTypes(): array;
2020

2121
/**
2222
* Process Node of matched type
23-
* @return Node|Node[]|null|NodeVisitor::*
23+
* @return Node|Node[]|null|NodeVisitor::REMOVE_NODE
2424
*/
2525
public function refactor(Node $node);
2626
}

src/NodeTypeResolver/Node/AttributeKey.php

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -92,12 +92,6 @@ final class AttributeKey
9292
*/
9393
public const CREATED_BY_RULE = 'created_by_rule';
9494

95-
/**
96-
* Helps with skipped below node
97-
* @var string
98-
*/
99-
public const SKIPPED_BY_RECTOR_RULE = 'skipped_rector_rule';
100-
10195
/**
10296
* @var string
10397
*/

src/ProcessAnalyzer/RectifiedAnalyzer.php

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -29,16 +29,11 @@ public function __construct(
2929
public function hasRectified(string $rectorClass, Node $node): bool
3030
{
3131
$originalNode = $node->getAttribute(AttributeKey::ORIGINAL_NODE);
32-
3332
if ($this->hasConsecutiveCreatedByRule($rectorClass, $node, $originalNode)) {
3433
return true;
3534
}
3635

37-
if ($this->isJustReprintedOverlappedTokenStart($node, $originalNode)) {
38-
return true;
39-
}
40-
41-
return $node->getAttribute(AttributeKey::SKIPPED_BY_RECTOR_RULE) === $rectorClass;
36+
return $this->isJustReprintedOverlappedTokenStart($node, $originalNode);
4237
}
4338

4439
/**

src/Rector/AbstractRector.php

Lines changed: 19 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
use PhpParser\Node\Stmt\Nop;
1515
use PhpParser\Node\Stmt\Property;
1616
use PhpParser\Node\Stmt\Trait_;
17+
use PhpParser\NodeTraverser;
1718
use PhpParser\NodeVisitor;
1819
use PhpParser\NodeVisitorAbstract;
1920
use PHPStan\Analyser\MutatingScope;
@@ -120,6 +121,9 @@ public function beforeTraverse(array $nodes): ?array
120121
return null;
121122
}
122123

124+
/**
125+
* @return NodeTraverser::REMOVE_NODE|Node|null
126+
*/
123127
final public function enterNode(Node $node): int|Node|null
124128
{
125129
if (! $this->isMatchingNodeType($node)) {
@@ -138,57 +142,47 @@ final public function enterNode(Node $node): int|Node|null
138142
// ensure origNode pulled before refactor to avoid changed during refactor, ref https://3v4l.org/YMEGN
139143
$originalNode = $node->getAttribute(AttributeKey::ORIGINAL_NODE) ?? $node;
140144

141-
$refactoredNode = $this->refactor($node);
145+
$refactoredNodeOrState = $this->refactor($node);
142146

143147
// nothing to change → continue
144-
if ($refactoredNode === null) {
148+
if ($refactoredNodeOrState === null) {
145149
return null;
146150
}
147151

148-
if ($refactoredNode === []) {
152+
if ($refactoredNodeOrState === []) {
149153
$errorMessage = sprintf(self::EMPTY_NODE_ARRAY_MESSAGE, static::class);
150154
throw new ShouldNotHappenException($errorMessage);
151155
}
152156

153-
$isIntRefactoredNode = is_int($refactoredNode);
157+
$isState = is_int($refactoredNodeOrState);
154158

155-
/**
156-
* If below node and/or its children not traversed on current rule
157-
* early return null with decorate current and children node with skipped by "only" current rule
158-
*/
159-
if ($isIntRefactoredNode) {
159+
if ($isState) {
160160
$this->createdByRuleDecorator->decorate($node, $originalNode, static::class);
161161

162-
if (in_array(
163-
$refactoredNode,
164-
[NodeVisitor::DONT_TRAVERSE_CHILDREN, NodeVisitor::DONT_TRAVERSE_CURRENT_AND_CHILDREN],
165-
true
166-
)) {
167-
$this->decorateCurrentAndChildren($node);
162+
// only remove node is supported
163+
if ($refactoredNodeOrState !== NodeVisitor::REMOVE_NODE) {
168164
return null;
169165
}
170166

171-
// @see NodeVisitor::* codes, e.g. removal of node of stopping the traversing
172-
if ($refactoredNode === NodeVisitor::REMOVE_NODE) {
173-
// log here, so we can remove the node in leaveNode() method
174-
$this->toBeRemovedNodeId = spl_object_id($originalNode);
175-
}
167+
// log here, so we can remove the node in leaveNode() method
168+
$this->toBeRemovedNodeId = spl_object_id($originalNode);
176169

177-
// notify this rule changing code
170+
// notify this rule changed code
178171
$rectorWithLineChange = new RectorWithLineChange(static::class, $originalNode->getStartLine());
179172
$this->file->addRectorClassWithLine($rectorWithLineChange);
180173

181-
return $refactoredNode === NodeVisitor::REMOVE_NODE
182-
? $originalNode
183-
: $refactoredNode;
174+
// keep original node as node will be removed in leaveNode()
175+
return $originalNode;
184176
}
185177

186-
return $this->postRefactorProcess($originalNode, $node, $refactoredNode, $filePath);
178+
return $this->postRefactorProcess($originalNode, $node, $refactoredNodeOrState, $filePath);
187179
}
188180

189181
/**
190182
* Replacing nodes in leaveNode() method avoids infinite recursion
191183
* see"infinite recursion" in https://github.com/nikic/PHP-Parser/blob/master/doc/component/Walking_the_AST.markdown
184+
*
185+
* @return \PhpParser\Node|\PhpParser\Node[]|NodeVisitor::REMOVE_NODE|null
192186
*/
193187
final public function leaveNode(Node $node): array|int|Node|null
194188
{
@@ -295,35 +289,6 @@ protected function mirrorComments(Node $newNode, Node $oldNode): void
295289
}
296290
}
297291

298-
private function decorateCurrentAndChildren(Node $node): void
299-
{
300-
// skip sole type, as no other nodes to filter out
301-
if (count($this->getNodeTypes()) === 1) {
302-
return;
303-
}
304-
305-
// filter only types that
306-
// 1. registered in getNodesTypes() method
307-
// 2. different with current node type, as already decorated above
308-
//
309-
$otherTypes = array_filter(
310-
$this->getNodeTypes(),
311-
static fn (string $nodeType): bool => $nodeType !== $node::class
312-
);
313-
314-
if ($otherTypes === []) {
315-
return;
316-
}
317-
318-
$this->traverseNodesWithCallable($node, static function (Node $subNode) use ($otherTypes): null {
319-
if (in_array($subNode::class, $otherTypes, true)) {
320-
$subNode->setAttribute(AttributeKey::SKIPPED_BY_RECTOR_RULE, static::class);
321-
}
322-
323-
return null;
324-
});
325-
}
326-
327292
/**
328293
* @param Node|Node[] $refactoredNode
329294
*/

0 commit comments

Comments
 (0)