Skip to content
Permalink
Browse files

Don’t warn about unused properties that override a used one

  • Loading branch information...
muglug committed Apr 17, 2019
1 parent a529f74 commit 86c6b6fa36210f487f8ae1d6c0d425d8a0c1c1db
@@ -433,7 +433,10 @@ public function checkClassReferences()
throw new \UnexpectedValueException('Should not be checking references');
}
$this->codebase->classlikes->checkClassReferences($this->codebase->methods, $this->debug_output);
$this->codebase->classlikes->checkClassReferences(
$this->codebase->methods,
$this->debug_output
);
}
/**
@@ -695,6 +695,7 @@ public function checkClassReferences(Methods $methods, bool $debug_output = fals
}
} else {
$this->checkMethodReferences($classlike_storage, $methods);
$this->checkPropertyReferences($classlike_storage);
}
}
}
@@ -926,6 +927,15 @@ private function checkMethodReferences(ClassLikeStorage $classlike_storage, Meth
}
}
}
}
/**
* @return void
*/
private function checkPropertyReferences(ClassLikeStorage $classlike_storage)
{
$project_analyzer = \Psalm\Internal\Analyzer\ProjectAnalyzer::getInstance();
$codebase = $project_analyzer->getCodebase();
foreach ($classlike_storage->properties as $property_name => $property_storage) {
$property_referenced = $this->file_reference_provider->isClassPropertyReferenced(
@@ -939,28 +949,32 @@ private function checkMethodReferences(ClassLikeStorage $classlike_storage, Meth
$property_id = $classlike_storage->name . '::$' . $property_name;
if ($property_storage->visibility === ClassLikeAnalyzer::VISIBILITY_PUBLIC) {
$issue = new PossiblyUnusedProperty(
'Cannot find uses of public property ' . $property_id,
$property_storage->location
);
$has_parent_references = isset($classlike_storage->overridden_property_ids[$property_name]);
if ($codebase->alter_code) {
if ($property_storage->stmt_location
&& isset($project_analyzer->getIssuesToFix()['PossiblyUnusedProperty'])
&& !$codebase->analyzer->hasMixedMemberName('$' . $property_name)
&& !IssueBuffer::isSuppressed($issue, $classlike_storage->suppressed_issues)
) {
FileManipulationBuffer::addForCodeLocation(
$property_storage->stmt_location,
'',
true
);
if (!$has_parent_references) {
$issue = new PossiblyUnusedProperty(
'Cannot find uses of public property ' . $property_id,
$property_storage->location
);
if ($codebase->alter_code) {
if ($property_storage->stmt_location
&& isset($project_analyzer->getIssuesToFix()['PossiblyUnusedProperty'])
&& !$codebase->analyzer->hasMixedMemberName('$' . $property_name)
&& !IssueBuffer::isSuppressed($issue, $classlike_storage->suppressed_issues)
) {
FileManipulationBuffer::addForCodeLocation(
$property_storage->stmt_location,
'',
true
);
}
} elseif (IssueBuffer::accepts(
$issue,
$classlike_storage->suppressed_issues
)) {
// fall through
}
} elseif (IssueBuffer::accepts(
$issue,
$classlike_storage->suppressed_issues
)) {
// fall through
}
} elseif (!isset($classlike_storage->declaring_method_ids['__get'])) {
$issue = new UnusedProperty(
@@ -347,6 +347,29 @@ function takesCallable(callable $c) : void {
takesCallable([C::class, "foo"]);',
],
'propertyAndMethodOverriddenDownstream' => [
'<?php
class A {
/** @var string */
public $foo = "hello";
public function bar() : void {}
}
class B extends A {
/** @var string */
public $foo = "goodbye";
public function bar() : void {}
}
function foo(A $a) : void {
echo $a->foo;
$a->bar();
}
foo(new B());',
],
];
}
@@ -477,6 +500,21 @@ public function bar() : void {}
(new C)->bar();',
'error_message' => 'PossiblyUnusedMethod',
],
'propertyOverriddenDownstreamAndNotUsed' => [
'<?php
class A {
/** @var string */
public $foo = "hello";
}
class B extends A {
/** @var string */
public $foo = "goodbye";
}
new B();',
'error_message' => 'PossiblyUnusedProperty',
],
];
}
}

0 comments on commit 86c6b6f

Please sign in to comment.
You can’t perform that action at this time.