|
6 | 6 |
|
7 | 7 | use PhpParser\Node; |
8 | 8 | use PhpParser\Node\Expr; |
| 9 | +use PhpParser\Node\Expr\ArrowFunction; |
| 10 | +use PhpParser\Node\Expr\Assign; |
9 | 11 | use PhpParser\Node\Expr\Closure; |
10 | 12 | use PhpParser\Node\Expr\PropertyFetch; |
11 | 13 | use PhpParser\Node\Expr\Variable; |
@@ -124,7 +126,7 @@ public function refactor(Node $node): ?Node |
124 | 126 |
|
125 | 127 | // referenced inside a nested closure that may run after setUp() - turning it into a local |
126 | 128 | // 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)) { |
128 | 130 | continue; |
129 | 131 | } |
130 | 132 |
|
@@ -196,35 +198,84 @@ private function isPropertyUsedOutsideSetUpClassMethod( |
196 | 198 | return $isPropertyUsed; |
197 | 199 | } |
198 | 200 |
|
199 | | - private function isPropertyUsedInNestedClosure(ClassMethod $setUpClassMethod, string $propertyName): bool |
| 201 | + private function isPropertyUsedInUnsafeNestedFunction(ClassMethod $setUpClassMethod, string $propertyName): bool |
200 | 202 | { |
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 | + } |
216 | 210 |
|
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 | + } |
219 | 219 |
|
220 | | - if ($usedPropertyFetch instanceof PropertyFetch) { |
| 220 | + if (! $this->isPropertyAssignedBefore($setUpClassMethod, $propertyName, $arrowFunction)) { |
221 | 221 | return true; |
222 | 222 | } |
223 | 223 | } |
224 | 224 |
|
225 | 225 | return false; |
226 | 226 | } |
227 | 227 |
|
| 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 | + |
228 | 279 | private function shouldSkipProperty( |
229 | 280 | bool $isFinalClass, |
230 | 281 | Property $property, |
|
0 commit comments