Open
Conversation
… warning This resolves the "WARNING: unclosed parentheses or section" error that occurred when parsing SQL statements containing ELSEIF keywords in IF/ELSEIF/ELSE/END IF blocks.
- Add context tracking to detect when inside IF conditions - Skip newlines for AND/OR tokens within IF conditions while preserving newline behavior for AND/OR in WHERE clauses and other contexts - This improves readability of formatted SQL with complex IF statements
mvorisek
reviewed
Jul 22, 2025
mvorisek
reviewed
Jul 22, 2025
src/SqlFormatter.php
Outdated
| } | ||
| } elseif ($token->isOfType(Token::TOKEN_TYPE_RESERVED_NEWLINE)) { | ||
| // Newline reserved words start a new line | ||
| // Newline reserved words start a new line, except for AND/OR within IF conditions |
Contributor
There was a problem hiding this comment.
There are more contexts where boolean expressions can be used the same way, at least CASE WHEN. Not sure if this optimization is welcomed in this PR.
Author
There was a problem hiding this comment.
done
The behavior is now:
- IF/ELSEIF/ELSIF conditions: AND/OR stay on the same line
- CASE WHEN conditions: AND/OR stay on the same line
- WHERE/HAVING clauses: AND/OR still get newlines (unchanged - this is probably desired for readability)
- JOIN ON conditions: AND/OR still get newlines (unchanged)
- Apply same-line AND/OR formatting to CASE WHEN conditions - Maintain newline behavior for WHERE/HAVING/JOIN clauses
4111b1c to
a9ec3ec
Compare
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.
primarily this resolves the "WARNING: unclosed parentheses or section" error that occurred when parsing SQL statements containing ELSEIF keywords in IF/ELSEIF/ELSE/END IF blocks.
second, I noticed and fixed that AND/OR weren't handled correctly inside IF blocks, where new lines were added incorrectly.