diff --git a/coder_sniffer/Drupal/Sniffs/Classes/UnusedUseStatementSniff.php b/coder_sniffer/Drupal/Sniffs/Classes/UnusedUseStatementSniff.php index d506a59c042738c86bf704f2c69680a6e7529142..c3755a9ad5abda8a9e22c1d218c7bb176fc71659 100644 --- a/coder_sniffer/Drupal/Sniffs/Classes/UnusedUseStatementSniff.php +++ b/coder_sniffer/Drupal/Sniffs/Classes/UnusedUseStatementSniff.php @@ -130,7 +130,22 @@ class UnusedUseStatementSniff implements Sniff ); // If a backslash is used before the class name then this is some other // 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; } diff --git a/coder_sniffer/Drupal/Test/Classes/UnusedUseStatementUnitTest.inc b/coder_sniffer/Drupal/Test/Classes/UnusedUseStatementUnitTest.inc index bbb2bc4c1e8024bb1f5922821b842c301c94321f..ec4124aa00e4b020d96f2e474a98d983cee55c0e 100644 --- a/coder_sniffer/Drupal/Test/Classes/UnusedUseStatementUnitTest.inc +++ b/coder_sniffer/Drupal/Test/Classes/UnusedUseStatementUnitTest.inc @@ -18,6 +18,9 @@ use /* I like weird comment placements */ MyNamespace\Depth\AnotherUnusedSameNam use MyNamespace\Depth\SomeClass as CoreSomeClass; use Some\Data\VarName; use Some\Data\VarName2 as AliasVarName2; +use Some\Data\SameAsAMethodName; +use Some\Data\Test9; +use Some\Data\Test10; /** * Bla. @@ -99,4 +102,25 @@ class Pum { 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(); + } + } diff --git a/coder_sniffer/Drupal/Test/Classes/UnusedUseStatementUnitTest.inc.fixed b/coder_sniffer/Drupal/Test/Classes/UnusedUseStatementUnitTest.inc.fixed index 79d1e0cea10664ce77583bbc2c9651be629f1fcd..2c62051894b8f078dd39088f8d4a83f7b833fa42 100644 --- a/coder_sniffer/Drupal/Test/Classes/UnusedUseStatementUnitTest.inc.fixed +++ b/coder_sniffer/Drupal/Test/Classes/UnusedUseStatementUnitTest.inc.fixed @@ -89,4 +89,25 @@ class Pum { 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(); + } + } diff --git a/coder_sniffer/Drupal/Test/Classes/UnusedUseStatementUnitTest.php b/coder_sniffer/Drupal/Test/Classes/UnusedUseStatementUnitTest.php index 7f09f148e372cb0805d5dff4a04ae08a29e1bb6f..699315e289b0d1283c6e2f1d657ddec5ad0f3a5b 100644 --- a/coder_sniffer/Drupal/Test/Classes/UnusedUseStatementUnitTest.php +++ b/coder_sniffer/Drupal/Test/Classes/UnusedUseStatementUnitTest.php @@ -45,6 +45,9 @@ class UnusedUseStatementUnitTest extends CoderSniffUnitTest 17 => 1, 19 => 1, 20 => 1, + 21 => 1, + 22 => 1, + 23 => 1, ); }//end getWarningList()