Skip to content

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

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 21, 2025

Description

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

The Squiz.WhiteSpace.MemberVarSpacing sniff intends to flag blank lines between a property docblock and the property declaration.

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

Given a code block which looks like this:

class MultipleBlankLinesInPreAmble {

    /**
     * Docblock.
     */

    #[MyAttribute]

    #[MyOtherAttribute]

    public $prop;
}

There will be only one error and it will read:

[x] Expected 0 blank lines after member var comment; 6 found (Squiz.WhiteSpace.MemberVarSpacing.AfterComment)

This is confusing as there are not 6 blank lines between the end of the docblock and the property, but four blank lines and two attribute lines.

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 correctly reflect the number of blank lines found for each error.

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

This commit changes the AfterComment 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 (error lines: 342 and 353).

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

Fixes squizlabs/PHP_CodeSniffer#3594

Squiz/MemberVarSpacing: bug fix - don't remove blank attribute lines

While blank lines between the docblock and the property declaration should be removed, blank lines within an attribute should be ignored as that's for a sniff dealing with attribute spacing to deal with and is outside of the scope of this sniff.

Fixed now.

Includes updated test.

Suggested changelog entry

Squiz.WhiteSpace.MemberVarSpacing: more accurate reporting on blank lines in the property "pre-amble" (i.e. docblock, attributes).

Related issues/external references

Note: this PR is part of a series of PRs to fix various issues in this sniff...

Related to #152

Types of changes

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

The `Squiz.WhiteSpace.MemberVarSpacing` sniff intends to flag blank lines between a property docblock and the property 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 MultipleBlankLinesInPreAmble {

    /**
     * Docblock.
     */

    #[MyAttribute]

    #[MyOtherAttribute]

    public $prop;
}
```

There will be only one error and it will read:
```
[x] Expected 0 blank lines after member var comment; 6 found (Squiz.WhiteSpace.MemberVarSpacing.AfterComment)
```

This is confusing as there are not 6 blank lines between the end of the docblock and the property, but four blank lines and two attribute lines.

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 correctly reflect the number of blank lines found for each error.

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

This commit changes the `AfterComment` 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 (error lines: 342 and 353).

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

Fixes squizlabs/PHP_CodeSniffer 3594
While blank lines between the docblock and the property declaration should be removed, blank lines _within_ an attribute should be ignored as that's for a sniff dealing with attribute spacing to deal with and is outside of the scope of this sniff.

Fixed now.

Includes updated test.
Comment on lines 391 to 398

// phpcs can fix this but not the next one
#[SingleAttribute]
public $property1;

#[SingleAttribute]
public $property2;
}
Copy link
Member

Choose a reason for hiding this comment

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

What hasn't been fixed for the second property here? Does the comment need to be updated?

Copy link
Member Author

Choose a reason for hiding this comment

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

See: squizlabs/PHP_CodeSniffer#3594

I've updated the comment now. Is this better ?

@jrfnl jrfnl added this to the 3.12.0 milestone Mar 8, 2025
@jrfnl jrfnl merged commit 384f8e8 into master Mar 15, 2025
58 checks passed
@jrfnl jrfnl deleted the feature/squiz-membervarspacing-bug-fix-blank-lines-in-preamble branch March 15, 2025 10:14
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.

Squiz.WhiteSpace.MemberVarSpacing can only fix property with attribute preceded by a comment
2 participants