Properly handle extending a class with the same name in Sniff
authorTim Düsterhus <duesterhus@woltlab.com>
Sat, 5 Jan 2013 17:26:25 +0000 (18:26 +0100)
committerTim Düsterhus <duesterhus@woltlab.com>
Sat, 5 Jan 2013 17:26:25 +0000 (18:26 +0100)
Fixes #1082

CodeSniff/WCF/Sniffs/Methods/MethodDeclarationSniff.php
CodeSniff/WCF/Sniffs/Namespaces/ClassMustBeImportedSniff.php

index 82eff153c10fcdbbd43ae9ab9d23cb03467a83d0..0117c4b37f51fc06eace58d20c14377cc1127733 100644 (file)
@@ -26,8 +26,7 @@ class WCF_Sniffs_Methods_MethodDeclarationSniff extends PHP_CodeSniffer_Standard
         *
         * @return void
         */
-       protected function processTokenWithinScope(PHP_CodeSniffer_File $phpcsFile, $stackPtr, $currScope)
-       {
+       protected function processTokenWithinScope(PHP_CodeSniffer_File $phpcsFile, $stackPtr, $currScope) {
                $tokens = $phpcsFile->getTokens();
                
                $methodName = $phpcsFile->getDeclarationName($stackPtr);
index be5038a2c8b78035a19ddca5477cfaa15cf545d5..736035c9000f60ea81b690f3087d2d0fbb390e14 100644 (file)
@@ -54,6 +54,13 @@ class WCF_Sniffs_Namespaces_ClassMustBeImportedSniff implements PHP_CodeSniffer_
                                                $class .= $tokens[$i]['content'];
                                        }
                                        
+                                       $extends = $phpcsFile->findPrevious(array(T_EXTENDS), $stackPtr - 1, null, false, null, true);
+                                       // are we trying to extend a class with the same name?
+                                       if ($extends !== false) {
+                                               $newClass = $phpcsFile->findPrevious(T_STRING, $extends);
+                                               if ($tokens[$newClass]['content'] == $tokens[$end - 1]['content']) return;
+                                       }
+                                       
                                        $error = 'Namespaced classes (%s) must be imported with use.';
                                        $data = array(
                                                $class