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

Conversation

jrfnl
Copy link
Member

@jrfnl jrfnl commented Feb 18, 2025

Description

The PEAR.Commenting.FunctionComment sniff intends to flag blank lines between a function docblock and the function declaration.

However, as things are, there are - IMO - two bugs in the logic for this:

Given a code block which looks like this:

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

    #[ReturnTypeWillChange]

    #[

        AnotherAttribute

    ]#[AndAThirdAsWell]

    public function blankLineDetectionA()
    {

    }//end blankLineDetectionA()
}

There will be only one error and it will read:

[x] There must be no blank lines after the function comment (PEAR.Commenting.FunctionComment.SpacingAfter)

This is confusing as the blank line may not be after the function comment, but after an attribute instead.

Additionally, the sniff also flags blank lines within attributes, while that is outside of the purview of the sniff. (Should be handled by an attribute specific sniff)

What I would expect, would be for the sniff to:
a) Throw a separate error for each (set of) blank lines found.
b) For the error message to more accurately reflect what is being flagged.

Note: while in PHPCS this gets confusing, the fixer already fixes this correctly.

This commit changes the SpacingAfter error to comply with the above expectations

Includes test, though there are also some pre-existing tests which show this issue and for which the behaviour is changed.

Note: while it will still be messy, it may be easier to review this PR while ignoring whitespace changes.

Suggested changelog entry

PEAR.Commenting.FunctionComment: improved message for "blank lines between docblock and declaration" check.
PEAR.Commenting.FunctionComment will no longer remove blank lines within attributes.

Related issues/external references

Loosely related to #152

Types of changes

  • Bug fix (non-breaking change which fixes an issue)

The `PEAR.Commenting.FunctionComment` sniff intends to flag blank lines between a function docblock and the function declaration.

However, as things are, there are - IMO - two bugs in the logic for this:

Given a code block which looks like this:
```php
class HandleBlankLinesBetweenDocblockAndDeclarationWithAttributes
{
    /**
     * Blank line between docblock and attribute.
     *
     * @return mixed
     */

    #[ReturnTypeWillChange]

    #[

        AnotherAttribute

    ]#[AndAThirdAsWell]

    public function blankLineDetectionA()
    {

    }//end blankLineDetectionA()
}
```

There will be only one error and it will read:
```
[x] There must be no blank lines after the function comment (PEAR.Commenting.FunctionComment.SpacingAfter)
```

This is confusing as the blank line may not be after the function comment, but after an attribute instead.

Additionally, the sniff also flags blank lines _within_ attributes, while that is outside of the purview of the sniff. (Should be handled by an attribute specific sniff)

What I would expect would be for the sniff to:
a) Throw a separate error for each (set of) blank lines found.
b) For the error message to more accurately reflect what is being flagged.

> Note: while in PHPCS this gets confusing, the fixer already fixes this correctly.

This commit changes the `SpacingAfter` error to comply with the above expectations

Includes test, though there are also some pre-existing tests which show this issue and for which the behaviour is changed.

_Note: while it will still be messy, it may be easier to review this PR while ignoring whitespace changes._
Copy link
Contributor

@rodrigoprimo rodrigoprimo left a comment

Choose a reason for hiding this comment

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

Looks good to me. I just left two questions about potentially adding two more tests.

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.

|| $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
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?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants