Skip to content

Commit 943b146

Browse files
authored
[cleanup] cleanup AbstractRector from DONT_TRAVERSE_* enums as no longer supported, all rules work without it now (#7708)
1 parent 8f66e97 commit 943b146

File tree

5 files changed

+28
-67
lines changed

5 files changed

+28
-67
lines changed

phpstan.neon

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

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: 18 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
use PhpParser\Node\Stmt\Interface_;
1313
use PhpParser\Node\Stmt\Property;
1414
use PhpParser\Node\Stmt\Trait_;
15+
use PhpParser\NodeTraverser;
1516
use PhpParser\NodeVisitor;
1617
use PhpParser\NodeVisitorAbstract;
1718
use PHPStan\Analyser\MutatingScope;
@@ -122,6 +123,9 @@ public function beforeTraverse(array $nodes): ?array
122123
return null;
123124
}
124125

126+
/**
127+
* @return NodeTraverser::REMOVE_NODE|Node|null
128+
*/
125129
final public function enterNode(Node $node): int|Node|null
126130
{
127131
if (! $this->isMatchingNodeType($node)) {
@@ -140,52 +144,41 @@ final public function enterNode(Node $node): int|Node|null
140144
// ensure origNode pulled before refactor to avoid changed during refactor, ref https://3v4l.org/YMEGN
141145
$originalNode = $node->getAttribute(AttributeKey::ORIGINAL_NODE) ?? $node;
142146

143-
$refactoredNode = $this->refactor($node);
147+
$refactoredNodeOrState = $this->refactor($node);
144148

145149
// nothing to change → continue
146-
if ($refactoredNode === null) {
150+
if ($refactoredNodeOrState === null) {
147151
return null;
148152
}
149153

150-
if ($refactoredNode === []) {
154+
if ($refactoredNodeOrState === []) {
151155
$errorMessage = sprintf(self::EMPTY_NODE_ARRAY_MESSAGE, static::class);
152156
throw new ShouldNotHappenException($errorMessage);
153157
}
154158

155-
$isIntRefactoredNode = is_int($refactoredNode);
159+
$isState = is_int($refactoredNodeOrState);
156160

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

164-
if (in_array(
165-
$refactoredNode,
166-
[NodeVisitor::DONT_TRAVERSE_CHILDREN, NodeVisitor::DONT_TRAVERSE_CURRENT_AND_CHILDREN],
167-
true
168-
)) {
169-
$this->decorateCurrentAndChildren($node);
164+
// only remove node is supported
165+
if ($refactoredNodeOrState !== NodeVisitor::REMOVE_NODE) {
166+
// @todo warn about unsupported state in the future
170167
return null;
171168
}
172169

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

179-
// notify this rule changing code
173+
// notify this rule changed code
180174
$rectorWithLineChange = new RectorWithLineChange(static::class, $originalNode->getStartLine());
181175
$this->file->addRectorClassWithLine($rectorWithLineChange);
182176

183-
return $refactoredNode === NodeVisitor::REMOVE_NODE
184-
? $originalNode
185-
: $refactoredNode;
177+
// keep original node as node will be removed in leaveNode()
178+
return $originalNode;
186179
}
187180

188-
return $this->postRefactorProcess($originalNode, $node, $refactoredNode, $filePath);
181+
return $this->postRefactorProcess($originalNode, $node, $refactoredNodeOrState, $filePath);
189182
}
190183

191184
/**
@@ -275,35 +268,6 @@ protected function mirrorComments(Node $newNode, Node $oldNode): void
275268
$this->commentsMerger->mirrorComments($newNode, $oldNode);
276269
}
277270

278-
private function decorateCurrentAndChildren(Node $node): void
279-
{
280-
// skip sole type, as no other nodes to filter out
281-
if (count($this->getNodeTypes()) === 1) {
282-
return;
283-
}
284-
285-
// filter only types that
286-
// 1. registered in getNodesTypes() method
287-
// 2. different with current node type, as already decorated above
288-
//
289-
$otherTypes = array_filter(
290-
$this->getNodeTypes(),
291-
static fn (string $nodeType): bool => $nodeType !== $node::class
292-
);
293-
294-
if ($otherTypes === []) {
295-
return;
296-
}
297-
298-
$this->traverseNodesWithCallable($node, static function (Node $subNode) use ($otherTypes): null {
299-
if (in_array($subNode::class, $otherTypes, true)) {
300-
$subNode->setAttribute(AttributeKey::SKIPPED_BY_RECTOR_RULE, static::class);
301-
}
302-
303-
return null;
304-
});
305-
}
306-
307271
/**
308272
* @param Node|Node[] $refactoredNode
309273
*/

0 commit comments

Comments
 (0)