diff --git a/lib/Doctrine/Sniffs/Arrays/OperatorSpacingAfterSniff.php b/lib/Doctrine/Sniffs/Arrays/OperatorSpacingAfterSniff.php new file mode 100644 index 00000000..ad9ee235 --- /dev/null +++ b/lib/Doctrine/Sniffs/Arrays/OperatorSpacingAfterSniff.php @@ -0,0 +1,117 @@ +isOperator($phpcsFile, $pointer)) { + return; + } + + $tokens = $phpcsFile->getTokens(); + + $this->ensureOneSpaceAfterOperator($phpcsFile, $pointer, $tokens); + } + + /** + * @param mixed[] $tokens + */ + private function ensureOneSpaceAfterOperator(File $file, int $pointer, array $tokens) : void + { + if (! $this->shouldValidateAfter($pointer, $tokens)) { + return; + } + + $numberOfSpaces = $this->numberOfSpaces($tokens[$pointer + 1]); + + if ($numberOfSpaces === 1 || ! $this->recordErrorAfter($file, $pointer, $tokens[$pointer], $numberOfSpaces)) { + return; + } + + if ($numberOfSpaces === 0) { + $file->fixer->addContent($pointer, ' '); + return; + } + + $file->fixer->replaceToken($pointer + 1, ' '); + } + + /** + * @param mixed[] $tokens + */ + private function shouldValidateAfter(int $pointer, array $tokens) : bool + { + if (! isset($tokens[$pointer + 1])) { + return false; + } + + return $tokens[$pointer]['code'] !== T_INLINE_THEN || $tokens[$pointer + 1]['code'] !== T_INLINE_ELSE; + } + + /** + * @param mixed[] $token + */ + private function recordErrorAfter(File $file, int $pointer, array $token, int $numberOfSpaces) : bool + { + return $file->addFixableError( + self::MESSAGE_AFTER, + $pointer, + 'NoSpaceAfter', + [$token['content'], $numberOfSpaces] + ); + } + + /** + * @param mixed[] $token + */ + private function numberOfSpaces(array $token) : int + { + if ($token['code'] !== T_WHITESPACE) { + return 0; + } + + return strlen($token['content']); + } +} diff --git a/tests/fixed/array_indentation.php b/tests/fixed/array_indentation.php index 626a7c37..dc966059 100644 --- a/tests/fixed/array_indentation.php +++ b/tests/fixed/array_indentation.php @@ -22,3 +22,12 @@ 9, ], ]; + +$arrayWithMultipleSpacesAfterAssigmentOperator = [ + 0, + 1, + 2, + 3, + 4, + 5, +]; diff --git a/tests/input/array_indentation.php b/tests/input/array_indentation.php index fa6e10ab..8a9bfedc 100644 --- a/tests/input/array_indentation.php +++ b/tests/input/array_indentation.php @@ -22,3 +22,12 @@ 9, ], ]; + +$arrayWithMultipleSpacesAfterAssigmentOperator = [ + 0, + 1, + 2, + 3, + 4, + 5, +];