Skip to content

Commit 03b9225

Browse files
authored
[CodeQuality] Skip NarrowUnusedSetUpDefinedPropertyRector for property captured by lazy/self-referencing arrow function (#694)
Follow-up to the closure guard. Arrow functions auto-capture by value at definition time, so narrowing a property referenced inside one is only safe when the property is already fully assigned before the arrow function (e.g. a mock yielded from a generator - kept narrowing). Skip narrowing when the assignment wraps or follows the arrow function (e.g. a self-referencing type definition, as in webonyx/graphql-php DeferredFieldsTest), where the captured local would still be unset and the closure would observe null.
1 parent 2852aed commit 03b9225

3 files changed

Lines changed: 108 additions & 20 deletions

File tree

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?php
2+
3+
namespace Rector\PHPUnit\Tests\CodeQuality\Rector\Class_\NarrowUnusedSetUpDefinedPropertyRector\Fixture;
4+
5+
use PHPUnit\Framework\TestCase;
6+
use Rector\PHPUnit\Tests\CodeQuality\Rector\Class_\NarrowUnusedSetUpDefinedPropertyRector\Source\LazyContainer;
7+
8+
final class SkipPropertySelfReferencedInArrowFunction extends TestCase
9+
{
10+
private LazyContainer $container;
11+
12+
protected function setUp(): void
13+
{
14+
$this->container = new LazyContainer([
15+
'self' => fn (): LazyContainer => $this->container,
16+
]);
17+
}
18+
19+
public function test()
20+
{
21+
$this->assertTrue(true);
22+
}
23+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?php
2+
3+
namespace Rector\PHPUnit\Tests\CodeQuality\Rector\Class_\NarrowUnusedSetUpDefinedPropertyRector\Source;
4+
5+
final class LazyContainer
6+
{
7+
/**
8+
* @param array<string, callable> $factories
9+
*/
10+
public function __construct(
11+
private array $factories
12+
) {
13+
}
14+
}

‎rules/CodeQuality/Rector/Class_/NarrowUnusedSetUpDefinedPropertyRector.php‎

Lines changed: 71 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66

77
use PhpParser\Node;
88
use PhpParser\Node\Expr;
9+
use PhpParser\Node\Expr\ArrowFunction;
10+
use PhpParser\Node\Expr\Assign;
911
use PhpParser\Node\Expr\Closure;
1012
use PhpParser\Node\Expr\PropertyFetch;
1113
use PhpParser\Node\Expr\Variable;
@@ -124,7 +126,7 @@ public function refactor(Node $node): ?Node
124126

125127
// referenced inside a nested closure that may run after setUp() - turning it into a local
126128
// variable would leave it out of the closure scope, as there is no "use" binding to add it
127-
if ($this->isPropertyUsedInNestedClosure($setUpClassMethod, $propertyName)) {
129+
if ($this->isPropertyUsedInUnsafeNestedFunction($setUpClassMethod, $propertyName)) {
128130
continue;
129131
}
130132

@@ -196,35 +198,84 @@ private function isPropertyUsedOutsideSetUpClassMethod(
196198
return $isPropertyUsed;
197199
}
198200

199-
private function isPropertyUsedInNestedClosure(ClassMethod $setUpClassMethod, string $propertyName): bool
201+
private function isPropertyUsedInUnsafeNestedFunction(ClassMethod $setUpClassMethod, string $propertyName): bool
200202
{
201-
// only regular closures are unsafe: they do not capture outer variables without an
202-
// explicit "use" binding. Arrow functions auto-capture by value, so narrowing is safe there.
203-
$closures = $this->nodeFinder->findInstanceOf($setUpClassMethod, Closure::class);
204-
205-
foreach ($closures as $closure) {
206-
$usedPropertyFetch = $this->nodeFinder->findFirst($closure, function (Node $node) use (
207-
$propertyName
208-
): bool {
209-
if (! $node instanceof PropertyFetch) {
210-
return false;
211-
}
212-
213-
if (! $this->isName($node->var, 'this')) {
214-
return false;
215-
}
203+
// a regular closure does not capture outer variables without an explicit "use" binding,
204+
// so turning the property into a local variable always leaves it undefined inside the closure
205+
foreach ($this->nodeFinder->findInstanceOf($setUpClassMethod, Closure::class) as $closure) {
206+
if ($this->refersToThisProperty($closure, $propertyName)) {
207+
return true;
208+
}
209+
}
216210

217-
return $this->isName($node->name, $propertyName);
218-
});
211+
// an arrow function auto-captures by value at definition time; that only matches the original
212+
// lazy "$this->property" read when the property assignment is already complete before the arrow
213+
// function is defined. A later or wrapping assignment (e.g. a self-referencing type definition)
214+
// would capture a not-yet-assigned variable
215+
foreach ($this->nodeFinder->findInstanceOf($setUpClassMethod, ArrowFunction::class) as $arrowFunction) {
216+
if (! $this->refersToThisProperty($arrowFunction, $propertyName)) {
217+
continue;
218+
}
219219

220-
if ($usedPropertyFetch instanceof PropertyFetch) {
220+
if (! $this->isPropertyAssignedBefore($setUpClassMethod, $propertyName, $arrowFunction)) {
221221
return true;
222222
}
223223
}
224224

225225
return false;
226226
}
227227

228+
private function refersToThisProperty(Node $node, string $propertyName): bool
229+
{
230+
$propertyFetch = $this->nodeFinder->findFirst($node, function (Node $subNode) use ($propertyName): bool {
231+
if (! $subNode instanceof PropertyFetch) {
232+
return false;
233+
}
234+
235+
if (! $this->isName($subNode->var, 'this')) {
236+
return false;
237+
}
238+
239+
return $this->isName($subNode->name, $propertyName);
240+
});
241+
242+
return $propertyFetch instanceof PropertyFetch;
243+
}
244+
245+
private function isPropertyAssignedBefore(
246+
ClassMethod $setUpClassMethod,
247+
string $propertyName,
248+
ArrowFunction $arrowFunction
249+
): bool {
250+
$arrowFunctionStartTokenPos = $arrowFunction->getStartTokenPos();
251+
252+
$assignBefore = $this->nodeFinder->findFirst($setUpClassMethod, function (Node $node) use (
253+
$propertyName,
254+
$arrowFunctionStartTokenPos
255+
): bool {
256+
if (! $node instanceof Assign) {
257+
return false;
258+
}
259+
260+
if (! $node->var instanceof PropertyFetch) {
261+
return false;
262+
}
263+
264+
if (! $this->isName($node->var->var, 'this')) {
265+
return false;
266+
}
267+
268+
if (! $this->isName($node->var->name, $propertyName)) {
269+
return false;
270+
}
271+
272+
// assignment fully completes before the arrow function starts
273+
return $node->getEndTokenPos() < $arrowFunctionStartTokenPos;
274+
});
275+
276+
return $assignBefore instanceof Assign;
277+
}
278+
228279
private function shouldSkipProperty(
229280
bool $isFinalClass,
230281
Property $property,

0 commit comments

Comments
 (0)