Skip to content

Commit a0e010a

Browse files
committed
Detect result use of prop assignment
1 parent d02d21e commit a0e010a

File tree

4 files changed

+26
-10
lines changed

4 files changed

+26
-10
lines changed

src/Visitor/PropertyWriteVisitor.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,10 @@ private function markPropertyWrites(Expr $expr): void
9090
{
9191
if ($this->isFetch($expr)) { // $this->prop =
9292
$expr->setAttribute(self::IS_PROPERTY_WRITE, true);
93+
94+
if (!$this->hasUnusedResult()) {
95+
$expr->setAttribute(self::IS_PROPERTY_WRITE_AND_READ, true);
96+
}
9397
}
9498

9599
if ($expr instanceof List_) { // [$this->first, $this->last] =
@@ -99,13 +103,21 @@ private function markPropertyWrites(Expr $expr): void
99103
}
100104
if ($this->isFetch($item->value)) {
101105
$item->value->setAttribute(self::IS_PROPERTY_WRITE, true);
106+
107+
if (!$this->hasUnusedResult()) {
108+
$item->value->setAttribute(self::IS_PROPERTY_WRITE_AND_READ, true);
109+
}
102110
}
103111
}
104112
}
105113

106114
while ($expr instanceof ArrayDimFetch) { // $this->array[] =
107115
if ($this->isFetch($expr->var)) {
108116
$expr->var->setAttribute(self::IS_PROPERTY_WRITE, true);
117+
118+
if (!$this->hasUnusedResult()) {
119+
$expr->var->setAttribute(self::IS_PROPERTY_WRITE_AND_READ, true);
120+
}
109121
break;
110122
}
111123
$expr = $expr->var;

tests/Rule/data/properties/write-array.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,13 @@ class Test
66
{
77
public array $array; // error: Property PropertyWriteArray\Test::array is never read
88
public array $array2; // error: Property PropertyWriteArray\Test::array2 is never read
9+
public array $array3;
910

1011
}
1112

12-
function test(): void {
13+
function test() {
1314
$test = new Test();
1415
$test->array['name'] = 'John';
1516
$test->array2['more']['name'] = 'John';
17+
return $test->array3[] = 1;
1618
}

tests/Rule/data/properties/write-coalesce.php

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,11 @@
44

55
class Coalesce
66
{
7-
public $prop; // error: Property PropertyWriteCoalesce\Coalesce::prop is never read
7+
public $prop1; // error: Property PropertyWriteCoalesce\Coalesce::prop1 is never read
8+
public $prop2;
89
}
910

10-
function test(Coalesce $c): void {
11-
$c->prop ??= 1;
11+
function test(Coalesce $c) {
12+
$c->prop1 ??= 1;
13+
return $c->prop2 ??= 2;
1214
}

tests/Rule/data/properties/write-multi.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,12 @@ class Person
66
{
77
public string $first; // error: Property PropertyMultiWrite\Person::first is never read
88
public string $last; // error: Property PropertyMultiWrite\Person::last is never read
9-
10-
public function __construct(string $name) {
11-
[$this->first, $this->last] = explode(' ', $name, 2);
12-
}
9+
public string $city;
10+
public string $zip;
11+
public string $country;
1312
}
1413

15-
function test(): void {
16-
new Person('John Doe');
14+
function test(Person $p) {
15+
[$p->first, $p->last] = ['John', 'Doe'];
16+
return [$p->city, $p->zip, $p->country] = ['Prague', '10300', 'CZ'];
1717
}

0 commit comments

Comments
 (0)