Skip to content

Generic/InlineControlStructure: two issues when fixing some do-while statements #483

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

Open
3 tasks done
rodrigoprimo opened this issue May 7, 2024 · 1 comment
Open
3 tasks done

Comments

@rodrigoprimo
Copy link
Contributor

rodrigoprimo commented May 7, 2024

Describe the bug

While working on #482, I found two issues in the Generic.ControlStructures.InlineControlStructure sniff when fixing some do-while statements that contain syntax errors.

Code sample

Code sample 1:

<?php

// missing semicolon and nothing but empty tokens after the while closing parenthesis
foreach ($array as $item)
    do {
        echo $i;
        $i++;
    } while ($i < 5)

Code sample 2:

<?php

// missing semicolon after while condition and non-empty tokens after it
foreach ($array as $item)
    do {
        echo $i;
        $i++;
    } while ($i < 5)

if ($test)
    echo 'test';

To reproduce

Steps to reproduce the behavior:

  1. Create a file called test.php with one of the code samples above.
  2. Run phpcbf --standard=Generic --sniffs=Generic.ControlStructures.InlineControlStructure test.php
  3. Check the modified files.

Modified code sample 1 (note the semicolon and closing curly bracket added after the PHP opening tag):

<?php
; 
}
// missing semicolon and nothing but empty tokens after the while closing parenthesis
foreach ($array as $item) {
    do {
        echo $i;
        $i++;
    } while ($i < 5)

Modified code sample 2 (note the curly bracket added after the while closing parenthesis and after the if statement):

<?php

// missing semicolon after while condition and non-empty tokens after it
foreach ($array as $item) {
    do {
        echo $i;
        $i++;
    } while ($i < 5) {

if ($test) {
    echo 'test';
}
    }
}

Expected behavior

I would expect PHPCS to detect the syntax error in both cases (missing semicolon after while closing parenthesis) and to bail instead of attempting to fix the code sample, introducing even more problems.

Versions (please complete the following information)

Operating System Ubuntu 23.10
PHP version 8.3
PHP_CodeSniffer version master
Standard Generic
Install type git clone

Please confirm

  • I have searched the issue list and am not opening a duplicate issue.
  • I confirm that this bug is a bug in PHP_CodeSniffer and not in one of the external standards.
  • I have verified the issue still exists in the master branch of PHP_CodeSniffer.
@jrfnl
Copy link
Member

jrfnl commented May 17, 2024

I would expect PHPCS to detect the syntax error in both cases (missing semicolon after while closing parenthesis) and to bail instead of attempting to fix the code sample, introducing even more problems.

I think the bailing early should only happen if it is a while which is part of a do-while and there is no other code after it, otherwise, the sniff would start to ignore while structures which are perfectly fine to fix like:

foreach ($array as $item)
    while ($i < 5)
        if ($test)
            echo 'test';

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants