Skip to content

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

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

Merged

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)

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.

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]['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?

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._
@jrfnl jrfnl force-pushed the feature/pear-functioncomment-improve-blank-lines-between-check branch from f7be770 to a8371a2 Compare April 13, 2025 02:04
@jrfnl
Copy link
Member Author

jrfnl commented Apr 13, 2025

Rebased without changes to get a passing build.

@jrfnl jrfnl added this to the 3.12.2 milestone Apr 13, 2025
@jrfnl jrfnl merged commit da52592 into master Apr 13, 2025
61 checks passed
@jrfnl jrfnl deleted the feature/pear-functioncomment-improve-blank-lines-between-check branch April 13, 2025 02:42
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