From 2387856e744597603137a0a1de46c1a15367482d Mon Sep 17 00:00:00 2001 From: David Joos Date: Tue, 13 Feb 2018 17:54:57 +0000 Subject: [PATCH] Address coding standard errors --- .travis.yml | 2 +- .../Arrays/MultiLineArrayCommaSniff.php | 8 +-- .../Sniffs/Commenting/AnnotationsSniff.php | 44 ++++++++++---- .../Sniffs/Commenting/ClassCommentSniff.php | 12 ++-- .../Commenting/FunctionCommentSniff.php | 4 +- Symfony/Sniffs/Commenting/LicenseSniff.php | 19 ++++--- .../Sniffs/Commenting/TypeHintingSniff.php | 40 +++++++++---- .../IdenticalComparisonSniff.php | 16 ++++-- .../ControlStructure/UnaryOperatorsSniff.php | 10 ++-- .../ControlStructure/YodaConditionsSniff.php | 23 +++++--- .../Sniffs/Errors/ExceptionMessageSniff.php | 26 ++++++--- Symfony/Sniffs/Errors/UserDeprecatedSniff.php | 18 +++--- .../Files/AlphanumericFilenameSniff.php | 15 +++-- .../Formatting/BlankLineBeforeReturnSniff.php | 3 +- .../Sniffs/Formatting/ReturnOrThrowSniff.php | 57 +++++++++++++------ Symfony/Sniffs/Functions/ArgumentsSniff.php | 28 ++++++--- Symfony/Sniffs/Functions/ReturnTypeSniff.php | 34 ++++++++--- Symfony/Sniffs/Scope/MethodScopeSniff.php | 9 ++- .../WhiteSpace/AssignmentSpacingSniff.php | 2 +- .../WhiteSpace/BinaryOperatorSpacingSniff.php | 10 ++-- build.xml | 1 - tests_bootstrap.php | 9 ++- 22 files changed, 259 insertions(+), 131 deletions(-) diff --git a/.travis.yml b/.travis.yml index 2561cd7..4b41677 100644 --- a/.travis.yml +++ b/.travis.yml @@ -13,7 +13,7 @@ before_script: - phpunit --version script: - - ant travis -Dcomposer.path=composer + - ant test -Dcomposer.path=composer matrix: allow_failures: diff --git a/Symfony/Sniffs/Arrays/MultiLineArrayCommaSniff.php b/Symfony/Sniffs/Arrays/MultiLineArrayCommaSniff.php index 0dcd8ba..87a1f4d 100644 --- a/Symfony/Sniffs/Arrays/MultiLineArrayCommaSniff.php +++ b/Symfony/Sniffs/Arrays/MultiLineArrayCommaSniff.php @@ -107,10 +107,10 @@ public function process(File $phpcsFile, $stackPtr) if ($fix === true) { $ptr = $phpcsFile->findPrevious( - array(T_WHITESPACE, T_COMMENT), - $closePtr-1, - $stackPtr, - true + array(T_WHITESPACE, T_COMMENT), + $closePtr-1, + $stackPtr, + true ); $phpcsFile->fixer->addContent($ptr, ','); diff --git a/Symfony/Sniffs/Commenting/AnnotationsSniff.php b/Symfony/Sniffs/Commenting/AnnotationsSniff.php index c2bcf0a..560ebf5 100644 --- a/Symfony/Sniffs/Commenting/AnnotationsSniff.php +++ b/Symfony/Sniffs/Commenting/AnnotationsSniff.php @@ -29,10 +29,12 @@ class AnnotationsSniff implements Sniff { - private static $pattern = '/^@([^\\\(]+).*$/i'; + private static $_pattern = '/^@([^\\\(]+).*$/i'; /** * Registers the tokens that this sniff wants to listen for. + * + * @return array */ public function register() { @@ -45,11 +47,9 @@ public function register() * Called when one of the token types that this sniff is listening for * is found. * - * @param \PHP_CodeSniffer\Files\File $phpcsFile The PHP_CodeSniffer file where the - * token was found. - * @param int $stackPtr The position in the PHP_CodeSniffer - * file's token stack where the token - * was found. + * @param File $phpcsFile The file where the token was found. + * @param int $stackPtr The position of the current token + * in the stack passed in $tokens. * * @return void|int Optionally returns a stack pointer. The sniff will not be * called again on the current file until the returned stack @@ -61,13 +61,35 @@ public function process(File $phpcsFile, $stackPtr) $tokens = $phpcsFile->getTokens(); $closer = $phpcsFile->findNext(T_DOC_COMMENT_CLOSE_TAG, $stackPtr); - if (false !== $next = $phpcsFile->findNext($this->register(), $stackPtr + 1, $closer)) { - $first = preg_replace(self::$pattern, '$1', $tokens[$stackPtr]['content']); - $second = preg_replace(self::$pattern, '$1', $tokens[$next]['content']); + if (false !== $next = $phpcsFile->findNext( + $this->register(), + $stackPtr + 1, + $closer + ) + ) { + $first = preg_replace( + self::$_pattern, + '$1', + $tokens[$stackPtr]['content'] + ); + $second = preg_replace( + self::$_pattern, + '$1', + $tokens[$next]['content'] + ); + + $stackPtrLine = $tokens[$stackPtr]['line']; + $nextLine = $tokens[$next]['line']; + + if ($first !== $second && $stackPtrLine + 2 > $nextLine) { + $error = 'Group annotations together '; + $error .= 'so that annotations of the same type '; + $error .= 'immediately follow each other, and annotations '; + $error .= 'of a different type are separated '; + $error .= 'by a single blank line'; - if ($first !== $second && $tokens[$stackPtr]['line'] + 2 > $tokens[$next]['line']) { $phpcsFile->addError( - 'Group annotations together so that annotations of the same type immediately follow each other, and annotations of a different type are separated by a single blank line', + $error, $stackPtr, 'Invalid' ); diff --git a/Symfony/Sniffs/Commenting/ClassCommentSniff.php b/Symfony/Sniffs/Commenting/ClassCommentSniff.php index 6513a10..99d68f2 100644 --- a/Symfony/Sniffs/Commenting/ClassCommentSniff.php +++ b/Symfony/Sniffs/Commenting/ClassCommentSniff.php @@ -14,7 +14,7 @@ namespace Symfony\Sniffs\Commenting; -use PHP_CodeSniffer\Standards\PEAR\Sniffs\Commenting\ClassCommentSniff as PearClassCommentSniff; +use PHP_CodeSniffer\Standards\PEAR\Sniffs\Commenting\ClassCommentSniff as Sniff; /** * Parses and verifies the doc comments for classes. @@ -38,7 +38,7 @@ * @license http://spdx.org/licenses/MIT MIT License * @link http://pear.php.net/package/PHP_CodeSniffer */ -class ClassCommentSniff extends PearClassCommentSniff +class ClassCommentSniff extends Sniff { /** * Tags in correct order and related info. @@ -101,10 +101,10 @@ class ClassCommentSniff extends PearClassCommentSniff /** * Processes each tag and raise an error if there are blacklisted tags. * - * @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. - * @param int $commentStart Position in the stack where the comment started. + * @param File $phpcsFile The file where the token was found. + * @param int $stackPtr The position of the current token + * in the stack passed in $tokens. + * @param int $commentStart Position in the stack where the comment started. * * @return void */ diff --git a/Symfony/Sniffs/Commenting/FunctionCommentSniff.php b/Symfony/Sniffs/Commenting/FunctionCommentSniff.php index 6b161a2..bad1d75 100644 --- a/Symfony/Sniffs/Commenting/FunctionCommentSniff.php +++ b/Symfony/Sniffs/Commenting/FunctionCommentSniff.php @@ -15,7 +15,7 @@ namespace Symfony\Sniffs\Commenting; use PHP_CodeSniffer\Files\File; -use PHP_CodeSniffer\Standards\PEAR\Sniffs\Commenting\FunctionCommentSniff as PearFunctionCommentSniff; +use PHP_CodeSniffer\Standards\PEAR\Sniffs\Commenting\FunctionCommentSniff as Sniff; /** * Symfony standard customization to PEARs FunctionCommentSniff. @@ -35,7 +35,7 @@ * @license http://spdx.org/licenses/MIT MIT License * @link http://pear.php.net/package/PHP_CodeSniffer */ -class FunctionCommentSniff extends PearFunctionCommentSniff +class FunctionCommentSniff extends Sniff { /** * Process the return comment of this function comment. diff --git a/Symfony/Sniffs/Commenting/LicenseSniff.php b/Symfony/Sniffs/Commenting/LicenseSniff.php index c0517f0..c740d32 100644 --- a/Symfony/Sniffs/Commenting/LicenseSniff.php +++ b/Symfony/Sniffs/Commenting/LicenseSniff.php @@ -31,6 +31,8 @@ class LicenseSniff implements Sniff /** * Registers the tokens that this sniff wants to listen for. + * + * @return array */ public function register() { @@ -43,11 +45,9 @@ public function register() * Called when one of the token types that this sniff is listening for * is found. * - * @param \PHP_CodeSniffer\Files\File $phpcsFile The PHP_CodeSniffer file where the - * token was found. - * @param int $stackPtr The position in the PHP_CodeSniffer - * file's token stack where the token - * was found. + * @param File $phpcsFile The file where the token was found. + * @param int $stackPtr The position of the current token + * in the stack passed in $tokens. * * @return void|int Optionally returns a stack pointer. The sniff will not be * called again on the current file until the returned stack @@ -56,9 +56,14 @@ public function register() */ public function process(File $phpcsFile, $stackPtr) { - if (false === $phpcsFile->findPrevious([T_COMMENT, T_DOC_COMMENT_OPEN_TAG], $stackPtr)) { + if (false === $phpcsFile->findPrevious( + [T_COMMENT, T_DOC_COMMENT_OPEN_TAG], + $stackPtr + ) + ) { $phpcsFile->addWarning( - 'The license block has to be present at the top of every PHP file, before the namespace', + 'The license block has to be present at the top + of every PHP file, before the namespace', $stackPtr, 'Warning' ); diff --git a/Symfony/Sniffs/Commenting/TypeHintingSniff.php b/Symfony/Sniffs/Commenting/TypeHintingSniff.php index d4576ca..40e6866 100644 --- a/Symfony/Sniffs/Commenting/TypeHintingSniff.php +++ b/Symfony/Sniffs/Commenting/TypeHintingSniff.php @@ -28,14 +28,14 @@ */ class TypeHintingSniff implements Sniff { - private static $blacklist = [ + private static $_blacklist = [ 'boolean' => 'bool', 'integer' => 'int', 'double' => 'float', 'real' => 'float', ]; - private static $casts = [ + private static $_casts = [ T_BOOL_CAST, T_INT_CAST, T_DOUBLE_CAST, @@ -43,6 +43,8 @@ class TypeHintingSniff implements Sniff /** * Registers the tokens that this sniff wants to listen for. + * + * @return array */ public function register() { @@ -58,11 +60,9 @@ public function register() * Called when one of the token types that this sniff is listening for * is found. * - * @param \PHP_CodeSniffer\Files\File $phpcsFile The PHP_CodeSniffer file where the - * token was found. - * @param int $stackPtr The position in the PHP_CodeSniffer - * file's token stack where the token - * was found. + * @param File $phpcsFile The file where the token was found. + * @param int $stackPtr The position of the current token + * in the stack passed in $tokens. * * @return void|int Optionally returns a stack pointer. The sniff will not be * called again on the current file until the returned stack @@ -76,13 +76,29 @@ public function process(File $phpcsFile, $stackPtr) if ('@var' === $tag['content']) { $type = $phpcsFile->findNext(T_DOC_COMMENT_STRING, $stackPtr + 1); - $hint = strtolower(preg_replace('/([^\s]+)[\s]+.*/', '$1', $tokens[$type]['content'])); - } elseif (in_array($tag['code'], self::$casts)) { - $hint = strtolower(preg_replace('/\(([^\s]+)\)/', '$1', $tag['content'])); + $hint = strtolower( + preg_replace( + '/([^\s]+)[\s]+.*/', + '$1', + $tokens[$type]['content'] + ) + ); + } elseif (in_array($tag['code'], self::$_casts)) { + $hint = strtolower( + preg_replace( + '/\(([^\s]+)\)/', + '$1', + $tag['content'] + ) + ); } - if (isset($hint) && isset(self::$blacklist[$hint])) { - $error = sprintf('For type-hinting in PHPDocs and casting, use %s instead of %s', self::$blacklist[$hint], $hint); + if (isset($hint) && isset(self::$_blacklist[$hint])) { + $error = sprintf( + 'For type-hinting in PHPDocs and casting, use %s instead of %s', + self::$_blacklist[$hint], + $hint + ); $phpcsFile->addError($error, $stackPtr, 'Invalid'); } diff --git a/Symfony/Sniffs/ControlStructure/IdenticalComparisonSniff.php b/Symfony/Sniffs/ControlStructure/IdenticalComparisonSniff.php index fde0bae..266b028 100644 --- a/Symfony/Sniffs/ControlStructure/IdenticalComparisonSniff.php +++ b/Symfony/Sniffs/ControlStructure/IdenticalComparisonSniff.php @@ -30,6 +30,8 @@ class IdenticalComparisonSniff implements Sniff { /** * Registers the tokens that this sniff wants to listen for. + * + * @return array */ public function register() { @@ -43,11 +45,9 @@ public function register() * Called when one of the token types that this sniff is listening for * is found. * - * @param \PHP_CodeSniffer\Files\File $phpcsFile The PHP_CodeSniffer file where the - * token was found. - * @param int $stackPtr The position in the PHP_CodeSniffer - * file's token stack where the token - * was found. + * @param File $phpcsFile The file where the token was found. + * @param int $stackPtr The position of the current token + * in the stack passed in $tokens. * * @return void|int Optionally returns a stack pointer. The sniff will not be * called again on the current file until the returned stack @@ -56,7 +56,11 @@ public function register() */ public function process(File $phpcsFile, $stackPtr) { - $phpcsFile->addWarning('Always use identical comparison unless you need type juggling', $stackPtr, 'Warning'); + $phpcsFile->addWarning( + 'Always use identical comparison unless you need type juggling', + $stackPtr, + 'Warning' + ); } } diff --git a/Symfony/Sniffs/ControlStructure/UnaryOperatorsSniff.php b/Symfony/Sniffs/ControlStructure/UnaryOperatorsSniff.php index eaba849..c7ca966 100644 --- a/Symfony/Sniffs/ControlStructure/UnaryOperatorsSniff.php +++ b/Symfony/Sniffs/ControlStructure/UnaryOperatorsSniff.php @@ -30,6 +30,8 @@ class UnaryOperatorsSniff implements Sniff { /** * Registers the tokens that this sniff wants to listen for. + * + * @return array */ public function register() { @@ -43,11 +45,9 @@ public function register() * Called when one of the token types that this sniff is listening for * is found. * - * @param \PHP_CodeSniffer\Files\File $phpcsFile The PHP_CodeSniffer file where the - * token was found. - * @param int $stackPtr The position in the PHP_CodeSniffer - * file's token stack where the token - * was found. + * @param File $phpcsFile The file where the token was found. + * @param int $stackPtr The position of the current token + * in the stack passed in $tokens. * * @return void|int Optionally returns a stack pointer. The sniff will not be * called again on the current file until the returned stack diff --git a/Symfony/Sniffs/ControlStructure/YodaConditionsSniff.php b/Symfony/Sniffs/ControlStructure/YodaConditionsSniff.php index c30bd0a..f0fcbfc 100644 --- a/Symfony/Sniffs/ControlStructure/YodaConditionsSniff.php +++ b/Symfony/Sniffs/ControlStructure/YodaConditionsSniff.php @@ -28,7 +28,7 @@ */ class YodaConditionsSniff implements Sniff { - private $yodas = array( + private $_yodas = array( T_IS_EQUAL, T_IS_IDENTICAL, T_IS_NOT_EQUAL, @@ -37,6 +37,8 @@ class YodaConditionsSniff implements Sniff /** * Registers the tokens that this sniff wants to listen for. + * + * @return array */ public function register() { @@ -50,11 +52,9 @@ public function register() * Called when one of the token types that this sniff is listening for * is found. * - * @param \PHP_CodeSniffer\Files\File $phpcsFile The PHP_CodeSniffer file where the - * token was found. - * @param int $stackPtr The position in the PHP_CodeSniffer - * file's token stack where the token - * was found. + * @param File $phpcsFile The file where the token was found. + * @param int $stackPtr The position of the current token + * in the stack passed in $tokens. * * @return void|int Optionally returns a stack pointer. The sniff will not be * called again on the current file until the returned stack @@ -68,14 +68,19 @@ public function process(File $phpcsFile, $stackPtr) $closer = $tokens[$stackPtr]['parenthesis_closer']; do { - $comparison = $phpcsFile->findNext($this->yodas, $opener + 1, $closer); + $comparison = $phpcsFile->findNext($this->_yodas, $opener + 1, $closer); if (false === $comparison) { break; } - if (T_VARIABLE === $tokens[$comparison - 2]['code'] && T_VARIABLE !== $tokens[$comparison + 2]['code']) { - $error = 'Use Yoda conditions when checking a variable against an expression to avoid an accidental assignment inside the condition statement'; + if (T_VARIABLE === $tokens[$comparison - 2]['code'] + && T_VARIABLE !== $tokens[$comparison + 2]['code'] + ) { + $error = 'Use Yoda conditions when checking a variable against '; + $error .= 'an expression to avoid an accidental assignment '; + $error .= 'inside the condition statement.'; + $phpcsFile->addError($error, $stackPtr, 'Invalid'); } diff --git a/Symfony/Sniffs/Errors/ExceptionMessageSniff.php b/Symfony/Sniffs/Errors/ExceptionMessageSniff.php index 710d6bf..9e138bb 100644 --- a/Symfony/Sniffs/Errors/ExceptionMessageSniff.php +++ b/Symfony/Sniffs/Errors/ExceptionMessageSniff.php @@ -31,6 +31,8 @@ class ExceptionMessageSniff implements Sniff { /** * Registers the tokens that this sniff wants to listen for. + * + * @return array */ public function register() { @@ -43,11 +45,9 @@ public function register() * Called when one of the token types that this sniff is listening for * is found. * - * @param \PHP_CodeSniffer\Files\File $phpcsFile The PHP_CodeSniffer file where the - * token was found. - * @param int $stackPtr The position in the PHP_CodeSniffer - * file's token stack where the token - * was found. + * @param File $phpcsFile The file where the token was found. + * @param int $stackPtr The position of the current token + * in the stack passed in $tokens. * * @return void|int Optionally returns a stack pointer. The sniff will not be * called again on the current file until the returned stack @@ -63,12 +63,20 @@ public function process(File $phpcsFile, $stackPtr) if ($opener === false) { return; } - - // Check the content of the found parenthesis only if it is in the same statement as the throw + + // check the content of the found parenthesis, + // only if it is in the same statement as the throw $endOfStatement = $phpcsFile->findNext(T_SEMICOLON, $stackPtr); if ($endOfStatement > $opener) { - if ($phpcsFile->findNext(T_STRING_CONCAT, $tokens[$opener]['parenthesis_opener'], $tokens[$opener]['parenthesis_closer'])) { - $error = 'Exception and error message strings must be concatenated using sprintf'; + if ($phpcsFile->findNext( + T_STRING_CONCAT, + $tokens[$opener]['parenthesis_opener'], + $tokens[$opener]['parenthesis_closer'] + ) + ) { + $error = 'Exception and error message strings must be '; + $error .= 'concatenated using sprintf'; + $phpcsFile->addError($error, $stackPtr, 'Invalid'); } } diff --git a/Symfony/Sniffs/Errors/UserDeprecatedSniff.php b/Symfony/Sniffs/Errors/UserDeprecatedSniff.php index 77a6403..5bc13df 100644 --- a/Symfony/Sniffs/Errors/UserDeprecatedSniff.php +++ b/Symfony/Sniffs/Errors/UserDeprecatedSniff.php @@ -31,6 +31,8 @@ class UserDeprecatedSniff implements Sniff { /** * Registers the tokens that this sniff wants to listen for. + * + * @return array */ public function register() { @@ -43,11 +45,9 @@ public function register() * Called when one of the token types that this sniff is listening for * is found. * - * @param \PHP_CodeSniffer\Files\File $phpcsFile The PHP_CodeSniffer file where the - * token was found. - * @param int $stackPtr The position in the PHP_CodeSniffer - * file's token stack where the token - * was found. + * @param File $phpcsFile The file where the token was found. + * @param int $stackPtr The position of the current token + * in the stack passed in $tokens. * * @return void|int Optionally returns a stack pointer. The sniff will not be * called again on the current file until the returned stack @@ -73,8 +73,12 @@ public function process(File $phpcsFile, $stackPtr) break; } - if ($tokens[$string]['content'] === 'E_USER_DEPRECATED' && '@' !== $tokens[$stackPtr -1]['content']) { - $error = 'Calls to trigger_error with type E_USER_DEPRECATED must be switched to opt-in via @ operator'; + if ($tokens[$string]['content'] === 'E_USER_DEPRECATED' + && '@' !== $tokens[$stackPtr -1]['content'] + ) { + $error = 'Calls to trigger_error with type E_USER_DEPRECATED '; + $error .= 'must be switched to opt-in via @ operator'; + $phpcsFile->addError($error, $stackPtr, 'Invalid'); break; diff --git a/Symfony/Sniffs/Files/AlphanumericFilenameSniff.php b/Symfony/Sniffs/Files/AlphanumericFilenameSniff.php index fa14835..28ad808 100644 --- a/Symfony/Sniffs/Files/AlphanumericFilenameSniff.php +++ b/Symfony/Sniffs/Files/AlphanumericFilenameSniff.php @@ -42,10 +42,14 @@ public function register() /** * Process. * - * @param \PHP_CodeSniffer\Files\File $phpcsFile - * @param int $stackPtr + * @param File $phpcsFile The file where the token was found. + * @param int $stackPtr The position of the current token + * in the stack passed in $tokens. * - * @return int|void + * @return void|int Optionally returns a stack pointer. The sniff will not be + * called again on the current file until the returned stack + * pointer is reached. Return (count($tokens) + 1) to skip + * the rest of the file. */ public function process(File $phpcsFile, $stackPtr) { @@ -58,7 +62,10 @@ public function process(File $phpcsFile, $stackPtr) $filename = str_replace('_', '', basename($filename, '.php')); if (false === ctype_alnum($filename)) { - $error = sprintf('Filename "%s" contains non alphanumeric characters', $filename); + $error = sprintf( + 'Filename "%s" contains non alphanumeric characters', + $filename + ); $phpcsFile->addError($error, $stackPtr, 'Invalid'); $phpcsFile->recordMetric($stackPtr, 'Alphanumeric filename', 'no'); } else { diff --git a/Symfony/Sniffs/Formatting/BlankLineBeforeReturnSniff.php b/Symfony/Sniffs/Formatting/BlankLineBeforeReturnSniff.php index f56c3be..40bdddd 100644 --- a/Symfony/Sniffs/Formatting/BlankLineBeforeReturnSniff.php +++ b/Symfony/Sniffs/Formatting/BlankLineBeforeReturnSniff.php @@ -104,9 +104,10 @@ public function process(File $phpcsFile, $stackPtr) if ($fix === true) { $phpcsFile->fixer->beginChangeset(); $i = 1; - while($tokens[$stackPtr-$i]['type'] == "T_WHITESPACE") { + while ($tokens[$stackPtr-$i]['type'] == "T_WHITESPACE") { $i++; } + $phpcsFile->fixer->addNewLine($stackPtr-$i); $phpcsFile->fixer->endChangeset(); } diff --git a/Symfony/Sniffs/Formatting/ReturnOrThrowSniff.php b/Symfony/Sniffs/Formatting/ReturnOrThrowSniff.php index a11c1f5..eb671a4 100644 --- a/Symfony/Sniffs/Formatting/ReturnOrThrowSniff.php +++ b/Symfony/Sniffs/Formatting/ReturnOrThrowSniff.php @@ -28,12 +28,12 @@ */ class ReturnOrThrowSniff implements Sniff { - private $openers = [ + private $_openers = [ T_IF, T_CASE, ]; - private $conditions = [ + private $_conditions = [ T_ELSEIF, T_ELSE, T_BREAK, @@ -41,6 +41,8 @@ class ReturnOrThrowSniff implements Sniff /** * Registers the tokens that this sniff wants to listen for. + * + * @return array */ public function register() { @@ -54,11 +56,9 @@ public function register() * Called when one of the token types that this sniff is listening for * is found. * - * @param \PHP_CodeSniffer\Files\File $phpcsFile The PHP_CodeSniffer file where the - * token was found. - * @param int $stackPtr The position in the PHP_CodeSniffer - * file's token stack where the token - * was found. + * @param File $phpcsFile The file where the token was found. + * @param int $stackPtr The position of the current token + * in the stack passed in $tokens. * * @return void|int Optionally returns a stack pointer. The sniff will not be * called again on the current file until the returned stack @@ -77,25 +77,47 @@ public function process(File $phpcsFile, $stackPtr) return; } - $opener = $phpcsFile->findPrevious($this->openers, $stackPtr, $tokens[$function]['scope_opener']); + $opener = $phpcsFile->findPrevious( + $this->_openers, + $stackPtr, + $tokens[$function]['scope_opener'] + ); + + $scopeCloserLine = $tokens[$tokens[$opener]['scope_closer']]['line']; // check whether the return / throw is within a if or case statement - if ($opener && $elem['line'] <= $tokens[$tokens[$opener]['scope_closer']]['line']) { - // check whether there's an elseif / else / break following the if or case statement - $condition = $phpcsFile->findNext($this->conditions, $stackPtr + 1, $tokens[$function]['scope_closer']); + if ($opener && $elem['line'] <= $scopeCloserLine) { + // check whether there's an elseif, else or break + // following the if or case statement + $condition = $phpcsFile->findNext( + $this->_conditions, + $stackPtr + 1, + $tokens[$function]['scope_closer'] + ); - if (false !== $condition){ + if (false !== $condition) { if (T_CASE === $tokens[$opener]['code']) { - $next = $phpcsFile->findNext([T_CASE, T_DEFAULT], $stackPtr + 1, $tokens[$function]['scope_closer']); + $next = $phpcsFile->findNext( + [T_CASE, T_DEFAULT], + $stackPtr + 1, + $tokens[$function]['scope_closer'] + ); - $err = (false === $next) ? true : $tokens[$condition]['line'] < $tokens[$next]['line']; + $err = true; + + if (false !== $next) { + $err = $tokens[$condition]['line'] < $tokens[$next]['line']; + } } else { - $err = $tokens[$condition]['line'] === $tokens[$tokens[$opener]['scope_closer']]['line']; + $err = $tokens[$condition]['line'] === $scopeCloserLine; } if ($err) { + $error = 'Do not use else, elseif, break after if and '; + $error .= 'case conditions which return or throw something'; + $phpcsFile->addError( - 'Do not use else, elseif, break after if and case conditions which return or throw something', + $error, $stackPtr, 'Invalid' ); @@ -103,5 +125,4 @@ public function process(File $phpcsFile, $stackPtr) } } } - -} \ No newline at end of file +} diff --git a/Symfony/Sniffs/Functions/ArgumentsSniff.php b/Symfony/Sniffs/Functions/ArgumentsSniff.php index ecf091d..e7009aa 100644 --- a/Symfony/Sniffs/Functions/ArgumentsSniff.php +++ b/Symfony/Sniffs/Functions/ArgumentsSniff.php @@ -30,6 +30,8 @@ class ArgumentsSniff implements Sniff { /** * Registers the tokens that this sniff wants to listen for. + * + * @return array */ public function register() { @@ -42,11 +44,9 @@ public function register() * Called when one of the token types that this sniff is listening for * is found. * - * @param \PHP_CodeSniffer\Files\File $phpcsFile The PHP_CodeSniffer file where the - * token was found. - * @param int $stackPtr The position in the PHP_CodeSniffer - * file's token stack where the token - * was found. + * @param File $phpcsFile The file where the token was found. + * @param int $stackPtr The position of the current token + * in the stack passed in $tokens. * * @return void|int Optionally returns a stack pointer. The sniff will not be * called again on the current file until the returned stack @@ -58,10 +58,20 @@ public function process(File $phpcsFile, $stackPtr) $tokens = $phpcsFile->getTokens(); $function = $tokens[$stackPtr]; - if ($tokens[$function['parenthesis_opener']]['line'] !== $tokens[$function['parenthesis_closer']]['line']) { - $error = 'Declare all the arguments on the same line as the method/function name, no matter how many arguments there are.'; - $phpcsFile->addError($error, $stackPtr, 'Invalid'); + $openerLine = $tokens[$function['parenthesis_opener']]['line']; + $closerLine = $tokens[$function['parenthesis_closer']]['line']; + + if ($openerLine !== $closerLine) { + $error = 'Declare all the arguments on the same line '; + $error .= 'as the method/function name, '; + $error .= 'no matter how many arguments there are.'; + + $phpcsFile->addError( + $error, + $stackPtr, + 'Invalid' + ); } } -} \ No newline at end of file +} diff --git a/Symfony/Sniffs/Functions/ReturnTypeSniff.php b/Symfony/Sniffs/Functions/ReturnTypeSniff.php index 955a297..082df6c 100644 --- a/Symfony/Sniffs/Functions/ReturnTypeSniff.php +++ b/Symfony/Sniffs/Functions/ReturnTypeSniff.php @@ -30,6 +30,8 @@ class ReturnTypeSniff implements Sniff { /** * Registers the tokens that this sniff wants to listen for. + * + * @return array */ public function register() { @@ -42,11 +44,9 @@ public function register() * Called when one of the token types that this sniff is listening for * is found. * - * @param \PHP_CodeSniffer\Files\File $phpcsFile The PHP_CodeSniffer file where the - * token was found. - * @param int $stackPtr The position in the PHP_CodeSniffer - * file's token stack where the token - * was found. + * @param File $phpcsFile The file where the token was found. + * @param int $stackPtr The position of the current token + * in the stack passed in $tokens. * * @return void|int Optionally returns a stack pointer. The sniff will not be * called again on the current file until the returned stack @@ -56,19 +56,37 @@ public function register() public function process(File $phpcsFile, $stackPtr) { $tokens = $phpcsFile->getTokens(); - $type = isset($tokens[$stackPtr]['scope_opener']) ? $phpcsFile->findNext(T_RETURN_TYPE, $stackPtr + 1, $tokens[$stackPtr]['scope_opener']) : false; + + $type = false; + + if (isset($tokens[$stackPtr]['scope_opener'])) { + $type = $phpcsFile->findNext( + T_RETURN_TYPE, + $stackPtr + 1, + $tokens[$stackPtr]['scope_opener'] + ); + } if (false !== $type && 'void' === strtolower($tokens[$type]['content'])) { $next = $stackPtr; do { - if (false === $next = $phpcsFile->findNext(T_RETURN, $next + 1, $tokens[$stackPtr]['scope_closer'])) { + if (false === $next = $phpcsFile->findNext( + T_RETURN, + $next + 1, + $tokens[$stackPtr]['scope_closer'] + ) + ) { break; } if (T_SEMICOLON !== $tokens[$next + 1]['code']) { + $error = 'Use return null; when a function explicitly'; + $error .= 'returns null values and use return; '; + $error .= 'when the function returns void values'; + $phpcsFile->addError( - 'Use return null; when a function explicitly returns null values and use return; when the function returns void values', + $error, $next, 'Invalid' ); diff --git a/Symfony/Sniffs/Scope/MethodScopeSniff.php b/Symfony/Sniffs/Scope/MethodScopeSniff.php index 8738645..cfa5ad2 100644 --- a/Symfony/Sniffs/Scope/MethodScopeSniff.php +++ b/Symfony/Sniffs/Scope/MethodScopeSniff.php @@ -12,7 +12,7 @@ * @link https://github.com/djoos/Symfony-coding-standard */ -namespace Symfony\Sniffs\Objects; +namespace Symfony\Sniffs\Scope; use PHP_CodeSniffer\Sniffs\AbstractScopeSniff; use PHP_CodeSniffer\Files\File; @@ -80,8 +80,11 @@ protected function processTokenWithinScope( /** * Process tokens outside scope. * - * @param File $phpcsFile - * @param int $stackPtr + * @param File $phpcsFile The file being scanned. + * @param int $stackPtr The position of the current token + * in the stack passed in $tokens. + * + * @return void */ protected function processTokenOutsideScope(File $phpcsFile, $stackPtr) { diff --git a/Symfony/Sniffs/WhiteSpace/AssignmentSpacingSniff.php b/Symfony/Sniffs/WhiteSpace/AssignmentSpacingSniff.php index 0372c33..8f1b389 100644 --- a/Symfony/Sniffs/WhiteSpace/AssignmentSpacingSniff.php +++ b/Symfony/Sniffs/WhiteSpace/AssignmentSpacingSniff.php @@ -67,7 +67,7 @@ public function process(File $phpcsFile, $stackPtr) $tokens = $phpcsFile->getTokens(); if (($tokens[$stackPtr - 1]['code'] !== T_WHITESPACE - || $tokens[$stackPtr + 1]['code'] !== T_WHITESPACE) + || $tokens[$stackPtr + 1]['code'] !== T_WHITESPACE) && $tokens[$stackPtr - 1]['content'] !== 'strict_types' ) { $phpcsFile->addError( diff --git a/Symfony/Sniffs/WhiteSpace/BinaryOperatorSpacingSniff.php b/Symfony/Sniffs/WhiteSpace/BinaryOperatorSpacingSniff.php index 747645f..8e80c04 100644 --- a/Symfony/Sniffs/WhiteSpace/BinaryOperatorSpacingSniff.php +++ b/Symfony/Sniffs/WhiteSpace/BinaryOperatorSpacingSniff.php @@ -19,7 +19,7 @@ use PHP_CodeSniffer\Util\Tokens; /** - * SBinaryOperatorSpacingSniff. + * BinaryOperatorSpacingSniff. * * Throws warnings if a binary operator isn't surrounded with whitespace. * @@ -31,7 +31,7 @@ * @license http://spdx.org/licenses/MIT MIT License * @link https://github.com/djoos/Symfony-coding-standard */ -class SBinaryOperatorSpacingSniff implements Sniff +class BinaryOperatorSpacingSniff implements Sniff { /** * A list of tokenizers this sniff supports. @@ -70,9 +70,9 @@ public function process(File $phpcsFile, $stackPtr) || $tokens[$stackPtr +1]['code'] !== T_WHITESPACE ) { $fix = $phpcsFile->addFixableError( - 'Add a single space around binary operators', - $stackPtr, - 'Invalid' + 'Add a single space around binary operators', + $stackPtr, + 'Invalid' ); if ($fix === true) { diff --git a/build.xml b/build.xml index 701813f..2a6499a 100644 --- a/build.xml +++ b/build.xml @@ -7,7 +7,6 @@ - diff --git a/tests_bootstrap.php b/tests_bootstrap.php index 4a9881f..54a1ecb 100644 --- a/tests_bootstrap.php +++ b/tests_bootstrap.php @@ -2,6 +2,8 @@ /** * Bootstrap file for PHP_CodeSniffer Symfony Coding Standard unit tests. * + * PHP version 5 + * * @category PHP * @package Symfony-coding-standard * @author Xaver Loppenstedt @@ -14,7 +16,11 @@ require_once __DIR__.'/vendor/squizlabs/php_codesniffer/tests/bootstrap.php'; // Add this Standard -PHP_CodeSniffer\Config::setConfigData('installed_paths', __DIR__.DIRECTORY_SEPARATOR.'Symfony', true); +PHP_CodeSniffer\Config::setConfigData( + 'installed_paths', + __DIR__.DIRECTORY_SEPARATOR.'Symfony', + true +); // Ignore all other Standards in tests $standards = PHP_CodeSniffer\Util\Standards::getInstalledStandards(); @@ -29,4 +35,3 @@ ); putenv("PHPCS_IGNORE_TESTS={$ignoredStandardsStr}"); -