1
0
mirror of https://github.com/danog/psalm.git synced 2024-11-30 04:39:00 +01:00

Detect unused properties that are written to inside arrays

This commit is contained in:
Matthew Brown 2021-12-11 11:42:05 -05:00
parent 19ae9e81d2
commit b558748db2
2 changed files with 37 additions and 2 deletions

View File

@ -228,7 +228,7 @@ class AtomicPropertyFetchAnalyzer
$naive_property_exists = $codebase->properties->propertyExists(
$property_id,
true,
!$in_assignment,
$statements_analyzer,
$context,
$codebase->collect_locations ? new CodeLocation($statements_analyzer->getSource(), $stmt) : null
@ -252,7 +252,7 @@ class AtomicPropertyFetchAnalyzer
if ($new_class_storage
&& ($codebase->properties->propertyExists(
$new_property_id,
true,
!$in_assignment,
$statements_analyzer,
$context,
$codebase->collect_locations

View File

@ -1195,6 +1195,17 @@ class UnusedCodeTest extends TestCase
foobar
',
],
'usedPropertyAsAssignmentKey' => [
'<?php
class A {
public string $foo = "bar";
public array $bar = [];
}
$a = new A();
$a->bar[$a->foo] = "bar";
print_r($a->bar);',
],
];
}
@ -1248,6 +1259,30 @@ class UnusedCodeTest extends TestCase
'error_message' => 'PossiblyUnusedProperty',
'error_levels' => ['UnusedVariable'],
],
'possiblyUnusedPropertyWrittenNeverRead' => [
'<?php
class A {
/** @var string */
public $foo = "hello";
}
$a = new A();
$a->foo = "bar";',
'error_message' => 'PossiblyUnusedProperty',
'error_levels' => ['UnusedVariable'],
],
'possiblyUnusedPropertyWithArrayWrittenNeverRead' => [
'<?php
class A {
/** @var list<string> */
public array $foo = [];
}
$a = new A();
$a->foo[] = "bar";',
'error_message' => 'PossiblyUnusedProperty',
'error_levels' => ['UnusedVariable'],
],
'unusedProperty' => [
'<?php
class A {