From d28326dd1e71df127d647ff4c0a3afcabe181ae9 Mon Sep 17 00:00:00 2001
From: Klaus Purer <klaus.purer@protonmail.ch>
Date: Sat, 4 May 2019 10:28:47 +0200
Subject: [PATCH] fix(UnusedUseStatement): Method names must not count as class
 name usage (#3052377)

---
 .../Classes/UnusedUseStatementSniff.php       | 17 ++++++++++++-
 .../Classes/UnusedUseStatementUnitTest.inc    | 24 +++++++++++++++++++
 .../UnusedUseStatementUnitTest.inc.fixed      | 21 ++++++++++++++++
 .../Classes/UnusedUseStatementUnitTest.php    |  3 +++
 4 files changed, 64 insertions(+), 1 deletion(-)

diff --git a/coder_sniffer/Drupal/Sniffs/Classes/UnusedUseStatementSniff.php b/coder_sniffer/Drupal/Sniffs/Classes/UnusedUseStatementSniff.php
index d506a59c..c3755a9a 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 bbb2bc4c..ec4124aa 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 79d1e0ce..2c620518 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 7f09f148..699315e2 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()
-- 
GitLab