From 8d17de131cd799e2a1a119cb9f3141521092262c Mon Sep 17 00:00:00 2001 From: =?utf8?q?Tim=20D=C3=BCsterhus?= Date: Sat, 6 May 2017 20:20:01 +0200 Subject: [PATCH] Update CodeSniffs to be compatible with CodeSniffer 3.0 --- .../WCF/Sniffs/Classes/ClassFileNameSniff.php | 9 +- .../ControlSignatureSniff.php | 9 +- ...ningFunctionBraceKernighanRitchieSniff.php | 9 +- .../Sniffs/Methods/MethodDeclarationSniff.php | 21 +- .../Namespaces/ClassMustBeImportedSniff.php | 9 +- .../Namespaces/SortedUseDeclarationSniff.php | 9 +- .../Sniffs/Namespaces/UseDeclarationSniff.php | 253 ++++++++++++------ .../WhiteSpace/SuperfluousWhitespaceSniff.php | 9 +- 8 files changed, 222 insertions(+), 106 deletions(-) diff --git a/CodeSniff/WCF/Sniffs/Classes/ClassFileNameSniff.php b/CodeSniff/WCF/Sniffs/Classes/ClassFileNameSniff.php index 4997382919..d50eb5c60b 100644 --- a/CodeSniff/WCF/Sniffs/Classes/ClassFileNameSniff.php +++ b/CodeSniff/WCF/Sniffs/Classes/ClassFileNameSniff.php @@ -1,4 +1,9 @@ and Marc McIntyre @@ -10,7 +15,7 @@ * @package com.woltlab.wcf * @category Community Framework */ -class WCF_Sniffs_Classes_ClassFileNameSniff implements PHP_CodeSniffer_Sniff { +class ClassFileNameSniff implements Sniff { /** * Returns an array of tokens this test wants to listen for. * @@ -32,7 +37,7 @@ class WCF_Sniffs_Classes_ClassFileNameSniff implements PHP_CodeSniffer_Sniff { * * @return void */ - public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr) { + public function process(File $phpcsFile, $stackPtr) { $tokens = $phpcsFile->getTokens(); $decName = $phpcsFile->findNext(T_STRING, $stackPtr); $fullPath = basename($phpcsFile->getFilename()); diff --git a/CodeSniff/WCF/Sniffs/ControlStructures/ControlSignatureSniff.php b/CodeSniff/WCF/Sniffs/ControlStructures/ControlSignatureSniff.php index a10dbad2af..ef0c0668b6 100644 --- a/CodeSniff/WCF/Sniffs/ControlStructures/ControlSignatureSniff.php +++ b/CodeSniff/WCF/Sniffs/ControlStructures/ControlSignatureSniff.php @@ -1,7 +1,8 @@ and Marc McIntyre @@ -10,7 +15,7 @@ * @package com.woltlab.wcf * @category Community Framework */ -class WCF_Sniffs_Functions_OpeningFunctionBraceKernighanRitchieSniff implements PHP_CodeSniffer_Sniff { +class OpeningFunctionBraceKernighanRitchieSniff implements Sniff { /** * Registers the tokens that this sniff wants to listen for. * @@ -29,7 +34,7 @@ class WCF_Sniffs_Functions_OpeningFunctionBraceKernighanRitchieSniff implements * * @return void */ - public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr) { + public function process(File $phpcsFile, $stackPtr) { $tokens = $phpcsFile->getTokens(); if (isset($tokens[$stackPtr]['scope_opener']) === false) { diff --git a/CodeSniff/WCF/Sniffs/Methods/MethodDeclarationSniff.php b/CodeSniff/WCF/Sniffs/Methods/MethodDeclarationSniff.php index 0117c4b37f..2bc178e668 100644 --- a/CodeSniff/WCF/Sniffs/Methods/MethodDeclarationSniff.php +++ b/CodeSniff/WCF/Sniffs/Methods/MethodDeclarationSniff.php @@ -1,4 +1,11 @@ and released under the terms of the BSD Licence. @@ -9,7 +16,7 @@ * @package com.woltlab.wcf * @category Community Framework */ -class WCF_Sniffs_Methods_MethodDeclarationSniff extends PHP_CodeSniffer_Standards_AbstractScopeSniff { +class MethodDeclarationSniff extends AbstractScopeSniff { /** * Constructs a Squiz_Sniffs_Scope_MethodScopeSniff. */ @@ -26,7 +33,7 @@ class WCF_Sniffs_Methods_MethodDeclarationSniff extends PHP_CodeSniffer_Standard * * @return void */ - protected function processTokenWithinScope(PHP_CodeSniffer_File $phpcsFile, $stackPtr, $currScope) { + protected function processTokenWithinScope(File $phpcsFile, $stackPtr, $currScope) { $tokens = $phpcsFile->getTokens(); $methodName = $phpcsFile->getDeclarationName($stackPtr); @@ -40,12 +47,12 @@ class WCF_Sniffs_Methods_MethodDeclarationSniff extends PHP_CodeSniffer_Standard $abstract = 0; $final = 0; - $find = PHP_CodeSniffer_Tokens::$methodPrefixes; + $find = Tokens::$methodPrefixes; $find[] = T_WHITESPACE; $prev = $phpcsFile->findPrevious($find, ($stackPtr - 1), null, true); $prefix = $stackPtr; - while (($prefix = $phpcsFile->findPrevious(PHP_CodeSniffer_Tokens::$methodPrefixes, ($prefix - 1), $prev)) !== false) { + while (($prefix = $phpcsFile->findPrevious(Tokens::$methodPrefixes, ($prefix - 1), $prev)) !== false) { switch ($tokens[$prefix]['code']) { case T_STATIC: $static = $prefix; @@ -77,4 +84,8 @@ class WCF_Sniffs_Methods_MethodDeclarationSniff extends PHP_CodeSniffer_Standard $phpcsFile->addError($error, $final, 'FinalBeforeVisibilityOrBeforeStatic'); } } -} \ No newline at end of file + + protected function processTokenOutsideScope(File $phpcsFile, $stackPtr) + { + } +} diff --git a/CodeSniff/WCF/Sniffs/Namespaces/ClassMustBeImportedSniff.php b/CodeSniff/WCF/Sniffs/Namespaces/ClassMustBeImportedSniff.php index 0e1ba74ca7..7aec928bad 100644 --- a/CodeSniff/WCF/Sniffs/Namespaces/ClassMustBeImportedSniff.php +++ b/CodeSniff/WCF/Sniffs/Namespaces/ClassMustBeImportedSniff.php @@ -1,4 +1,9 @@ getTokens(); // skip files in global namespace diff --git a/CodeSniff/WCF/Sniffs/Namespaces/SortedUseDeclarationSniff.php b/CodeSniff/WCF/Sniffs/Namespaces/SortedUseDeclarationSniff.php index dc8b87ae5f..9df7e5b475 100644 --- a/CodeSniff/WCF/Sniffs/Namespaces/SortedUseDeclarationSniff.php +++ b/CodeSniff/WCF/Sniffs/Namespaces/SortedUseDeclarationSniff.php @@ -1,4 +1,9 @@ and released under the terms of the BSD Licence. @@ -9,7 +14,7 @@ * @package com.woltlab.wcf * @category Community Framework */ -class WCF_Sniffs_Namespaces_SortedUseDeclarationSniff implements PHP_CodeSniffer_Sniff { +class SortedUseDeclarationSniff implements Sniff { /** * Returns an array of tokens this test wants to listen for. * @@ -28,7 +33,7 @@ class WCF_Sniffs_Namespaces_SortedUseDeclarationSniff implements PHP_CodeSniffer * * @return void */ - public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr) { + public function process(File $phpcsFile, $stackPtr) { $tokens = $phpcsFile->getTokens(); $classes = array(); diff --git a/CodeSniff/WCF/Sniffs/Namespaces/UseDeclarationSniff.php b/CodeSniff/WCF/Sniffs/Namespaces/UseDeclarationSniff.php index 1d9bac295a..7070320ee7 100644 --- a/CodeSniff/WCF/Sniffs/Namespaces/UseDeclarationSniff.php +++ b/CodeSniff/WCF/Sniffs/Namespaces/UseDeclarationSniff.php @@ -1,4 +1,9 @@ and released under the terms of the BSD Licence. @@ -9,90 +14,164 @@ * @package com.woltlab.wcf * @category Community Framework */ -class WCF_Sniffs_Namespaces_UseDeclarationSniff implements PHP_CodeSniffer_Sniff { - /** - * Returns an array of tokens this test wants to listen for. - * - * @return array - */ - public function register() { - return array(T_USE); - } - - /** - * Processes this test, when one of its tokens is encountered. - * - * @param PHP_CodeSniffer_File $phpcsFile The file being scanned. - * @param int $stackPtr The position of the current token in - * the stack passed in $tokens. - * - * @return void - */ - public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr) { - $tokens = $phpcsFile->getTokens(); - - // Ignore USE keywords inside closures. - $next = $phpcsFile->findNext(T_WHITESPACE, ($stackPtr + 1), null, true); - if ($tokens[$next]['code'] === T_OPEN_PARENTHESIS) { - return; - } - - // Only one USE declaration allowed per statement. - $next = $phpcsFile->findNext(array(T_COMMA, T_SEMICOLON), ($stackPtr + 1)); - if ($tokens[$next]['code'] === T_COMMA) { - $error = 'There must be one USE keyword per declaration'; - $phpcsFile->addError($error, $stackPtr, 'MultipleDeclarations'); - } - - // Leading NS_SEPARATOR must be omitted - $next = $phpcsFile->findNext(array(T_NS_SEPARATOR, T_STRING), ($stackPtr + 1)); - if ($tokens[$next]['code'] === T_NS_SEPARATOR) { - $error = 'Leading backslash must be omitted.'; - $phpcsFile->addError($error, $stackPtr, 'LeadingNSSeparator'); - } - - // Make sure this USE comes after the first namespace declaration. - $prev = $phpcsFile->findPrevious(T_NAMESPACE, ($stackPtr - 1)); - if ($prev !== false) { - $first = $phpcsFile->findNext(T_NAMESPACE, 1); - if ($prev !== $first) { - $error = 'USE declarations must go after the first namespace declaration'; - $phpcsFile->addError($error, $stackPtr, 'UseAfterNamespace'); - } - } - - $end = $phpcsFile->findNext(T_SEMICOLON, ($stackPtr + 1)); - $next = $phpcsFile->findNext(T_WHITESPACE, ($end + 1), null, true); - if ($tokens[$next]['code'] === T_USE) { - $diff = $tokens[$next]['line'] - $tokens[$stackPtr]['line'] - 1; - if ($diff !== 0) { - $error = 'There must not be any blank lines between use statements; %s found;'; - $data = array($diff); - $phpcsFile->addError($error, $stackPtr, 'SpaceBetweenUse', $data); - } - } - - // Only interested in the last USE statement from here onwards. - $nextUse = $phpcsFile->findNext(T_USE, ($stackPtr + 1)); - if ($nextUse !== false) { - $next = $phpcsFile->findNext(T_WHITESPACE, ($nextUse + 1), null, true); - if ($tokens[$next]['code'] !== T_OPEN_PARENTHESIS) { - return; - } - } - - $end = $phpcsFile->findNext(T_SEMICOLON, ($stackPtr + 1)); - $next = $phpcsFile->findNext(T_WHITESPACE, ($end + 1), null, true); - $diff = ($tokens[$next]['line'] - $tokens[$end]['line'] - 1); - if ($diff !== 1) { - if ($diff < 0) { - $diff = 0; - } - - $error = 'There must be one blank line after the last USE statement; %s found;'; - $data = array($diff); - $phpcsFile->addError($error, $stackPtr, 'SpaceAfterLastUse', $data); - } - - } -} +class UseDeclarationSniff implements Sniff +{ + + + /** + * Returns an array of tokens this test wants to listen for. + * + * @return array + */ + public function register() + { + return array(T_USE); + + }//end register() + + + /** + * Processes this test, when one of its tokens is encountered. + * + * @param PHP_CodeSniffer_File $phpcsFile The file being scanned. + * @param int $stackPtr The position of the current token in + * the stack passed in $tokens. + * + * @return void + */ + public function process(File $phpcsFile, $stackPtr) + { + if ($this->_shouldIgnoreUse($phpcsFile, $stackPtr) === true) { + return; + } + + $tokens = $phpcsFile->getTokens(); + + // One space after the use keyword. + if ($tokens[($stackPtr + 1)]['content'] !== ' ') { + $error = 'There must be a single space after the USE keyword'; + $fix = $phpcsFile->addFixableError($error, $stackPtr, 'SpaceAfterUse'); + if ($fix === true) { + $phpcsFile->fixer->replaceToken(($stackPtr + 1), ' '); + } + } + + // Only one USE declaration allowed per statement. + $next = $phpcsFile->findNext(array(T_COMMA, T_SEMICOLON), ($stackPtr + 1)); + if ($tokens[$next]['code'] === T_COMMA) { + $error = 'There must be one USE keyword per declaration'; + $fix = $phpcsFile->addFixableError($error, $stackPtr, 'MultipleDeclarations'); + if ($fix === true) { + $phpcsFile->fixer->replaceToken($next, ';'.$phpcsFile->eolChar.'use '); + } + } + + // Leading NS_SEPARATOR must be omitted + $next = $phpcsFile->findNext(array(T_NS_SEPARATOR, T_STRING), ($stackPtr + 1)); + if ($tokens[$next]['code'] === T_NS_SEPARATOR) { + $error = 'Leading backslash must be omitted.'; + $phpcsFile->addError($error, $stackPtr, 'LeadingNSSeparator'); + } + + // Make sure this USE comes after the first namespace declaration. + $prev = $phpcsFile->findPrevious(T_NAMESPACE, ($stackPtr - 1)); + if ($prev !== false) { + $first = $phpcsFile->findNext(T_NAMESPACE, 1); + if ($prev !== $first) { + $error = 'USE declarations must go after the first namespace declaration'; + $phpcsFile->addError($error, $stackPtr, 'UseAfterNamespace'); + } + } + + $end = $phpcsFile->findNext(T_SEMICOLON, ($stackPtr + 1)); + $next = $phpcsFile->findNext(T_WHITESPACE, ($end + 1), null, true); + if ($tokens[$next]['code'] === T_USE) { + $diff = $tokens[$next]['line'] - $tokens[$stackPtr]['line'] - 1; + if ($diff !== 0) { + $error = 'There must not be any blank lines between use statements; %s found;'; + $data = array($diff); + $phpcsFile->addError($error, $stackPtr, 'SpaceBetweenUse', $data); + } + } + + // Only interested in the last USE statement from here onwards. + $nextUse = $phpcsFile->findNext(T_USE, ($stackPtr + 1)); + while ($this->_shouldIgnoreUse($phpcsFile, $nextUse) === true) { + $nextUse = $phpcsFile->findNext(T_USE, ($nextUse + 1)); + if ($nextUse === false) { + break; + } + } + + if ($nextUse !== false) { + return; + } + + $end = $phpcsFile->findNext(T_SEMICOLON, ($stackPtr + 1)); + $next = $phpcsFile->findNext(T_WHITESPACE, ($end + 1), null, true); + + if ($tokens[$next]['code'] === T_CLOSE_TAG) { + return; + } + + $diff = ($tokens[$next]['line'] - $tokens[$end]['line'] - 1); + if ($diff !== 1) { + if ($diff < 0) { + $diff = 0; + } + + $error = 'There must be one blank line after the last USE statement; %s found;'; + $data = array($diff); + $fix = $phpcsFile->addFixableError($error, $stackPtr, 'SpaceAfterLastUse', $data); + if ($fix === true) { + if ($diff === 0) { + $phpcsFile->fixer->addNewline($end); + } else { + $phpcsFile->fixer->beginChangeset(); + for ($i = ($end + 1); $i < $next; $i++) { + if ($tokens[$i]['line'] === $tokens[$next]['line']) { + break; + } + + $phpcsFile->fixer->replaceToken($i, ''); + } + + $phpcsFile->fixer->addNewline($end); + $phpcsFile->fixer->endChangeset(); + } + } + }//end if + + }//end process() + + + /** + * Check if this use statement is part of the namespace block. + * + * @param PHP_CodeSniffer_File $phpcsFile The file being scanned. + * @param int $stackPtr The position of the current token in + * the stack passed in $tokens. + * + * @return void + */ + private function _shouldIgnoreUse(File $phpcsFile, $stackPtr) + { + $tokens = $phpcsFile->getTokens(); + + // Ignore USE keywords inside closures. + $next = $phpcsFile->findNext(T_WHITESPACE, ($stackPtr + 1), null, true); + if ($tokens[$next]['code'] === T_OPEN_PARENTHESIS) { + return true; + } + + // Ignore USE keywords for traits. + if ($phpcsFile->hasCondition($stackPtr, array(T_CLASS, T_TRAIT)) === true) { + return true; + } + + return false; + + }//end _shouldIgnoreUse() + + +}//end class diff --git a/CodeSniff/WCF/Sniffs/WhiteSpace/SuperfluousWhitespaceSniff.php b/CodeSniff/WCF/Sniffs/WhiteSpace/SuperfluousWhitespaceSniff.php index ec4cf0bd7e..204b16c121 100644 --- a/CodeSniff/WCF/Sniffs/WhiteSpace/SuperfluousWhitespaceSniff.php +++ b/CodeSniff/WCF/Sniffs/WhiteSpace/SuperfluousWhitespaceSniff.php @@ -1,4 +1,9 @@ and Marc McIntyre @@ -10,7 +15,7 @@ * @package com.woltlab.wcf * @category Community Framework */ -class WCF_Sniffs_WhiteSpace_SuperfluousWhitespaceSniff implements PHP_CodeSniffer_Sniff { +class SuperfluousWhitespaceSniff implements Sniff { /** * A list of tokenizers this sniff supports. * @@ -52,7 +57,7 @@ class WCF_Sniffs_WhiteSpace_SuperfluousWhitespaceSniff implements PHP_CodeSniffe * @param int $stackPtr The position of the current token in the * stack passed in $tokens. */ - public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr) { + public function process(File $phpcsFile, $stackPtr) { $tokens = $phpcsFile->getTokens(); if ($tokens[$stackPtr]['code'] === T_OPEN_TAG) { -- 2.20.1