diff --git a/src/Standards/Squiz/Sniffs/WhiteSpace/MemberVarSpacingSniff.php b/src/Standards/Squiz/Sniffs/WhiteSpace/MemberVarSpacingSniff.php index 96cfd7aa95..c5597012e6 100644 --- a/src/Standards/Squiz/Sniffs/WhiteSpace/MemberVarSpacingSniff.php +++ b/src/Standards/Squiz/Sniffs/WhiteSpace/MemberVarSpacingSniff.php @@ -82,60 +82,56 @@ protected function processMemberVar(File $phpcsFile, $stackPtr) break; } - if (isset(Tokens::$commentTokens[$tokens[$prev]['code']]) === true) { + if ($tokens[$prev]['code'] === T_DOC_COMMENT_CLOSE_TAG) { + $start = $prev; + } else if (isset(Tokens::$commentTokens[$tokens[$prev]['code']]) === true) { // Assume the comment belongs to the member var if it is on a line by itself. $prevContent = $phpcsFile->findPrevious(Tokens::$emptyTokens, ($prev - 1), null, true); if ($tokens[$prevContent]['line'] !== $tokens[$prev]['line']) { - // Check the spacing, but then skip it. - $foundLines = ($tokens[$startOfStatement]['line'] - $tokens[$prev]['line'] - 1); - if ($foundLines > 0) { - for ($i = ($prev + 1); $i < $startOfStatement; $i++) { - if ($tokens[$i]['column'] !== 1) { - continue; - } - - if ($tokens[$i]['code'] === T_WHITESPACE - && $tokens[$i]['line'] !== $tokens[($i + 1)]['line'] - ) { - $error = 'Expected 0 blank lines after member var comment; %s found'; - $data = [$foundLines]; - $fix = $phpcsFile->addFixableError($error, $prev, 'AfterComment', $data); - if ($fix === true) { - $phpcsFile->fixer->beginChangeset(); - // Inline comments have the newline included in the content but - // docblocks do not. - if ($tokens[$prev]['code'] === T_COMMENT) { - $phpcsFile->fixer->replaceToken($prev, rtrim($tokens[$prev]['content'])); - } - - for ($i = ($prev + 1); $i <= $startOfStatement; $i++) { - if ($tokens[$i]['line'] === $tokens[$startOfStatement]['line']) { - break; - } - - // Remove the newline after the docblock, and any entirely - // empty lines before the member var. - if (($tokens[$i]['code'] === T_WHITESPACE - && $tokens[$i]['line'] === $tokens[$prev]['line']) - || ($tokens[$i]['column'] === 1 - && $tokens[$i]['line'] !== $tokens[($i + 1)]['line']) - ) { - $phpcsFile->fixer->replaceToken($i, ''); - } - } - - $phpcsFile->fixer->addNewline($prev); - $phpcsFile->fixer->endChangeset(); - }//end if - - break; - }//end if - }//end for - }//end if - $start = $prev; - }//end if - }//end if + } + } + + // Check for blank lines between the docblock/comment and the property declaration. + for ($i = ($start + 1); $i < $startOfStatement; $i++) { + if (isset($tokens[$i]['attribute_closer']) === true) { + $i = $tokens[$i]['attribute_closer']; + continue; + } + + if ($tokens[$i]['column'] !== 1 + || $tokens[$i]['code'] !== T_WHITESPACE + || $tokens[$i]['line'] === $tokens[($i + 1)]['line'] + // Do not report blank lines after a PHPCS annotation as removing the blank lines could change the meaning. + || isset(Tokens::$phpcsCommentTokens[$tokens[($i - 1)]['code']]) === true + ) { + continue; + } + + // We found a blank line which should be reported. + $nextNonWhitespace = $phpcsFile->findNext(T_WHITESPACE, ($i + 1), null, true); + $foundLines = ($tokens[$nextNonWhitespace]['line'] - $tokens[$i]['line']); + + $error = 'Expected no blank lines between the member var comment and the declaration; %s found'; + $data = [$foundLines]; + $fix = $phpcsFile->addFixableError($error, $i, 'AfterComment', $data); + + if ($fix === true) { + $phpcsFile->fixer->beginChangeset(); + + for ($j = $i; $j < $nextNonWhitespace; $j++) { + if ($tokens[$j]['line'] === $tokens[$nextNonWhitespace]['line']) { + break; + } + + $phpcsFile->fixer->replaceToken($j, ''); + } + + $phpcsFile->fixer->endChangeset(); + } + + $i = $nextNonWhitespace; + }//end for // There needs to be n blank lines before the var, not counting comments. if ($start === $startOfStatement) { diff --git a/src/Standards/Squiz/Tests/WhiteSpace/MemberVarSpacingUnitTest.1.inc b/src/Standards/Squiz/Tests/WhiteSpace/MemberVarSpacingUnitTest.1.inc index b2e9db886e..33979c2b68 100644 --- a/src/Standards/Squiz/Tests/WhiteSpace/MemberVarSpacingUnitTest.1.inc +++ b/src/Standards/Squiz/Tests/WhiteSpace/MemberVarSpacingUnitTest.1.inc @@ -385,3 +385,33 @@ class NoPreambleMultilineDeclaration { static int $prop = 1; } + +class MultipleBlankLinesInPreAmble { + + /** + * Docblock. + */ + + #[MyAttribute] + + + #[ + + BlankLinesWithinAnAttributeShouldBeLeftAlone + + ] + + public $prop; +} + +final class BlankLinesBetweenVsAttributesWithoutCommentIssueSquiz3594 +{ + + // phpcs can fix this but not the next one + #[SingleAttribute] + + public $property1; + #[SingleAttribute] + + public $property2; +} diff --git a/src/Standards/Squiz/Tests/WhiteSpace/MemberVarSpacingUnitTest.1.inc.fixed b/src/Standards/Squiz/Tests/WhiteSpace/MemberVarSpacingUnitTest.1.inc.fixed index 86835dfc6d..f3ea2c9ca4 100644 --- a/src/Standards/Squiz/Tests/WhiteSpace/MemberVarSpacingUnitTest.1.inc.fixed +++ b/src/Standards/Squiz/Tests/WhiteSpace/MemberVarSpacingUnitTest.1.inc.fixed @@ -375,3 +375,28 @@ class NoPreambleMultilineDeclaration { static int $prop = 1; } + +class MultipleBlankLinesInPreAmble { + + /** + * Docblock. + */ + #[MyAttribute] + #[ + + BlankLinesWithinAnAttributeShouldBeLeftAlone + + ] + public $prop; +} + +final class BlankLinesBetweenVsAttributesWithoutCommentIssueSquiz3594 +{ + + // phpcs can fix this but not the next one + #[SingleAttribute] + public $property1; + + #[SingleAttribute] + public $property2; +} diff --git a/src/Standards/Squiz/Tests/WhiteSpace/MemberVarSpacingUnitTest.php b/src/Standards/Squiz/Tests/WhiteSpace/MemberVarSpacingUnitTest.php index 1031df5d2c..2ecba697aa 100644 --- a/src/Standards/Squiz/Tests/WhiteSpace/MemberVarSpacingUnitTest.php +++ b/src/Standards/Squiz/Tests/WhiteSpace/MemberVarSpacingUnitTest.php @@ -44,9 +44,9 @@ public function getErrorList($testFile='') 50 => 1, 73 => 1, 86 => 1, - 106 => 1, + 107 => 1, 115 => 1, - 150 => 1, + 151 => 1, 160 => 1, 165 => 1, 177 => 1, @@ -66,9 +66,10 @@ public function getErrorList($testFile='') 288 => 1, 292 => 1, 333 => 1, - 342 => 1, + 343 => 1, + 345 => 1, 346 => 1, - 353 => 1, + 355 => 1, 357 => 1, 366 => 1, 377 => 1, @@ -76,6 +77,12 @@ public function getErrorList($testFile='') 379 => 1, 380 => 1, 384 => 1, + 394 => 1, + 396 => 1, + 403 => 1, + 412 => 1, + 415 => 1, + 416 => 1, ]; default: