diff --git a/.github/workflows/php.yml b/.github/workflows/php.yml index 24897d8..91b6f0c 100644 --- a/.github/workflows/php.yml +++ b/.github/workflows/php.yml @@ -11,7 +11,7 @@ jobs: runs-on: ubuntu-latest strategy: matrix: - php-versions: ['7.0', '7.1', '7.2', '7.3', '7.4'] + php-versions: ['7.0', '7.1', '7.2', '7.3', '7.4', '8.0', '8.1', '8.2'] steps: - name: Checkout uses: actions/checkout@v2 diff --git a/DWS/Helpers/Array.php b/DWS/Helpers/Arrays.php similarity index 78% rename from DWS/Helpers/Array.php rename to DWS/Helpers/Arrays.php index 0993c2a..8d873c6 100644 --- a/DWS/Helpers/Array.php +++ b/DWS/Helpers/Arrays.php @@ -6,26 +6,30 @@ * @subpackage Helpers */ +namespace DWS\Helpers; + +use PHP_CodeSniffer\Files\File; + /** * A collection of helper methods for arrays. * * @package DWS * @subpackage Helpers */ -final class DWS_Helpers_Array +final class Arrays { /** * Returns the positions of all of the commas that belong to the array beginning at $arrayStart. * - * @param PHP_CodeSniffer_File $phpcsFile The file where the array is declared. + * @param PHP_CodeSniffer\Files\File $phpcsFile The file where the array is declared. * @param int $arrayStart The position of the opening parenthesis or bracket for the array in the file. * * @return array The stack pointers for all of the commas in the array (excluding commas in nested arrays, functions, etc.). */ - public static function commaPositions(PHP_CodeSniffer_File $phpcsFile, $arrayStart) + public static function commaPositions(File $phpcsFile, $arrayStart) { $tokens = $phpcsFile->getTokens(); - $arrayEnd = DWS_Helpers_Bracket::bracketEnd($phpcsFile, $arrayStart); + $arrayEnd = Bracket::bracketEnd($phpcsFile, $arrayStart); $commas = []; for ($i = $arrayStart + 1; $i <= $arrayEnd; $i++) { diff --git a/DWS/Helpers/Bracket.php b/DWS/Helpers/Bracket.php index fc08cd2..1fc258f 100644 --- a/DWS/Helpers/Bracket.php +++ b/DWS/Helpers/Bracket.php @@ -6,23 +6,27 @@ * @subpackage Helpers */ +namespace DWS\Helpers; + +use PHP_CodeSniffer\Files\File; + /** * A collection of helper methods for bracketed expressions. * * @package DWS * @subpackage Helpers */ -final class DWS_Helpers_Bracket +final class Bracket { /** * Given a pointer to a bracketed expression (such as T_ARRAY or T_OPEN_SHORT_ARRAY), returns the stack pointer for the opening bracket. * - * @param PHP_CodeSniffer_File $phpcsFile The file where the bracketed expression is declared. + * @param PHP_CodeSniffer\Files\File $phpcsFile The file where the bracketed expression is declared. * @param int $stackPtr The position of the expression element in the stack in $phpcsFile. * * @return int The position of the opening bracket, or if not found, the given $stackPtr. */ - public static function bracketStart(PHP_CodeSniffer_File $phpcsFile, $stackPtr) + public static function bracketStart(File $phpcsFile, $stackPtr) { $tokens = $phpcsFile->getTokens(); if (array_key_exists('parenthesis_opener', $tokens[$stackPtr])) { @@ -37,12 +41,12 @@ public static function bracketStart(PHP_CodeSniffer_File $phpcsFile, $stackPtr) /** * Given a pointer to a bracketed expression (such as T_ARRAY or T_OPEN_SHORT_ARRAY), returns the stack pointer for the ending bracket. * - * @param PHP_CodeSniffer_File $phpcsFile The file where the bracketed expression is declared. + * @param PHP_CodeSniffer\Files\File $phpcsFile The file where the bracketed expression is declared. * @param int $stackPtr The position of the expression element in the stack in $phpcsFile. * * @return int The position of the ending bracket, or if not found, the given $stackPtr. */ - public static function bracketEnd(PHP_CodeSniffer_File $phpcsFile, $stackPtr) + public static function bracketEnd(File $phpcsFile, $stackPtr) { $tokens = $phpcsFile->getTokens(); if (array_key_exists('parenthesis_closer', $tokens[$stackPtr])) { diff --git a/DWS/Helpers/Operator.php b/DWS/Helpers/Operator.php index 36860e6..6173e2e 100644 --- a/DWS/Helpers/Operator.php +++ b/DWS/Helpers/Operator.php @@ -6,23 +6,28 @@ * @subpackage Helpers */ +namespace DWS\Helpers; + +use PHP_CodeSniffer\Files\File; +use PHP_CodeSniffer\Util\Tokens; + /** * A collection of helper methods for operators. * * @package DWS * @subpackage Helpers */ -final class DWS_Helpers_Operator +final class Operator { /** * Given a pointer to a bracketed expression (such as T_ARRAY or T_OPEN_SHORT_ARRAY), returns the stack pointer for the opening bracket. * - * @param PHP_CodeSniffer_File $phpcsFile The file where the bracketed expression is declared. + * @param PHP_CodeSniffer\Files\File $phpcsFile The file where the bracketed expression is declared. * @param int $stackPtr The position of the expression element in the stack in $phpcsFile. * * @return int The position of the opening bracket, or if not found, the given $stackPtr. */ - public static function isUnary(PHP_CodeSniffer_File $phpcsFile, $stackPtr) + public static function isUnary(File $phpcsFile, $stackPtr) { $tokens = $phpcsFile->getTokens(); @@ -38,9 +43,9 @@ public static function isUnary(PHP_CodeSniffer_File $phpcsFile, $stackPtr) $prev = $phpcsFile->findPrevious(T_WHITESPACE, $stackPtr - 1, null, true); $unaryHints = array_unique( array_merge( - PHP_CodeSniffer_Tokens::$comparisonTokens, - PHP_CodeSniffer_Tokens::$operators, - PHP_CodeSniffer_Tokens::$assignmentTokens, + Tokens::$comparisonTokens, + Tokens::$operators, + Tokens::$assignmentTokens, [ T_RETURN, T_COMMA, diff --git a/DWS/Helpers/WhiteSpace.php b/DWS/Helpers/WhiteSpace.php index 14b4911..ad8f2b4 100644 --- a/DWS/Helpers/WhiteSpace.php +++ b/DWS/Helpers/WhiteSpace.php @@ -6,23 +6,27 @@ * @subpackage Helpers */ +namespace DWS\Helpers; + +use PHP_CodeSniffer\Files\File; + /** * A collection of helper methods for whitespace. * * @package DWS * @subpackage Helpers */ -final class DWS_Helpers_Whitespace +final class Whitespace { /** * Returns the indentation level of the requested line. * - * @param PHP_CodeSniffer_File $phpcsFile The file in question. + * @param PHP_CodeSniffer\Files\File $phpcsFile The file in question. * @param int $stackPtr The stack pointer to the element on the line in question. * * @return int The normalized column number for the initial non-whitespace token on the line. */ - public static function indentOfLine(PHP_CodeSniffer_File $phpcsFile, $stackPtr) + public static function indentOfLine(File $phpcsFile, $stackPtr) { $tokens = $phpcsFile->getTokens(); $beginningElement = $stackPtr; diff --git a/DWS/Sniffs/Arrays/ArrayDeclarationSniff.php b/DWS/Sniffs/Arrays/ArrayDeclarationSniff.php index aebab06..21f8e43 100644 --- a/DWS/Sniffs/Arrays/ArrayDeclarationSniff.php +++ b/DWS/Sniffs/Arrays/ArrayDeclarationSniff.php @@ -6,13 +6,19 @@ * @subpackage Sniffs */ +namespace DWS\Sniffs\Arrays; + +use DWS\Helpers\Bracket; +use PHP_CodeSniffer\Sniffs\Sniff; +use PHP_CodeSniffer\Files\File; + /** * A test to ensure that arrays conform to the array coding standard. * * @package DWS * @subpackage Sniffs */ -final class DWS_Sniffs_Arrays_ArrayDeclarationSniff implements PHP_CodeSniffer_Sniff +final class ArrayDeclarationSniff implements Sniff { /** * Returns an array of tokens this test wants to listen for. @@ -27,16 +33,16 @@ public function register() /** * Processes this sniff, when one of its tokens is encountered. * - * @param PHP_CodeSniffer_File $phpcsFile The current file being checked. + * @param PHP_CodeSniffer\Files\File $phpcsFile The current file being checked. * @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) + public function process(File $phpcsFile, $stackPtr) { $tokens = $phpcsFile->getTokens(); - $arrayStart = DWS_Helpers_Bracket::bracketStart($phpcsFile, $stackPtr); - $arrayEnd = DWS_Helpers_Bracket::bracketEnd($phpcsFile, $stackPtr); + $arrayStart = Bracket::bracketStart($phpcsFile, $stackPtr); + $arrayEnd = Bracket::bracketEnd($phpcsFile, $stackPtr); if (!in_array($arrayStart, [$stackPtr, $stackPtr + 1])) { $phpcsFile->addError('No whitespace allowed between the array keyword and the opening parenthesis', $stackPtr, 'SpaceAfterKeyword'); diff --git a/DWS/Sniffs/Arrays/ArrowSpacingSniff.php b/DWS/Sniffs/Arrays/ArrowSpacingSniff.php index 3814600..8cc9216 100644 --- a/DWS/Sniffs/Arrays/ArrowSpacingSniff.php +++ b/DWS/Sniffs/Arrays/ArrowSpacingSniff.php @@ -6,13 +6,18 @@ * @subpackage Sniffs */ +namespace DWS\Sniffs\Arrays; + +use PHP_CodeSniffer\Sniffs\Sniff; +use PHP_CodeSniffer\Files\File; + /** * A test to ensure that arrows in arrays are set with proper whitespace. * * @package DWS * @subpackage Sniffs */ -final class DWS_Sniffs_Arrays_ArrowSpacingSniff implements PHP_CodeSniffer_Sniff +final class ArrowSpacingSniff implements Sniff { /** * Returns an array of tokens this test wants to listen for. @@ -27,12 +32,12 @@ public function register() /** * Processes this sniff, when one of its tokens is encountered. * - * @param PHP_CodeSniffer_File $phpcsFile The current file being checked. + * @param PHP_CodeSniffer\Files\File $phpcsFile The current file being checked. * @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) + public function process(File $phpcsFile, $stackPtr) { $tokens = $phpcsFile->getTokens(); diff --git a/DWS/Sniffs/Arrays/CommaSpacingSniff.php b/DWS/Sniffs/Arrays/CommaSpacingSniff.php index 0c5b0c6..d4e5294 100644 --- a/DWS/Sniffs/Arrays/CommaSpacingSniff.php +++ b/DWS/Sniffs/Arrays/CommaSpacingSniff.php @@ -6,13 +6,19 @@ * @subpackage Sniffs */ +namespace DWS\Sniffs\Arrays; + +use DWS\Helpers; +use PHP_CodeSniffer\Sniffs\Sniff; +use PHP_CodeSniffer\Files\File; + /** * A test to ensure that commas in arrays are set with proper whitespace. * * @package DWS * @subpackage Sniffs */ -final class DWS_Sniffs_Arrays_CommaSpacingSniff implements PHP_CodeSniffer_Sniff +final class CommaSpacingSniff implements Sniff { /** * Returns an array of tokens this test wants to listen for. @@ -27,20 +33,20 @@ public function register() /** * Processes this sniff, when one of its tokens is encountered. * - * @param PHP_CodeSniffer_File $phpcsFile The current file being checked. + * @param PHP_CodeSniffer\Files\File $phpcsFile The current file being checked. * @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) + public function process(File $phpcsFile, $stackPtr) { $tokens = $phpcsFile->getTokens(); - $arrayStart = DWS_Helpers_Bracket::bracketStart($phpcsFile, $stackPtr); - $arrayEnd = DWS_Helpers_Bracket::bracketEnd($phpcsFile, $stackPtr); + $arrayStart = Helpers\Bracket::bracketStart($phpcsFile, $stackPtr); + $arrayEnd = Helpers\Bracket::bracketEnd($phpcsFile, $stackPtr); $isSingleLine = $tokens[$arrayStart]['line'] === $tokens[$arrayEnd]['line']; - foreach (DWS_Helpers_Array::commaPositions($phpcsFile, $arrayStart) as $comma) { + foreach (Helpers\Arrays::commaPositions($phpcsFile, $arrayStart) as $comma) { if ($tokens[$comma - 1]['code'] === T_WHITESPACE) { $phpcsFile->addError('No whitespace allowed before commas in an array', $comma, 'SpaceBeforeComma'); } diff --git a/DWS/Sniffs/Arrays/TrailingCommaSniff.php b/DWS/Sniffs/Arrays/TrailingCommaSniff.php index 9ef1a11..e3b66f2 100644 --- a/DWS/Sniffs/Arrays/TrailingCommaSniff.php +++ b/DWS/Sniffs/Arrays/TrailingCommaSniff.php @@ -6,13 +6,20 @@ * @subpackage Sniffs */ +namespace DWS\Sniffs\Arrays; + +use DWS\Helpers; +use PHP_CodeSniffer\Sniffs\Sniff; +use PHP_CodeSniffer\Files\File; +use PHP_CodeSniffer\Util\Tokens; + /** * A test to ensure that trailing commas are included for multi-line arrays only. * * @package DWS * @subpackage Sniffs */ -final class DWS_Sniffs_Arrays_TrailingCommaSniff implements PHP_CodeSniffer_Sniff +final class TrailingCommaSniff implements Sniff { /** * Returns an array of tokens this test wants to listen for. @@ -27,30 +34,30 @@ public function register() /** * Processes this sniff, when one of its tokens is encountered. * - * @param PHP_CodeSniffer_File $phpcsFile The current file being checked. + * @param PHP_CodeSniffer\Files\File $phpcsFile The current file being checked. * @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) + public function process(File $phpcsFile, $stackPtr) { $tokens = $phpcsFile->getTokens(); - $arrayStart = DWS_Helpers_Bracket::bracketStart($phpcsFile, $stackPtr); - $arrayEnd = DWS_Helpers_Bracket::bracketEnd($phpcsFile, $stackPtr); + $arrayStart = Helpers\Bracket::bracketStart($phpcsFile, $stackPtr); + $arrayEnd = Helpers\Bracket::bracketEnd($phpcsFile, $stackPtr); $isSingleLine = $tokens[$arrayStart]['line'] === $tokens[$arrayEnd]['line']; - $commas = DWS_Helpers_Array::commaPositions($phpcsFile, $arrayStart); + $commas = Helpers\Arrays::commaPositions($phpcsFile, $arrayStart); $lastComma = array_pop($commas); - $trailingComma = $phpcsFile->findNext(PHP_CodeSniffer_Tokens::$emptyTokens, $lastComma + 1, $arrayEnd, true) === false; + $trailingComma = $phpcsFile->findNext(Tokens::$emptyTokens, $lastComma + 1, $arrayEnd, true) === false; if ($isSingleLine) { if ($trailingComma) { $phpcsFile->addError('No trailing comma allowed on single-line arrays', $lastComma, 'SingleLineTrailingComma'); } } elseif (!$trailingComma) { - $previousItem = $phpcsFile->findPrevious(PHP_CodeSniffer_Tokens::$emptyTokens, $arrayEnd - 1, $arrayStart, true); + $previousItem = $phpcsFile->findPrevious(Tokens::$emptyTokens, $arrayEnd - 1, $arrayStart, true); $phpcsFile->addError('Trailing comma required for multi-line arrays', $previousItem, 'MultiLineTrailingComma'); } } diff --git a/DWS/Sniffs/ControlStructures/ControlSignatureSniff.php b/DWS/Sniffs/ControlStructures/ControlSignatureSniff.php index 33d32dd..0fe551a 100644 --- a/DWS/Sniffs/ControlStructures/ControlSignatureSniff.php +++ b/DWS/Sniffs/ControlStructures/ControlSignatureSniff.php @@ -6,13 +6,17 @@ * @subpackage Sniffs */ +namespace DWS\Sniffs\ControlStructures; + +use PHP_CodeSniffer\Sniffs\AbstractPatternSniff; + /** * Verifies that control statements conform to their coding standards. * * @package DWS * @subpackage Sniffs */ -final class DWS_Sniffs_ControlStructures_ControlSignatureSniff extends PHP_CodeSniffer_Standards_AbstractPatternSniff +final class ControlSignatureSniff extends AbstractPatternSniff { /** * Initialze the parent AbstractPatternSniff to ignore comments diff --git a/DWS/Sniffs/Formatting/MultilineBracketedExpressionIndentSniff.php b/DWS/Sniffs/Formatting/MultilineBracketedExpressionIndentSniff.php index 94bcb1b..fb85dbb 100644 --- a/DWS/Sniffs/Formatting/MultilineBracketedExpressionIndentSniff.php +++ b/DWS/Sniffs/Formatting/MultilineBracketedExpressionIndentSniff.php @@ -7,6 +7,13 @@ * @subpackage Sniffs */ +namespace DWS\Sniffs\Formatting; + +use DWS\Helpers; +use PHP_CodeSniffer\Sniffs\Sniff; +use PHP_CodeSniffer\Files\File; +use PHP_CodeSniffer\Util\Tokens; + /** * A test to ensure that multi-line bracketed expressions (arrays, function definitions, function calls, control structure conditions, etc.) are * indented correctly. @@ -14,7 +21,7 @@ * @package DWS * @subpackage Sniffs */ -final class DWS_Sniffs_Formatting_MultilineBracketedExpressionIndentSniff implements PHP_CodeSniffer_Sniff +final class MultilineBracketedExpressionIndentSniff implements Sniff { /** * Returns an array of tokens this test wants to listen for. @@ -29,29 +36,29 @@ public function register() /** * Processes this sniff, when one of its tokens is encountered. * - * @param PHP_CodeSniffer_File $phpcsFile The current file being checked. + * @param PHP_CodeSniffer\Files\File $phpcsFile The current file being checked. * @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) + public function process(File $phpcsFile, $stackPtr) { $tokens = $phpcsFile->getTokens(); - $bracketStart = DWS_Helpers_Bracket::bracketStart($phpcsFile, $stackPtr); - $bracketEnd = DWS_Helpers_Bracket::bracketEnd($phpcsFile, $stackPtr); + $bracketStart = Helpers\Bracket::bracketStart($phpcsFile, $stackPtr); + $bracketEnd = Helpers\Bracket::bracketEnd($phpcsFile, $stackPtr); if ($tokens[$bracketStart]['line'] === $tokens[$bracketEnd]['line']) { return; // Single-line expressions don't have indentation to worry about. } - $beginningIndent = DWS_Helpers_WhiteSpace::indentOfLine($phpcsFile, $bracketStart); + $beginningIndent = Helpers\WhiteSpace::indentOfLine($phpcsFile, $bracketStart); if ($tokens[$bracketEnd]['column'] !== $beginningIndent) { $data = [$beginningIndent - 1, $tokens[$bracketEnd]['column'] - 1, $tokens[$bracketEnd]['content']]; $phpcsFile->addError('Expected indentation of %s spaces, %s found for ending bracket "%s"', $bracketEnd, 'EndIndent', $data); } - $nextToken = $phpcsFile->findNext(PHP_CodeSniffer_Tokens::$emptyTokens, $bracketStart + 1, $bracketEnd, true); + $nextToken = $phpcsFile->findNext(Tokens::$emptyTokens, $bracketStart + 1, $bracketEnd, true); if ($nextToken !== false && $tokens[$nextToken]['line'] === $tokens[$bracketStart]['line']) { $data = [$tokens[$bracketStart]['content']]; $phpcsFile->addError('Expected first item after opening "%s" to be indented on a new line', $nextToken, 'OpeningIndent', $data); @@ -59,7 +66,7 @@ public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr) $firstOnLine = $bracketStart + 1; for ($line = $tokens[$bracketStart]['line'] + 1; $line < $tokens[$bracketEnd]['line']; $line++) { - while ($tokens[$firstOnLine]['line'] < $line || in_array($tokens[$firstOnLine]['code'], PHP_CodeSniffer_Tokens::$emptyTokens)) { + while ($tokens[$firstOnLine]['line'] < $line || in_array($tokens[$firstOnLine]['code'], Tokens::$emptyTokens)) { if (array_key_exists('parenthesis_closer', $tokens[$firstOnLine])) { $firstOnLine = $tokens[$firstOnLine]['parenthesis_closer'] + 1; $line = $tokens[$firstOnLine]['line']; diff --git a/DWS/Sniffs/Scope/VariableScopeSniff.php b/DWS/Sniffs/Scope/VariableScopeSniff.php index 3bb9357..0e2fd03 100644 --- a/DWS/Sniffs/Scope/VariableScopeSniff.php +++ b/DWS/Sniffs/Scope/VariableScopeSniff.php @@ -6,13 +6,19 @@ * @subpackage Sniffs */ +namespace DWS\Sniffs\Scope; + +use PHP_CodeSniffer\Sniffs\AbstractVariableSniff; +use PHP_CodeSniffer\Files\File; +use PHP_CodeSniffer\Util\Tokens; + /** * Verifies that all variables are declared in the proper scope. * * @package DWS * @subpackage Sniffs */ -final class DWS_Sniffs_Scope_VariableScopeSniff extends PHP_CodeSniffer_Standards_AbstractVariableSniff +final class VariableScopeSniff extends AbstractVariableSniff { /** * This stores the first scope level that a variable is encountered @@ -22,24 +28,24 @@ final class DWS_Sniffs_Scope_VariableScopeSniff extends PHP_CodeSniffer_Standard /** * Processes normal variables. * - * @param PHP_CodeSniffer_File $phpcsFile The file where this token was found. + * @param PHP_CodeSniffer\Files\File $phpcsFile The file where this token was found. * @param int $stackPtr The position where the token was found. * * @return void */ - protected function processVariable(PHP_CodeSniffer_File $phpcsFile, $stackPtr) + protected function processVariable(File $phpcsFile, $stackPtr) { $tokens = $phpcsFile->getTokens(); $variableName = $tokens[$stackPtr]['content']; $scopeIdentifier = $phpcsFile->getFilename() . $variableName; $level = $tokens[$stackPtr]['level']; $functionIndex = $phpcsFile->findPrevious(T_FUNCTION, $stackPtr); - $lastScopeOpen = $phpcsFile->findPrevious(PHP_CodeSniffer_Tokens::$scopeOpeners, $stackPtr); + $lastScopeOpen = $phpcsFile->findPrevious(Tokens::$scopeOpeners, $stackPtr); //Inline scope openers do not increment the level value $scopeOpenDistance = $tokens[$stackPtr]['line'] - $tokens[$lastScopeOpen]['line']; if ( - in_array($tokens[$lastScopeOpen]['code'], PHP_CodeSniffer_Tokens::$scopeOpeners) === true + in_array($tokens[$lastScopeOpen]['code'], Tokens::$scopeOpeners) === true && ($scopeOpenDistance === 1 || $scopeOpenDistance === 0)//Include the variables in the condition && $tokens[$stackPtr]['level'] === $tokens[$lastScopeOpen]['level'] ) { @@ -77,12 +83,12 @@ protected function processVariable(PHP_CodeSniffer_File $phpcsFile, $stackPtr) /** * Processes the function tokens within the class. * - * @param PHP_CodeSniffer_File $phpcsFile The file where this token was found. + * @param PHP_CodeSniffer\Files\File $phpcsFile The file where this token was found. * @param int $stackPtr The position where the token was found. * * @return void */ - protected function processMemberVar(PHP_CodeSniffer_File $phpcsFile, $stackPtr) + protected function processMemberVar(File $phpcsFile, $stackPtr) { //Do Nothing } @@ -90,12 +96,12 @@ protected function processMemberVar(PHP_CodeSniffer_File $phpcsFile, $stackPtr) /** * Processes variables in double quoted strings. * - * @param PHP_CodeSniffer_File $phpcsFile The file where this token was found. + * @param PHP_CodeSniffer\Files\File $phpcsFile The file where this token was found. * @param int $stackPtr The position where the token was found. * * @return void */ - protected function processVariableInString(PHP_CodeSniffer_File $phpcsFile, $stackPtr) + protected function processVariableInString(File $phpcsFile, $stackPtr) { //Do Nothing } diff --git a/DWS/Sniffs/Strings/DoubleQuoteUsageSniff.php b/DWS/Sniffs/Strings/DoubleQuoteUsageSniff.php index bf6ec35..466d72a 100644 --- a/DWS/Sniffs/Strings/DoubleQuoteUsageSniff.php +++ b/DWS/Sniffs/Strings/DoubleQuoteUsageSniff.php @@ -6,13 +6,18 @@ * @subpackage Sniffs */ +namespace DWS\Sniffs\Strings; + +use PHP_CodeSniffer\Sniffs\Sniff; +use PHP_CodeSniffer\Files\File; + /** * Makes sure that any use of Double Quotes are warranted. * * @package DWS * @subpackage Sniffs */ -final class DWS_Sniffs_Strings_DoubleQuoteUsageSniff implements PHP_CodeSniffer_Sniff +final class DoubleQuoteUsageSniff implements Sniff { /** * Returns an array of tokens this test wants to listen for. @@ -27,12 +32,12 @@ public function register() /** * Processes this test, when one of its tokens is encountered. * - * @param PHP_CodeSniffer_File $phpcsFile The file being scanned. + * @param PHP_CodeSniffer\Files\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) + public function process(File $phpcsFile, $stackPtr) { $tokens = $phpcsFile->getTokens(); diff --git a/DWS/Sniffs/Strings/EmbeddedVariablesSniff.php b/DWS/Sniffs/Strings/EmbeddedVariablesSniff.php index 75850e7..f6949ff 100644 --- a/DWS/Sniffs/Strings/EmbeddedVariablesSniff.php +++ b/DWS/Sniffs/Strings/EmbeddedVariablesSniff.php @@ -6,13 +6,18 @@ * @subpackage Sniffs */ +namespace DWS\Sniffs\Strings; + +use PHP_CodeSniffer\Sniffs\Sniff; +use PHP_CodeSniffer\Files\File; + /** * Makes sure that all variables embedded in strings are enclosed in braces. * * @package DWS * @subpackage Sniffs */ -final class DWS_Sniffs_Strings_EmbeddedVariablesSniff implements PHP_CodeSniffer_Sniff +final class EmbeddedVariablesSniff implements Sniff { /** * Returns an array of tokens this test wants to listen for. @@ -27,12 +32,12 @@ public function register() /** * Processes this test, when one of its tokens is encountered. * - * @param PHP_CodeSniffer_File $phpcsFile The file being scanned. + * @param PHP_CodeSniffer\Files\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) + public function process(File $phpcsFile, $stackPtr) { $tokens = $phpcsFile->getTokens(); diff --git a/DWS/Sniffs/Strings/UnnecessaryStringConcatSniff.php b/DWS/Sniffs/Strings/UnnecessaryStringConcatSniff.php index f1aa27f..5002828 100644 --- a/DWS/Sniffs/Strings/UnnecessaryStringConcatSniff.php +++ b/DWS/Sniffs/Strings/UnnecessaryStringConcatSniff.php @@ -7,6 +7,12 @@ * @subpackage Sniffs */ +namespace DWS\Sniffs\Strings; + +use PHP_CodeSniffer\Sniffs\Sniff; +use PHP_CodeSniffer\Files\File; +use PHP_CodeSniffer\Util\Tokens; + /** * Checks that two strings are not concatenated together; suggests * using one string instead. @@ -14,7 +20,7 @@ * @package DWS * @subpackage Sniffs */ -final class DWS_Sniffs_Strings_UnnecessaryStringConcatSniff implements PHP_CodeSniffer_Sniff +final class UnnecessaryStringConcatSniff implements Sniff { /** * The limit that the length of a line should not exceed. @@ -43,12 +49,12 @@ public function register() /** * Processes this test, when one of its tokens is encountered. * - * @param PHP_CodeSniffer_File $phpcsFile The file being scanned. + * @param PHP_CodeSniffer\Files\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) + public function process(File $phpcsFile, $stackPtr) { // Work out which type of file this is for. $tokens = $phpcsFile->getTokens(); @@ -60,7 +66,7 @@ public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr) $prev = $phpcsFile->findPrevious(T_WHITESPACE, $stackPtr - 1, null, true); $next = $phpcsFile->findNext(T_WHITESPACE, $stackPtr + 1, null, true); - $stringTokens = PHP_CodeSniffer_Tokens::$stringTokens; + $stringTokens = Tokens::$stringTokens; //See if the characters before and after the concatenation are quotes if ( in_array($tokens[$prev]['code'], $stringTokens) !== true || diff --git a/DWS/Sniffs/WhiteSpace/ControlStructureInteriorSpacingSniff.php b/DWS/Sniffs/WhiteSpace/ControlStructureInteriorSpacingSniff.php index 298517f..84cd7da 100644 --- a/DWS/Sniffs/WhiteSpace/ControlStructureInteriorSpacingSniff.php +++ b/DWS/Sniffs/WhiteSpace/ControlStructureInteriorSpacingSniff.php @@ -6,13 +6,18 @@ * @subpackage Sniffs */ +namespace DWS\Sniffs\WhiteSpace; + +use PHP_CodeSniffer\Sniffs\Sniff; +use PHP_CodeSniffer\Files\File; + /** * Verifies that there are no blank lines at the beginning or end of control structures. * * @package DWS * @subpackage Sniffs */ -final class DWS_Sniffs_WhiteSpace_ControlStructureInteriorSpacingSniff implements PHP_CodeSniffer_Sniff +final class ControlStructureInteriorSpacingSniff implements Sniff { /** * Returns an array of tokens this test wants to listen for. @@ -27,12 +32,12 @@ public function register() /** * Processes this test, when one of its tokens is encountered. * - * @param PHP_CodeSniffer_File $phpcsFile The file being scanned. + * @param PHP_CodeSniffer\Files\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) + public function process(File $phpcsFile, $stackPtr) { $tokens = $phpcsFile->getTokens(); $token = $tokens[$stackPtr]; diff --git a/DWS/Sniffs/WhiteSpace/ControlStructureTrailingSpacingSniff.php b/DWS/Sniffs/WhiteSpace/ControlStructureTrailingSpacingSniff.php index 4809d9a..f00078c 100644 --- a/DWS/Sniffs/WhiteSpace/ControlStructureTrailingSpacingSniff.php +++ b/DWS/Sniffs/WhiteSpace/ControlStructureTrailingSpacingSniff.php @@ -8,6 +8,11 @@ * @subpackage Sniffs */ +namespace DWS\Sniffs\WhiteSpace; + +use PHP_CodeSniffer\Sniffs\Sniff; +use PHP_CodeSniffer\Files\File; + /** * Verifies that no whitespace proceeds the first content of the file, that there is * no whitespace at the end of a line, that there are no extra new lines before or after @@ -16,7 +21,7 @@ * @package DWS * @subpackage Sniffs */ -final class DWS_Sniffs_WhiteSpace_ControlStructureTrailingSpacingSniff implements PHP_CodeSniffer_Sniff +final class ControlStructureTrailingSpacingSniff implements Sniff { /** * Returns an array of tokens this test wants to listen for. @@ -31,12 +36,12 @@ public function register() /** * Processes this test, when one of its tokens is encountered. * - * @param PHP_CodeSniffer_File $phpcsFile The file being scanned. + * @param PHP_CodeSniffer\Files\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) + public function process(File $phpcsFile, $stackPtr) { $tokens = $phpcsFile->getTokens(); $token = $tokens[$stackPtr]; diff --git a/DWS/Sniffs/WhiteSpace/OperatorSpacingSniff.php b/DWS/Sniffs/WhiteSpace/OperatorSpacingSniff.php index 8c0eb8f..6ac8c1d 100644 --- a/DWS/Sniffs/WhiteSpace/OperatorSpacingSniff.php +++ b/DWS/Sniffs/WhiteSpace/OperatorSpacingSniff.php @@ -6,13 +6,20 @@ * @subpackage Sniffs */ +namespace DWS\Sniffs\WhiteSpace; + +use DWS\Helpers; +use PHP_CodeSniffer\Sniffs\Sniff; +use PHP_CodeSniffer\Files\File; +use PHP_CodeSniffer\Util\Tokens; + /** * Verifies that there is one space around operators. * * @package DWS * @subpackage Sniffs */ -final class DWS_Sniffs_WhiteSpace_OperatorSpacingSniff implements PHP_CodeSniffer_Sniff +final class OperatorSpacingSniff implements Sniff { /** * Returns an array of tokens this test wants to listen for. @@ -23,10 +30,10 @@ public function register() { return array_unique( array_merge( - PHP_CodeSniffer_Tokens::$comparisonTokens, - PHP_CodeSniffer_Tokens::$operators, - PHP_CodeSniffer_Tokens::$assignmentTokens, - PHP_CodeSniffer_Tokens::$booleanOperators, + Tokens::$comparisonTokens, + Tokens::$operators, + Tokens::$assignmentTokens, + Tokens::$booleanOperators, [T_INLINE_THEN, T_INLINE_ELSE, T_BOOLEAN_NOT, T_STRING_CONCAT] ) ); @@ -36,12 +43,12 @@ public function register() /** * Processes this test, when one of its tokens is encountered. * - * @param PHP_CodeSniffer_File $phpcsFile The file being scanned. + * @param PHP_CodeSniffer\Files\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) + public function process(File $phpcsFile, $stackPtr) { $tokens = $phpcsFile->getTokens(); $operator = $tokens[$stackPtr]['content']; @@ -57,7 +64,7 @@ public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr) $nextNonEmpty = $phpcsFile->findNext(T_WHITESPACE, $stackPtr + 1, null, true); $nextSplitsLine = $tokens[$nextNonEmpty]['line'] !== $tokens[$stackPtr]['line']; - if (DWS_Helpers_Operator::isUnary($phpcsFile, $stackPtr)) { + if (Helpers\Operator::isUnary($phpcsFile, $stackPtr)) { if ( !$previousSplitsLine && $previousSpaces !== 1 && diff --git a/DWS/Sniffs/WhiteSpace/SuperfluousWhitespaceSniff.php b/DWS/Sniffs/WhiteSpace/SuperfluousWhitespaceSniff.php index 00e2559..2efc9c3 100644 --- a/DWS/Sniffs/WhiteSpace/SuperfluousWhitespaceSniff.php +++ b/DWS/Sniffs/WhiteSpace/SuperfluousWhitespaceSniff.php @@ -8,6 +8,12 @@ * @subpackage Sniffs */ +namespace DWS\Sniffs\WhiteSpace; + +use PHP_CodeSniffer\Sniffs\Sniff; +use PHP_CodeSniffer\Files\File; +use PHP_CodeSniffer\Util\Tokens; + /** * Verifies that no whitespace proceeds the first content of the file, that there is * no whitespace at the end of a line, that there are no extra new lines before or after @@ -16,7 +22,7 @@ * @package DWS * @subpackage Sniffs */ -final class DWS_Sniffs_WhiteSpace_SuperfluousWhitespaceSniff implements PHP_CodeSniffer_Sniff +final class SuperfluousWhitespaceSniff implements Sniff { /** * Returns an array of tokens this test wants to listen for. @@ -25,7 +31,7 @@ final class DWS_Sniffs_WhiteSpace_SuperfluousWhitespaceSniff implements PHP_Code */ public function register() { - $tokens = PHP_CodeSniffer_Tokens::$scopeOpeners; + $tokens = Tokens::$scopeOpeners; $tokens[] = T_OPEN_TAG; $tokens[] = T_WHITESPACE; $tokens[] = T_COMMENT; @@ -35,12 +41,12 @@ public function register() /** * Processes this sniff, when one of its tokens is encountered. * - * @param PHP_CodeSniffer_File $phpcsFile The file being scanned. + * @param PHP_CodeSniffer\Files\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) + public function process(File $phpcsFile, $stackPtr) { $tokens = $phpcsFile->getTokens(); $tokenContent = null; @@ -61,7 +67,7 @@ public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr) $phpcsFile->addError('Additional whitespace found at start of file', $stackPtr, 'StartFile'); } elseif ( - in_array($tokens[$stackPtr]['code'], PHP_CodeSniffer_Tokens::$scopeOpeners) + in_array($tokens[$stackPtr]['code'], Tokens::$scopeOpeners) && array_key_exists('scope_opener', $tokens[$stackPtr]) ) { $classOpen = $tokens[$stackPtr]['scope_opener']; diff --git a/composer.json b/composer.json index 5288892..8ac7822 100644 --- a/composer.json +++ b/composer.json @@ -24,11 +24,17 @@ "sort-packages": true }, "require": { - "php": "^7.0", - "squizlabs/php_codesniffer": "^2.6" + "php": "^7.0 || ^8.0", + "squizlabs/php_codesniffer": "^3" }, "require-dev": { - "phpunit/phpunit": ">=6" + "php-coveralls/php-coveralls": "^2.5", + "phpunit/phpunit": "^6.5 || ^9" + }, + "autoload": { + "psr-4": { + "DWS\\": "src" + } }, "scripts": { "lint": "vendor/bin/phpcs", diff --git a/tests/AbstractSniffUnitTest.php b/tests/AbstractSniffUnitTest.php index 0a7c00a..1b7df25 100644 --- a/tests/AbstractSniffUnitTest.php +++ b/tests/AbstractSniffUnitTest.php @@ -20,13 +20,6 @@ */ abstract class AbstractSniffUnitTest extends TestCase { - /** - * The PHP_CodeSniffer object used for testing. - * - * @var PHP_CodeSniffer - */ - protected static $_phpcs = null; - /** * Tests the extending classes Sniff class. * @@ -36,13 +29,10 @@ abstract class AbstractSniffUnitTest extends TestCase */ public final function runTest() { - if (self::$_phpcs === null) { - self::$_phpcs = new PHP_CodeSniffer(); - self::$_phpcs->cli->setCommandLineValues(['-s']); - } - - self::$_phpcs->process([], 'DWS', [$this->_getSniffName()]); - self::$_phpcs->setIgnorePatterns([]); + $config = new \PHP_CodeSniffer\Config(); + $ruleset = new \PHP_CodeSniffer\Ruleset($config); + $ruleset->registerSniffs([dirname(__DIR__) . $this->_getSniffName()], [], []); + $ruleset->populateTokenListeners(); $testFile = dirname(__DIR__) . '/tests/' . str_replace('_', '/', get_class($this)) . '.inc'; if (!file_exists($testFile)) { @@ -52,7 +42,8 @@ public final function runTest() $phpcsFile = null; try { - $phpcsFile = self::$_phpcs->processFile($testFile); + $phpcsFile = new \PHP_CodeSniffer\Files\LocalFile($testFile, $ruleset, $config); + $phpcsFile->process(); } catch (Exception $e) { $this->fail("An unexpected exception has been caught: {$e->getMessage()}"); } @@ -73,12 +64,12 @@ public final function runTest() /** * Generate a list of test failures for a given sniffed file. * - * @param PHP_CodeSniffer_File $file The file being tested. + * @param PHP_CodeSniffer\Files\File $file The file being tested. * * @return array - * @throws PHP_CodeSniffer_Exception + * @throws \PHP_CodeSniffer\Exception\RuntimeException */ - public function generateFailureMessages(PHP_CodeSniffer_File $file) + public function generateFailureMessages(\PHP_CodeSniffer\Files\File $file) { $testFile = $file->getFilename(); $foundErrors = $file->getErrors(); @@ -87,11 +78,11 @@ public function generateFailureMessages(PHP_CodeSniffer_File $file) $expectedWarnings = $this->getWarningList(basename($testFile)); if (!is_array($expectedErrors)) { - throw new PHP_CodeSniffer_Exception('getErrorList() must return an array'); + throw new \PHP_CodeSniffer\Exception\RuntimeException('getErrorList() must return an array'); } if (!is_array($expectedWarnings)) { - throw new PHP_CodeSniffer_Exception('getWarningList() must return an array'); + throw new \PHP_CodeSniffer\Exception\RuntimeException('getWarningList() must return an array'); } /* diff --git a/tests/DWS/Sniffs/Arrays/ArrayDeclarationSniffTest.php b/tests/DWS/Sniffs/Arrays/ArrayDeclarationSniffTest.php index aae9abc..31ad003 100644 --- a/tests/DWS/Sniffs/Arrays/ArrayDeclarationSniffTest.php +++ b/tests/DWS/Sniffs/Arrays/ArrayDeclarationSniffTest.php @@ -1,5 +1,4 @@ add('', __DIR__); + +require dirname(__DIR__) . '/vendor/squizlabs/php_codesniffer/tests/bootstrap.php';