From 1a66638aa969685cde86da1d9ec3c6e484630edb Mon Sep 17 00:00:00 2001 From: =?utf8?q?Tim=20D=C3=BCsterhus?= Date: Sat, 5 Jan 2013 18:26:25 +0100 Subject: [PATCH] Properly handle extending a class with the same name in Sniff Fixes #1082 --- CodeSniff/WCF/Sniffs/Methods/MethodDeclarationSniff.php | 3 +-- .../WCF/Sniffs/Namespaces/ClassMustBeImportedSniff.php | 7 +++++++ 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/CodeSniff/WCF/Sniffs/Methods/MethodDeclarationSniff.php b/CodeSniff/WCF/Sniffs/Methods/MethodDeclarationSniff.php index 82eff153c1..0117c4b37f 100644 --- a/CodeSniff/WCF/Sniffs/Methods/MethodDeclarationSniff.php +++ b/CodeSniff/WCF/Sniffs/Methods/MethodDeclarationSniff.php @@ -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); diff --git a/CodeSniff/WCF/Sniffs/Namespaces/ClassMustBeImportedSniff.php b/CodeSniff/WCF/Sniffs/Namespaces/ClassMustBeImportedSniff.php index be5038a2c8..736035c900 100644 --- a/CodeSniff/WCF/Sniffs/Namespaces/ClassMustBeImportedSniff.php +++ b/CodeSniff/WCF/Sniffs/Namespaces/ClassMustBeImportedSniff.php @@ -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 -- 2.20.1