Skip to content

phpcbf on if/else with trailing comment generates erroneous code #1190

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

Closed
shadiakiki1986 opened this issue Oct 8, 2016 · 1 comment
Closed

Comments

@shadiakiki1986
Copy link

shadiakiki1986 commented Oct 8, 2016

I have a file test.php with the following content

<?php
$out=array_map(function($test) { if($test) return 1; else return 2; }, $input);
$out=array_map(function($test) { if($test) return 1; else return 2; }, $input); // this line is as the above but with a trailing comment

when I run vendor/bin/phpcbf test.php, I get the following fixed file

<?php                                                                      
$out=array_map(
    function ($test) {
        if($test) { return 1; 
        } else { return 2;
        } 
    }, $input
);
$out=array_map(
    function ($test) {
        if($test) { return 1; else { return 2;
        } 
        }, $input
        ); // this line is as the above but with a trailing comment
    }

Notice the if brace did not close before the else, and that the array_map parentheses close too early.
I'm using "squizlabs/php_codesniffer": "^2.7" in my composer.json file

@gsherwood
Copy link
Member

Confirming that before the fix, if you run with the PSR2 standard, you get this:

<?php
$out=array_map(function ($test) {
    if ($test) {
        return 1;
    } else {
        return 2;
    }
}, $input);
$out=array_map(function ($test) {
    if ($test) {
        return 1;
        else {
            return 2;
        }
    }, $input); // this line is as the above but with a trailing comment
}

After the fix, if you run with the PSR2 standard, you get this:

<?php
$out=array_map(function ($test) {
    if ($test) {
        return 1;
    } else {
        return 2;
    }
}, $input);
$out=array_map(function ($test) {
    if ($test) {
        return 1;
    } else {
        return 2;
    }
}, $input); // this line is as the above but with a trailing comment

Thanks for the bug report. It was very clear and helpful.

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