Skip to content

Commit

Permalink
Cleanup and improve readability:
Browse files Browse the repository at this point in the history
Avoid duplicate if conditions
Use switch/case instead of ifs when possible
  • Loading branch information
bperel committed Nov 26, 2018
1 parent 8e37bb3 commit 513ed81
Show file tree
Hide file tree
Showing 28 changed files with 457 additions and 473 deletions.
117 changes: 56 additions & 61 deletions src/Components/CaseExpression.php
Original file line number Diff line number Diff line change
Expand Up @@ -112,84 +112,79 @@ public static function parse(Parser $parser, TokensList $list, array $options =
}

if ($state === 0) {
if ($token->type === Token::TYPE_KEYWORD
&& $token->keyword === 'WHEN'
) {
++$list->idx; // Skip 'WHEN'
$new_condition = Condition::parse($parser, $list);
$type = 1;
$state = 1;
$ret->conditions[] = $new_condition;
} elseif ($token->type === Token::TYPE_KEYWORD
&& $token->keyword === 'ELSE'
) {
++$list->idx; // Skip 'ELSE'
$ret->else_result = Expression::parse($parser, $list);
$state = 0; // last clause of CASE expression
} elseif ($token->type === Token::TYPE_KEYWORD
&& $token->keyword === 'END'
) {
$state = 3; // end of CASE expression
++$list->idx;
break;
} elseif ($token->type === Token::TYPE_KEYWORD) {
$parser->error('Unexpected keyword.', $token);
break;
if ($token->type === Token::TYPE_KEYWORD) {
switch($token->keyword) {
case 'WHEN':
++$list->idx; // Skip 'WHEN'
$new_condition = Condition::parse($parser, $list);
$type = 1;
$state = 1;
$ret->conditions[] = $new_condition;
break;
case 'ELSE':
++$list->idx; // Skip 'ELSE'
$ret->else_result = Expression::parse($parser, $list);
$state = 0; // last clause of CASE expression
break;
case 'END':
$state = 3; // end of CASE expression
++$list->idx;
break;
default:
$parser->error('Unexpected keyword.', $token);
break;
}
} else {
$ret->value = Expression::parse($parser, $list);
$type = 0;
$state = 1;
}
} elseif ($state === 1) {
if ($type === 0) {
if ($token->type === Token::TYPE_KEYWORD
&& $token->keyword === 'WHEN'
) {
++$list->idx; // Skip 'WHEN'
$new_value = Expression::parse($parser, $list);
$state = 2;
$ret->compare_values[] = $new_value;
} elseif ($token->type === Token::TYPE_KEYWORD
&& $token->keyword === 'ELSE'
) {
++$list->idx; // Skip 'ELSE'
$ret->else_result = Expression::parse($parser, $list);
$state = 0; // last clause of CASE expression
} elseif ($token->type === Token::TYPE_KEYWORD
&& $token->keyword === 'END'
) {
$state = 3; // end of CASE expression
++$list->idx;
break;
} elseif ($token->type === Token::TYPE_KEYWORD) {
$parser->error('Unexpected keyword.', $token);
break;
if ($token->type === Token::TYPE_KEYWORD) {
switch($token->keyword) {
case 'WHEN':
++$list->idx; // Skip 'WHEN'
$new_value = Expression::parse($parser, $list);
$state = 2;
$ret->compare_values[] = $new_value;
break;
case 'ELSE':
++$list->idx; // Skip 'ELSE'
$ret->else_result = Expression::parse($parser, $list);
$state = 0; // last clause of CASE expression
break;
case 'END':
$state = 3; // end of CASE expression
++$list->idx;
break;
default:
$parser->error('Unexpected keyword.', $token);
break;
}
}
} else {
if ($token->type === Token::TYPE_KEYWORD
&& $token->keyword === 'THEN'
) {
} else if ($token->type === Token::TYPE_KEYWORD) {
if ($token->keyword === 'THEN') {
++$list->idx; // Skip 'THEN'
$new_result = Expression::parse($parser, $list);
$state = 0;
$ret->results[] = $new_result;
} elseif ($token->type === Token::TYPE_KEYWORD) {
} else {
$parser->error('Unexpected keyword.', $token);
break;
}
break;
}
} elseif ($state === 2) {
if ($type === 0) {
if ($token->type === Token::TYPE_KEYWORD
&& $token->keyword === 'THEN'
) {
++$list->idx; // Skip 'THEN'
$new_result = Expression::parse($parser, $list);
$ret->results[] = $new_result;
$state = 1;
} elseif ($token->type === Token::TYPE_KEYWORD) {
$parser->error('Unexpected keyword.', $token);
break;
if ($token->type === Token::TYPE_KEYWORD) {
if ($token->keyword === 'THEN') {
++$list->idx; // Skip 'THEN'
$new_result = Expression::parse($parser, $list);
$ret->results[] = $new_result;
$state = 1;
} else {
$parser->error('Unexpected keyword.', $token);
}
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/Components/Condition.php
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ public static function parse(Parser $parser, TokensList $list, array $options =

// Conditions are delimited by logical operators.
if (in_array($token->value, static::$DELIMITERS, true)) {
if (($betweenBefore) && ($token->value === 'AND')) {
if ($betweenBefore && ($token->value === 'AND')) {
// The syntax of keyword `BETWEEN` is hard-coded.
$betweenBefore = false;
} else {
Expand Down Expand Up @@ -170,7 +170,7 @@ public static function parse(Parser $parser, TokensList $list, array $options =
if ($token->value === 'BETWEEN') {
$betweenBefore = true;
}
if (($brackets === 0) && (empty(static::$ALLOWED_KEYWORDS[$token->value]))) {
if (($brackets === 0) && empty(static::$ALLOWED_KEYWORDS[$token->value])) {
break;
}
}
Expand Down
6 changes: 3 additions & 3 deletions src/Components/CreateDefinition.php
Original file line number Diff line number Diff line change
Expand Up @@ -259,7 +259,7 @@ public static function parse(Parser $parser, TokensList $list, array $options =
}
$state = 5;
} elseif ($state === 5) {
if ((!empty($expr->type)) || (!empty($expr->key))) {
if (!empty($expr->type) || !empty($expr->key)) {
$ret[] = $expr;
}
$expr = new self();
Expand All @@ -281,7 +281,7 @@ public static function parse(Parser $parser, TokensList $list, array $options =
}

// Last iteration was not saved.
if ((!empty($expr->type)) || (!empty($expr->key))) {
if (!empty($expr->type) || !empty($expr->key)) {
$ret[] = $expr;
}

Expand Down Expand Up @@ -315,7 +315,7 @@ public static function build($component, array $options = array())
$tmp .= 'CONSTRAINT ';
}

if ((isset($component->name)) && ($component->name !== '')) {
if (isset($component->name) && ($component->name !== '')) {
$tmp .= Context::escape($component->name) . ' ';
}

Expand Down
2 changes: 1 addition & 1 deletion src/Components/DataType.php
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ public static function parse(Parser $parser, TokensList $list, array $options =
*/
public static function build($component, array $options = array())
{
$name = (empty($options['lowercase'])) ?
$name = empty($options['lowercase']) ?
$component->name : strtolower($component->name);

$parameters = '';
Expand Down
46 changes: 24 additions & 22 deletions src/Components/Expression.php
Original file line number Diff line number Diff line change
Expand Up @@ -215,8 +215,8 @@ public static function parse(Parser $parser, TokensList $list, array $options =
}

if ($token->type === Token::TYPE_KEYWORD) {
if (($brackets > 0) && (empty($ret->subquery))
&& (!empty(Parser::$STATEMENT_PARSERS[$token->keyword]))
if (($brackets > 0) && empty($ret->subquery)
&& !empty(Parser::$STATEMENT_PARSERS[$token->keyword])
) {
// A `(` was previously found and this keyword is the
// beginning of a statement, so this is a subquery.
Expand Down Expand Up @@ -282,39 +282,41 @@ public static function parse(Parser $parser, TokensList $list, array $options =
}

if ($token->type === Token::TYPE_OPERATOR) {
if ((!empty($options['breakOnParentheses']))
if (!empty($options['breakOnParentheses'])
&& (($token->value === '(') || ($token->value === ')'))
) {
// No brackets were expected.
break;
}
if ($token->value === '(') {
++$brackets;
if ((empty($ret->function)) && ($prev[1] !== null)
if (empty($ret->function) && ($prev[1] !== null)
&& (($prev[1]->type === Token::TYPE_NONE)
|| ($prev[1]->type === Token::TYPE_SYMBOL)
|| (($prev[1]->type === Token::TYPE_KEYWORD)
&& ($prev[1]->flags & Token::FLAG_KEYWORD_FUNCTION)))
) {
$ret->function = $prev[1]->value;
}
} elseif ($token->value === ')' && $brackets == 0) {
// Not our bracket
break;
} elseif ($token->value === ')') {
--$brackets;
if ($brackets === 0) {
if (!empty($options['parenthesesDelimited'])) {
// The current token is the last bracket, the next
// one will be outside the expression.
$ret->expr .= $token->token;
++$list->idx;
if ($brackets == 0) {
// Not our bracket
break;
} else {
--$brackets;
if ($brackets === 0) {
if (!empty($options['parenthesesDelimited'])) {
// The current token is the last bracket, the next
// one will be outside the expression.
$ret->expr .= $token->token;
++$list->idx;
break;
}
} elseif ($brackets < 0) {
// $parser->error('Unexpected closing bracket.', $token);
// $brackets = 0;
break;
}
} elseif ($brackets < 0) {
// $parser->error('Unexpected closing bracket.', $token);
// $brackets = 0;
break;
}
} elseif ($token->value === ',') {
// Expressions are comma-delimited.
Expand Down Expand Up @@ -362,7 +364,7 @@ public static function parse(Parser $parser, TokensList $list, array $options =
// Found a `.` which means we expect a column name and
// the column name we parsed is actually the table name
// and the table name is actually a database name.
if ((!empty($ret->database)) || ($dot)) {
if (!empty($ret->database) || $dot) {
$parser->error('Unexpected dot.', $token);
}
$ret->database = $ret->table;
Expand Down Expand Up @@ -426,13 +428,13 @@ public static function build($component, array $options = array())
$ret = $component->expr;
} else {
$fields = array();
if ((isset($component->database)) && ($component->database !== '')) {
if (isset($component->database) && ($component->database !== '')) {
$fields[] = $component->database;
}
if ((isset($component->table)) && ($component->table !== '')) {
if (isset($component->table) && ($component->table !== '')) {
$fields[] = $component->table;
}
if ((isset($component->column)) && ($component->column !== '')) {
if (isset($component->column) && ($component->column !== '')) {
$fields[] = $component->column;
}
$ret = implode('.', Context::escape($fields));
Expand Down
8 changes: 2 additions & 6 deletions src/Components/IntoKeyword.php
Original file line number Diff line number Diff line change
Expand Up @@ -237,11 +237,7 @@ public function parseFileOptions(Parser $parser, TokensList $list, $keyword = 'F
static::$FIELDS_OPTIONS
);

if ($keyword === 'FIELDS') {
$this->fields_keyword = true;
} else {
$this->fields_keyword = false;
}
$this->fields_keyword = ($keyword === 'FIELDS');
} else {
// parse line options
$this->lines_options = OptionsArray::parse(
Expand Down Expand Up @@ -272,7 +268,7 @@ public static function build($component, array $options = array())

$fields_options_str = OptionsArray::build($component->fields_options);
if (trim($fields_options_str) !== '') {
$ret .= ($component->fields_keyword) ? ' FIELDS' : ' COLUMNS';
$ret .= $component->fields_keyword ? ' FIELDS' : ' COLUMNS';
$ret .= ' ' . $fields_options_str;
}

Expand Down
37 changes: 20 additions & 17 deletions src/Components/JoinKeyword.php
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ public static function parse(Parser $parser, TokensList $list, array $options =

if ($state === 0) {
if (($token->type === Token::TYPE_KEYWORD)
&& (!empty(static::$JOINS[$token->keyword]))
&& !empty(static::$JOINS[$token->keyword])
) {
$expr->type = static::$JOINS[$token->keyword];
$state = 1;
Expand All @@ -163,22 +163,25 @@ public static function parse(Parser $parser, TokensList $list, array $options =
$state = 2;
} elseif ($state === 2) {
if ($token->type === Token::TYPE_KEYWORD) {
if ($token->keyword === 'ON') {
$state = 3;
} elseif ($token->keyword === 'USING') {
$state = 4;
} else {
if (($token->type === Token::TYPE_KEYWORD)
&& (!empty(static::$JOINS[$token->keyword]))
) {
$ret[] = $expr;
$expr = new self();
$expr->type = static::$JOINS[$token->keyword];
$state = 1;
} else {
/* Next clause is starting */
break;
}
switch($token->keyword) {
case 'ON':
$state = 3;
break;
case 'USING':
$state = 4;
break;
default:
if (!empty(static::$JOINS[$token->keyword])
) {
$ret[] = $expr;
$expr = new self();
$expr->type = static::$JOINS[$token->keyword];
$state = 1;
} else {
/* Next clause is starting */
break;
}
break;
}
}
} elseif ($state === 3) {
Expand Down
2 changes: 1 addition & 1 deletion src/Components/ParameterDefinition.php
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ public static function parse(Parser $parser, TokensList $list, array $options =
}

// Last iteration was not saved.
if ((isset($expr->name)) && ($expr->name !== '')) {
if (isset($expr->name) && ($expr->name !== '')) {
$ret[] = $expr;
}

Expand Down
4 changes: 2 additions & 2 deletions src/Components/UnionKeyword.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ class UnionKeyword extends Component
public static function build($component, array $options = array())
{
$tmp = array();
foreach ($component as $component) {
$tmp[] = $component[0] . ' ' . $component[1];
foreach ($component as $componentPart) {
$tmp[] = $componentPart[0] . ' ' . $componentPart[1];
}

return implode(' ', $tmp);
Expand Down
Loading

0 comments on commit 513ed81

Please sign in to comment.