Skip to content

Commit 0a9ab22

Browse files
committed
fix build
1 parent d522fdb commit 0a9ab22

5 files changed

Lines changed: 338 additions & 5 deletions

File tree

‎src/Node/ClassStatementsGatherer.php‎

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
use PHPStan\Node\Printer\ExprPrinter;
1919
use PHPStan\Node\Property\PropertyAssign;
2020
use PHPStan\Node\Property\PropertyRead;
21+
use PHPStan\Node\Property\PropertyReferenceBinding;
2122
use PHPStan\Node\Property\PropertyWrite;
2223
use PHPStan\Reflection\ClassReflection;
2324
use PHPStan\Reflection\MethodReflection;
@@ -234,8 +235,10 @@ private function gatherNodes(Node $node, Scope $scope): void
234235
return;
235236
}
236237
if ($node instanceof Expr\AssignRef) {
237-
if ($node->var instanceof PropertyFetch || $node->var instanceof StaticPropertyFetch) {
238-
// the property now shares its value with the referenced variable, which can read it elsewhere
238+
if (
239+
($node->var instanceof PropertyFetch || $node->var instanceof StaticPropertyFetch)
240+
&& PropertyReferenceBinding::isReadThroughReference($node->expr, $scope, $this->methods)
241+
) {
239242
$this->propertyUsages[] = new PropertyRead($node->var, $scope);
240243
}
241244
if (!$node->expr instanceof PropertyFetch && !$node->expr instanceof StaticPropertyFetch) {
Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
<?php declare(strict_types = 1);
2+
3+
namespace PHPStan\Node\Property;
4+
5+
use PhpParser\Node;
6+
use PhpParser\Node\Expr;
7+
use PhpParser\Node\Expr\Assign;
8+
use PhpParser\Node\Expr\FuncCall;
9+
use PhpParser\Node\Expr\Variable;
10+
use PhpParser\NodeFinder;
11+
use PHPStan\Analyser\Scope;
12+
use PHPStan\Node\ClassMethod;
13+
use PHPStan\Reflection\MethodReflection;
14+
use function in_array;
15+
use function is_string;
16+
use function spl_object_id;
17+
use function strtolower;
18+
19+
/**
20+
* Decides whether binding a property by reference (`$this->prop = &$expr`)
21+
* lets its value be read through the other side of the reference.
22+
*
23+
* Called from the native ClassStatementsGatherer in turbo-ext as well.
24+
*/
25+
final class PropertyReferenceBinding
26+
{
27+
28+
private const VARIABLE_READING_FUNCTIONS = ['compact', 'get_defined_vars'];
29+
30+
/**
31+
* @param array<ClassMethod> $methods methods gathered so far in the class
32+
*/
33+
public static function isReadThroughReference(Expr $referencedExpr, Scope $scope, array $methods): bool
34+
{
35+
if (!$referencedExpr instanceof Variable || !is_string($referencedExpr->name)) {
36+
return true;
37+
}
38+
if ($scope->isInAnonymousFunction()) {
39+
return true;
40+
}
41+
42+
$function = $scope->getFunction();
43+
if (!$function instanceof MethodReflection) {
44+
return true;
45+
}
46+
47+
$methodName = strtolower($function->getName());
48+
$methodNode = null;
49+
foreach ($methods as $method) {
50+
if ($method->getNode()->name->toLowerString() !== $methodName) {
51+
continue;
52+
}
53+
$methodNode = $method->getNode();
54+
}
55+
if ($methodNode === null || $methodNode->stmts === null) {
56+
return true;
57+
}
58+
59+
$variableName = $referencedExpr->name;
60+
foreach ($methodNode->params as $param) {
61+
if (!$param->var instanceof Variable || $param->var->name !== $variableName) {
62+
continue;
63+
}
64+
65+
// the caller holds the other side of a by-ref parameter
66+
return $param->byRef;
67+
}
68+
69+
// a local variable keeps the value alive only while something else in the method uses it
70+
// (global/static declarations, closure uses, further reads, passing it on, ...)
71+
$overwrittenVariables = [];
72+
$nodeFinder = new NodeFinder();
73+
foreach ($nodeFinder->findInstanceOf($methodNode->stmts, Assign::class) as $assign) {
74+
$overwrittenVariables[spl_object_id($assign->var)] = true;
75+
}
76+
77+
return $nodeFinder->findFirst($methodNode->stmts, static function (Node $node) use ($referencedExpr, $variableName, $overwrittenVariables): bool {
78+
if ($node instanceof Variable) {
79+
if (!is_string($node->name)) {
80+
return true;
81+
}
82+
83+
return $node->name === $variableName
84+
&& $node !== $referencedExpr
85+
&& !isset($overwrittenVariables[spl_object_id($node)]);
86+
}
87+
88+
return $node instanceof FuncCall
89+
&& $node->name instanceof Node\Name
90+
&& in_array($node->name->toLowerString(), self::VARIABLE_READING_FUNCTIONS, true);
91+
}) !== null;
92+
}
93+
94+
}

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

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -541,4 +541,28 @@ public function testBug15886Static(): void
541541
$this->analyse([__DIR__ . '/data/bug-15886-static.php'], []);
542542
}
543543

544+
public function testBug15886Local(): void
545+
{
546+
$this->alwaysWrittenTags = [];
547+
$this->alwaysReadTags = [];
548+
549+
$this->analyse([__DIR__ . '/data/bug-15886-local.php'], [
550+
[
551+
'Property Bug15886Local\\ThrowawayLocal::$cache is never read, only written.',
552+
9,
553+
'See: https://phpstan.org/developing-extensions/always-read-written-properties',
554+
],
555+
[
556+
'Property Bug15886Local\\ByValueParameter::$cache is never read, only written.',
557+
28,
558+
'See: https://phpstan.org/developing-extensions/always-read-written-properties',
559+
],
560+
[
561+
'Static property Bug15886Local\\StaticPropertyThrowawayLocal::$cache is never read, only written.',
562+
173,
563+
'See: https://phpstan.org/developing-extensions/always-read-written-properties',
564+
],
565+
]);
566+
}
567+
544568
}
Lines changed: 186 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,186 @@
1+
<?php declare(strict_types = 1);
2+
3+
namespace Bug15886Local;
4+
5+
class ThrowawayLocal
6+
{
7+
8+
/** @var list<string> */
9+
private array $cache;
10+
11+
public function __construct()
12+
{
13+
$tmp = [];
14+
$this->cache = &$tmp;
15+
}
16+
17+
public function add(): void
18+
{
19+
$this->cache[] = 'x';
20+
}
21+
22+
}
23+
24+
class ByValueParameter
25+
{
26+
27+
/** @var list<string> */
28+
private array $cache;
29+
30+
/** @param list<string> $tmp */
31+
public function __construct(array $tmp)
32+
{
33+
$this->cache = &$tmp;
34+
}
35+
36+
public function add(): void
37+
{
38+
$this->cache[] = 'x';
39+
}
40+
41+
}
42+
43+
class LocalReadAfterwards
44+
{
45+
46+
/** @var list<string> */
47+
private array $cache;
48+
49+
public function __construct()
50+
{
51+
$tmp = [];
52+
$this->cache = &$tmp;
53+
$this->add();
54+
echo count($tmp);
55+
}
56+
57+
public function add(): void
58+
{
59+
$this->cache[] = 'x';
60+
}
61+
62+
}
63+
64+
class LocalPassedOn
65+
{
66+
67+
/** @var list<string> */
68+
private array $cache;
69+
70+
/** @param \ArrayObject<int, mixed> $registry */
71+
public function __construct(\ArrayObject $registry)
72+
{
73+
$tmp = [];
74+
$this->cache = &$tmp;
75+
$registry->append($tmp);
76+
}
77+
78+
public function add(): void
79+
{
80+
$this->cache[] = 'x';
81+
}
82+
83+
}
84+
85+
class GlobalVariable
86+
{
87+
88+
/** @var list<string> */
89+
private array $cache;
90+
91+
public function __construct()
92+
{
93+
global $tmp;
94+
$this->cache = &$tmp;
95+
}
96+
97+
public function add(): void
98+
{
99+
$this->cache[] = 'x';
100+
}
101+
102+
}
103+
104+
class StaticVariable
105+
{
106+
107+
/** @var list<string> */
108+
private array $cache;
109+
110+
public function __construct()
111+
{
112+
static $tmp = [];
113+
$this->cache = &$tmp;
114+
}
115+
116+
public function add(): void
117+
{
118+
$this->cache[] = 'x';
119+
}
120+
121+
}
122+
123+
class CapturedByClosure
124+
{
125+
126+
/** @var list<string> */
127+
private array $cache;
128+
129+
public \Closure $count;
130+
131+
public function __construct()
132+
{
133+
$tmp = [];
134+
$this->cache = &$tmp;
135+
$this->count = static function () use (&$tmp): int {
136+
return count($tmp);
137+
};
138+
}
139+
140+
public function add(): void
141+
{
142+
$this->cache[] = 'x';
143+
}
144+
145+
}
146+
147+
class Compacted
148+
{
149+
150+
/** @var list<string> */
151+
private array $cache;
152+
153+
/** @return array<string, mixed> */
154+
public function bind(): array
155+
{
156+
$tmp = [];
157+
$this->cache = &$tmp;
158+
159+
return compact('tmp');
160+
}
161+
162+
public function add(): void
163+
{
164+
$this->cache[] = 'x';
165+
}
166+
167+
}
168+
169+
class StaticPropertyThrowawayLocal
170+
{
171+
172+
/** @var list<string> */
173+
private static array $cache = [];
174+
175+
public static function bind(): void
176+
{
177+
$tmp = [];
178+
self::$cache = &$tmp;
179+
}
180+
181+
public static function add(): void
182+
{
183+
self::$cache[] = 'x';
184+
}
185+
186+
}

‎turbo-ext/src/ClassStatementsGatherer.cpp‎

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -238,6 +238,30 @@ class ClassStatementsGatherer
238238
return pt_type_call_static_ce(typeUtils, PT_LC("findthistype"), 1, type);
239239
}
240240

241+
/* PHPStan\Node\Property\PropertyReferenceBinding::isReadThroughReference($expr, $scope, $this->methods),
242+
* resolved by name like findThisType(); only reached for reference
243+
* assignments to a property, so the PHP helper is not ported */
244+
bool isReadThroughReference(zval *expr, zval *scope, bool &result)
245+
{
246+
static const char name[] = "PHPStan\\Node\\Property\\PropertyReferenceBinding";
247+
zv::Str className = zv::Str::adopt(zend_string_init(name, sizeof(name) - 1, 0));
248+
zend_class_entry *helper = zend_lookup_class(className.get());
249+
if (UNEXPECTED(helper == NULL)) {
250+
if (!EG(exception)) {
251+
zend_throw_error(NULL, "Class \"%s\" not found", name);
252+
}
253+
return false;
254+
}
255+
zval args[3];
256+
ZVAL_COPY_VALUE(&args[0], expr);
257+
ZVAL_COPY_VALUE(&args[1], scope);
258+
ZVAL_COPY_VALUE(&args[2], OBJ_PROP_NUM(self, slots::methods));
259+
zv::Val isRead = pt_type_call_static_ce(helper, PT_LC("isreadthroughreference"), 3, args);
260+
if (UNEXPECTED(isRead.isUndef())) return false;
261+
result = Z_TYPE_P(isRead.raw()) == IS_TRUE;
262+
return true;
263+
}
264+
241265
bool gatherNodes(zend_object *node, zval *scope)
242266
{
243267
bool inClass;
@@ -327,11 +351,13 @@ class ClassStatementsGatherer
327351
}
328352
if (isInstanceOf(node, PT_CLASS_ASSIGN_REF_EXPR)) {
329353
zval *var = pt_csg_assign_ref_var.of(node);
330-
if (var != NULL && Z_TYPE_P(var) == IS_OBJECT && isPropertyFetchLike(Z_OBJ_P(var))) {
331-
if (UNEXPECTED(!pushNodeScope(slots::propertyUsages, PT_CLASS_PROPERTY_READ, var, scope))) return false;
332-
}
333354
zval *expr = pt_csg_assign_ref_expr.of(node);
334355
if (UNEXPECTED(expr == NULL || Z_TYPE_P(expr) != IS_OBJECT)) return !EG(exception);
356+
if (var != NULL && Z_TYPE_P(var) == IS_OBJECT && isPropertyFetchLike(Z_OBJ_P(var))) {
357+
bool isRead;
358+
if (UNEXPECTED(!isReadThroughReference(expr, scope, isRead))) return false;
359+
if (isRead && UNEXPECTED(!pushNodeScope(slots::propertyUsages, PT_CLASS_PROPERTY_READ, var, scope))) return false;
360+
}
335361
if (!isPropertyFetchLike(Z_OBJ_P(expr))) return gatherNodes(Z_OBJ_P(expr), scope);
336362
if (UNEXPECTED(!pushNodeScope(slots::propertyUsages, PT_CLASS_PROPERTY_READ, expr, scope))) return false;
337363
return pushPropertyWrite(expr, scope, false, &nodeZv);

0 commit comments

Comments
 (0)