From fd04cdd138cf2800cdb951bd5b7c8d3118fa5647 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mantas=20Jonu=C5=A1as?= Date: Mon, 20 Apr 2015 14:23:36 +0300 Subject: [PATCH 1/3] Fix for not operator --- Sniffs/WhiteSpace/OperatorSpacingSniff.php | 6 ++++++ Tests/Unit/WhiteSpace/OperatorSpacingSniffTest.php | 1 + .../Unit/WhiteSpace/OperatorSpacingSniffTest.phptest | 12 ++++++++++++ 3 files changed, 19 insertions(+) diff --git a/Sniffs/WhiteSpace/OperatorSpacingSniff.php b/Sniffs/WhiteSpace/OperatorSpacingSniff.php index ff7de32..ee5feb8 100644 --- a/Sniffs/WhiteSpace/OperatorSpacingSniff.php +++ b/Sniffs/WhiteSpace/OperatorSpacingSniff.php @@ -90,6 +90,12 @@ public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr) return; } + //Ongr. If it's correct usage of bool operator skip it. + if ($tokens[$stackPtr]['code'] === T_BOOLEAN_NOT) { + // Skip for '!' case. + return; + } + // Skip default values in function declarations. if ($tokens[$stackPtr]['code'] === T_EQUAL || $tokens[$stackPtr]['code'] === T_MINUS diff --git a/Tests/Unit/WhiteSpace/OperatorSpacingSniffTest.php b/Tests/Unit/WhiteSpace/OperatorSpacingSniffTest.php index dbe0ed9..4459a2e 100644 --- a/Tests/Unit/WhiteSpace/OperatorSpacingSniffTest.php +++ b/Tests/Unit/WhiteSpace/OperatorSpacingSniffTest.php @@ -22,6 +22,7 @@ protected function getErrorList() { return [ 12 => 1, + 16 => 1, ]; } diff --git a/Tests/Unit/WhiteSpace/OperatorSpacingSniffTest.phptest b/Tests/Unit/WhiteSpace/OperatorSpacingSniffTest.phptest index f71b15b..a71f25c 100644 --- a/Tests/Unit/WhiteSpace/OperatorSpacingSniffTest.phptest +++ b/Tests/Unit/WhiteSpace/OperatorSpacingSniffTest.phptest @@ -12,3 +12,15 @@ if (! $foo) { true; } + +if (! false) { + true; +} + +if (!false) { + true; +} + +if (!$foo) { + true; +} From 4e272745f8ef1c3ff7b28a4efb89d62bc2920b97 Mon Sep 17 00:00:00 2001 From: Mantas Simkus Date: Tue, 21 Apr 2015 10:38:12 +0300 Subject: [PATCH 2/3] fixed @deprecated --- Sniffs/Commenting/FunctionCommentSniff.php | 71 ++++++++++++++++--- .../Commenting/FunctionCommentSniffTest.php | 4 +- .../FunctionCommentSniffTest.phptest | 36 ++++++++++ 3 files changed, 102 insertions(+), 9 deletions(-) diff --git a/Sniffs/Commenting/FunctionCommentSniff.php b/Sniffs/Commenting/FunctionCommentSniff.php index 14da26f..772670e 100644 --- a/Sniffs/Commenting/FunctionCommentSniff.php +++ b/Sniffs/Commenting/FunctionCommentSniff.php @@ -102,6 +102,7 @@ public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr) } $this->processReturn($phpcsFile, $stackPtr, $commentStart); $this->processThrows($phpcsFile, $stackPtr, $commentStart); + $this->processDeprecated($phpcsFile, $stackPtr, $commentStart); $this->processParams($phpcsFile, $stackPtr, $commentStart); $this->processComments($phpcsFile, $stackPtr, $commentStart); @@ -299,9 +300,9 @@ protected function processReturn(PHP_CodeSniffer_File $phpcsFile, $stackPtr, $co }//end if $comment = null; - if ($tokens[($tag + 2)]['code'] === T_DOC_COMMENT_STRING) { + if ($tokens[($return + 2)]['code'] === T_DOC_COMMENT_STRING) { $matches = array(); - preg_match('/([^\s]+)(?:\s+(.*))?/', $tokens[($tag + 2)]['content'], $matches); + preg_match('/([^\s]+)(?:\s+(.*))?/', $tokens[($return + 2)]['content'], $matches); if (isset($matches[2]) === true) { $comment = $matches[2]; } @@ -312,26 +313,26 @@ protected function processReturn(PHP_CodeSniffer_File $phpcsFile, $stackPtr, $co $firstChar = $comment{0}; if (preg_match('|\p{Lu}|u', $firstChar) === 0) { $error = 'Return comment must start with a capital letter'; - $fix = $phpcsFile->addFixableError($error, ($tag + 2), 'ReturnCommentNotCapital'); + $fix = $phpcsFile->addFixableError($error, ($return + 2), 'ReturnCommentNotCapital'); if ($fix === true) { $newComment = ucfirst($comment); - $tokenLength = strlen($tokens[($tag + 2)]['content']); + $tokenLength = strlen($tokens[($return + 2)]['content']); $commentLength = strlen($comment); $tokenWithoutComment - = substr($tokens[($tag + 2)]['content'], 0, $tokenLength - $commentLength); + = substr($tokens[($return + 2)]['content'], 0, $tokenLength - $commentLength); - $phpcsFile->fixer->replaceToken(($tag + 2), $tokenWithoutComment . $newComment); + $phpcsFile->fixer->replaceToken(($return + 2), $tokenWithoutComment . $newComment); } } $lastChar = substr($comment, -1); if ($lastChar !== '.') { $error = 'Return comment must end with a full stop'; - $fix = $phpcsFile->addFixableError($error, ($tag + 2), 'ReturnCommentFullStop'); + $fix = $phpcsFile->addFixableError($error, ($return + 2), 'ReturnCommentFullStop'); if ($fix === true) { - $phpcsFile->fixer->addContent(($tag + 2), '.'); + $phpcsFile->fixer->addContent(($return + 2), '.'); } } } @@ -414,6 +415,60 @@ protected function processThrows(PHP_CodeSniffer_File $phpcsFile, $stackPtr, $co }//end processThrows() + /** + * Process any deprecated tags that this function comment has. + * + * @param PHP_CodeSniffer_File $phpcsFile The file being scanned. + * @param int $stackPtr The position of the current token + * in the stack passed in $tokens. + * @param int $commentStart The position in the stack where the comment started. + * + * @return void + */ + protected function processDeprecated(PHP_CodeSniffer_File $phpcsFile, $stackPtr, $commentStart) + { + $tokens = $phpcsFile->getTokens(); + foreach ($tokens[$commentStart]['comment_tags'] as $pos => $tag) { + if ($tokens[$tag]['content'] !== '@deprecated') { + continue; + } + + $content = $tokens[($tag + 2)]['content']; + if (empty($content) === true || $tokens[($tag + 2)]['code'] !== T_DOC_COMMENT_STRING) { + $error = 'Deprecated type missing for @deprecated tag in function comment'; + $phpcsFile->addError($error, $tag, 'MissingDeprecatedType'); + } else { + //ONGR Validate that return comment begins with capital letter and ends with full stop. + if ($content !== null) { + $firstChar = $content{0}; + if (preg_match('|\p{Lu}|u', $firstChar) === 0) { + $error = 'Deprecated comment must start with a capital letter'; + $fix = $phpcsFile->addFixableError($error, ($tag + 2), 'DeprecatedCommentNotCapital'); + + if ($fix === true) { + $newComment = ucfirst($content); + $tokenLength = strlen($tokens[($tag + 2)]['content']); + $commentLength = strlen($content); + $tokenWithoutComment + = substr($tokens[($tag + 2)]['content'], 0, $tokenLength - $commentLength); + + $phpcsFile->fixer->replaceToken(($tag + 2), $tokenWithoutComment . $newComment); + } + } + + $lastChar = substr($content, -1); + if ($lastChar !== '.') { + $error = 'Deprecated comment must end with a full stop'; + $fix = $phpcsFile->addFixableError($error, ($tag + 2), 'DeprecatedCommentFullStop'); + + if ($fix === true) { + $phpcsFile->fixer->addContent(($tag + 2), '.'); + } + } + } + } + } + } /** * Process the function parameter comments. diff --git a/Tests/Unit/Commenting/FunctionCommentSniffTest.php b/Tests/Unit/Commenting/FunctionCommentSniffTest.php index c204ded..adbcd0e 100644 --- a/Tests/Unit/Commenting/FunctionCommentSniffTest.php +++ b/Tests/Unit/Commenting/FunctionCommentSniffTest.php @@ -35,7 +35,9 @@ protected function getErrorList() 89 => 1, 99 => 1, 109 => 2, - 117 => 1 + 117 => 1, + 129 => 1, + 153 => 2 ]; } diff --git a/Tests/Unit/Commenting/FunctionCommentSniffTest.phptest b/Tests/Unit/Commenting/FunctionCommentSniffTest.phptest index fd57f0b..a8c2eac 100644 --- a/Tests/Unit/Commenting/FunctionCommentSniffTest.phptest +++ b/Tests/Unit/Commenting/FunctionCommentSniffTest.phptest @@ -120,4 +120,40 @@ class FunctionCommentSniffTest { return foo($bar); } + + /** + * Mapping is compared with loaded, if needed updates it and returns true. + * + * @param array $types Types to update. + * + * @return bool Foo + * + * @throws \LogicException + * + * @deprecated Will be removed in 1.0. Please now use Bar#foo(). + */ + public function foo(array $types = []) + { + return $this->bar($types); + } + + /** + * Short description. + * + * @deprecated Will be removed in 1.0. Please now use Bar#foo(). + */ + public function updateMapping() + { + return $this->updateTypes(); + } + + /** + * Short description. + * + * @deprecated will be removed in 1.0. Please now use Connection#updateTypes() + */ + public function updateMapping() + { + return $this->updateTypes(); + } } From 5fd551ffdae23d5b0baf43323e3c087b11308087 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mantas=20Jonu=C5=A1as?= Date: Tue, 21 Apr 2015 16:15:47 +0300 Subject: [PATCH 3/3] Removed the posibility to split string in array --- Sniffs/Arrays/ArrayDeclarationSniff.php | 114 +++++++----------- .../Unit/Arrays/ArrayDeclarationSniffTest.php | 1 - .../Arrays/ArrayDeclarationSniffTest.phptest | 9 -- 3 files changed, 41 insertions(+), 83 deletions(-) diff --git a/Sniffs/Arrays/ArrayDeclarationSniff.php b/Sniffs/Arrays/ArrayDeclarationSniff.php index b7f3bcc..a7fa367 100644 --- a/Sniffs/Arrays/ArrayDeclarationSniff.php +++ b/Sniffs/Arrays/ArrayDeclarationSniff.php @@ -544,9 +544,9 @@ public function processMultiLineArray(PHP_CodeSniffer_File $phpcsFile, $stackPtr // } // // if ($singleValue === true) { - // Array cannot be empty, so this is a multi-line array with - // a single value. It should be defined on single line. - $error = 'Multi-line array contains a single value; use single-line array instead'; + // Array cannot be empty, so this is a multi-line array with + // a single value. It should be defined on single line. + $error = 'Multi-line array contains a single value; use single-line array instead'; // $fix = $phpcsFile->addFixableError($error, $stackPtr, 'MultiLineNotAllowed'); // // if ($fix === true) { @@ -635,7 +635,7 @@ public function processMultiLineArray(PHP_CodeSniffer_File $phpcsFile, $stackPtr // $phpcsFile->fixer->addNewlineBefore($value['value']); // } // } else - if ($tokens[($value['value'] - 1)]['code'] === T_WHITESPACE) { + if ($tokens[($value['value'] - 1)]['code'] === T_WHITESPACE) { $expected = $statementStart + 4; $first = $phpcsFile->findFirstOnLine(T_WHITESPACE, $value['value'], true); @@ -812,89 +812,57 @@ public function processMultiLineArray(PHP_CodeSniffer_File $phpcsFile, $stackPtr // } // }//end if //ongr end - // Check each line ends in a comma or dot (string concat). + // Check each line ends in a comma. // if ($tokens[$index['value']]['code'] !== T_ARRAY // && $tokens[$index['value']]['code'] !== T_OPEN_SHORT_ARRAY // ) { - $valueLine = $tokens[$index['value']]['line']; - $nextComma = false; - $nextDot = false; - for ($i = ($index['value'] + 1); $i < $arrayEnd; $i++) { - // Skip bracketed statements, like function calls. - if ($tokens[$i]['code'] === T_OPEN_PARENTHESIS) { - $i = $tokens[$i]['parenthesis_closer']; - $valueLine = $tokens[$i]['line']; - continue; - } + $valueLine = $tokens[$index['value']]['line']; + $nextComma = false; + for ($i = ($index['value'] + 1); $i < $arrayEnd; $i++) { + // Skip bracketed statements, like function calls. + if ($tokens[$i]['code'] === T_OPEN_PARENTHESIS) { + $i = $tokens[$i]['parenthesis_closer']; + $valueLine = $tokens[$i]['line']; + continue; + } - if ($tokens[$i]['code'] === T_COMMA) { - $nextComma = $i; - break; - } - else if($tokens[$i]['code'] === T_STRING_CONCAT) { - $nextDot = $i; - break; - } + if ($tokens[$i]['code'] === T_COMMA) { + $nextComma = $i; + break; } + } - if (($nextComma === false && $nextDot === false) - || ($tokens[$nextComma]['line'] !== $valueLine && $tokens[$nextDot]['line'] !== $valueLine) - && $tokens[$index['value']]['code'] !== T_OPEN_SHORT_ARRAY - ) { - $error = 'Each line in an array declaration must end in a comma'; - $fix = $phpcsFile->addFixableError($error, $index['value'], 'NoComma'); + if (($nextComma === false) || ($tokens[$nextComma]['line'] !== $valueLine) && $tokens[$index['value']]['code'] !== T_OPEN_SHORT_ARRAY) { + $error = 'Each line in an array declaration must end in a comma'; + $fix = $phpcsFile->addFixableError($error, $index['value'], 'NoComma'); - if ($fix === true) { - // Find the end of the line and put a comma there. - for ($i = ($index['value'] + 1); $i < $phpcsFile->numTokens; $i++) { - if ($tokens[$i]['line'] > $tokens[$index['value']]['line']) { - break; - } + if ($fix === true) { + // Find the end of the line and put a comma there. + for ($i = ($index['value'] + 1); $i < $phpcsFile->numTokens; $i++) { + if ($tokens[$i]['line'] > $tokens[$index['value']]['line']) { + break; } - - $phpcsFile->fixer->addContentBefore(($i - 1), ','); } - } - - // Check that there is no space before the comma. - if ($nextComma !== false && $tokens[($nextComma - 1)]['code'] === T_WHITESPACE) { - $content = $tokens[($nextComma - 2)]['content']; - $spaceLength = $tokens[($nextComma - 1)]['length']; - $error = 'Expected 0 spaces between "%s" and comma; %s found'; - $data = array( - $content, - $spaceLength, - ); - $fix = $phpcsFile->addFixableError($error, $nextComma, 'SpaceBeforeComma', $data); - if ($fix === true) { - $phpcsFile->fixer->replaceToken(($nextComma - 1), ''); - } + $phpcsFile->fixer->addContentBefore(($i - 1), ','); } + } - if ($nextDot !== false) { - $spacesExpected = $this->getStatementStartColumn($phpcsFile, $nextDot) + 3; - - $indentation = $nextDot; - while($tokens[$nextDot]['line'] == $tokens[$indentation]['line']) { - $indentation++; - } - - $spacesFound = 0; - if ($tokens[$indentation]['code'] == T_WHITESPACE) { - $spacesFound = $tokens[$indentation]['length']; - } - - if ($spacesExpected != $spacesFound) { - $error = 'Array value not aligned correctly; expected %s spaces but found %s'; - $data = array( - $spacesExpected, - $spacesFound, - ); + // Check that there is no space before the comma. + if ($nextComma !== false && $tokens[($nextComma - 1)]['code'] === T_WHITESPACE) { + $content = $tokens[($nextComma - 2)]['content']; + $spaceLength = $tokens[($nextComma - 1)]['length']; + $error = 'Expected 0 spaces between "%s" and comma; %s found'; + $data = array( + $content, + $spaceLength, + ); - $phpcsFile->addError($error, $indentation, 'ValueNotAligned', $data); - } + $fix = $phpcsFile->addFixableError($error, $nextComma, 'SpaceBeforeComma', $data); + if ($fix === true) { + $phpcsFile->fixer->replaceToken(($nextComma - 1), ''); } + } // }//end if }//end foreach diff --git a/Tests/Unit/Arrays/ArrayDeclarationSniffTest.php b/Tests/Unit/Arrays/ArrayDeclarationSniffTest.php index 69e0b17..fcf74d8 100644 --- a/Tests/Unit/Arrays/ArrayDeclarationSniffTest.php +++ b/Tests/Unit/Arrays/ArrayDeclarationSniffTest.php @@ -60,7 +60,6 @@ protected function getErrorList() 135 => 1, 137 => 1, 148 => 1, - 153 => 1, ]; } diff --git a/Tests/Unit/Arrays/ArrayDeclarationSniffTest.phptest b/Tests/Unit/Arrays/ArrayDeclarationSniffTest.phptest index 78dd84f..e527053 100644 --- a/Tests/Unit/Arrays/ArrayDeclarationSniffTest.phptest +++ b/Tests/Unit/Arrays/ArrayDeclarationSniffTest.phptest @@ -148,12 +148,3 @@ $data = [ 2 => test(2, 3) , ]; -$data = [ - 0 => 'foo' . - 'bar', -]; - -$data = [ - 0 => 'foo' . - 'bar', -];