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

else if restyles with undesirable nesting #4

Closed
edsrzf opened this issue Sep 24, 2023 · 4 comments
Closed

else if restyles with undesirable nesting #4

edsrzf opened this issue Sep 24, 2023 · 4 comments

Comments

@edsrzf
Copy link
Contributor

edsrzf commented Sep 24, 2023

php-styler restyles code using else if:

if ($foo) {
    $foo = 'bar';
} else if ($bar) {
    $foo = 'baz';
}

...to this:

if ($foo) {
    $foo = 'bar';
} else {
    if ($bar) {
        $foo = 'baz';
    }
}

I think the styled code should use elseif instead.

@pmjones
Copy link
Owner

pmjones commented Sep 24, 2023

Absolutely it should -- and the test case for that passes (cf. tests/Examples/if.php). Can you send along your code snippet where it does not?

@pmjones
Copy link
Owner

pmjones commented Sep 24, 2023

Oh wait I see what you're saying -- the space between else and if looks like the culprit. At the worst it should maintain the space between them, not nest them. I'll work on it!

@pmjones
Copy link
Owner

pmjones commented Sep 24, 2023

This probably cannot be fixed in the Styler -- the trouble originates in the Parser. The underlying PHP-Parser nodes, from which PHP-Styler builds the presentations, do not recognize else if to be the same thing elseif. If you run the following code through php-styler preview --debug-parser ...

<?php
if (1) {
    // foo
} else if (1) {
    // bar
}

... you will see that the the PhpParser\Node\Stmt\If_ structure has no elseifs in it, only an else. As such, the Styler is faithfully reconstructing the code as recognized by the Parser.

I can imagine doing something in the Styler that would visit the underlying stmts in the else to see if there is an if immediately after it, and no other if statements at the same level, but that sounds a little tricky, and prone to breaking the logic of the original code, depending on the body of the elseif block. (Hope you get what I mean there.)

The correct fix would be for PHP-Parser to recognize else if as elseif under the proper conditions, but my guess is that would be tricky too -- it would need to look ahead to see if the branch is enclosed in braces, instead of being an unbraced single statement, among other things. I will see about adding a ticket there, unless you get to it before me.

In any case, thank you for the report, and I'm sorry I can't make a good fix in a timely manner.

@pmjones
Copy link
Owner

pmjones commented Sep 24, 2023

@edsrzf I was wrong, this was an easy fix. Thanks for your patience, and thanks to @nikic for the pointer.

pmjones added a commit that referenced this issue Sep 25, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants