From a40c114b397ac7cff23905085c6be0ea557c8bd6 Mon Sep 17 00:00:00 2001 From: Markus Staab Date: Sun, 29 Nov 2026 14:35:31 +0100 Subject: [PATCH 1/5] Fix "Property is never read, only written" false positive, when initialized by ref --- src/Node/ClassStatementsGatherer.php | 4 +++ .../UnusedPrivatePropertyRuleTest.php | 8 +++++ .../PHPStan/Rules/DeadCode/data/bug-15886.php | 29 +++++++++++++++++++ turbo-ext/src/ClassStatementsGatherer.cpp | 5 ++++ 4 files changed, 46 insertions(+) create mode 100644 tests/PHPStan/Rules/DeadCode/data/bug-15886.php diff --git a/src/Node/ClassStatementsGatherer.php b/src/Node/ClassStatementsGatherer.php index 71a56858423..233c1ddad42 100644 --- a/src/Node/ClassStatementsGatherer.php +++ b/src/Node/ClassStatementsGatherer.php @@ -228,6 +228,10 @@ private function gatherNodes(Node $node, Scope $scope): void return; } if ($node instanceof Expr\AssignRef) { + if ($node->var instanceof PropertyFetch || $node->var instanceof StaticPropertyFetch) { + // the property now shares its value with the referenced variable, which can read it elsewhere + $this->propertyUsages[] = new PropertyRead($node->var, $scope); + } if (!$node->expr instanceof PropertyFetch && !$node->expr instanceof StaticPropertyFetch) { $this->gatherNodes($node->expr, $scope); return; diff --git a/tests/PHPStan/Rules/DeadCode/UnusedPrivatePropertyRuleTest.php b/tests/PHPStan/Rules/DeadCode/UnusedPrivatePropertyRuleTest.php index b49feb428af..0fcb851e669 100644 --- a/tests/PHPStan/Rules/DeadCode/UnusedPrivatePropertyRuleTest.php +++ b/tests/PHPStan/Rules/DeadCode/UnusedPrivatePropertyRuleTest.php @@ -516,4 +516,12 @@ public function testBug12201(): void ]); } + public function testBug15886(): void + { + $this->alwaysWrittenTags = []; + $this->alwaysReadTags = []; + + $this->analyse([__DIR__ . '/data/bug-15886.php'], []); + } + } diff --git a/tests/PHPStan/Rules/DeadCode/data/bug-15886.php b/tests/PHPStan/Rules/DeadCode/data/bug-15886.php new file mode 100644 index 00000000000..e248ed0a4c7 --- /dev/null +++ b/tests/PHPStan/Rules/DeadCode/data/bug-15886.php @@ -0,0 +1,29 @@ + $forwarded + */ +function byRefConsumedArg(array &$forwarded):void { + new class($forwarded) + { + /** + * @var list + */ + private array $forwarded; + + /** + * @param list $forwarded + */ + public function __construct(array &$forwarded) + { + $this->forwarded = &$forwarded; + } + + public function notify(): void + { + $this->forwarded[] = 'suite finished'; + } + }; +} diff --git a/turbo-ext/src/ClassStatementsGatherer.cpp b/turbo-ext/src/ClassStatementsGatherer.cpp index 2754ebdc2ef..78236a88a68 100644 --- a/turbo-ext/src/ClassStatementsGatherer.cpp +++ b/turbo-ext/src/ClassStatementsGatherer.cpp @@ -84,6 +84,7 @@ static NodeProp pt_csg_property_assign_fetch = PT_NODE_PROP(PT_CLASS_PROPERTY_AS static NodeProp pt_csg_property_assign_expr = PT_NODE_PROP(PT_CLASS_PROPERTY_ASSIGN_NODE, "assignedExpr"); static NodeProp pt_csg_coalesce_var = PT_NODE_PROP(PT_CLASS_COALESCE_ASSIGN_OP_EXPR, "var"); static NodeProp pt_csg_assign_ref_expr = PT_NODE_PROP(PT_CLASS_ASSIGN_REF_EXPR, "expr"); +static NodeProp pt_csg_assign_ref_var = PT_NODE_PROP(PT_CLASS_ASSIGN_REF_EXPR, "var"); static NodeProp pt_csg_variable_name = PT_NODE_PROP(PT_CLASS_VARIABLE, "name"); static NodeProp pt_csg_array_dim_fetch_var = PT_NODE_PROP(PT_CLASS_ARRAY_DIM_FETCH, "var"); static NodeProp pt_csg_param_flags = PT_NODE_PROP(PT_CLASS_PARAM, "flags"); @@ -316,6 +317,10 @@ class ClassStatementsGatherer return gatherNodes(Z_OBJ_P(var), scope); } if (isInstanceOf(node, PT_CLASS_ASSIGN_REF_EXPR)) { + zval *var = pt_csg_assign_ref_var.of(node); + if (var != NULL && Z_TYPE_P(var) == IS_OBJECT && isPropertyFetchLike(Z_OBJ_P(var))) { + if (UNEXPECTED(!pushNodeScope(slots::propertyUsages, PT_CLASS_PROPERTY_READ, var, scope))) return false; + } zval *expr = pt_csg_assign_ref_expr.of(node); if (UNEXPECTED(expr == NULL || Z_TYPE_P(expr) != IS_OBJECT)) return !EG(exception); if (!isPropertyFetchLike(Z_OBJ_P(expr))) return gatherNodes(Z_OBJ_P(expr), scope); From 39ed1e53fea7a39b2674d31a6655de152afa2426 Mon Sep 17 00:00:00 2001 From: Markus Staab Date: Sun, 29 Nov 2026 14:45:10 +0100 Subject: [PATCH 2/5] Fix "Property is never read, only written" false positive, when initialized by ref with promoted properties --- src/Node/ClassStatementsGatherer.php | 8 +++- .../UnusedPrivatePropertyRuleTest.php | 9 ++++ .../DeadCode/data/bug-15886-promoted.php | 42 +++++++++++++++++++ turbo-ext/src/ClassStatementsGatherer.cpp | 9 ++++ 4 files changed, 67 insertions(+), 1 deletion(-) create mode 100644 tests/PHPStan/Rules/DeadCode/data/bug-15886-promoted.php diff --git a/src/Node/ClassStatementsGatherer.php b/src/Node/ClassStatementsGatherer.php index 233c1ddad42..d69c97933a6 100644 --- a/src/Node/ClassStatementsGatherer.php +++ b/src/Node/ClassStatementsGatherer.php @@ -159,8 +159,14 @@ private function gatherNodes(Node $node, Scope $scope): void if ($node instanceof ClassPropertyNode) { $this->properties[] = $node; if ($node->isPromoted()) { + $propertyFetch = new PropertyFetch(new Expr\Variable('this'), new Identifier($node->getName())); + $originalNode = $node->getOriginalNode(); + if ($originalNode instanceof Node\Param && $originalNode->byRef) { + // the property shares its value with the referenced argument, which can read it elsewhere + $this->propertyUsages[] = new PropertyRead($propertyFetch, $scope); + } $this->propertyUsages[] = new PropertyWrite( - new PropertyFetch(new Expr\Variable('this'), new Identifier($node->getName())), + $propertyFetch, $scope, true, $node, diff --git a/tests/PHPStan/Rules/DeadCode/UnusedPrivatePropertyRuleTest.php b/tests/PHPStan/Rules/DeadCode/UnusedPrivatePropertyRuleTest.php index 0fcb851e669..2283e32cf78 100644 --- a/tests/PHPStan/Rules/DeadCode/UnusedPrivatePropertyRuleTest.php +++ b/tests/PHPStan/Rules/DeadCode/UnusedPrivatePropertyRuleTest.php @@ -524,4 +524,13 @@ public function testBug15886(): void $this->analyse([__DIR__ . '/data/bug-15886.php'], []); } + #[RequiresPhp('>= 8.0.0')] + public function testBug15886Promoted(): void + { + $this->alwaysWrittenTags = []; + $this->alwaysReadTags = []; + + $this->analyse([__DIR__ . '/data/bug-15886-promoted.php'], []); + } + } diff --git a/tests/PHPStan/Rules/DeadCode/data/bug-15886-promoted.php b/tests/PHPStan/Rules/DeadCode/data/bug-15886-promoted.php new file mode 100644 index 00000000000..9e5a5627522 --- /dev/null +++ b/tests/PHPStan/Rules/DeadCode/data/bug-15886-promoted.php @@ -0,0 +1,42 @@ += 8.0 + +declare(strict_types = 1); + +namespace Bug15886Promoted; + +/** + * @param list $forwarded + */ +function byRefConsumedArg(array &$forwarded):void { + new class($forwarded) + { + /** + * @param list $forwarded + */ + public function __construct(private array &$forwarded) + { + } + + public function notify(): void + { + $this->forwarded[] = 'suite finished'; + } + }; +} + +class Foo +{ + + /** + * @param list $forwarded + */ + public function __construct(private array &$forwarded) + { + } + + public function notify(): void + { + $this->forwarded[] = 'suite finished'; + } + +} diff --git a/turbo-ext/src/ClassStatementsGatherer.cpp b/turbo-ext/src/ClassStatementsGatherer.cpp index 78236a88a68..174ffe4364d 100644 --- a/turbo-ext/src/ClassStatementsGatherer.cpp +++ b/turbo-ext/src/ClassStatementsGatherer.cpp @@ -67,6 +67,7 @@ using visitors::isInstanceOf; static NodeProp pt_csg_class_property_node_name = PT_NODE_PROP(PT_CLASS_CLASS_PROPERTY_NODE, "name"); static NodeProp pt_csg_class_property_node_is_promoted = PT_NODE_PROP(PT_CLASS_CLASS_PROPERTY_NODE, "isPromoted"); +static NodeProp pt_csg_class_property_node_original = PT_NODE_PROP(PT_CLASS_CLASS_PROPERTY_NODE, "originalNode"); static NodeProp pt_csg_static_call_name = PT_NODE_PROP(PT_CLASS_STATIC_CALL, "name"); static NodeProp pt_csg_static_call_class = PT_NODE_PROP(PT_CLASS_STATIC_CALL, "class"); static NodeProp pt_csg_identifier_name = PT_IDENTIFIER_PROP; @@ -88,6 +89,7 @@ static NodeProp pt_csg_assign_ref_var = PT_NODE_PROP(PT_CLASS_ASSIGN_REF_EXPR, " static NodeProp pt_csg_variable_name = PT_NODE_PROP(PT_CLASS_VARIABLE, "name"); static NodeProp pt_csg_array_dim_fetch_var = PT_NODE_PROP(PT_CLASS_ARRAY_DIM_FETCH, "var"); static NodeProp pt_csg_param_flags = PT_NODE_PROP(PT_CLASS_PARAM, "flags"); +static NodeProp pt_csg_param_by_ref = PT_NODE_PROP(PT_CLASS_PARAM, "byRef"); static NodeProp pt_csg_param_hooks = PT_NODE_PROP(PT_CLASS_PARAM, "hooks"); static NodeProp pt_csg_param_var = PT_NODE_PROP(PT_CLASS_PARAM, "var"); static NodeProp pt_csg_arg_value = PT_NODE_PROP(PT_CLASS_ARG, "value"); @@ -263,6 +265,13 @@ class ClassStatementsGatherer } zv::Val fetch = thisPropertyFetch(name, NULL); if (UNEXPECTED(fetch.isUndef())) return false; + zval *original = pt_csg_class_property_node_original.of(node); + if (original != NULL && Z_TYPE_P(original) == IS_OBJECT && isInstanceOf(Z_OBJ_P(original), PT_CLASS_PARAM)) { + zval *byRef = pt_csg_param_by_ref.of(Z_OBJ_P(original)); + if (byRef != NULL && Z_TYPE_P(byRef) == IS_TRUE) { + if (UNEXPECTED(!pushNodeScope(slots::propertyUsages, PT_CLASS_PROPERTY_READ, fetch.raw(), scope))) return false; + } + } return pushPropertyWrite(fetch.raw(), scope, true, &nodeZv); } return true; From d522fdb94ce66831393364df04425d7a4869e5d6 Mon Sep 17 00:00:00 2001 From: Markus Staab Date: Sun, 29 Nov 2026 15:15:38 +0100 Subject: [PATCH 3/5] Fix "Property is never read, only written" false positive, when initialized by ref with static property --- .../UnusedPrivatePropertyRuleTest.php | 8 +++ .../Rules/DeadCode/data/bug-15886-static.php | 49 +++++++++++++++++++ 2 files changed, 57 insertions(+) create mode 100644 tests/PHPStan/Rules/DeadCode/data/bug-15886-static.php diff --git a/tests/PHPStan/Rules/DeadCode/UnusedPrivatePropertyRuleTest.php b/tests/PHPStan/Rules/DeadCode/UnusedPrivatePropertyRuleTest.php index 2283e32cf78..3a1a122e0f0 100644 --- a/tests/PHPStan/Rules/DeadCode/UnusedPrivatePropertyRuleTest.php +++ b/tests/PHPStan/Rules/DeadCode/UnusedPrivatePropertyRuleTest.php @@ -533,4 +533,12 @@ public function testBug15886Promoted(): void $this->analyse([__DIR__ . '/data/bug-15886-promoted.php'], []); } + public function testBug15886Static(): void + { + $this->alwaysWrittenTags = []; + $this->alwaysReadTags = []; + + $this->analyse([__DIR__ . '/data/bug-15886-static.php'], []); + } + } diff --git a/tests/PHPStan/Rules/DeadCode/data/bug-15886-static.php b/tests/PHPStan/Rules/DeadCode/data/bug-15886-static.php new file mode 100644 index 00000000000..3d75347f13e --- /dev/null +++ b/tests/PHPStan/Rules/DeadCode/data/bug-15886-static.php @@ -0,0 +1,49 @@ + + */ + private static array $forwarded = []; + + /** + * @param list $forwarded + */ + public static function init(array &$forwarded): void + { + self::$forwarded = &$forwarded; + } + + public static function notify(): void + { + self::$forwarded[] = 'suite finished'; + } + +} + +class Bar +{ + + /** + * @var list + */ + private static array $forwarded = []; + + /** + * @param list $forwarded + */ + public static function init(array &$forwarded): void + { + static::$forwarded = &$forwarded; + } + + public static function notify(): void + { + static::$forwarded[] = 'suite finished'; + } + +} From 0a9ab221c11a0db294064f5abdf82c8779c3e268 Mon Sep 17 00:00:00 2001 From: Markus Staab Date: Sun, 29 Nov 2026 15:49:08 +0100 Subject: [PATCH 4/5] fix build --- src/Node/ClassStatementsGatherer.php | 7 +- .../Property/PropertyReferenceBinding.php | 94 +++++++++ .../UnusedPrivatePropertyRuleTest.php | 24 +++ .../Rules/DeadCode/data/bug-15886-local.php | 186 ++++++++++++++++++ turbo-ext/src/ClassStatementsGatherer.cpp | 32 ++- 5 files changed, 338 insertions(+), 5 deletions(-) create mode 100644 src/Node/Property/PropertyReferenceBinding.php create mode 100644 tests/PHPStan/Rules/DeadCode/data/bug-15886-local.php diff --git a/src/Node/ClassStatementsGatherer.php b/src/Node/ClassStatementsGatherer.php index d69c97933a6..2cdb4792cfa 100644 --- a/src/Node/ClassStatementsGatherer.php +++ b/src/Node/ClassStatementsGatherer.php @@ -18,6 +18,7 @@ use PHPStan\Node\Printer\ExprPrinter; use PHPStan\Node\Property\PropertyAssign; use PHPStan\Node\Property\PropertyRead; +use PHPStan\Node\Property\PropertyReferenceBinding; use PHPStan\Node\Property\PropertyWrite; use PHPStan\Reflection\ClassReflection; use PHPStan\Reflection\MethodReflection; @@ -234,8 +235,10 @@ private function gatherNodes(Node $node, Scope $scope): void return; } if ($node instanceof Expr\AssignRef) { - if ($node->var instanceof PropertyFetch || $node->var instanceof StaticPropertyFetch) { - // the property now shares its value with the referenced variable, which can read it elsewhere + if ( + ($node->var instanceof PropertyFetch || $node->var instanceof StaticPropertyFetch) + && PropertyReferenceBinding::isReadThroughReference($node->expr, $scope, $this->methods) + ) { $this->propertyUsages[] = new PropertyRead($node->var, $scope); } if (!$node->expr instanceof PropertyFetch && !$node->expr instanceof StaticPropertyFetch) { diff --git a/src/Node/Property/PropertyReferenceBinding.php b/src/Node/Property/PropertyReferenceBinding.php new file mode 100644 index 00000000000..2cf52f34cf5 --- /dev/null +++ b/src/Node/Property/PropertyReferenceBinding.php @@ -0,0 +1,94 @@ +prop = &$expr`) + * lets its value be read through the other side of the reference. + * + * Called from the native ClassStatementsGatherer in turbo-ext as well. + */ +final class PropertyReferenceBinding +{ + + private const VARIABLE_READING_FUNCTIONS = ['compact', 'get_defined_vars']; + + /** + * @param array $methods methods gathered so far in the class + */ + public static function isReadThroughReference(Expr $referencedExpr, Scope $scope, array $methods): bool + { + if (!$referencedExpr instanceof Variable || !is_string($referencedExpr->name)) { + return true; + } + if ($scope->isInAnonymousFunction()) { + return true; + } + + $function = $scope->getFunction(); + if (!$function instanceof MethodReflection) { + return true; + } + + $methodName = strtolower($function->getName()); + $methodNode = null; + foreach ($methods as $method) { + if ($method->getNode()->name->toLowerString() !== $methodName) { + continue; + } + $methodNode = $method->getNode(); + } + if ($methodNode === null || $methodNode->stmts === null) { + return true; + } + + $variableName = $referencedExpr->name; + foreach ($methodNode->params as $param) { + if (!$param->var instanceof Variable || $param->var->name !== $variableName) { + continue; + } + + // the caller holds the other side of a by-ref parameter + return $param->byRef; + } + + // a local variable keeps the value alive only while something else in the method uses it + // (global/static declarations, closure uses, further reads, passing it on, ...) + $overwrittenVariables = []; + $nodeFinder = new NodeFinder(); + foreach ($nodeFinder->findInstanceOf($methodNode->stmts, Assign::class) as $assign) { + $overwrittenVariables[spl_object_id($assign->var)] = true; + } + + return $nodeFinder->findFirst($methodNode->stmts, static function (Node $node) use ($referencedExpr, $variableName, $overwrittenVariables): bool { + if ($node instanceof Variable) { + if (!is_string($node->name)) { + return true; + } + + return $node->name === $variableName + && $node !== $referencedExpr + && !isset($overwrittenVariables[spl_object_id($node)]); + } + + return $node instanceof FuncCall + && $node->name instanceof Node\Name + && in_array($node->name->toLowerString(), self::VARIABLE_READING_FUNCTIONS, true); + }) !== null; + } + +} diff --git a/tests/PHPStan/Rules/DeadCode/UnusedPrivatePropertyRuleTest.php b/tests/PHPStan/Rules/DeadCode/UnusedPrivatePropertyRuleTest.php index 3a1a122e0f0..c7896366704 100644 --- a/tests/PHPStan/Rules/DeadCode/UnusedPrivatePropertyRuleTest.php +++ b/tests/PHPStan/Rules/DeadCode/UnusedPrivatePropertyRuleTest.php @@ -541,4 +541,28 @@ public function testBug15886Static(): void $this->analyse([__DIR__ . '/data/bug-15886-static.php'], []); } + public function testBug15886Local(): void + { + $this->alwaysWrittenTags = []; + $this->alwaysReadTags = []; + + $this->analyse([__DIR__ . '/data/bug-15886-local.php'], [ + [ + 'Property Bug15886Local\\ThrowawayLocal::$cache is never read, only written.', + 9, + 'See: https://phpstan.org/developing-extensions/always-read-written-properties', + ], + [ + 'Property Bug15886Local\\ByValueParameter::$cache is never read, only written.', + 28, + 'See: https://phpstan.org/developing-extensions/always-read-written-properties', + ], + [ + 'Static property Bug15886Local\\StaticPropertyThrowawayLocal::$cache is never read, only written.', + 173, + 'See: https://phpstan.org/developing-extensions/always-read-written-properties', + ], + ]); + } + } diff --git a/tests/PHPStan/Rules/DeadCode/data/bug-15886-local.php b/tests/PHPStan/Rules/DeadCode/data/bug-15886-local.php new file mode 100644 index 00000000000..6bbce7b7905 --- /dev/null +++ b/tests/PHPStan/Rules/DeadCode/data/bug-15886-local.php @@ -0,0 +1,186 @@ + */ + private array $cache; + + public function __construct() + { + $tmp = []; + $this->cache = &$tmp; + } + + public function add(): void + { + $this->cache[] = 'x'; + } + +} + +class ByValueParameter +{ + + /** @var list */ + private array $cache; + + /** @param list $tmp */ + public function __construct(array $tmp) + { + $this->cache = &$tmp; + } + + public function add(): void + { + $this->cache[] = 'x'; + } + +} + +class LocalReadAfterwards +{ + + /** @var list */ + private array $cache; + + public function __construct() + { + $tmp = []; + $this->cache = &$tmp; + $this->add(); + echo count($tmp); + } + + public function add(): void + { + $this->cache[] = 'x'; + } + +} + +class LocalPassedOn +{ + + /** @var list */ + private array $cache; + + /** @param \ArrayObject $registry */ + public function __construct(\ArrayObject $registry) + { + $tmp = []; + $this->cache = &$tmp; + $registry->append($tmp); + } + + public function add(): void + { + $this->cache[] = 'x'; + } + +} + +class GlobalVariable +{ + + /** @var list */ + private array $cache; + + public function __construct() + { + global $tmp; + $this->cache = &$tmp; + } + + public function add(): void + { + $this->cache[] = 'x'; + } + +} + +class StaticVariable +{ + + /** @var list */ + private array $cache; + + public function __construct() + { + static $tmp = []; + $this->cache = &$tmp; + } + + public function add(): void + { + $this->cache[] = 'x'; + } + +} + +class CapturedByClosure +{ + + /** @var list */ + private array $cache; + + public \Closure $count; + + public function __construct() + { + $tmp = []; + $this->cache = &$tmp; + $this->count = static function () use (&$tmp): int { + return count($tmp); + }; + } + + public function add(): void + { + $this->cache[] = 'x'; + } + +} + +class Compacted +{ + + /** @var list */ + private array $cache; + + /** @return array */ + public function bind(): array + { + $tmp = []; + $this->cache = &$tmp; + + return compact('tmp'); + } + + public function add(): void + { + $this->cache[] = 'x'; + } + +} + +class StaticPropertyThrowawayLocal +{ + + /** @var list */ + private static array $cache = []; + + public static function bind(): void + { + $tmp = []; + self::$cache = &$tmp; + } + + public static function add(): void + { + self::$cache[] = 'x'; + } + +} diff --git a/turbo-ext/src/ClassStatementsGatherer.cpp b/turbo-ext/src/ClassStatementsGatherer.cpp index 174ffe4364d..e1af8a8c815 100644 --- a/turbo-ext/src/ClassStatementsGatherer.cpp +++ b/turbo-ext/src/ClassStatementsGatherer.cpp @@ -238,6 +238,30 @@ class ClassStatementsGatherer return pt_type_call_static_ce(typeUtils, PT_LC("findthistype"), 1, type); } + /* PHPStan\Node\Property\PropertyReferenceBinding::isReadThroughReference($expr, $scope, $this->methods), + * resolved by name like findThisType(); only reached for reference + * assignments to a property, so the PHP helper is not ported */ + bool isReadThroughReference(zval *expr, zval *scope, bool &result) + { + static const char name[] = "PHPStan\\Node\\Property\\PropertyReferenceBinding"; + zv::Str className = zv::Str::adopt(zend_string_init(name, sizeof(name) - 1, 0)); + zend_class_entry *helper = zend_lookup_class(className.get()); + if (UNEXPECTED(helper == NULL)) { + if (!EG(exception)) { + zend_throw_error(NULL, "Class \"%s\" not found", name); + } + return false; + } + zval args[3]; + ZVAL_COPY_VALUE(&args[0], expr); + ZVAL_COPY_VALUE(&args[1], scope); + ZVAL_COPY_VALUE(&args[2], OBJ_PROP_NUM(self, slots::methods)); + zv::Val isRead = pt_type_call_static_ce(helper, PT_LC("isreadthroughreference"), 3, args); + if (UNEXPECTED(isRead.isUndef())) return false; + result = Z_TYPE_P(isRead.raw()) == IS_TRUE; + return true; + } + bool gatherNodes(zend_object *node, zval *scope) { bool inClass; @@ -327,11 +351,13 @@ class ClassStatementsGatherer } if (isInstanceOf(node, PT_CLASS_ASSIGN_REF_EXPR)) { zval *var = pt_csg_assign_ref_var.of(node); - if (var != NULL && Z_TYPE_P(var) == IS_OBJECT && isPropertyFetchLike(Z_OBJ_P(var))) { - if (UNEXPECTED(!pushNodeScope(slots::propertyUsages, PT_CLASS_PROPERTY_READ, var, scope))) return false; - } zval *expr = pt_csg_assign_ref_expr.of(node); if (UNEXPECTED(expr == NULL || Z_TYPE_P(expr) != IS_OBJECT)) return !EG(exception); + if (var != NULL && Z_TYPE_P(var) == IS_OBJECT && isPropertyFetchLike(Z_OBJ_P(var))) { + bool isRead; + if (UNEXPECTED(!isReadThroughReference(expr, scope, isRead))) return false; + if (isRead && UNEXPECTED(!pushNodeScope(slots::propertyUsages, PT_CLASS_PROPERTY_READ, var, scope))) return false; + } if (!isPropertyFetchLike(Z_OBJ_P(expr))) return gatherNodes(Z_OBJ_P(expr), scope); if (UNEXPECTED(!pushNodeScope(slots::propertyUsages, PT_CLASS_PROPERTY_READ, expr, scope))) return false; return pushPropertyWrite(expr, scope, false, &nodeZv); From 845e12c45ba7d8881d53e274da14a9fe6fcee970 Mon Sep 17 00:00:00 2001 From: Markus Staab Date: Sun, 29 Nov 2026 16:07:35 +0100 Subject: [PATCH 5/5] fix by array-offset --- src/Node/ClassStatementsGatherer.php | 8 ++- .../UnusedPrivatePropertyRuleTest.php | 14 ++++ .../DeadCode/data/bug-15886-array-offset.php | 68 +++++++++++++++++++ turbo-ext/src/ClassStatementsGatherer.cpp | 3 + 4 files changed, 91 insertions(+), 2 deletions(-) create mode 100644 tests/PHPStan/Rules/DeadCode/data/bug-15886-array-offset.php diff --git a/src/Node/ClassStatementsGatherer.php b/src/Node/ClassStatementsGatherer.php index 2cdb4792cfa..33bd8b55f6a 100644 --- a/src/Node/ClassStatementsGatherer.php +++ b/src/Node/ClassStatementsGatherer.php @@ -235,11 +235,15 @@ private function gatherNodes(Node $node, Scope $scope): void return; } if ($node instanceof Expr\AssignRef) { + $boundVar = $node->var; + while ($boundVar instanceof ArrayDimFetch) { + $boundVar = $boundVar->var; + } if ( - ($node->var instanceof PropertyFetch || $node->var instanceof StaticPropertyFetch) + ($boundVar instanceof PropertyFetch || $boundVar instanceof StaticPropertyFetch) && PropertyReferenceBinding::isReadThroughReference($node->expr, $scope, $this->methods) ) { - $this->propertyUsages[] = new PropertyRead($node->var, $scope); + $this->propertyUsages[] = new PropertyRead($boundVar, $scope); } if (!$node->expr instanceof PropertyFetch && !$node->expr instanceof StaticPropertyFetch) { $this->gatherNodes($node->expr, $scope); diff --git a/tests/PHPStan/Rules/DeadCode/UnusedPrivatePropertyRuleTest.php b/tests/PHPStan/Rules/DeadCode/UnusedPrivatePropertyRuleTest.php index c7896366704..c05b94935d8 100644 --- a/tests/PHPStan/Rules/DeadCode/UnusedPrivatePropertyRuleTest.php +++ b/tests/PHPStan/Rules/DeadCode/UnusedPrivatePropertyRuleTest.php @@ -565,4 +565,18 @@ public function testBug15886Local(): void ]); } + public function testBug15886ArrayOffset(): void + { + $this->alwaysWrittenTags = []; + $this->alwaysReadTags = []; + + $this->analyse([__DIR__ . '/data/bug-15886-array-offset.php'], [ + [ + 'Property Bug15886ArrayOffset\\ArrayOffsetThrowawayLocal::$counters is never read, only written.', + 56, + 'See: https://phpstan.org/developing-extensions/always-read-written-properties', + ], + ]); + } + } diff --git a/tests/PHPStan/Rules/DeadCode/data/bug-15886-array-offset.php b/tests/PHPStan/Rules/DeadCode/data/bug-15886-array-offset.php new file mode 100644 index 00000000000..1c0884b31da --- /dev/null +++ b/tests/PHPStan/Rules/DeadCode/data/bug-15886-array-offset.php @@ -0,0 +1,68 @@ + */ + private array $counters = []; + + public function bind(int &$counter): void + { + $this->counters['requests'] = &$counter; + } + + public function reset(): void + { + $this->counters['requests'] = 0; + } +} + +class NestedArrayOffset +{ + /** @var array> */ + private array $counters = []; + + public function bind(int &$counter): void + { + $this->counters['http']['requests'] = &$counter; + } + + public function reset(): void + { + $this->counters['http']['requests'] = 0; + } +} + +class StaticArrayOffset +{ + /** @var array */ + private static array $counters = []; + + public static function bind(int &$counter): void + { + self::$counters['requests'] = &$counter; + } + + public static function reset(): void + { + self::$counters['requests'] = 0; + } +} + +class ArrayOffsetThrowawayLocal +{ + /** @var array */ + private array $counters = []; + + public function bind(): void + { + $counter = 0; + $this->counters['requests'] = &$counter; + } + + public function reset(): void + { + $this->counters['requests'] = 0; + } +} diff --git a/turbo-ext/src/ClassStatementsGatherer.cpp b/turbo-ext/src/ClassStatementsGatherer.cpp index e1af8a8c815..218dba9c7ea 100644 --- a/turbo-ext/src/ClassStatementsGatherer.cpp +++ b/turbo-ext/src/ClassStatementsGatherer.cpp @@ -353,6 +353,9 @@ class ClassStatementsGatherer zval *var = pt_csg_assign_ref_var.of(node); zval *expr = pt_csg_assign_ref_expr.of(node); if (UNEXPECTED(expr == NULL || Z_TYPE_P(expr) != IS_OBJECT)) return !EG(exception); + while (var != NULL && Z_TYPE_P(var) == IS_OBJECT && isInstanceOf(Z_OBJ_P(var), PT_CLASS_ARRAY_DIM_FETCH)) { + var = pt_csg_array_dim_fetch_var.of(Z_OBJ_P(var)); + } if (var != NULL && Z_TYPE_P(var) == IS_OBJECT && isPropertyFetchLike(Z_OBJ_P(var))) { bool isRead; if (UNEXPECTED(!isReadThroughReference(expr, scope, isRead))) return false;