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:
parent
19ae9e81d2
commit
b558748db2
@ -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
|
||||
|
@ -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 {
|
||||
|
Loading…
Reference in New Issue
Block a user