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 / improve parse error handling #832

Merged

Conversation

jrfnl
Copy link
Member

@jrfnl jrfnl commented Feb 18, 2025

Description

Squiz/MemberVarSpacing: rename test case file

... to allow for adding additional test case files for code containing parse errors.

Squiz/MemberVarSpacing: bug fix / improve parse error handling

The Squiz.WhiteSpace.MemberVarSpacing sniff checks the number of blank lines before a property declaration.

To determine the number of blank lines before a property, it tries to find the start of the statement by:

  • First finding the first modifier keyword before the variable (to skip over a potential type declaration);
  • And then walking over the other modifiers until it finds the first one for the statement;
  • After that, it checks for potential docblocks and attributes and skips over those.

Only after all that it checks the number of blank lines.

The first step however leads to problems when, during live coding, a property would be declared without a modifier keyword.
In that case, the sniff could walk back much further than it should, potentially misidentifying a modifier keyword for a function for the modifier keyword for the property.

While this is an edge-case as it is not customary for properties to be declared after functions, the sniff should still handle this situation correctly.

Fixed by changing the logic of the sniff to stop searching earlier.

Includes new test case files, both of which demonstrate the bug.
Additionally, the test in the 1 file safeguards that the current behaviour of the sniff for multi-property declarations is not aversely affected by the fix.

Suggested changelog entry

Squiz.WhiteSpace.MemberVarSpacing: prevent potential fixer conflict during live coding

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)

... to allow for adding additional test case files for code containing parse errors.
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.

LGTM! I just left an inline comment with a question about one of the tokens used to find the end of the previous statement.

The `Squiz.WhiteSpace.MemberVarSpacing` sniff checks the number of blank lines before a property declaration.

To determine the number of blank lines before a property, it tries to find the start of the statement by:
* First finding the first modifier keyword before the variable (to skip over a potential type declaration);
* And then walking over the other modifiers until it finds the first one for the statement;
* After that, it checks for potential docblocks and attributes and skips over those.

Only after all that it checks the number of blank lines.

The first step however leads to problems when, during live coding, a property would be declared without a modifier keyword.
In that case, the sniff could walk back much further than it should, potentially misidentifying a modifier keyword for a function for the modifier keyword for the property.

While this is an edge-case as it is not customary for properties to be declared _after_ functions, the sniff should still handle this situation correctly.

Fixed by changing the logic of the sniff to stop searching earlier.

Includes new test case files, both of which demonstrate the bug.
Additionally, the test in the `1` file safeguards that the current behaviour of the sniff for multi-property declarations is not aversely affected by the fix.
@jrfnl jrfnl force-pushed the feature/squiz-membervarspacing-improve-parse-error-handling branch from 4e43040 to 98f7cff Compare February 20, 2025 15:15
@jrfnl jrfnl merged commit 46fbb5d into master Feb 21, 2025
57 checks passed
@jrfnl jrfnl deleted the feature/squiz-membervarspacing-improve-parse-error-handling branch February 21, 2025 23:32
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