From c8f1e66f718d922c4633786de3434dfb72fc9a6e Mon Sep 17 00:00:00 2001 From: Michael Moravec Date: Tue, 26 Dec 2017 20:04:02 +0100 Subject: [PATCH] Add more new sniffs --- .../Sniffs/Spacing/ControlStructureSniff.php | 117 ------------ lib/Doctrine/Sniffs/Spacing/EnsureSpaces.php | 4 +- .../Sniffs/Spacing/SpaceOnNotSniff.php | 28 --- lib/Doctrine/ruleset.xml | 171 ++++++++++++++++-- tests/expected_report.txt | 6 +- tests/fixed/not_spacing.php | 8 +- 6 files changed, 169 insertions(+), 165 deletions(-) delete mode 100644 lib/Doctrine/Sniffs/Spacing/ControlStructureSniff.php delete mode 100644 lib/Doctrine/Sniffs/Spacing/SpaceOnNotSniff.php diff --git a/lib/Doctrine/Sniffs/Spacing/ControlStructureSniff.php b/lib/Doctrine/Sniffs/Spacing/ControlStructureSniff.php deleted file mode 100644 index 4752679b..00000000 --- a/lib/Doctrine/Sniffs/Spacing/ControlStructureSniff.php +++ /dev/null @@ -1,117 +0,0 @@ -getTokens(); - - if ( ! isset($tokens[$stackPtr][self::OPENER_NAME], $tokens[$stackPtr][self::CLOSER_NAME])) { - return; - } - - $openerPosition = $tokens[$stackPtr][self::OPENER_NAME]; - $closerPosition = $tokens[$stackPtr][self::CLOSER_NAME]; - - $this->validateParenthesisOpener($phpcsFile, $tokens, $stackPtr, $openerPosition); - $this->validateParenthesisCloser($phpcsFile, $tokens, $stackPtr, $openerPosition, $closerPosition); - } - - private function validateParenthesisOpener(File $file, array $tokens, int $position, int $openerPosition) : void - { - $nextTokenPosition = $openerPosition + 1; - $nextToken = $tokens[$nextTokenPosition]; - - if ($nextToken['code'] !== T_WHITESPACE || $tokens[$nextTokenPosition + 1]['code'] === T_BOOLEAN_NOT) { - return; - } - - $spaces = $nextToken['length']; - - if (\strpos($nextToken['content'], $file->eolChar) !== false) { - $spaces = 'newline'; - } - - $file->recordMetric($position, 'Spaces after control structure open parenthesis', $spaces); - - if ($spaces === 0) { - return; - } - - if ( ! $file->addFixableError(self::OPENER_MESSAGE, $nextTokenPosition, 'AfterOpenBrace', [$spaces])) { - return; - } - - $file->fixer->replaceToken($nextTokenPosition, ''); - } - - private function validateParenthesisCloser( - File $file, - array $tokens, - int $position, - int $openerPosition, - int $closerPosition - ) : void { - if ($tokens[$openerPosition]['line'] !== $tokens[$closerPosition]['line']) { - return; - } - - $previousTokenPosition = $closerPosition - 1; - $previousToken = $tokens[$previousTokenPosition]; - $spaces = 0; - - if ($previousToken['code'] === T_WHITESPACE) { - $spaces = $previousToken['length']; - } - - $file->recordMetric($position, 'Spaces before control structure close parenthesis', $spaces); - - if ($spaces === 0) { - return; - } - - if ( ! $file->addFixableError(self::CLOSER_MESSAGE, $previousTokenPosition, 'BeforeCloseBrace', [$spaces])) { - return; - } - - $file->fixer->replaceToken($previousTokenPosition, ''); - } -} diff --git a/lib/Doctrine/Sniffs/Spacing/EnsureSpaces.php b/lib/Doctrine/Sniffs/Spacing/EnsureSpaces.php index 4fc524e2..36ec2b41 100644 --- a/lib/Doctrine/Sniffs/Spacing/EnsureSpaces.php +++ b/lib/Doctrine/Sniffs/Spacing/EnsureSpaces.php @@ -16,7 +16,7 @@ protected function ensureSpaceBefore(File $file, array $tokens, int $position, s return; } - if ( ! $file->addFixableError($message, $position, 'before', ['before', $spacing])) { + if (! $file->addFixableError($message, $position, 'before', ['before', $spacing])) { return; } @@ -36,7 +36,7 @@ protected function ensureSpaceAfter(File $file, array $tokens, int $position, st return; } - if ( ! $file->addFixableError($message, $position, 'after', ['after', $spacing])) { + if (! $file->addFixableError($message, $position, 'after', ['after', $spacing])) { return; } diff --git a/lib/Doctrine/Sniffs/Spacing/SpaceOnNotSniff.php b/lib/Doctrine/Sniffs/Spacing/SpaceOnNotSniff.php deleted file mode 100644 index 92116ac9..00000000 --- a/lib/Doctrine/Sniffs/Spacing/SpaceOnNotSniff.php +++ /dev/null @@ -1,28 +0,0 @@ -getTokens(); - - $this->ensureSpaceBefore($phpcsFile, $tokens, $stackPtr, self::MESSAGE); - $this->ensureSpaceAfter($phpcsFile, $tokens, $stackPtr, self::MESSAGE); - } -} diff --git a/lib/Doctrine/ruleset.xml b/lib/Doctrine/ruleset.xml index 9c17cde6..81286720 100644 --- a/lib/Doctrine/ruleset.xml +++ b/lib/Doctrine/ruleset.xml @@ -3,26 +3,143 @@ The Doctrine coding standard. - - - - + - - - error + + + + + + + + - + + + + + + + + - - - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + error + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + @@ -30,4 +147,36 @@ + + + + Variable "%s" not allowed in double quoted string; use sprintf() or concatenation instead + + + + + + + + + + + + + + + + + + + + + + + + + + + 5 + diff --git a/tests/expected_report.txt b/tests/expected_report.txt index 0b74045c..b4104987 100644 --- a/tests/expected_report.txt +++ b/tests/expected_report.txt @@ -3,12 +3,12 @@ PHP CODE SNIFFER REPORT SUMMARY ---------------------------------------------------------------------- FILE ERRORS WARNINGS ---------------------------------------------------------------------- -tests/input/concatenation_spacing.php 19 0 +tests/input/concatenation_spacing.php 24 0 tests/input/not_spacing.php 7 0 tests/input/return_type_on_closures.php 21 0 -tests/input/return_type_on_methods.php 16 0 +tests/input/return_type_on_methods.php 17 0 ---------------------------------------------------------------------- -A TOTAL OF 63 ERRORS AND 0 WARNINGS WERE FOUND IN 4 FILES +A TOTAL OF 69 ERRORS AND 0 WARNINGS WERE FOUND IN 4 FILES ---------------------------------------------------------------------- PHPCBF CAN FIX 63 OF THESE SNIFF VIOLATIONS AUTOMATICALLY ---------------------------------------------------------------------- diff --git a/tests/fixed/not_spacing.php b/tests/fixed/not_spacing.php index 990e44e3..4c274bed 100644 --- a/tests/fixed/not_spacing.php +++ b/tests/fixed/not_spacing.php @@ -2,18 +2,18 @@ $test = 1; -if ( ! $test > 0) { +if (! $test > 0) { echo 1; -} elseif ( ! $test === 0) { +} elseif (! $test === 0) { echo 0; } else { echo -1; } -while ( ! true) { +while (! true) { echo 1; } do { echo 1; -} while ( ! true); +} while (! true);