Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Squiz/MemberVarSpacing: bug fix - handling of blank lines in pre-amble #840

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
96 changes: 46 additions & 50 deletions src/Standards/Squiz/Sniffs/WhiteSpace/MemberVarSpacingSniff.php
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
15 changes: 11 additions & 4 deletions src/Standards/Squiz/Tests/WhiteSpace/MemberVarSpacingUnitTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -66,16 +66,23 @@ 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,
378 => 1,
379 => 1,
380 => 1,
384 => 1,
394 => 1,
396 => 1,
403 => 1,
412 => 1,
415 => 1,
416 => 1,
];

default:
Expand Down