Skip to content

Commit

Permalink
Merge pull request #206 from bperel/cleanup
Browse files Browse the repository at this point in the history
Cleanup the code

* Avoid duplicate if conditions
* Use switch/case instead of ifs when possible
* Use triple (in)equalities when type compatibility is ensured
  • Loading branch information
devenbansod committed Dec 9, 2018
2 parents ad6a0ce + 4b3684f commit 5e8dbcf
Show file tree
Hide file tree
Showing 36 changed files with 491 additions and 506 deletions.
2 changes: 1 addition & 1 deletion src/Components/AlterOperation.php
Original file line number Diff line number Diff line change
Expand Up @@ -236,7 +236,7 @@ public static function parse(Parser $parser, TokensList $list, array $options =
// We have reached the end of ALTER operation and suddenly found
// a start to new statement, but have not find a delimiter between them

if (!($token->value == 'SET' && $list->tokens[$list->idx - 1]->value == 'CHARACTER')) {
if (!($token->value === 'SET' && $list->tokens[$list->idx - 1]->value === 'CHARACTER')) {
$parser->error(
'A new statement was found, but no delimiter between it and the previous one.',
$token
Expand Down
2 changes: 1 addition & 1 deletion src/Components/Array2d.php
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ public static function parse(Parser $parser, TokensList $list, array $options =
$arrCount = count($arr->values);
if ($count === -1) {
$count = $arrCount;
} elseif ($arrCount != $count) {
} elseif ($arrCount !== $count) {
$parser->error(
sprintf(
Translator::gettext('%1$d values were expected, but found %2$d.'),
Expand Down
88 changes: 43 additions & 45 deletions src/Components/CaseExpression.php
Original file line number Diff line number Diff line change
Expand Up @@ -112,58 +112,56 @@ 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 2;
default:
$parser->error('Unexpected keyword.', $token);
break 2;
}
} 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 2;
default:
$parser->error('Unexpected keyword.', $token);
break 2;
}
}
} else {
if ($token->type === Token::TYPE_KEYWORD
Expand Down
6 changes: 3 additions & 3 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 All @@ -179,7 +179,7 @@ public static function parse(Parser $parser, TokensList $list, array $options =
if ($token->value === '(') {
++$brackets;
} elseif ($token->value === ')') {
if ($brackets == 0) {
if ($brackets === 0) {
break;
}
--$brackets;
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
44 changes: 23 additions & 21 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;
// 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
12 changes: 4 additions & 8 deletions src/Components/IntoKeyword.php
Original file line number Diff line number Diff line change
Expand Up @@ -207,10 +207,10 @@ public static function parse(Parser $parser, TokensList $list, array $options =
$ret->dest = $token->value;

$state = 3;
} elseif ($state == 3) {
} elseif ($state === 3) {
$ret->parseFileOptions($parser, $list, $token->value);
$state = 4;
} elseif ($state == 4) {
} elseif ($state === 4) {
if ($token->type === Token::TYPE_KEYWORD && $token->keyword !== 'LINES') {
break;
}
Expand All @@ -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 2;
}
break;
}
}
} elseif ($state === 3) {
Expand Down
8 changes: 4 additions & 4 deletions src/Components/OptionsArray.php
Original file line number Diff line number Diff line change
Expand Up @@ -247,10 +247,10 @@ public static function parse(Parser $parser, TokensList $list, array $options =
*/
if ($state === 1
&& $lastOption
&& ($lastOption[1] == 'expr'
|| $lastOption[1] == 'var'
|| $lastOption[1] == 'var='
|| $lastOption[1] == 'expr=')
&& ($lastOption[1] === 'expr'
|| $lastOption[1] === 'var'
|| $lastOption[1] === 'var='
|| $lastOption[1] === 'expr=')
) {
$parser->error(
sprintf(
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
Loading

0 comments on commit 5e8dbcf

Please sign in to comment.