[CodeQuality] Skip alternative syntax (if/endif) in ShortenElseIfRector#8157
Merged
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes rectorphp/rector#9803
ShortenElseIfRectorproduced invalid PHP when a file used the alternative control syntax (if:/else:/endif). It mergedelse { if }intoelseifregardless of syntax style: the freshly createdElseIf_node prints braces while the surrounding unchanged nodes keep their colons, yielding mixed brace/colon output that fails to parse (and drops the trailingendif).Before (broken output)
After
Files using alternative syntax are skipped, both when the outer
ifuses it and when only the nestedifdoes. Normal brace syntax is unchanged:if ($cond1) { return $action1; -} else { - if ($cond2) { - return $action2; - } +} elseif ($cond2) { + return $action2; }Detection reuses the same
getOldTokens()token-scan approach as the siblingCompleteMissingIfElseBracketRector: scan tokens after the condition and skip if:appears before{.Two
skip_*fixtures added (full alternative syntax + brace-outer/alt-inner).