Skip to content

Brace an if body that an else would otherwise bind into - #26

Open
danslo wants to merge 1 commit into
rectorphp:mainfrom
danslo:fix-dangling-else
Open

danslo wants to merge 1 commit into
rectorphp:mainfrom
danslo:fix-dangling-else

Conversation

@danslo

@danslo danslo commented Nov 18, 2026

Copy link
Copy Markdown

A tree built in code, rather than parsed, can hold an if with an else whose body is an if without one. The formatter printed that body without braces, so the outer else bound to the inner if:

if ($p) if ($a) echo 1; else echo 3;

It now braces such a body, also where the open if ends a loop body or an elseif:

if ($p) {
    if ($a) echo 1;
} else echo 3;

Added a formatter test with a hand-built tree.

A tree built by hand can hold an if whose body ends with an if without an else. Printed without braces, the outer else then belongs to the inner if.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

1 participant