Skip to content
Permalink
Browse files

Fix checks for parent method implementers

  • Loading branch information...
muglug committed Apr 22, 2019
1 parent cf5fb43 commit 473c8cb83209de1b66a1487400c0ea47a2ee65cc
Showing with 83 additions and 10 deletions.
  1. +23 −8 src/Psalm/Internal/Codebase/ClassLikes.php
  2. +60 −2 tests/FileManipulationTest.php
@@ -811,6 +811,9 @@ private function checkMethodReferences(ClassLikeStorage $classlike_storage, Meth
$has_parent_references = false;
$has_variable_calls = $codebase->analyzer->hasMixedMemberName(strtolower($method_name))
|| $codebase->analyzer->hasMixedMemberName(strtolower($classlike_storage->name . '::'));
if (isset($classlike_storage->overridden_method_ids[$method_name_lc])) {
foreach ($classlike_storage->overridden_method_ids[$method_name_lc] as $parent_method_id) {
$parent_method_storage = $methods->getStorage($parent_method_id);
@@ -821,18 +824,22 @@ private function checkMethodReferences(ClassLikeStorage $classlike_storage, Meth
if (!$parent_method_storage->abstract || $parent_method_referenced) {
$has_parent_references = true;
break;
}
}
}
$has_variable_calls = $codebase->analyzer->hasMixedMemberName(strtolower($method_name))
|| $codebase->analyzer->hasMixedMemberName(strtolower($classlike_storage->name . '::'));
foreach ($classlike_storage->parent_classes as $parent_method_fqcln) {
if ($codebase->analyzer->hasMixedMemberName(
strtolower($parent_method_fqcln) . '::'
)) {
$has_variable_calls = true;
}
}
foreach ($classlike_storage->class_implements as $fq_interface_name) {
$interface_storage = $this->classlike_storage_provider->get($fq_interface_name);
if (!$codebase->analyzer->hasMixedMemberName(
if ($codebase->analyzer->hasMixedMemberName(
strtolower($fq_interface_name) . '::'
)) {
$has_variable_calls = true;
@@ -845,7 +852,6 @@ private function checkMethodReferences(ClassLikeStorage $classlike_storage, Meth
if ($interface_method_referenced) {
$has_parent_references = true;
break;
}
}
}
@@ -882,7 +888,7 @@ private function checkMethodReferences(ClassLikeStorage $classlike_storage, Meth
} elseif (!isset($classlike_storage->declaring_method_ids['__call'])) {
$has_variable_calls = $codebase->analyzer->hasMixedMemberName(
strtolower($classlike_storage->name . '::')
);
) || $codebase->analyzer->hasMixedMemberName(strtolower($method_name));
$issue = new UnusedMethod(
'Cannot find ' . ($has_variable_calls ? 'explicit' : 'any')
@@ -896,7 +902,7 @@ private function checkMethodReferences(ClassLikeStorage $classlike_storage, Meth
if ($method_storage->stmt_location
&& !$declaring_classlike_storage->is_trait
&& isset($project_analyzer->getIssuesToFix()['UnusedMethod'])
&& !$codebase->analyzer->hasMixedMemberName(strtolower($method_name))
&& !$has_variable_calls
&& !IssueBuffer::isSuppressed($issue, $method_storage->suppressed_issues)
) {
FileManipulationBuffer::addForCodeLocation(
@@ -974,8 +980,17 @@ private function checkPropertyReferences(ClassLikeStorage $classlike_storage)
$has_variable_calls = $codebase->analyzer->hasMixedMemberName('$' . $property_name)
|| $codebase->analyzer->hasMixedMemberName(strtolower($classlike_storage->name) . '::$');
foreach ($classlike_storage->parent_classes as $parent_method_fqcln) {
if ($codebase->analyzer->hasMixedMemberName(
strtolower($parent_method_fqcln) . '::$'
)) {
$has_variable_calls = true;
break;
}
}
foreach ($classlike_storage->class_implements as $fq_interface_name) {
if (!$codebase->analyzer->hasMixedMemberName(
if ($codebase->analyzer->hasMixedMemberName(
strtolower($fq_interface_name) . '::$'
)) {
$has_variable_calls = true;
@@ -1771,6 +1771,35 @@ function foo(A $a, string $var) {
['PossiblyUnusedMethod'],
true,
],
'dontRemovePossiblyUnusedMethodWithVariableCallOnParent' => [
'<?php
class A { }
class B extends A {
public function foo() : void {}
}
function foo(A $a, string $var) {
echo $a->$var();
}
foo(new B);',
'<?php
class A { }
class B extends A {
public function foo() : void {}
}
function foo(A $a, string $var) {
echo $a->$var();
}
foo(new B);',
'7.1',
['PossiblyUnusedMethod'],
true,
],
'removePossiblyUnusedMethodWithVariableCall' => [
'<?php
class A {
@@ -1963,7 +1992,7 @@ class A implements I {
}
function foo(I $i, string $var) {
echo $a->$var;
echo $i->$var;
}
foo(new A(), "foo");',
@@ -1975,7 +2004,7 @@ class A implements I {
}
function foo(I $i, string $var) {
echo $a->$var;
echo $i->$var;
}
foo(new A(), "foo");',
@@ -2025,6 +2054,35 @@ function foo(A $a, string $var) {
['PossiblyUnusedProperty'],
true,
],
'dontRemovePossiblyUnusedPropertyWithVariableAssignmentOnParent' => [
'<?php
class A {}
class B extends A {
public $foo = "hello";
}
function foo(A $a, string $var) {
$a->$var = "hello";
}
foo(new B);',
'<?php
class A {}
class B extends A {
public $foo = "hello";
}
function foo(A $a, string $var) {
$a->$var = "hello";
}
foo(new B);',
'7.1',
['PossiblyUnusedProperty'],
true,
],
'dontRemovePossiblyUnusedPropertyWithStaticVariableAssignment' => [
'<?php
class A {

0 comments on commit 473c8cb

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