Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Detect unused properties that are written to inside arrays #7129

Merged
merged 2 commits into from Dec 11, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
8 changes: 0 additions & 8 deletions src/Psalm/Codebase.php
Expand Up @@ -95,14 +95,6 @@ class Codebase
*/
public $use_referencing_locations = [];

/**
* A map of file names to the classes that they contain explicit references to
* used in collaboration with use_referencing_locations
*
* @var array<string, array<lowercase-string, bool>>
*/
public $use_referencing_files = [];

/**
* @var FileStorageProvider
*/
Expand Down
5 changes: 0 additions & 5 deletions src/Psalm/Context.php
Expand Up @@ -235,11 +235,6 @@ class Context
*/
public $parent_context;

/**
* @var array<string, Type\Union>
*/
public $possible_param_types = [];

/**
* A list of vars that have been assigned to
*
Expand Down
2 changes: 0 additions & 2 deletions src/Psalm/Internal/Analyzer/CanAlias.php
Expand Up @@ -72,8 +72,6 @@ public function visitUse(PhpParser\Node\Stmt\Use_ $stmt): void
// register the path
$codebase->use_referencing_locations[$use_path_lc][] =
new CodeLocation($this, $use);

$codebase->use_referencing_files[$this->getFilePath()][$use_path_lc] = true;
}

if ($codebase->alter_code) {
Expand Down
Expand Up @@ -459,12 +459,6 @@ function (array $carry, Clause $clause): array {
}
}

if ($if_scope->possible_param_types) {
foreach ($if_scope->possible_param_types as $var => $type) {
$context->possible_param_types[$var] = $type;
}
}

if ($if_scope->reasonable_clauses
&& (count($if_scope->reasonable_clauses) > 1 || !$if_scope->reasonable_clauses[0]->wedge)
) {
Expand Down
Expand Up @@ -228,7 +228,7 @@ public static function analyze(

$naive_property_exists = $codebase->properties->propertyExists(
$property_id,
true,
!$in_assignment,
$statements_analyzer,
$context,
$codebase->collect_locations ? new CodeLocation($statements_analyzer->getSource(), $stmt) : null
Expand All @@ -252,7 +252,7 @@ public static function analyze(
if ($new_class_storage
&& ($codebase->properties->propertyExists(
$new_property_id,
true,
!$in_assignment,
$statements_analyzer,
$context,
$codebase->collect_locations
Expand Down
13 changes: 0 additions & 13 deletions src/Psalm/Internal/PhpVisitor/Reflector/ClassLikeNodeScanner.php
Expand Up @@ -1285,19 +1285,6 @@ private function visitClassConstDeclaration(
}

$constant_storage->description = $description;

foreach ($stmt->attrGroups as $attr_group) {
foreach ($attr_group->attrs as $attr) {
$constant_storage->attributes[] = AttributeResolver::resolve(
$this->codebase,
$this->file_scanner,
$this->file_storage,
$this->aliases,
$attr,
$this->storage->name ?? null
);
}
}
}
}

Expand Down
Expand Up @@ -1368,7 +1368,7 @@ private static function handleTemplates(
): array {
$storage->template_types = [];

foreach ($docblock_info->templates as $i => $template_map) {
foreach ($docblock_info->templates as $template_map) {
$template_name = $template_map[0];

if ($template_map[1] !== null && $template_map[2] !== null) {
Expand Down Expand Up @@ -1416,8 +1416,6 @@ private static function handleTemplates(
'fn-' . strtolower($cased_function_id) => $template_type,
];
}

$storage->template_covariants[$i] = $template_map[3];
}

return array_merge($template_types ?: [], $storage->template_types);
Expand Down
7 changes: 0 additions & 7 deletions src/Psalm/Internal/Scope/IfScope.php
Expand Up @@ -74,13 +74,6 @@ class IfScope
*/
public $reasonable_clauses = [];

/**
* Variables that were mixed, but are no longer
*
* @var array<string, Type\Union>|null
*/
public $possible_param_types;

/**
* @var string[]
*/
Expand Down
5 changes: 0 additions & 5 deletions src/Psalm/Storage/ClassConstantStorage.php
Expand Up @@ -38,11 +38,6 @@ class ClassConstantStorage
*/
public $deprecated = false;

/**
* @var list<AttributeStorage>
*/
public $attributes = [];

/**
* @var ?string
*/
Expand Down
5 changes: 0 additions & 5 deletions src/Psalm/Storage/FunctionLikeStorage.php
Expand Up @@ -120,11 +120,6 @@ abstract class FunctionLikeStorage
*/
public $template_types;

/**
* @var array<int, bool>|null
*/
public $template_covariants;

/**
* @var array<int, Assertion>
*/
Expand Down
35 changes: 35 additions & 0 deletions tests/UnusedCodeTest.php
Expand Up @@ -1195,6 +1195,17 @@ public function validate(): void
foobar
',
],
'usedPropertyAsAssignmentKey' => [
'<?php
class A {
public string $foo = "bar";
public array $bar = [];
}

$a = new A();
$a->bar[$a->foo] = "bar";
print_r($a->bar);',
],
];
}

Expand Down Expand Up @@ -1248,6 +1259,30 @@ class A {
'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 {
Expand Down