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/OperatorSpacing: bug fix - prevent fixer conflict #427

Merged
merged 2 commits into from
Apr 9, 2024

Conversation

jrfnl
Copy link
Member

@jrfnl jrfnl commented Mar 31, 2024

Description

Squiz/OperatorSpacing: move parse error test to own test case file

Squiz/OperatorSpacing: bug fix - prevent fixer conflict

Another one in the fixer conflict series.

When running the Squiz standard over all test case files, a fixer conflict in the Squiz.WhiteSpace.OperatorSpacing sniff was discovered via the tests in the ./src/Standards/Generic/Tests/CodeAnalysis/RequireExplicitBooleanOperatorPrecedenceUnitTest.inc file for a code sample like this:

$foo = $var // Comment.
    ? false // Comment.
    : true;

The conflict essentially comes down to the Squiz.WhiteSpace.OperatorSpacing trying to remove the new line before the ? and the : operator, but failing to do so as the new line is included in the comment token on the previous line and the sniff only adjusts/removes whitespace tokens.

Original errors for the code sample added in the test case file:

 487 | ERROR | [x] Expected 1 space before "?"; newline found (Squiz.WhiteSpace.OperatorSpacing.SpacingBefore)
 488 | ERROR | [x] Expected 1 space before ":"; newline found (Squiz.WhiteSpace.OperatorSpacing.SpacingBefore)
 493 | ERROR | [x] Expected 1 space before "?"; newline found (Squiz.WhiteSpace.OperatorSpacing.SpacingBefore)
 494 | ERROR | [x] Expected 1 space before ":"; newline found (Squiz.WhiteSpace.OperatorSpacing.SpacingBefore)
 499 | ERROR | [x] Expected 1 space before "?"; newline found (Squiz.WhiteSpace.OperatorSpacing.SpacingBefore)
 504 | ERROR | [x] Expected 1 space before ":"; newline found (Squiz.WhiteSpace.OperatorSpacing.SpacingBefore)

The fix proposed in this PR changes the sniff to make the SpacingBefore error code non-fixable if the previous non-whitespace token is a comment token - even when the comment token doesn't contain a new line.

While the sniff could auto-fix when the comment token doesn't contain a new line, I have chosen to disable the auto-fixer for those cases anyway as it is not for the sniff to determine whether the comment should be moved, removed or should stay where it is.

With these changes, the code sample in the test case file now yields the following errors:

 487 | ERROR | [x] Expected 1 space before "?"; newline found (Squiz.WhiteSpace.OperatorSpacing.SpacingBefore)
 488 | ERROR | [x] Expected 1 space before ":"; newline found (Squiz.WhiteSpace.OperatorSpacing.SpacingBefore)
 493 | ERROR | [ ] Expected 1 space before "?"; newline found (Squiz.WhiteSpace.OperatorSpacing.SpacingBefore)
 494 | ERROR | [ ] Expected 1 space before ":"; newline found (Squiz.WhiteSpace.OperatorSpacing.SpacingBefore)
 499 | ERROR | [ ] Expected 1 space before "?"; newline found (Squiz.WhiteSpace.OperatorSpacing.SpacingBefore)
 504 | ERROR | [ ] Expected 1 space before ":"; newline found (Squiz.WhiteSpace.OperatorSpacing.SpacingBefore)

This effectively fixes the fixer conflict.

👉 The diff will be easier to review while ignoring whitespace changes.


Fixer Conflict details
        * fixed 0 violations, starting loop 48 *
        => Changeset started by Squiz.WhiteSpace.OperatorSpacing:258
                Q: Squiz.WhiteSpace.OperatorSpacing:267 replaced token 911 (T_WHITESPACE on line 72) " ?" => " ?"
                A: Squiz.WhiteSpace.OperatorSpacing:268 replaced token 911 (T_WHITESPACE on line 72) " ?" => " ?"
        => Changeset ended: 1 changes applied
        => Changeset started by Squiz.WhiteSpace.OperatorSpacing:258
                Q: Squiz.WhiteSpace.OperatorSpacing:267 replaced token 925 (T_WHITESPACE on line 73) " :" => " :"
                A: Squiz.WhiteSpace.OperatorSpacing:268 replaced token 925 (T_WHITESPACE on line 73) " :" => " :"
        => Changeset ended: 1 changes applied
        => Fixing file: 2/2 violations remaining [made 48 passes]...
        * fixed 2 violations, starting loop 49 *
        => Changeset started by Squiz.WhiteSpace.OperatorSpacing:258
                Q: Squiz.WhiteSpace.OperatorSpacing:267 replaced token 911 (T_WHITESPACE on line 72) " ?" => " ?"
                **** Squiz.WhiteSpace.OperatorSpacing:268 has possible conflict with another sniff on loop 47; caused by the following change ****
                **** replaced token 911 (T_WHITESPACE on line 72) " ?" => " ?" ****
                **** ignoring all changes until next loop ****
        => Changeset failed to apply
        => Fixing file: 0/2 violations remaining [made 49 passes]...

Suggested changelog entry

Fixed Squiz.WhiteSpace.OperatorSpacing : when there was a new line before an operator, but the line before it ended on a comment, the fixer would get into a conflict state.

Related issues/external references

Related to #143
Related to #152

Types of changes

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

@jrfnl jrfnl added this to the 3.9.1 milestone Mar 31, 2024
@jrfnl jrfnl requested a review from fredden March 31, 2024 15:06
@jrfnl jrfnl modified the milestones: 3.9.1, 3.9.2 Mar 31, 2024
Copy link
Member

@fredden fredden left a comment

Choose a reason for hiding this comment

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

Discussed and reviewed on a call. The changes here (including the tests) make sense.

jrfnl added 2 commits April 9, 2024 14:54
Another one in the fixer conflict series.

When running the `Squiz` standard over all test case files, a fixer conflict in the `Squiz.WhiteSpace.OperatorSpacing` sniff was discovered via the tests in the `./src/Standards/Generic/Tests/CodeAnalysis/RequireExplicitBooleanOperatorPrecedenceUnitTest.inc` file for a code sample like this:
```php
$foo = $var // Comment.
    ? false // Comment.
    : true;

```

The conflict essentially comes down to the `Squiz.WhiteSpace.OperatorSpacing` trying to remove the new line before the `?` and the `:` operator, but failing to do so as the new line is included in the comment token on the previous line and the sniff only adjusts/removes whitespace tokens.

Original errors for the code sample added in the test case file:
```
 487 | ERROR | [x] Expected 1 space before "?"; newline found (Squiz.WhiteSpace.OperatorSpacing.SpacingBefore)
 488 | ERROR | [x] Expected 1 space before ":"; newline found (Squiz.WhiteSpace.OperatorSpacing.SpacingBefore)
 493 | ERROR | [x] Expected 1 space before "?"; newline found (Squiz.WhiteSpace.OperatorSpacing.SpacingBefore)
 494 | ERROR | [x] Expected 1 space before ":"; newline found (Squiz.WhiteSpace.OperatorSpacing.SpacingBefore)
 499 | ERROR | [x] Expected 1 space before "?"; newline found (Squiz.WhiteSpace.OperatorSpacing.SpacingBefore)
 504 | ERROR | [x] Expected 1 space before ":"; newline found (Squiz.WhiteSpace.OperatorSpacing.SpacingBefore)
```

The fix proposed in this PR changes the sniff to make the `SpacingBefore` error code non-fixable if the previous non-whitespace token is a comment token - even when the comment token doesn't contain a new line.

While the sniff _could_ auto-fix when the comment token doesn't contain a new line, I have chosen to disable the auto-fixer for those cases anyway as it is not for the sniff to determine whether the comment should be moved, removed or should stay where it is.

With these changes, the code sample in the test case file now yields the following errors:
```
 487 | ERROR | [x] Expected 1 space before "?"; newline found (Squiz.WhiteSpace.OperatorSpacing.SpacingBefore)
 488 | ERROR | [x] Expected 1 space before ":"; newline found (Squiz.WhiteSpace.OperatorSpacing.SpacingBefore)
 493 | ERROR | [ ] Expected 1 space before "?"; newline found (Squiz.WhiteSpace.OperatorSpacing.SpacingBefore)
 494 | ERROR | [ ] Expected 1 space before ":"; newline found (Squiz.WhiteSpace.OperatorSpacing.SpacingBefore)
 499 | ERROR | [ ] Expected 1 space before "?"; newline found (Squiz.WhiteSpace.OperatorSpacing.SpacingBefore)
 504 | ERROR | [ ] Expected 1 space before ":"; newline found (Squiz.WhiteSpace.OperatorSpacing.SpacingBefore)
```

This effectively fixes the fixer conflict.

:point_right: The diff will be easier to review while ignoring whitespace changes.

---
<details>
  <summary>Fixer Conflict details</summary>

```
        * fixed 0 violations, starting loop 48 *
        => Changeset started by Squiz.WhiteSpace.OperatorSpacing:258
                Q: Squiz.WhiteSpace.OperatorSpacing:267 replaced token 911 (T_WHITESPACE on line 72) " ?" => " ?"
                A: Squiz.WhiteSpace.OperatorSpacing:268 replaced token 911 (T_WHITESPACE on line 72) " ?" => " ?"
        => Changeset ended: 1 changes applied
        => Changeset started by Squiz.WhiteSpace.OperatorSpacing:258
                Q: Squiz.WhiteSpace.OperatorSpacing:267 replaced token 925 (T_WHITESPACE on line 73) " :" => " :"
                A: Squiz.WhiteSpace.OperatorSpacing:268 replaced token 925 (T_WHITESPACE on line 73) " :" => " :"
        => Changeset ended: 1 changes applied
        => Fixing file: 2/2 violations remaining [made 48 passes]...
        * fixed 2 violations, starting loop 49 *
        => Changeset started by Squiz.WhiteSpace.OperatorSpacing:258
                Q: Squiz.WhiteSpace.OperatorSpacing:267 replaced token 911 (T_WHITESPACE on line 72) " ?" => " ?"
                **** Squiz.WhiteSpace.OperatorSpacing:268 has possible conflict with another sniff on loop 47; caused by the following change ****
                **** replaced token 911 (T_WHITESPACE on line 72) " ?" => " ?" ****
                **** ignoring all changes until next loop ****
        => Changeset failed to apply
        => Fixing file: 0/2 violations remaining [made 49 passes]...
```
</details>

---

Related to 152
@jrfnl jrfnl force-pushed the feature/squiz-operatorspacing-prevent-fixer-conflict branch from 1770f36 to 195e941 Compare April 9, 2024 12:58
@jrfnl
Copy link
Member Author

jrfnl commented Apr 9, 2024

Rebased without changes. Merging once the build passes.

@jrfnl jrfnl merged commit 8a2b6b1 into master Apr 9, 2024
44 checks passed
@jrfnl jrfnl deleted the feature/squiz-operatorspacing-prevent-fixer-conflict branch April 9, 2024 13:19
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