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

PEAR/FunctionComment: bug fix - handling of blank lines in pre-amble #830

Open
wants to merge 1 commit 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
39 changes: 25 additions & 14 deletions src/Standards/PEAR/Sniffs/Commenting/FunctionCommentSniff.php
Original file line number Diff line number Diff line change
Expand Up @@ -119,33 +119,44 @@ public function process(File $phpcsFile, $stackPtr)
return;
}

// Check there are no blank lines in the preamble for the property,
// but ignore blank lines _within_ attributes as that's not the concern of this sniff.
if ($tokens[$commentEnd]['line'] !== ($tokens[$stackPtr]['line'] - 1)) {
for ($i = ($commentEnd + 1); $i < $stackPtr; $i++) {
if ($tokens[$i]['column'] !== 1) {
// Skip over the contents of attributes.
if (isset($tokens[$i]['attribute_closer']) === true) {
$i = $tokens[$i]['attribute_closer'];
continue;
}

if ($tokens[$i]['code'] === T_WHITESPACE
&& $tokens[$i]['line'] !== $tokens[($i + 1)]['line']
if ($tokens[$i]['column'] !== 1
|| $tokens[$i]['code'] !== T_WHITESPACE
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Have you considered adding a test where this condition is true and $tokens[$i]['column'] is false? As far as I can check, in the current tests whenever $tokens[$i]['code'] !== T_WHITESPACE is true, $tokens[$i]['column'] !== 1 is also true. Thus the former is never executed.

Copy link
Member Author

@jrfnl jrfnl Mar 14, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, I did not, as those are pre-existing conditions which I've not changed. I've just grouped them into one if - continue as having multiple didn't make much sense.

Writing tests for pre-existing code is outside the scope of this PR.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That is fair.

I asked because this condition was inverted. There are test cases that cover the first token of a line between the comment end and the function declaration being T_WHITESPACE, but there is no test case for it not being T_WHITESPACE.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

They are inverted because the flow has changed from "complies with condition -> throw error" to "does not comply with condition -> continue without error".

That is not a functional change. The behaviour of the sniff with regards to those conditions is unchanged - and outside the scope of this PR.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not saying the sniff couldn't use more tests, most sniffs can. Those additional tests though, do not belong with the functional change this PR is making, so should be added in a separate PR.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sounds good to me.

|| $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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Similar to the above, have you considered adding a test where isset(Tokens::$phpcsCommentTokens[$tokens[($i - 1)]['code']]) === true is true?

) {
$error = 'There must be no blank lines after the function comment';
$fix = $phpcsFile->addFixableError($error, $commentEnd, 'SpacingAfter');
continue;
}

if ($fix === true) {
$phpcsFile->fixer->beginChangeset();
$nextNonWhitespace = $phpcsFile->findNext(T_WHITESPACE, ($i + 1), null, true);
$error = 'There must be no blank lines between the function comment and the declaration';
$fix = $phpcsFile->addFixableError($error, $i, 'SpacingAfter');

while ($i < $stackPtr
&& $tokens[$i]['code'] === T_WHITESPACE
&& $tokens[$i]['line'] !== $tokens[($i + 1)]['line']
) {
$phpcsFile->fixer->replaceToken($i++, '');
if ($fix === true) {
$phpcsFile->fixer->beginChangeset();

for ($j = $i; $j < $nextNonWhitespace; $j++) {
if ($tokens[$j]['line'] === $tokens[$nextNonWhitespace]['line']) {
break;
}

$phpcsFile->fixer->endChangeset();
$phpcsFile->fixer->replaceToken($j, '');
}

break;
$phpcsFile->fixer->endChangeset();
}

$i = $nextNonWhitespace;
}//end for
}//end if

Expand Down
38 changes: 38 additions & 0 deletions src/Standards/PEAR/Tests/Commenting/FunctionCommentUnitTest.inc
Original file line number Diff line number Diff line change
Expand Up @@ -510,3 +510,41 @@ class SpacingAfter {

public function multipleLinesSomeEmpty() {}
}

class HandleBlankLinesBetweenDocblockAndDeclarationWithAttributes
{
/**
* Blank line between docblock and attribute.
*
* @return mixed
*/

#[ReturnTypeWillChange]





#[

AnotherAttribute

]#[AndAThirdAsWell]

public function blankLineDetectionA()
{

}//end blankLineDetectionA()

/**
* Blank line between attribute and function declaration.
*
* @return mixed
*/
#[ReturnTypeWillChange]

public function blankLineDetectionB()
{

}//end blankLineDetectionB()
}//end class
Original file line number Diff line number Diff line change
Expand Up @@ -489,3 +489,33 @@ class SpacingAfter {
*/
public function multipleLinesSomeEmpty() {}
}

class HandleBlankLinesBetweenDocblockAndDeclarationWithAttributes
{
/**
* Blank line between docblock and attribute.
*
* @return mixed
*/
#[ReturnTypeWillChange]
#[

AnotherAttribute

]#[AndAThirdAsWell]
public function blankLineDetectionA()
{

}//end blankLineDetectionA()

/**
* Blank line between attribute and function declaration.
*
* @return mixed
*/
#[ReturnTypeWillChange]
public function blankLineDetectionB()
{

}//end blankLineDetectionB()
}//end class
15 changes: 10 additions & 5 deletions src/Standards/PEAR/Tests/Commenting/FunctionCommentUnitTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -75,11 +75,16 @@ public function getErrorList()
364 => 1,
406 => 1,
417 => 1,
455 => 1,
464 => 1,
473 => 1,
485 => 1,
501 => 1,
456 => 1,
466 => 1,
474 => 1,
476 => 1,
486 => 1,
502 => 1,
521 => 1,
523 => 1,
533 => 1,
545 => 1,
];

}//end getErrorList()
Expand Down
Loading