Skip to content

Commit dca28a2

Browse files
committed
fix by array-offset
1 parent 9734c86 commit dca28a2

4 files changed

Lines changed: 91 additions & 2 deletions

File tree

‎src/Node/ClassStatementsGatherer.php‎

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -235,11 +235,15 @@ private function gatherNodes(Node $node, Scope $scope): void
235235
return;
236236
}
237237
if ($node instanceof Expr\AssignRef) {
238+
$boundVar = $node->var;
239+
while ($boundVar instanceof ArrayDimFetch) {
240+
$boundVar = $boundVar->var;
241+
}
238242
if (
239-
($node->var instanceof PropertyFetch || $node->var instanceof StaticPropertyFetch)
243+
($boundVar instanceof PropertyFetch || $boundVar instanceof StaticPropertyFetch)
240244
&& PropertyReferenceBinding::isReadThroughReference($node->expr, $scope, $this->methods)
241245
) {
242-
$this->propertyUsages[] = new PropertyRead($node->var, $scope);
246+
$this->propertyUsages[] = new PropertyRead($boundVar, $scope);
243247
}
244248
if (!$node->expr instanceof PropertyFetch && !$node->expr instanceof StaticPropertyFetch) {
245249
$this->gatherNodes($node->expr, $scope);

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

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -565,4 +565,18 @@ public function testBug15886Local(): void
565565
]);
566566
}
567567

568+
public function testBug15886ArrayOffset(): void
569+
{
570+
$this->alwaysWrittenTags = [];
571+
$this->alwaysReadTags = [];
572+
573+
$this->analyse([__DIR__ . '/data/bug-15886-array-offset.php'], [
574+
[
575+
'Property Bug15886ArrayOffset\\ArrayOffsetThrowawayLocal::$counters is never read, only written.',
576+
56,
577+
'See: https://phpstan.org/developing-extensions/always-read-written-properties',
578+
],
579+
]);
580+
}
581+
568582
}
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
<?php declare(strict_types = 1);
2+
3+
namespace Bug15886ArrayOffset;
4+
5+
class ArrayOffset
6+
{
7+
/** @var array<string, int> */
8+
private array $counters = [];
9+
10+
public function bind(int &$counter): void
11+
{
12+
$this->counters['requests'] = &$counter;
13+
}
14+
15+
public function reset(): void
16+
{
17+
$this->counters['requests'] = 0;
18+
}
19+
}
20+
21+
class NestedArrayOffset
22+
{
23+
/** @var array<string, array<string, int>> */
24+
private array $counters = [];
25+
26+
public function bind(int &$counter): void
27+
{
28+
$this->counters['http']['requests'] = &$counter;
29+
}
30+
31+
public function reset(): void
32+
{
33+
$this->counters['http']['requests'] = 0;
34+
}
35+
}
36+
37+
class StaticArrayOffset
38+
{
39+
/** @var array<string, int> */
40+
private static array $counters = [];
41+
42+
public static function bind(int &$counter): void
43+
{
44+
self::$counters['requests'] = &$counter;
45+
}
46+
47+
public static function reset(): void
48+
{
49+
self::$counters['requests'] = 0;
50+
}
51+
}
52+
53+
class ArrayOffsetThrowawayLocal
54+
{
55+
/** @var array<string, int> */
56+
private array $counters = [];
57+
58+
public function bind(): void
59+
{
60+
$counter = 0;
61+
$this->counters['requests'] = &$counter;
62+
}
63+
64+
public function reset(): void
65+
{
66+
$this->counters['requests'] = 0;
67+
}
68+
}

‎turbo-ext/src/ClassStatementsGatherer.cpp‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -353,6 +353,9 @@ class ClassStatementsGatherer
353353
zval *var = pt_csg_assign_ref_var.of(node);
354354
zval *expr = pt_csg_assign_ref_expr.of(node);
355355
if (UNEXPECTED(expr == NULL || Z_TYPE_P(expr) != IS_OBJECT)) return !EG(exception);
356+
while (var != NULL && Z_TYPE_P(var) == IS_OBJECT && isInstanceOf(Z_OBJ_P(var), PT_CLASS_ARRAY_DIM_FETCH)) {
357+
var = pt_csg_array_dim_fetch_var.of(Z_OBJ_P(var));
358+
}
356359
if (var != NULL && Z_TYPE_P(var) == IS_OBJECT && isPropertyFetchLike(Z_OBJ_P(var))) {
357360
bool isRead;
358361
if (UNEXPECTED(!isReadThroughReference(expr, scope, isRead))) return false;

0 commit comments

Comments
 (0)