From c4bf9cad66da885cc843cc24d708661d9d8fbb95 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jaroslav=20Hansl=C3=ADk?= Date: Sat, 28 Mar 2020 23:04:31 +0100 Subject: [PATCH] DuplicateSpacesSniff: Improved detection --- .../Sniffs/Whitespaces/DuplicateSpacesSniff.php | 7 ++++++- .../Sniffs/Whitespaces/DuplicateSpacesSniffTest.php | 13 +++++++++++++ ...teSpacesIgnoreSpacesInAnnotationErrors.fixed.php | 6 ++++++ ...uplicateSpacesIgnoreSpacesInAnnotationErrors.php | 6 ++++++ 4 files changed, 31 insertions(+), 1 deletion(-) create mode 100644 tests/Sniffs/Whitespaces/data/duplicateSpacesIgnoreSpacesInAnnotationErrors.fixed.php create mode 100644 tests/Sniffs/Whitespaces/data/duplicateSpacesIgnoreSpacesInAnnotationErrors.php diff --git a/SlevomatCodingStandard/Sniffs/Whitespaces/DuplicateSpacesSniff.php b/SlevomatCodingStandard/Sniffs/Whitespaces/DuplicateSpacesSniff.php index fcbb50c57..a5dc83985 100644 --- a/SlevomatCodingStandard/Sniffs/Whitespaces/DuplicateSpacesSniff.php +++ b/SlevomatCodingStandard/Sniffs/Whitespaces/DuplicateSpacesSniff.php @@ -15,6 +15,7 @@ use function str_replace; use function strlen; use const PREG_OFFSET_CAPTURE; +use const T_DOC_COMMENT_CLOSE_TAG; use const T_DOC_COMMENT_OPEN_TAG; use const T_DOC_COMMENT_STAR; use const T_DOC_COMMENT_STRING; @@ -81,7 +82,11 @@ public function process(File $phpcsFile, $whitespacePointer): void if ($this->ignoreSpacesInAnnotation) { $pointerBefore = TokenHelper::findPrevious($phpcsFile, [T_DOC_COMMENT_OPEN_TAG, T_DOC_COMMENT_TAG], $whitespacePointer - 1); - if ($pointerBefore !== null && $tokens[$pointerBefore]['code'] === T_DOC_COMMENT_TAG) { + if ( + $pointerBefore !== null + && $tokens[$pointerBefore]['code'] === T_DOC_COMMENT_TAG + && $tokens[$whitespacePointer + 1]['code'] !== T_DOC_COMMENT_CLOSE_TAG + ) { return; } } diff --git a/tests/Sniffs/Whitespaces/DuplicateSpacesSniffTest.php b/tests/Sniffs/Whitespaces/DuplicateSpacesSniffTest.php index 31b3bb61a..fc1c35b4f 100644 --- a/tests/Sniffs/Whitespaces/DuplicateSpacesSniffTest.php +++ b/tests/Sniffs/Whitespaces/DuplicateSpacesSniffTest.php @@ -48,4 +48,17 @@ public function testIgnoreSpacesInAnnotationNoErrors(): void self::assertNoSniffErrorInFile($report); } + public function testIgnoreSpacesInAnnotationErrors(): void + { + $report = self::checkFile(__DIR__ . '/data/duplicateSpacesIgnoreSpacesInAnnotationErrors.php', [ + 'ignoreSpacesInAnnotation' => true, + ]); + + self::assertSame(1, $report->getErrorCount()); + + self::assertSniffError($report, 3, DuplicateSpacesSniff::CODE_DUPLICATE_SPACES, 'Duplicate spaces at position 21.'); + + self::assertAllFixedInFile($report); + } + } diff --git a/tests/Sniffs/Whitespaces/data/duplicateSpacesIgnoreSpacesInAnnotationErrors.fixed.php b/tests/Sniffs/Whitespaces/data/duplicateSpacesIgnoreSpacesInAnnotationErrors.fixed.php new file mode 100644 index 000000000..6c7b8f8b9 --- /dev/null +++ b/tests/Sniffs/Whitespaces/data/duplicateSpacesIgnoreSpacesInAnnotationErrors.fixed.php @@ -0,0 +1,6 @@ +