From ea3ff0005e78dacb38399be1c884bbbc0c0399db Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jaroslav=20Hansl=C3=ADk?= Date: Sat, 9 Mar 2024 13:27:04 +0100 Subject: [PATCH] Fixed internal error in CommentHelper --- .../Helpers/CommentHelper.php | 24 +++++++++++++------ .../Commenting/data/emptyCommentNoErrors.php | 3 +++ 2 files changed, 20 insertions(+), 7 deletions(-) diff --git a/SlevomatCodingStandard/Helpers/CommentHelper.php b/SlevomatCodingStandard/Helpers/CommentHelper.php index 4dd226fdb..3b8cb9236 100644 --- a/SlevomatCodingStandard/Helpers/CommentHelper.php +++ b/SlevomatCodingStandard/Helpers/CommentHelper.php @@ -5,7 +5,7 @@ use PHP_CodeSniffer\Files\File; use PHP_CodeSniffer\Util\Tokens; use function array_key_exists; -use function array_merge; +use function in_array; use function preg_match; use function strpos; use const T_COMMENT; @@ -38,13 +38,23 @@ public static function getCommentEndPointer(File $phpcsFile, int $commentStartPo return null; } - $nextPointerAfterComment = TokenHelper::findNextExcluding( - $phpcsFile, - array_merge([T_COMMENT], Tokens::$phpcsCommentTokens), - $commentStartPointer + 1 - ); + $commentEndPointer = $commentStartPointer; + + for ($i = $commentStartPointer + 1; $i < $phpcsFile->numTokens; $i++) { + if ($tokens[$i]['code'] === T_COMMENT) { + $commentEndPointer = $i; + continue; + } + + if (in_array($tokens[$i]['code'], Tokens::$phpcsCommentTokens, true)) { + $commentEndPointer = $i; + continue; + } + + break; + } - return $nextPointerAfterComment - 1; + return $commentEndPointer; } public static function getMultilineCommentStartPointer(File $phpcsFile, int $commentEndPointer): int diff --git a/tests/Sniffs/Commenting/data/emptyCommentNoErrors.php b/tests/Sniffs/Commenting/data/emptyCommentNoErrors.php index 8c8596eb2..c83d15d97 100644 --- a/tests/Sniffs/Commenting/data/emptyCommentNoErrors.php +++ b/tests/Sniffs/Commenting/data/emptyCommentNoErrors.php @@ -41,3 +41,6 @@ $some = 'sample_code'; // phpcs:enable */ + +/* not empty +multiline comment */ \ No newline at end of file