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
Loading
Loading
Loading
Loading
+16 −1
Original line number Diff line number Diff line
@@ -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;
                }

+24 −0
Original line number Diff line number Diff line
@@ -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();
  }

}
+21 −0
Original line number Diff line number Diff line
@@ -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();
  }

}
+3 −0
Original line number Diff line number Diff line
@@ -45,6 +45,9 @@ class UnusedUseStatementUnitTest extends CoderSniffUnitTest
                17 => 1,
                19 => 1,
                20 => 1,
                21 => 1,
                22 => 1,
                23 => 1,
               );

    }//end getWarningList()