Skip to content
Snippets Groups Projects
Unverified Commit d28326dd authored by Klaus Purer's avatar Klaus Purer Committed by GitHub
Browse files

fix(UnusedUseStatement): Method names must not count as class name usage (#3052377)

parent 2d05248a
Branches
Tags
No related merge requests found
...@@ -130,7 +130,22 @@ class UnusedUseStatementSniff implements Sniff ...@@ -130,7 +130,22 @@ class UnusedUseStatementSniff implements Sniff
); );
// If a backslash is used before the class name then this is some other // If a backslash is used before the class name then this is some other
// use statement. // use statement.
if ($tokens[$beforeUsage]['code'] !== T_USE && $tokens[$beforeUsage]['code'] !== T_NS_SEPARATOR) { if (in_array(
$tokens[$beforeUsage]['code'],
[
T_USE,
T_NS_SEPARATOR,
// If an object operator is used then this is a method call
// with the same name as the class name. Which means this is
// not referring to the class.
T_OBJECT_OPERATOR,
// Function definition, not class invocation.
T_FUNCTION,
// Static method call, not class invocation.
T_DOUBLE_COLON,
]
) === false
) {
return; return;
} }
......
...@@ -18,6 +18,9 @@ use /* I like weird comment placements */ MyNamespace\Depth\AnotherUnusedSameNam ...@@ -18,6 +18,9 @@ use /* I like weird comment placements */ MyNamespace\Depth\AnotherUnusedSameNam
use MyNamespace\Depth\SomeClass as CoreSomeClass; use MyNamespace\Depth\SomeClass as CoreSomeClass;
use Some\Data\VarName; use Some\Data\VarName;
use Some\Data\VarName2 as AliasVarName2; use Some\Data\VarName2 as AliasVarName2;
use Some\Data\SameAsAMethodName;
use Some\Data\Test9;
use Some\Data\Test10;
/** /**
* Bla. * Bla.
...@@ -99,4 +102,25 @@ class Pum { ...@@ -99,4 +102,25 @@ class Pum {
return $x['test']; return $x['test'];
} }
/**
* Call a method here that has the same name as a class name.
*/
protected function test8() {
$this->sameAsAMethodName();
}
/**
* Method definition has same name as class name.
*/
protected function test9() {
}
/**
* Static method calls should not be confused with class names.
*/
protected function test10() {
Something::test10();
}
} }
...@@ -89,4 +89,25 @@ class Pum { ...@@ -89,4 +89,25 @@ class Pum {
return $x['test']; return $x['test'];
} }
/**
* Call a method here that has the same name as a class name.
*/
protected function test8() {
$this->sameAsAMethodName();
}
/**
* Method definition has same name as class name.
*/
protected function test9() {
}
/**
* Static method calls should not be confused with class names.
*/
protected function test10() {
Something::test10();
}
} }
...@@ -45,6 +45,9 @@ class UnusedUseStatementUnitTest extends CoderSniffUnitTest ...@@ -45,6 +45,9 @@ class UnusedUseStatementUnitTest extends CoderSniffUnitTest
17 => 1, 17 => 1,
19 => 1, 19 => 1,
20 => 1, 20 => 1,
21 => 1,
22 => 1,
23 => 1,
); );
}//end getWarningList() }//end getWarningList()
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment