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

Generic/JumbledIncrementer: fix non-problematic bug and improve test coverage #385

Merged

Conversation

rodrigoprimo
Copy link
Contributor

@rodrigoprimo rodrigoprimo commented Mar 8, 2024

Description

This PR adds a few commits related to the Generic.CodeAnalysis.JumbledIncrementer sniff:

  • Fix a non-problematic bug that would happen when handling an inner for loop without a closing parenthesis. In those cases, $end would be set to null (src/Standards/Generic/Sniffs/CodeAnalysis/JumbledIncrementerSniff.php#L116) instead of set to the index of the closing parenthesis token. This problem did not cause any issues as $next <= $end when $end is null is always false, and the for loop that looks for incrementers ends early.
  • Improve test coverage
  • Improve the name of a variable

A few things that I noticed while working on this PR that I would like to confirm if they are indeed issues (probably in separate PRs):

$label = 'label';

for ($i = 1; $i <= 10; $i++, print $label) {
    for ($j = 0; $j < 5; $j++, print $label);
}

Suggested changelog entry

Generic.CodeAnalysis.JumbledIncrementer: fixed a non-problematic bug when the inner for loop is missing the closing parenthesis

Related issues/external references

Part of #146

Types of changes

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to change)
    • This change is only breaking for integrators, not for external standards or end-users.
  • Documentation improvement

PR checklist

  • I have checked there is no other PR open for the same change.
  • I have read the Contribution Guidelines.
  • I grant the project the right to include and distribute the code under the BSD-3-Clause license (and I have the right to grant these rights).
  • I have added tests to cover my changes.
  • I have verified that the code complies with the projects coding standards.
  • [Required for new sniffs] I have added XML documentation for the sniff.

Copy link
Member

@jrfnl jrfnl left a comment

Choose a reason for hiding this comment

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

@rodrigoprimo Thank you for this PR and my apologies for taking a while before reviewing it.

Generally speaking, all looking good! Thank you for making these changes.

There's a few small nitpicks, but nothing major.

While reviewing this PR, I noticed a few other things the sniff does not correctly take into account. I will open separate issues for those.

@rodrigoprimo
Copy link
Contributor Author

Thanks for the review, @jrfnl!

I believe I addressed the points that you raised. I had to force push commits to the branch as I dropped the commit that renamed a variable in the sniff code and added a new commit in between commits to rename variables for the pre-existing tests.

This PR is ready for a final check.

@jrfnl
Copy link
Member

jrfnl commented Apr 10, 2024

As per the comment I left in my previous review, I've now opened two issues for potential follow-up improvements for the sniff: #440 and #441

I also realized that I hadn't answered the questions you included in the description yet.

The sniff documentation and its name only mention incrementers, but the sniff also works for decrementers. Should we update the documentation? I guess the name is a bit more complex to change, so I'm inclined to think we should not change it.

Good catch and yes, please feel invited to improve the documentation (docblock + XML ?).

And as you already surmised, changing the sniff name would be a breaking change, so is not on the table.

The sniff assumes that any variable in the third part of the for loop condition is an incrementer or decrementer which is not true. The code below triggers the sniff and I'm not sure if it should:

I fully agree with you that this is a bug. Issue #441 contains a range of additional code samples running into the same issue. Please add your code sample in a comment to that ticket so it won't be overlooked by whomever starts working on that ticket.

@jrfnl jrfnl added this to the 3.9.2 milestone Apr 10, 2024
Copy link
Member

@jrfnl jrfnl left a comment

Choose a reason for hiding this comment

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

@rodrigoprimo Thank you for working on this and making the requested updates.

I'll go ahead and reorganize the commits and rebase the PR before merging it.

I will fix up that comment change which got undone while doing that.

Doing this to be able to create tests with syntax errors on separate
files.
Using `$same` instead of `$i` should help highlight which variable is
expected to trigger the sniff when reading the test code.
The sniff was wrongly assuming that the inner for loop would always have
a closing parenthesis which is not true when PHPCS is used with live
coding. This meant that `$end` could be set to `null`
(https://github.com/PHPCSStandards/PHP_CodeSniffer/blob/84acf4e56f110db8e75cb9a575c5727df637643c/src/Standards/Generic/Sniffs/CodeAnalysis/JumbledIncrementerSniff.php#L116). This did not cause any issues as `$next <= $end`
when `$end` is `null` is always false, so the for loop that looks for
incrementers was not executed.

The code was changed to bail early when the inner for loop is missing
the closing parenthesis and a test was added.
This commit adds tests for:
- for alternative syntax
- for loops missing one or more condition parts
- sniff bails early when it encounters a parse error (missing for body
  and inner for loop missing opening parenthesis
- for loops with more than one incrementer
- for loops with decrementers
@jrfnl jrfnl force-pushed the test-coverage-jumbled-incrementer branch from fc724d5 to fd7b175 Compare April 10, 2024 15:07
@jrfnl jrfnl merged commit b3a6b32 into PHPCSStandards:master Apr 10, 2024
38 checks passed
@rodrigoprimo rodrigoprimo deleted the test-coverage-jumbled-incrementer branch April 10, 2024 18:23
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