Skip to content

Commit 4cd2ef2

Browse files
committed
Fix "Property is never read, only written" false positive, when initialized by ref with promoted properties
1 parent 6b259f8 commit 4cd2ef2

4 files changed

Lines changed: 67 additions & 1 deletion

File tree

‎src/Node/ClassStatementsGatherer.php‎

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -159,8 +159,14 @@ private function gatherNodes(Node $node, Scope $scope): void
159159
if ($node instanceof ClassPropertyNode) {
160160
$this->properties[] = $node;
161161
if ($node->isPromoted()) {
162+
$propertyFetch = new PropertyFetch(new Expr\Variable('this'), new Identifier($node->getName()));
163+
$originalNode = $node->getOriginalNode();
164+
if ($originalNode instanceof Node\Param && $originalNode->byRef) {
165+
// the property shares its value with the referenced argument, which can read it elsewhere
166+
$this->propertyUsages[] = new PropertyRead($propertyFetch, $scope);
167+
}
162168
$this->propertyUsages[] = new PropertyWrite(
163-
new PropertyFetch(new Expr\Variable('this'), new Identifier($node->getName())),
169+
$propertyFetch,
164170
$scope,
165171
true,
166172
$node,

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -524,4 +524,13 @@ public function testBug15886(): void
524524
$this->analyse([__DIR__ . '/data/bug-15886.php'], []);
525525
}
526526

527+
#[RequiresPhp('>= 8.0.0')]
528+
public function testBug15886Promoted(): void
529+
{
530+
$this->alwaysWrittenTags = [];
531+
$this->alwaysReadTags = [];
532+
533+
$this->analyse([__DIR__ . '/data/bug-15886-promoted.php'], []);
534+
}
535+
527536
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
<?php // lint >= 8.0
2+
3+
declare(strict_types = 1);
4+
5+
namespace Bug15886Promoted;
6+
7+
/**
8+
* @param list<string> $forwarded
9+
*/
10+
function byRefConsumedArg(array &$forwarded):void {
11+
new class($forwarded)
12+
{
13+
/**
14+
* @param list<string> $forwarded
15+
*/
16+
public function __construct(private array &$forwarded)
17+
{
18+
}
19+
20+
public function notify(): void
21+
{
22+
$this->forwarded[] = 'suite finished';
23+
}
24+
};
25+
}
26+
27+
class Foo
28+
{
29+
30+
/**
31+
* @param list<string> $forwarded
32+
*/
33+
public function __construct(private array &$forwarded)
34+
{
35+
}
36+
37+
public function notify(): void
38+
{
39+
$this->forwarded[] = 'suite finished';
40+
}
41+
42+
}

‎turbo-ext/src/ClassStatementsGatherer.cpp‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ using visitors::isInstanceOf;
6767

6868
static NodeProp pt_csg_class_property_node_name = PT_NODE_PROP(PT_CLASS_CLASS_PROPERTY_NODE, "name");
6969
static NodeProp pt_csg_class_property_node_is_promoted = PT_NODE_PROP(PT_CLASS_CLASS_PROPERTY_NODE, "isPromoted");
70+
static NodeProp pt_csg_class_property_node_original = PT_NODE_PROP(PT_CLASS_CLASS_PROPERTY_NODE, "originalNode");
7071
static NodeProp pt_csg_static_call_name = PT_NODE_PROP(PT_CLASS_STATIC_CALL, "name");
7172
static NodeProp pt_csg_static_call_class = PT_NODE_PROP(PT_CLASS_STATIC_CALL, "class");
7273
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, "
8889
static NodeProp pt_csg_variable_name = PT_NODE_PROP(PT_CLASS_VARIABLE, "name");
8990
static NodeProp pt_csg_array_dim_fetch_var = PT_NODE_PROP(PT_CLASS_ARRAY_DIM_FETCH, "var");
9091
static NodeProp pt_csg_param_flags = PT_NODE_PROP(PT_CLASS_PARAM, "flags");
92+
static NodeProp pt_csg_param_by_ref = PT_NODE_PROP(PT_CLASS_PARAM, "byRef");
9193
static NodeProp pt_csg_param_hooks = PT_NODE_PROP(PT_CLASS_PARAM, "hooks");
9294
static NodeProp pt_csg_param_var = PT_NODE_PROP(PT_CLASS_PARAM, "var");
9395
static NodeProp pt_csg_arg_value = PT_NODE_PROP(PT_CLASS_ARG, "value");
@@ -263,6 +265,13 @@ class ClassStatementsGatherer
263265
}
264266
zv::Val fetch = thisPropertyFetch(name, NULL);
265267
if (UNEXPECTED(fetch.isUndef())) return false;
268+
zval *original = pt_csg_class_property_node_original.of(node);
269+
if (original != NULL && Z_TYPE_P(original) == IS_OBJECT && isInstanceOf(Z_OBJ_P(original), PT_CLASS_PARAM)) {
270+
zval *byRef = pt_csg_param_by_ref.of(Z_OBJ_P(original));
271+
if (byRef != NULL && Z_TYPE_P(byRef) == IS_TRUE) {
272+
if (UNEXPECTED(!pushNodeScope(slots::propertyUsages, PT_CLASS_PROPERTY_READ, fetch.raw(), scope))) return false;
273+
}
274+
}
266275
return pushPropertyWrite(fetch.raw(), scope, true, &nodeZv);
267276
}
268277
return true;

0 commit comments

Comments
 (0)