Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 9 additions & 6 deletions src/SqlFormatter.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@ final class SqlFormatter
private readonly Highlighter $highlighter;
private readonly Tokenizer $tokenizer;

private const INDENT_TYPE_BLOCK = 'block';
private const INDENT_TYPE_SPECIAL = 'special';

public function __construct(Highlighter|null $highlighter = null)
{
$this->tokenizer = new Tokenizer();
Expand Down Expand Up @@ -77,14 +80,14 @@ public function format(string $string, string $indentString = ' '): string
if ($increaseSpecialIndent) {
$indentLevel++;
$increaseSpecialIndent = false;
array_unshift($indentTypes, 'special');
array_unshift($indentTypes, self::INDENT_TYPE_SPECIAL);
}

// If we are increasing the block indent level now
if ($increaseBlockIndent) {
$indentLevel++;
$increaseBlockIndent = false;
array_unshift($indentTypes, 'block');
array_unshift($indentTypes, self::INDENT_TYPE_BLOCK);
}

// If we need a new line before the token
Expand Down Expand Up @@ -207,7 +210,7 @@ public function format(string $string, string $indentString = ' '): string

// Reset indent level
while ($j = array_shift($indentTypes)) {
if ($j !== 'special') {
if ($j !== self::INDENT_TYPE_SPECIAL) {
break;
}

Expand All @@ -230,9 +233,9 @@ public function format(string $string, string $indentString = ' '): string
// Top level reserved words start a new line and increase the special indent level
$increaseSpecialIndent = true;

// If the last indent type was 'special', decrease the special indent for this round
// If the last indent type was special, decrease the special indent for this round
reset($indentTypes);
if (current($indentTypes) === 'special') {
if (current($indentTypes) === self::INDENT_TYPE_SPECIAL) {
$indentLevel--;
array_shift($indentTypes);
}
Expand Down Expand Up @@ -341,7 +344,7 @@ public function format(string $string, string $indentString = ' '): string
}

// If there are unmatched parentheses
if (array_search('block', $indentTypes) !== false) {
if (array_search(self::INDENT_TYPE_BLOCK, $indentTypes) !== false) {
$return = rtrim($return, ' ');
$return .= $this->highlighter->highlightErrorMessage(
'WARNING: unclosed parentheses or section',
Expand Down