Skip to content

[CodingStyle] Add AlternativeIfToBracketRector#8158

Merged
TomasVotruba merged 2 commits into
mainfrom
add-alternative-if-to-bracket-rector
Jul 8, 2026
Merged

[CodingStyle] Add AlternativeIfToBracketRector#8158
TomasVotruba merged 2 commits into
mainfrom
add-alternative-if-to-bracket-rector

Conversation

@TomasVotruba

Copy link
Copy Markdown
Member

Follow-up to #8157.

Adds AlternativeIfToBracketRector (CodingStyle) that converts the alternative if/elseif/else/endif control syntax to bracket syntax. Complements #8157, where ShortenElseIfRector now skips alternative syntax rather than producing broken mixed output — this rule lets you normalize such files to braces instead.

Change

-if ($value) :
+if ($value) {
     $result = 1;
-else :
+} else {
     $result = 2;
-endif;
+}

Full chain:

-if ($a) :
+if ($a) {
     $x = 1;
-elseif ($b) :
+} elseif ($b) {
     $x = 2;
-else :
+} else {
     $x = 3;
-endif;
+}

How

Detects alternative syntax with the same getOldTokens() scan used by the sibling CompleteMissingIfElseBracketRector (a : before { after the condition), then clears ORIGINAL_NODE on the if, its elseifs and else to force a bracket reprint. Bracket syntax is left untouched. Implements HTMLAverseRectorInterface so template files mixing inline HTML are skipped.

@TomasVotruba TomasVotruba merged commit e9ef0ac into main Jul 8, 2026
64 of 65 checks passed
@TomasVotruba TomasVotruba deleted the add-alternative-if-to-bracket-rector branch July 8, 2026 16:21
@samsonasik

Copy link
Copy Markdown
Member

There is already rule for it: CompleteMissingIfElseBracketRector

final class CompleteMissingIfElseBracketRector extends AbstractRector implements HTMLAverseRectorInterface

@TomasVotruba

TomasVotruba commented Jul 8, 2026

Copy link
Copy Markdown
Member Author

It's slightly different rule. I could not find any fixture handling : there

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.

2 participants