Skip to content

Commit 190b583

Browse files
committed
fix(RemoveToolkitArgFromImageToolkitOperationConstructorRector): skip closures in $toolkit count
The 'exactly once' usage count for $toolkit descended into nested closures and arrow-functions, where a `$toolkit` parameter shadow or `use ($toolkit)` capture would have inflated the count and caused the rector to refuse to rewrite an otherwise-valid constructor. Contrib search (api.tresbien.tech) found zero ImageToolkitOperationBase subclasses with this pattern, so the impact is purely defensive — but the tightening is cheap and locks in the intended outer-scope-only semantic. No fixture added (no realistic trigger).
1 parent bd3fcfd commit 190b583

1 file changed

Lines changed: 11 additions & 2 deletions

File tree

src/Drupal11/Rector/Deprecation/RemoveToolkitArgFromImageToolkitOperationConstructorRector.php

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

77
use PhpParser\Node;
88
use PhpParser\Node\Arg;
9+
use PhpParser\Node\Expr\ArrowFunction;
10+
use PhpParser\Node\Expr\Closure;
911
use PhpParser\Node\Expr\StaticCall;
1012
use PhpParser\Node\Expr\Variable;
1113
use PhpParser\Node\Stmt\Class_;
14+
use PhpParser\NodeVisitor;
1215
use Rector\Rector\AbstractRector;
1316
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
1417
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
@@ -109,9 +112,15 @@ public function refactor(Node $node): ?Node
109112
}
110113

111114
// Count all usages of the $toolkit variable inside the constructor body
112-
// to ensure it is only passed to parent::__construct().
115+
// to ensure it is only passed to parent::__construct(). Skip the
116+
// bodies of nested closures and arrow-functions — a `$toolkit`
117+
// parameter or `use ($toolkit)` capture there shadows the outer
118+
// variable and must not affect the outer-scope count.
113119
$toolkitUsageCount = 0;
114-
$this->traverseNodesWithCallable($constructor->stmts ?? [], function (Node $innerNode) use ($toolkitVarName, &$toolkitUsageCount): ?Node {
120+
$this->traverseNodesWithCallable($constructor->stmts ?? [], function (Node $innerNode) use ($toolkitVarName, &$toolkitUsageCount): null|int {
121+
if ($innerNode instanceof Closure || $innerNode instanceof ArrowFunction) {
122+
return NodeVisitor::DONT_TRAVERSE_CHILDREN;
123+
}
115124
if ($innerNode instanceof Variable && $this->isName($innerNode, $toolkitVarName)) {
116125
++$toolkitUsageCount;
117126
}

0 commit comments

Comments
 (0)