Skip to content

Commit cb51265

Browse files
committed
fix
1 parent 494ffa9 commit cb51265

File tree

1 file changed

+15
-12
lines changed

1 file changed

+15
-12
lines changed

src/Rector/AbstractRector.php

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
use PhpParser\Node\FunctionLike;
99
use PhpParser\Node\Name;
1010
use PhpParser\Node\PropertyItem;
11-
use PhpParser\Node\Stmt\Class_;
1211
use PhpParser\Node\Stmt\ClassMethod;
1312
use PhpParser\Node\Stmt\Const_;
1413
use PhpParser\Node\Stmt\InlineHTML;
@@ -25,6 +24,7 @@
2524
use Rector\Application\Provider\CurrentFileProvider;
2625
use Rector\BetterPhpDocParser\PhpDocInfo\PhpDocInfo;
2726
use Rector\ChangesReporting\ValueObject\RectorWithLineChange;
27+
use Rector\Contract\PhpParser\Node\StmtsAwareInterface;
2828
use Rector\Contract\Rector\HTMLAverseRectorInterface;
2929
use Rector\Contract\Rector\RectorInterface;
3030
use Rector\Exception\ShouldNotHappenException;
@@ -299,31 +299,34 @@ protected function mirrorComments(Node $newNode, Node $oldNode): void
299299

300300
private function decorateCurrentAndChildren(Node $node): void
301301
{
302-
$node->setAttribute(AttributeKey::SKIPPED_BY_RECTOR_RULE, static::class);
303-
304302
// filter only types that
305303
// 1. registered in getNodesTypes() method
306304
// 2. different with current node type, as already decorated above
307305
//
306+
$types = $this->getNodeTypes();
308307
$otherTypes = array_filter(
309-
$this->getNodeTypes(),
308+
$types,
310309
static fn (string $nodeType): bool => $nodeType !== $node::class
311310
);
312311

313-
$this->traverseNodesWithCallable($node, static function (Node $subNode) use ($node, $otherTypes): null|int|Node {
314-
// early check here as included in other types defined in getNodeTypes()
315-
if (in_array($subNode::class, $otherTypes, true)) {
312+
$isCurrentNode = false;
313+
$this->traverseNodesWithCallable($node, static function (Node $subNode) use ($node, $types, $otherTypes, &$isCurrentNode): int|Node {
314+
// first visited is current node itself
315+
if (! $isCurrentNode) {
316+
$isCurrentNode = true;
316317
$subNode->setAttribute(AttributeKey::SKIPPED_BY_RECTOR_RULE, static::class);
317318
return $subNode;
318319
}
319320

320-
// already set
321-
if ($node === $subNode) {
322-
return null;
321+
// early check here as included in other types defined in getNodeTypes()
322+
if (in_array($subNode::class, $otherTypes, true)) {
323+
$subNode->setAttribute(AttributeKey::SKIPPED_BY_RECTOR_RULE, static::class);
324+
return $subNode;
323325
}
324326

325-
// inner class/function are out of scope
326-
if ($subNode instanceof Class_ || $subNode instanceof FunctionLike) {
327+
// exact StmtsAwareInterface will be visited by itself, and requires revalidated
328+
// no need to apply skip by current rule
329+
if ($types === [StmtsAwareInterface::class] && $node instanceof FunctionLike && $subNode instanceof FunctionLike) {
327330
return NodeVisitor::DONT_TRAVERSE_CURRENT_AND_CHILDREN;
328331
}
329332

0 commit comments

Comments
 (0)