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/ForLoopShouldBeWhileLoop: improve sniff code coverage + fix potential PHP 8.3 deprecation notice #226

Commits on Jan 3, 2024

  1. Generic/ForLoopShouldBeWhileLoop: rename test case file

    Doing this to be able to create a test with a syntax error on a separate
    file.
    rodrigoprimo committed Jan 3, 2024
    Configuration menu
    Copy the full SHA
    e50784d View commit details
    Browse the repository at this point in the history

Commits on Jan 4, 2024

  1. Generic/ForLoopShouldBeWhileLoop: improve test coverage

    This commit adds the following groups of tests:
    
    - A few tests that do not trigger the sniff but should help to
    protect against regressions in the future. Including tests using the for
    loop alternative syntax.
    - A test to exercise code in the sniff to bail early if the `for` keyword is
    found without a opening parenthesis. This part was not covered before.
    rodrigoprimo committed Jan 4, 2024
    Configuration menu
    Copy the full SHA
    9c4b38c View commit details
    Browse the repository at this point in the history

Commits on Jan 5, 2024

  1. Generic/ForLoopShouldBeWhileLoop: fix E_DEPRECATED error

    This commit fixes an issue in the sniff that could result in the following E_DEPRECATED
    error when running PHP 8.3:
    
    ```
    Decrement on type null has no effect, this will change in the next major version of PHP
    
    src/Standards/Generic/Sniffs/CodeAnalysis/ForLoopShouldBeWhileLoopSniff.php:65
    ```
    
    This sniff relies on finding the position of the open and closing
    parentheses for a given `for` loop. However, the problem was that there was
    no defensive code for cases when the closing parenthesis is missing. The
    sniff would still work when running PHP >= 8.2, but on PHP 8.3 it would
    throw the deprecated message above.
    
    This would happen because since there is no closing parenthesis `$end` is set to null,
    and $next <= $end always evaluates to false
    (https://github.com/PHPCSStandards/PHP_CodeSniffer/blob/84acf4e56f110db8e75cb9a575c5727df637643c/src/Standards/Generic/Sniffs/CodeAnalysis/ForLoopShouldBeWhileLoopSniff.php#L74).
    
    The issue was fixed by bailing early if the closing parenthesis is
    missing. A test with a `for` loop without the closing parenthesis was added.
    rodrigoprimo committed Jan 5, 2024
    Configuration menu
    Copy the full SHA
    1ebc09e View commit details
    Browse the repository at this point in the history