Skip to content

Commit d42b402

Browse files
Merge pull request #466 from kamil-tekiela/merge-else-if
Merge else if
2 parents 332054f + 91f820c commit d42b402

File tree

3 files changed

+35
-43
lines changed

3 files changed

+35
-43
lines changed

src/Components/CaseExpression.php

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -156,16 +156,14 @@ public static function parse(Parser $parser, TokensList $list, array $options =
156156
break 2;
157157
}
158158
}
159-
} else {
160-
if ($token->type === Token::TYPE_KEYWORD && $token->keyword === 'THEN') {
161-
++$list->idx; // Skip 'THEN'
162-
$newResult = Expression::parse($parser, $list);
163-
$state = 0;
164-
$ret->results[] = $newResult;
165-
} elseif ($token->type === Token::TYPE_KEYWORD) {
166-
$parser->error('Unexpected keyword.', $token);
167-
break;
168-
}
159+
} elseif ($token->type === Token::TYPE_KEYWORD && $token->keyword === 'THEN') {
160+
++$list->idx; // Skip 'THEN'
161+
$newResult = Expression::parse($parser, $list);
162+
$state = 0;
163+
$ret->results[] = $newResult;
164+
} elseif ($token->type === Token::TYPE_KEYWORD) {
165+
$parser->error('Unexpected keyword.', $token);
166+
break;
169167
}
170168
} elseif ($state === 2) {
171169
if ($type === 0) {

src/Parser.php

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -494,21 +494,19 @@ public function parse(): void
494494
}
495495

496496
$list->idx = $lastIdx;
497-
} else {
497+
} elseif (empty(static::$statementParsers[$token->keyword])) {
498498
// Checking if it is a known statement that can be parsed.
499-
if (empty(static::$statementParsers[$token->keyword])) {
500-
if (! isset(static::$statementParsers[$token->keyword])) {
501-
// A statement is considered recognized if the parser
502-
// is aware that it is a statement, but it does not have
503-
// a parser for it yet.
504-
$this->error('Unrecognized statement type.', $token);
505-
}
506-
507-
// Skipping to the end of this statement.
508-
$list->getNextOfType(Token::TYPE_DELIMITER);
509-
$prevLastIdx = $list->idx;
510-
continue;
499+
if (! isset(static::$statementParsers[$token->keyword])) {
500+
// A statement is considered recognized if the parser
501+
// is aware that it is a statement, but it does not have
502+
// a parser for it yet.
503+
$this->error('Unrecognized statement type.', $token);
511504
}
505+
506+
// Skipping to the end of this statement.
507+
$list->getNextOfType(Token::TYPE_DELIMITER);
508+
$prevLastIdx = $list->idx;
509+
continue;
512510
}
513511

514512
/**

src/Utils/Formatter.php

Lines changed: 16 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -528,28 +528,24 @@ public function formatList($list)
528528

529529
// Finishing the line.
530530
if ($lineEnded) {
531-
$ret .= $this->options['line_ending']
532-
. str_repeat($this->options['indentation'], (int) $indent);
533-
531+
$ret .= $this->options['line_ending'] . str_repeat($this->options['indentation'], (int) $indent);
534532
$lineEnded = false;
535-
} else {
536-
// If the line ended there is no point in adding whitespaces.
533+
} elseif (
534+
$prev->keyword === 'DELIMITER'
535+
|| ! (
536+
($prev->type === Token::TYPE_OPERATOR && ($prev->value === '.' || $prev->value === '('))
537+
// No space after . (
538+
|| ($curr->type === Token::TYPE_OPERATOR
539+
&& ($curr->value === '.' || $curr->value === ','
540+
|| $curr->value === '(' || $curr->value === ')'))
541+
// No space before . , ( )
542+
|| $curr->type === Token::TYPE_DELIMITER && mb_strlen((string) $curr->value, 'UTF-8') < 2
543+
)
544+
) {
545+
// If the line ended, there is no point in adding whitespaces.
537546
// Also, some tokens do not have spaces before or after them.
538-
if (
539-
// A space after delimiters that are longer than 2 characters.
540-
$prev->keyword === 'DELIMITER'
541-
|| ! (
542-
($prev->type === Token::TYPE_OPERATOR && ($prev->value === '.' || $prev->value === '('))
543-
// No space after . (
544-
|| ($curr->type === Token::TYPE_OPERATOR
545-
&& ($curr->value === '.' || $curr->value === ','
546-
|| $curr->value === '(' || $curr->value === ')'))
547-
// No space before . , ( )
548-
|| $curr->type === Token::TYPE_DELIMITER && mb_strlen((string) $curr->value, 'UTF-8') < 2
549-
)
550-
) {
551-
$ret .= ' ';
552-
}
547+
// A space after delimiters that are longer than 2 characters.
548+
$ret .= ' ';
553549
}
554550
}
555551

0 commit comments

Comments
 (0)