diff --git a/SymfonyCustom/Sniffs/Arrays/ArrayDeclarationSniff.php b/SymfonyCustom/Sniffs/Arrays/ArrayDeclarationSniff.php index c088e35..9511809 100644 --- a/SymfonyCustom/Sniffs/Arrays/ArrayDeclarationSniff.php +++ b/SymfonyCustom/Sniffs/Arrays/ArrayDeclarationSniff.php @@ -7,6 +7,7 @@ use PHP_CodeSniffer\Files\File; use PHP_CodeSniffer\Sniffs\Sniff; use PHP_CodeSniffer\Util\Tokens; +use SymfonyCustom\Sniffs\FixerHelper; /** * A test to ensure that arrays conform to the array coding standard. @@ -74,12 +75,7 @@ public function process(File $phpcsFile, $stackPtr): void ); if ($fix) { - $phpcsFile->fixer->beginChangeset(); - for ($i = $stackPtr + 1; $i < $arrayStart; $i++) { - $phpcsFile->fixer->replaceToken($i, ''); - } - - $phpcsFile->fixer->endChangeset(); + FixerHelper::removeAll($phpcsFile, $stackPtr + 1, $arrayStart); } } } else { @@ -100,12 +96,7 @@ public function process(File $phpcsFile, $stackPtr): void ); if ($fix) { - $phpcsFile->fixer->beginChangeset(); - for ($i = $arrayStart + 1; $i < $arrayEnd; $i++) { - $phpcsFile->fixer->replaceToken($i, ''); - } - - $phpcsFile->fixer->endChangeset(); + FixerHelper::removeAll($phpcsFile, $arrayStart + 1, $arrayEnd); } } @@ -310,11 +301,7 @@ public function processMultiLineArray(File $phpcsFile, int $stackPtr, int $start [$currentIndent, $tokens[$end]['column'] - 1] ); if ($fix) { - if (0 === $found) { - $phpcsFile->fixer->addContent($end - 1, str_repeat(' ', $expected)); - } else { - $phpcsFile->fixer->replaceToken($end - 1, str_repeat(' ', $expected)); - } + FixerHelper::fixWhitespaceBefore($phpcsFile, $end, $expected, $found); } } @@ -542,11 +529,7 @@ public function processMultiLineArray(File $phpcsFile, int $stackPtr, int $start ); if ($fix) { - if (0 === $found) { - $phpcsFile->fixer->addContent($value['value'] - 1, str_repeat(' ', $expected)); - } else { - $phpcsFile->fixer->replaceToken($value['value'] - 1, str_repeat(' ', $expected)); - } + FixerHelper::fixWhitespaceBefore($phpcsFile, $value['value'], $expected, $found); } } } @@ -651,7 +634,7 @@ public function processMultiLineArray(File $phpcsFile, int $stackPtr, int $start ); if ($fix) { - $this->align($phpcsFile, $index['index'], $expected, $found); + FixerHelper::fixWhitespaceBefore($phpcsFile, $index['index'], $expected, $found); } continue; @@ -675,7 +658,7 @@ public function processMultiLineArray(File $phpcsFile, int $stackPtr, int $start ); if ($fix) { - $this->align($phpcsFile, $index['arrow'], $expected, $found); + FixerHelper::fixWhitespaceBefore($phpcsFile, $index['arrow'], $expected, $found); } continue; @@ -698,34 +681,9 @@ public function processMultiLineArray(File $phpcsFile, int $stackPtr, int $start ); if ($fix) { - $this->align($phpcsFile, $index['value'], $expected, $found); + FixerHelper::fixWhitespaceBefore($phpcsFile, $index['value'], $expected, $found); } } } } - - /** - * @param File $phpcsFile - * @param int $elementIndex - * @param int $expected - * @param int|string $found - */ - private function align(File $phpcsFile, int $elementIndex, int $expected, $found): void - { - if ('newline' === $found) { - $phpcsFile->fixer->beginChangeset(); - - $prev = $phpcsFile->findPrevious(T_WHITESPACE, $elementIndex - 1, null, true); - for ($i = $prev + 1; $i < $elementIndex; $i++) { - $phpcsFile->fixer->replaceToken($i, ''); - } - - $phpcsFile->fixer->replaceToken($elementIndex - 1, str_repeat(' ', $expected)); - $phpcsFile->fixer->endChangeset(); - } elseif (0 === $found) { - $phpcsFile->fixer->addContent($elementIndex - 1, str_repeat(' ', $expected)); - } else { - $phpcsFile->fixer->replaceToken($elementIndex - 1, str_repeat(' ', $expected)); - } - } } diff --git a/SymfonyCustom/Sniffs/Commenting/DocCommentForbiddenTagsSniff.php b/SymfonyCustom/Sniffs/Commenting/DocCommentForbiddenTagsSniff.php index c3bd4f0..79e34dd 100644 --- a/SymfonyCustom/Sniffs/Commenting/DocCommentForbiddenTagsSniff.php +++ b/SymfonyCustom/Sniffs/Commenting/DocCommentForbiddenTagsSniff.php @@ -36,7 +36,7 @@ public function process(File $phpcsFile, $stackPtr): void $phpcsFile->addError( 'The %s annotation is forbidden to use', $stackPtr, - '', + 'Invalid', [$tokens[$stackPtr]['content']] ); } diff --git a/SymfonyCustom/Sniffs/Commenting/DocCommentGroupSameTypeSniff.php b/SymfonyCustom/Sniffs/Commenting/DocCommentGroupSameTypeSniff.php index f45996a..4f17d99 100644 --- a/SymfonyCustom/Sniffs/Commenting/DocCommentGroupSameTypeSniff.php +++ b/SymfonyCustom/Sniffs/Commenting/DocCommentGroupSameTypeSniff.php @@ -6,6 +6,7 @@ use PHP_CodeSniffer\Files\File; use PHP_CodeSniffer\Sniffs\Sniff; +use SymfonyCustom\Sniffs\FixerHelper; use SymfonyCustom\Sniffs\SniffHelper; /** @@ -70,7 +71,12 @@ public function process(File $phpcsFile, $stackPtr): void ); if ($fix) { - $this->removeLines($phpcsFile, $previousElement, $previousLine + 1, $commentTagLine - 1); + FixerHelper::removeLines( + $phpcsFile, + $previousElement, + $previousLine + 1, + $commentTagLine + ); } } } elseif ($currentIsCustom && $previousIsCustom) { @@ -82,7 +88,12 @@ public function process(File $phpcsFile, $stackPtr): void ); if ($fix) { - $this->removeLines($phpcsFile, $previousElement, $previousLine + 1, $commentTagLine - 1); + FixerHelper::removeLines( + $phpcsFile, + $previousElement, + $previousLine + 1, + $commentTagLine + ); } } } elseif (!$currentIsCustom && !$isNewType) { @@ -106,7 +117,12 @@ public function process(File $phpcsFile, $stackPtr): void $phpcsFile->fixer->addContentBefore($firstOnLine, $content.$phpcsFile->eolChar); } else { - $this->removeLines($phpcsFile, $previousElement, $previousLine + 2, $commentTagLine - 1); + FixerHelper::removeLines( + $phpcsFile, + $previousElement, + $previousLine + 2, + $commentTagLine + ); } } } @@ -119,29 +135,4 @@ public function process(File $phpcsFile, $stackPtr): void } } } - - /** - * @param File $phpcsFile - * @param int $fromPtr - * @param int $fromLine - * @param int $toLine - */ - private function removeLines(File $phpcsFile, int $fromPtr, int $fromLine, int $toLine): void - { - $tokens = $phpcsFile->getTokens(); - - $phpcsFile->fixer->beginChangeset(); - - for ($i = $fromPtr;; $i++) { - if ($tokens[$i]['line'] > $toLine) { - break; - } - - if ($fromLine <= $tokens[$i]['line']) { - $phpcsFile->fixer->replaceToken($i, ''); - } - } - - $phpcsFile->fixer->endChangeset(); - } } diff --git a/SymfonyCustom/Sniffs/Commenting/DocCommentSniff.php b/SymfonyCustom/Sniffs/Commenting/DocCommentSniff.php index ca7c114..7fa4ab6 100644 --- a/SymfonyCustom/Sniffs/Commenting/DocCommentSniff.php +++ b/SymfonyCustom/Sniffs/Commenting/DocCommentSniff.php @@ -6,6 +6,7 @@ use PHP_CodeSniffer\Files\File; use PHP_CodeSniffer\Sniffs\Sniff; +use SymfonyCustom\Sniffs\FixerHelper; /** * Ensures doc blocks follow basic formatting. @@ -38,10 +39,7 @@ public function process(File $phpcsFile, $stackPtr): void $commentEnd = $tokens[$stackPtr]['comment_closer']; - $empty = [ - T_DOC_COMMENT_WHITESPACE, - T_DOC_COMMENT_STAR, - ]; + $empty = [T_DOC_COMMENT_WHITESPACE, T_DOC_COMMENT_STAR]; $short = $phpcsFile->findNext($empty, $stackPtr + 1, $commentEnd, true); if (false === $short) { @@ -114,16 +112,7 @@ public function process(File $phpcsFile, $stackPtr): void ); if ($fix) { - $phpcsFile->fixer->beginChangeset(); - for ($i = $stackPtr + 1; $i < $short; $i++) { - if ($tokens[$i + 1]['line'] === $tokens[$short]['line']) { - break; - } - - $phpcsFile->fixer->replaceToken($i, ''); - } - - $phpcsFile->fixer->endChangeset(); + FixerHelper::removeAll($phpcsFile, $stackPtr + 1, $short); } } @@ -163,16 +152,7 @@ public function process(File $phpcsFile, $stackPtr): void ); if ($fix) { - $phpcsFile->fixer->beginChangeset(); - for ($i = $prev + 1; $i < $commentEnd; $i++) { - if ($tokens[$i + 1]['line'] === $tokens[$commentEnd]['line']) { - break; - } - - $phpcsFile->fixer->replaceToken($i, ''); - } - - $phpcsFile->fixer->endChangeset(); + FixerHelper::removeAll($phpcsFile, $prev + 1, $commentEnd); } } } diff --git a/SymfonyCustom/Sniffs/Commenting/FunctionCommentSniff.php b/SymfonyCustom/Sniffs/Commenting/FunctionCommentSniff.php index a0b2232..6181649 100644 --- a/SymfonyCustom/Sniffs/Commenting/FunctionCommentSniff.php +++ b/SymfonyCustom/Sniffs/Commenting/FunctionCommentSniff.php @@ -100,8 +100,6 @@ public function process(File $phpcsFile, $stackPtr): void } } } - - $this->processWhitespace($phpcsFile, $commentStart); } /** @@ -215,58 +213,6 @@ protected function processParams(File $phpcsFile, $stackPtr, $commentStart): voi parent::processParams($phpcsFile, $stackPtr, $commentStart); } - /** - * @param File $phpcsFile - * @param int $commentStart - * @param bool $hasComment - */ - private function processWhitespace(File $phpcsFile, int $commentStart, bool $hasComment = true): void - { - $tokens = $phpcsFile->getTokens(); - $before = $phpcsFile->findPrevious(T_WHITESPACE, $commentStart - 1, null, true); - - $startLine = $tokens[$commentStart]['line']; - $prevLine = $tokens[$before]['line']; - - $found = $startLine - $prevLine - 1; - - // Skip for class opening - if ($found < 1 && T_OPEN_CURLY_BRACKET !== $tokens[$before]['code']) { - if ($found < 0) { - $found = 0; - } - - if ($hasComment) { - $error = 'Expected 1 blank line before docblock; %s found'; - $rule = 'SpacingBeforeDocblock'; - } else { - $error = 'Expected 1 blank line before function; %s found'; - $rule = 'SpacingBeforeFunction'; - } - - $fix = $phpcsFile->addFixableError($error, $commentStart, $rule, [$found]); - - if ($fix) { - if ($found > 1) { - $phpcsFile->fixer->beginChangeset(); - - for ($i = $before + 1; $i < $commentStart - 1; $i++) { - $phpcsFile->fixer->replaceToken($i, ''); - } - - $phpcsFile->fixer->endChangeset(); - } else { - // Try and maintain indentation. - if (T_WHITESPACE === $tokens[$commentStart - 1]['code']) { - $phpcsFile->fixer->addNewlineBefore($commentStart - 1); - } else { - $phpcsFile->fixer->addNewlineBefore($commentStart); - } - } - } - } - } - /** * @param File $phpcsFile * @param int $stackPtr diff --git a/SymfonyCustom/Sniffs/Commenting/VariableCommentSniff.php b/SymfonyCustom/Sniffs/Commenting/VariableCommentSniff.php index 7dac343..d96ac83 100644 --- a/SymfonyCustom/Sniffs/Commenting/VariableCommentSniff.php +++ b/SymfonyCustom/Sniffs/Commenting/VariableCommentSniff.php @@ -110,8 +110,6 @@ public function processMemberVar(File $phpcsFile, $stackPtr): void $phpcsFile->fixer->replaceToken($string, implode(' ', $newContent)); } } - - $this->processWhitespace($phpcsFile, $commentStart); } /** @@ -129,52 +127,4 @@ protected function processVariable(File $phpcsFile, $stackPtr): void protected function processVariableInString(File $phpcsFile, $stackPtr): void { } - - /** - * @param File $phpcsFile - * @param int $commentStart - */ - private function processWhitespace(File $phpcsFile, int $commentStart): void - { - $tokens = $phpcsFile->getTokens(); - $before = $phpcsFile->findPrevious(T_WHITESPACE, $commentStart - 1, null, true); - - $startLine = $tokens[$commentStart]['line']; - $prevLine = $tokens[$before]['line']; - - $found = $startLine - $prevLine - 1; - - // Skip for class opening - if ($found < 1 && T_OPEN_CURLY_BRACKET !== $tokens[$before]['code']) { - if ($found < 0) { - $found = 0; - } - - $fix = $phpcsFile->addFixableError( - 'Expected 1 blank line before docblock; %s found', - $commentStart, - 'SpacingBeforeDocblock', - [$found] - ); - - if ($fix) { - if ($found > 1) { - $phpcsFile->fixer->beginChangeset(); - - for ($i = $before + 1; $i < $commentStart - 1; $i++) { - $phpcsFile->fixer->replaceToken($i, ''); - } - - $phpcsFile->fixer->endChangeset(); - } else { - // Try and maintain indentation. - if (T_WHITESPACE === $tokens[$commentStart - 1]['code']) { - $phpcsFile->fixer->addNewlineBefore($commentStart - 1); - } else { - $phpcsFile->fixer->addNewlineBefore($commentStart); - } - } - } - } - } } diff --git a/SymfonyCustom/Sniffs/FixerHelper.php b/SymfonyCustom/Sniffs/FixerHelper.php new file mode 100644 index 0000000..ecf8e52 --- /dev/null +++ b/SymfonyCustom/Sniffs/FixerHelper.php @@ -0,0 +1,91 @@ +getTokens(); + + $phpcsFile->fixer->beginChangeset(); + + $i = $fromPtr; + while (isset($tokens[$i]) && $i < $toPtr) { + $phpcsFile->fixer->replaceToken($i, ''); + + $i++; + } + + $phpcsFile->fixer->endChangeset(); + } + + /** + * @param File $phpcsFile + * @param int $fromPtr + * @param int $fromLine + * @param int $toLine + */ + public static function removeLines(File $phpcsFile, int $fromPtr, int $fromLine, int $toLine): void + { + $tokens = $phpcsFile->getTokens(); + + $phpcsFile->fixer->beginChangeset(); + + $i = $fromPtr; + while (isset($tokens[$i]) && $tokens[$i]['line'] < $toLine) { + if ($fromLine <= $tokens[$i]['line']) { + $phpcsFile->fixer->replaceToken($i, ''); + } + + $i++; + } + + $phpcsFile->fixer->endChangeset(); + } + + /** + * @param File $phpcsFile + * @param int $stackPtr + * @param int $expected + * @param int|string $found + */ + public static function fixWhitespaceBefore( + File $phpcsFile, + int $stackPtr, + int $expected, + $found + ): void { + $phpcsFile->fixer->beginChangeset(); + + if (0 === $found) { + $phpcsFile->fixer->addContent($stackPtr - 1, str_repeat(' ', $expected)); + } else { + if ('newline' === $found) { + $prev = $phpcsFile->findPrevious(T_WHITESPACE, $stackPtr - 1, null, true); + + for ($i = $prev + 1; $i < $stackPtr; $i++) { + $phpcsFile->fixer->replaceToken($i, ''); + } + } + + $phpcsFile->fixer->replaceToken($stackPtr - 1, str_repeat(' ', $expected)); + } + + $phpcsFile->fixer->endChangeset(); + } +} diff --git a/SymfonyCustom/Sniffs/Namespaces/AlphabeticallySortedUseSniff.php b/SymfonyCustom/Sniffs/Namespaces/AlphabeticallySortedUseSniff.php index 3348424..2db5411 100644 --- a/SymfonyCustom/Sniffs/Namespaces/AlphabeticallySortedUseSniff.php +++ b/SymfonyCustom/Sniffs/Namespaces/AlphabeticallySortedUseSniff.php @@ -62,42 +62,6 @@ public function process(File $phpcsFile, $stackPtr): int return $stackPtr + 1; } - // Check empty lines between use statements. - // There must be no empty lines between use statements. - $lineDiff = $tokens[$use['ptrUse']]['line'] - $tokens[$lastUse['ptrUse']]['line']; - if ($lineDiff > 1) { - $fix = $phpcsFile->addFixableError( - 'There must not be any empty line between use statement', - $use['ptrUse'], - 'EmptyLine' - ); - - if ($fix) { - $phpcsFile->fixer->beginChangeset(); - for ($i = $lastUse['ptrEnd'] + 1; $i < $use['ptrUse']; ++$i) { - if (false !== strpos($tokens[$i]['content'], $phpcsFile->eolChar)) { - $phpcsFile->fixer->replaceToken($i, ''); - --$lineDiff; - - if (1 === $lineDiff) { - break; - } - } - } - $phpcsFile->fixer->endChangeset(); - } - } elseif (0 === $lineDiff) { - $fix = $phpcsFile->addFixableError( - 'Each use statement must be in new line', - $use['ptrUse'], - 'TheSameLine' - ); - - if ($fix) { - $phpcsFile->fixer->addNewline($lastUse['ptrEnd']); - } - } - $lastUse = $use; } diff --git a/SymfonyCustom/Sniffs/Namespaces/NamespaceDeclarationSniff.php b/SymfonyCustom/Sniffs/Namespaces/NamespaceDeclarationSniff.php deleted file mode 100644 index 03c9a6a..0000000 --- a/SymfonyCustom/Sniffs/Namespaces/NamespaceDeclarationSniff.php +++ /dev/null @@ -1,80 +0,0 @@ -getTokens(); - - for ($i = $stackPtr - 1; $i > 0; $i--) { - if ($tokens[$i]['line'] === $tokens[$stackPtr]['line']) { - continue; - } - - break; - } - - // The $i var now points to the last token on the line before the - // namespace declaration, which must be a blank line. - $previous = $phpcsFile->findPrevious(T_WHITESPACE, $i, null, true); - if (false === $previous) { - return; - } - - $diff = ($tokens[$i]['line'] - $tokens[$previous]['line']); - if (1 === $diff) { - return; - } - - if ($diff < 0) { - $diff = 0; - } - - $fix = $phpcsFile->addFixableError( - 'There must be one blank line before the namespace declaration', - $stackPtr, - 'BlankLineBefore' - ); - - if ($fix) { - if (0 === $diff) { - $phpcsFile->fixer->addNewlineBefore($stackPtr); - } else { - $phpcsFile->fixer->beginChangeset(); - for ($x = $stackPtr - 1; $x > $previous; $x--) { - if ($tokens[$x]['line'] === $tokens[$previous]['line']) { - break; - } - - $phpcsFile->fixer->replaceToken($x, ''); - } - - $phpcsFile->fixer->addNewlineBefore($stackPtr); - $phpcsFile->fixer->endChangeset(); - } - } - } -} diff --git a/SymfonyCustom/Sniffs/SniffHelper.php b/SymfonyCustom/Sniffs/SniffHelper.php index 22d78f2..55afa59 100644 --- a/SymfonyCustom/Sniffs/SniffHelper.php +++ b/SymfonyCustom/Sniffs/SniffHelper.php @@ -8,7 +8,7 @@ use PHP_CodeSniffer\Util\Tokens; /** - * class Helper + * class SniffHelper * * @internal */ diff --git a/SymfonyCustom/Sniffs/WhiteSpace/UnaryOperatorSpacingSniff.php b/SymfonyCustom/Sniffs/WhiteSpace/UnaryOperatorSpacingSniff.php index 8b4ded6..febc82c 100644 --- a/SymfonyCustom/Sniffs/WhiteSpace/UnaryOperatorSpacingSniff.php +++ b/SymfonyCustom/Sniffs/WhiteSpace/UnaryOperatorSpacingSniff.php @@ -8,7 +8,7 @@ use PHP_CodeSniffer\Sniffs\Sniff; /** - * Ensures there are no spaces +/- sign operators or "!" boolean negators. + * Ensures there are no spaces +/- sign operators. */ class UnaryOperatorSpacingSniff implements Sniff { diff --git a/SymfonyCustom/Tests/Commenting/FunctionCommentUnitTest.inc.fixed b/SymfonyCustom/Tests/Commenting/FunctionCommentUnitTest.inc.fixed deleted file mode 100644 index 8e26488..0000000 --- a/SymfonyCustom/Tests/Commenting/FunctionCommentUnitTest.inc.fixed +++ /dev/null @@ -1,134 +0,0 @@ - 1, - 10 => 2, + 10 => 1, 43 => 1, - 48 => 2, + 48 => 1, 76 => 2, 83 => 1, - 93 => 1, - 102 => 1, 125 => 1, ]; } diff --git a/SymfonyCustom/Tests/Commenting/VariableCommentUnitTest.inc.fixed b/SymfonyCustom/Tests/Commenting/VariableCommentUnitTest.inc.fixed index cea13a4..00546ce 100644 --- a/SymfonyCustom/Tests/Commenting/VariableCommentUnitTest.inc.fixed +++ b/SymfonyCustom/Tests/Commenting/VariableCommentUnitTest.inc.fixed @@ -122,7 +122,6 @@ class VariableCommentUnitTest * */ public $emptyVarDoc = ''; - /** * @var int */ diff --git a/SymfonyCustom/Tests/Commenting/VariableCommentUnitTest.php b/SymfonyCustom/Tests/Commenting/VariableCommentUnitTest.php index af0ef67..8d73a93 100644 --- a/SymfonyCustom/Tests/Commenting/VariableCommentUnitTest.php +++ b/SymfonyCustom/Tests/Commenting/VariableCommentUnitTest.php @@ -26,7 +26,6 @@ protected function getErrorList(): array 73 => 1, 84 => 1, 123 => 1, - 125 => 1, 131 => 1, ]; } diff --git a/SymfonyCustom/Tests/Namespaces/AlphabeticallySortedUseUnitTest.php b/SymfonyCustom/Tests/Namespaces/AlphabeticallySortedUseUnitTest.php index 08ac990..89ec959 100644 --- a/SymfonyCustom/Tests/Namespaces/AlphabeticallySortedUseUnitTest.php +++ b/SymfonyCustom/Tests/Namespaces/AlphabeticallySortedUseUnitTest.php @@ -21,8 +21,6 @@ protected function getErrorList(): array return [ 7 => 1, 22 => 1, - 34 => 1, - 39 => 2, 58 => 1, ]; } diff --git a/SymfonyCustom/Tests/Namespaces/NamespaceDeclarationUnitTest.inc b/SymfonyCustom/Tests/Namespaces/NamespaceDeclarationUnitTest.inc deleted file mode 100644 index d7e095f..0000000 --- a/SymfonyCustom/Tests/Namespaces/NamespaceDeclarationUnitTest.inc +++ /dev/null @@ -1,8 +0,0 @@ - 1, - 7 => 1, - 8 => 1, - ]; - } - - /** - * @return array - */ - protected function getWarningList(): array - { - return []; - } -} diff --git a/SymfonyCustom/ruleset.xml b/SymfonyCustom/ruleset.xml index e58071a..e5b5a4c 100755 --- a/SymfonyCustom/ruleset.xml +++ b/SymfonyCustom/ruleset.xml @@ -7,12 +7,17 @@ */vendor/* - + - - + + + + + + + error @@ -53,7 +58,19 @@ + + + + + + + + + + + + @@ -109,25 +126,6 @@ - - - - - - - - - - - - - - - - 5 - - -