Skip to content

Commit b84e05c

Browse files
committed
Observe writes through arrow function reference parameters
1 parent e5e374f commit b84e05c

5 files changed

Lines changed: 42 additions & 4 deletions

File tree

‎src/Analyser/ExprHandler/ArrowFunctionHandler.php‎

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
use PHPStan\Analyser\TypeSpecifierContext;
1818
use PHPStan\Analyser\VariableFlow;
1919
use PHPStan\DependencyInjection\AutowiredService;
20+
use function is_string;
2021

2122
/**
2223
* @implements ExprHandler<ArrowFunction>
@@ -92,7 +93,15 @@ public function processExpr(NodeScopeResolver $nodeScopeResolver, Stmt $stmt, Ex
9293

9394
public static function getVariableFlow(ArrowFunction $expr, ExpressionResult $bodyResult): VariableFlow
9495
{
95-
return VariableFlow::arrow($expr, $bodyResult->getVariableFlow());
96+
$outputs = [];
97+
foreach ($expr->params as $param) {
98+
if (!$param->byRef || !$param->var instanceof Expr\Variable || !is_string($param->var->name)) {
99+
continue;
100+
}
101+
$outputs[] = VariableFlow::read($param->var->name);
102+
}
103+
104+
return VariableFlow::arrow($expr, $bodyResult->getVariableFlow(), VariableFlow::sequence(...$outputs));
96105
}
97106

98107
}

‎src/Analyser/VariableFlow.php‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,9 +72,9 @@ public static function choice(?self ...$branches): ?self
7272
return new VariableSequenceFlow(self::CHOICE, $branches);
7373
}
7474

75-
public static function arrow(ArrowFunction $arrow, ?self $body): self
75+
public static function arrow(ArrowFunction $arrow, ?self $body, ?self $outputs): self
7676
{
77-
return new VariableControlFlow(self::ARROW, [$body], arrow: $arrow);
77+
return new VariableControlFlow(self::ARROW, [$body, $outputs], arrow: $arrow);
7878
}
7979

8080
public static function read(string $name): ?self

‎src/Analyser/VariableLivenessResolver.php‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,8 @@ private function liveBefore(?VariableFlow $flow, array $next, VariableFlowContex
174174
throw new ShouldNotHappenException();
175175
}
176176
if ($flow->kind === VariableFlow::ARROW && $flow->arrow !== null) {
177-
$names = $this->liveBefore($flow->children[0], [], new VariableFlowContext([]));
177+
$outputs = $this->liveBefore($flow->children[1], [], new VariableFlowContext([]));
178+
$names = $this->liveBefore($flow->children[0], $outputs, new VariableFlowContext($outputs, uncaught: $outputs));
178179
foreach ($flow->arrow->params as $param) {
179180
if (!$param->var instanceof Node\Expr\Variable || !is_string($param->var->name)) {
180181
continue;

‎tests/PHPStan/Rules/DeadCode/UnusedVariableRuleTest.php‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -380,4 +380,11 @@ public function testNestedOffsetWrites(): void
380380
$this->analyse([__DIR__ . '/data/unused-variable-nested-offset-writes.php'], []);
381381
}
382382

383+
public function testArrowReferenceParameters(): void
384+
{
385+
$this->analyse([__DIR__ . '/data/unused-variable-arrow-reference.php'], [
386+
['Value assigned to variable $value is never read.', 14],
387+
]);
388+
}
389+
383390
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php
2+
// lint >= 8.0
3+
4+
namespace UnusedVariableArrowReference;
5+
6+
function normalize(array $values): array
7+
{
8+
array_walk_recursive($values, fn (&$value) => $value = trim($value));
9+
return $values;
10+
}
11+
12+
function callback(): \Closure
13+
{
14+
$value = 1;
15+
return fn (&$value) => $value = 2;
16+
}
17+
18+
function terminatingCallback(): \Closure
19+
{
20+
return fn (&$value) => throw new \RuntimeException($value = 'changed');
21+
}

0 commit comments

Comments
 (0)