diff options
author | Bruno Perel <brunoperel@gmail.com> | 2018-11-26 19:56:17 +0100 |
---|---|---|
committer | Bruno Perel <brunoperel@gmail.com> | 2018-11-26 20:08:52 +0100 |
commit | 513ed8175bdc0fc17a192ea29757cf8fee178c2a (patch) | |
tree | c87f72dcdc4d5f6f86e5da4d212e7be92cf9506c | |
parent | 8e37bb398b9bf7449c6855f3e0639156cef35e7e (diff) | |
download | sql-parser-513ed8175bdc0fc17a192ea29757cf8fee178c2a.zip sql-parser-513ed8175bdc0fc17a192ea29757cf8fee178c2a.tar.gz sql-parser-513ed8175bdc0fc17a192ea29757cf8fee178c2a.tar.bz2 |
Cleanup and improve readability:
Avoid duplicate if conditions
Use switch/case instead of ifs when possible
28 files changed, 457 insertions, 473 deletions
diff --git a/src/Components/CaseExpression.php b/src/Components/CaseExpression.php index 820947b..eb91f5f 100644 --- a/src/Components/CaseExpression.php +++ b/src/Components/CaseExpression.php @@ -112,29 +112,28 @@ class CaseExpression extends Component } 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; @@ -142,54 +141,50 @@ class CaseExpression extends Component } } 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); + } } } } diff --git a/src/Components/Condition.php b/src/Components/Condition.php index 99ab34c..4e12b3f 100644 --- a/src/Components/Condition.php +++ b/src/Components/Condition.php @@ -142,7 +142,7 @@ class Condition extends Component // 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 { @@ -170,7 +170,7 @@ class Condition extends Component 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; } } diff --git a/src/Components/CreateDefinition.php b/src/Components/CreateDefinition.php index f85ad0f..21ddae9 100644 --- a/src/Components/CreateDefinition.php +++ b/src/Components/CreateDefinition.php @@ -259,7 +259,7 @@ class CreateDefinition extends Component } $state = 5; } elseif ($state === 5) { - if ((!empty($expr->type)) || (!empty($expr->key))) { + if (!empty($expr->type) || !empty($expr->key)) { $ret[] = $expr; } $expr = new self(); @@ -281,7 +281,7 @@ class CreateDefinition extends Component } // Last iteration was not saved. - if ((!empty($expr->type)) || (!empty($expr->key))) { + if (!empty($expr->type) || !empty($expr->key)) { $ret[] = $expr; } @@ -315,7 +315,7 @@ class CreateDefinition extends Component $tmp .= 'CONSTRAINT '; } - if ((isset($component->name)) && ($component->name !== '')) { + if (isset($component->name) && ($component->name !== '')) { $tmp .= Context::escape($component->name) . ' '; } diff --git a/src/Components/DataType.php b/src/Components/DataType.php index 52d8775..51f04fa 100644 --- a/src/Components/DataType.php +++ b/src/Components/DataType.php @@ -153,7 +153,7 @@ class DataType extends Component */ public static function build($component, array $options = array()) { - $name = (empty($options['lowercase'])) ? + $name = empty($options['lowercase']) ? $component->name : strtolower($component->name); $parameters = ''; diff --git a/src/Components/Expression.php b/src/Components/Expression.php index cce797f..4a83f63 100644 --- a/src/Components/Expression.php +++ b/src/Components/Expression.php @@ -215,8 +215,8 @@ class Expression extends Component } 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. @@ -282,7 +282,7 @@ class Expression extends Component } if ($token->type === Token::TYPE_OPERATOR) { - if ((!empty($options['breakOnParentheses'])) + if (!empty($options['breakOnParentheses']) && (($token->value === '(') || ($token->value === ')')) ) { // No brackets were expected. @@ -290,7 +290,7 @@ class Expression extends Component } 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) @@ -298,23 +298,25 @@ class Expression extends Component ) { $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. @@ -362,7 +364,7 @@ class Expression extends Component // 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; @@ -426,13 +428,13 @@ class Expression extends Component $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)); diff --git a/src/Components/IntoKeyword.php b/src/Components/IntoKeyword.php index 17615f6..dd3f7f6 100644 --- a/src/Components/IntoKeyword.php +++ b/src/Components/IntoKeyword.php @@ -237,11 +237,7 @@ class IntoKeyword extends Component 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( @@ -272,7 +268,7 @@ class IntoKeyword extends Component $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; } diff --git a/src/Components/JoinKeyword.php b/src/Components/JoinKeyword.php index 55b69ae..0b5aae9 100644 --- a/src/Components/JoinKeyword.php +++ b/src/Components/JoinKeyword.php @@ -151,7 +151,7 @@ class JoinKeyword extends Component 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; @@ -163,22 +163,25 @@ class JoinKeyword extends Component $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) { diff --git a/src/Components/ParameterDefinition.php b/src/Components/ParameterDefinition.php index 82a3567..1f810f5 100644 --- a/src/Components/ParameterDefinition.php +++ b/src/Components/ParameterDefinition.php @@ -138,7 +138,7 @@ class ParameterDefinition extends Component } // Last iteration was not saved. - if ((isset($expr->name)) && ($expr->name !== '')) { + if (isset($expr->name) && ($expr->name !== '')) { $ret[] = $expr; } diff --git a/src/Components/UnionKeyword.php b/src/Components/UnionKeyword.php index 4aa0f94..2de5126 100644 --- a/src/Components/UnionKeyword.php +++ b/src/Components/UnionKeyword.php @@ -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); diff --git a/src/Context.php b/src/Context.php index 4aaa956..b381aa7 100644 --- a/src/Context.php +++ b/src/Context.php @@ -255,10 +255,8 @@ abstract class Context $str = strtoupper($str); if (isset(static::$KEYWORDS[$str])) { - if ($isReserved) { - if (!(static::$KEYWORDS[$str] & Token::FLAG_KEYWORD_RESERVED)) { - return null; - } + if ($isReserved && !(static::$KEYWORDS[$str] & Token::FLAG_KEYWORD_RESERVED)) { + return null; } return static::$KEYWORDS[$str]; @@ -321,15 +319,15 @@ abstract class Context if ($str[0] === '#') { return Token::FLAG_COMMENT_BASH; } elseif (($len > 1) && ($str[0] === '/') && ($str[1] === '*')) { - return (($len > 2) && ($str[2] == '!')) ? + return (($len > 2) && ($str[2] === '!')) ? Token::FLAG_COMMENT_MYSQL_CMD : Token::FLAG_COMMENT_C; } elseif (($len > 1) && ($str[0] === '*') && ($str[1] === '/')) { return Token::FLAG_COMMENT_C; } elseif (($len > 2) && ($str[0] === '-') - && ($str[1] === '-') && (static::isWhitespace($str[2])) + && ($str[1] === '-') && static::isWhitespace($str[2]) ) { return Token::FLAG_COMMENT_SQL; - } elseif (($len == 2) && $end && ($str[0] === '-') && ($str[1] === '-')) { + } elseif (($len === 2) && $end && ($str[0] === '-') && ($str[1] === '-')) { return Token::FLAG_COMMENT_SQL; } diff --git a/src/Lexer.php b/src/Lexer.php index f5972d8..ef1ca68 100644 --- a/src/Lexer.php +++ b/src/Lexer.php @@ -883,7 +883,7 @@ class Lexer extends Core if ($this->last < $this->len) { if (($str = $this->parseString('`')) === null) { - if (($str = static::parseUnknown()) === null) { + if (($str = $this->parseUnknown()) === null) { $this->error( 'Variable name was expected.', $this->str[$this->last], diff --git a/src/Parser.php b/src/Parser.php index 11196ef..ab97855 100644 --- a/src/Parser.php +++ b/src/Parser.php @@ -340,7 +340,7 @@ class Parser extends Core */ public function __construct($list = null, $strict = false) { - if ((is_string($list)) || ($list instanceof UtfString)) { + if (is_string($list) || ($list instanceof UtfString)) { $lexer = new Lexer($list, $strict); $this->list = $lexer->list; } elseif ($list instanceof TokensList) { @@ -488,7 +488,7 @@ class Parser extends Core $prevLastIdx = $list->idx; // Handles unions. - if ((!empty($unionType)) + if (!empty($unionType) && ($lastStatement instanceof SelectStatement) && ($statement instanceof SelectStatement) ) { diff --git a/src/Statement.php b/src/Statement.php index f7ed9ab..50acb71 100644 --- a/src/Statement.php +++ b/src/Statement.php @@ -260,7 +260,7 @@ abstract class Statement // ON DUPLICATE KEY UPDATE ... // has to be parsed in parent statement (INSERT or REPLACE) // so look for it and break - if ($this instanceof \PhpMyAdmin\SqlParser\Statements\SelectStatement + if ($this instanceof Statements\SelectStatement && $token->value === 'ON' ) { ++$list->idx; // Skip ON @@ -303,8 +303,8 @@ abstract class Statement $options = array(); // Looking for duplicated clauses. - if ((!empty(Parser::$KEYWORD_PARSERS[$token->value])) - || (!empty(Parser::$STATEMENT_PARSERS[$token->value])) + if (!empty(Parser::$KEYWORD_PARSERS[$token->value]) + || !empty(Parser::$STATEMENT_PARSERS[$token->value]) ) { if (!empty($parsedClauses[$token->value])) { $parser->error( @@ -327,8 +327,8 @@ abstract class Statement // Checking if this is the beginning of the statement. if (!empty(Parser::$STATEMENT_PARSERS[$token->keyword])) { - if ((!empty(static::$CLAUSES)) // Undefined for some statements. - && (empty(static::$CLAUSES[$token->value])) + if (!empty(static::$CLAUSES) // Undefined for some statements. + && empty(static::$CLAUSES[$token->value]) ) { // Some keywords (e.g. `SET`) may be the beginning of a // statement and a clause. @@ -356,7 +356,7 @@ abstract class Statement } elseif ($class === null) { // Handle special end options in Select statement // See Statements\SelectStatement::$END_OPTIONS - if ($this instanceof \PhpMyAdmin\SqlParser\Statements\SelectStatement + if ($this instanceof Statements\SelectStatement && ($token->value === 'FOR UPDATE' || $token->value === 'LOCK IN SHARE MODE') ) { diff --git a/src/Statements/CreateStatement.php b/src/Statements/CreateStatement.php index 6510e52..c164c25 100644 --- a/src/Statements/CreateStatement.php +++ b/src/Statements/CreateStatement.php @@ -279,41 +279,43 @@ class CreateStatement extends Statement . OptionsArray::build($this->options) . ' ' . Expression::build($this->name) . ' ' . OptionsArray::build($this->entityOptions); - } elseif ($this->options->has('TABLE') && !is_null($this->select)) { - return 'CREATE ' - . OptionsArray::build($this->options) . ' ' - . Expression::build($this->name) . ' ' - . $this->select->build(); - } elseif ($this->options->has('TABLE') && !is_null($this->like)) { - return 'CREATE ' - . OptionsArray::build($this->options) . ' ' - . Expression::build($this->name) . ' LIKE ' - . Expression::build($this->like); } elseif ($this->options->has('TABLE')) { - $partition = ''; + if (!is_null($this->select)) { + return 'CREATE ' + . OptionsArray::build($this->options) . ' ' + . Expression::build($this->name) . ' ' + . $this->select->build(); + } elseif (!is_null($this->like)) { + return 'CREATE ' + . OptionsArray::build($this->options) . ' ' + . Expression::build($this->name) . ' LIKE ' + . Expression::build($this->like); + } else { + $partition = ''; + + if (!empty($this->partitionBy)) { + $partition .= "\nPARTITION BY " . $this->partitionBy; + } + if (!empty($this->partitionsNum)) { + $partition .= "\nPARTITIONS " . $this->partitionsNum; + } + if (!empty($this->subpartitionBy)) { + $partition .= "\nSUBPARTITION BY " . $this->subpartitionBy; + } + if (!empty($this->subpartitionsNum)) { + $partition .= "\nSUBPARTITIONS " . $this->subpartitionsNum; + } + if (!empty($this->partitions)) { + $partition .= "\n" . PartitionDefinition::build($this->partitions); + } - if (!empty($this->partitionBy)) { - $partition .= "\nPARTITION BY " . $this->partitionBy; - } - if (!empty($this->partitionsNum)) { - $partition .= "\nPARTITIONS " . $this->partitionsNum; - } - if (!empty($this->subpartitionBy)) { - $partition .= "\nSUBPARTITION BY " . $this->subpartitionBy; + return 'CREATE ' + . OptionsArray::build($this->options) . ' ' + . Expression::build($this->name) . ' ' + . $fields + . OptionsArray::build($this->entityOptions) + . $partition; } - if (!empty($this->subpartitionsNum)) { - $partition .= "\nSUBPARTITIONS " . $this->subpartitionsNum; - } - if (!empty($this->partitions)) { - $partition .= "\n" . PartitionDefinition::build($this->partitions); - } - - return 'CREATE ' - . OptionsArray::build($this->options) . ' ' - . Expression::build($this->name) . ' ' - . $fields - . OptionsArray::build($this->entityOptions) - . $partition; } elseif ($this->options->has('VIEW')) { return 'CREATE ' . OptionsArray::build($this->options) . ' ' @@ -327,8 +329,8 @@ class CreateStatement extends Statement . OptionsArray::build($this->entityOptions) . ' ' . 'ON ' . Expression::build($this->table) . ' ' . 'FOR EACH ROW ' . TokensList::build($this->body); - } elseif (($this->options->has('PROCEDURE')) - || ($this->options->has('FUNCTION')) + } elseif ($this->options->has('PROCEDURE') + || $this->options->has('FUNCTION') ) { $tmp = ''; if ($this->options->has('FUNCTION')) { @@ -370,7 +372,7 @@ class CreateStatement extends Statement ) ); - if ((!isset($this->name)) || ($this->name === '')) { + if (!isset($this->name) || ($this->name === '')) { $parser->error( 'The name of the entity was expected.', $list->tokens[$list->idx] @@ -396,150 +398,148 @@ class CreateStatement extends Statement $list, static::$DB_OPTIONS ); - } elseif ($this->options->has('TABLE') - && ($token->type == Token::TYPE_KEYWORD) - && ($token->keyword == 'SELECT') - ) { - /* CREATE TABLE ... SELECT */ - $this->select = new SelectStatement($parser, $list); - } elseif ($this->options->has('TABLE') - && ($token->type == Token::TYPE_KEYWORD) && ($token->keyword == 'AS') - && ($list->tokens[$nextidx]->type == Token::TYPE_KEYWORD) - && ($list->tokens[$nextidx]->value == 'SELECT') - ) { - /* CREATE TABLE ... AS SELECT */ - $list->idx = $nextidx; - $this->select = new SelectStatement($parser, $list); - } elseif ($this->options->has('TABLE') - && $token->type == Token::TYPE_KEYWORD - && $token->keyword == 'LIKE' - ) { - /* CREATE TABLE `new_tbl` LIKE 'orig_tbl' */ - $list->idx = $nextidx; - $this->like = Expression::parse( - $parser, - $list, - array( - 'parseField' => 'table', - 'breakOnAlias' => true, - ) - ); - // The 'LIKE' keyword was found, but no table_name was found next to it - if ($this->like == null) { - $parser->error( - 'A table name was expected.', - $list->tokens[$list->idx] - ); - } } elseif ($this->options->has('TABLE')) { - $this->fields = CreateDefinition::parse($parser, $list); - if (empty($this->fields)) { - $parser->error( - 'At least one column definition was expected.', - $list->tokens[$list->idx] + if (($token->type == Token::TYPE_KEYWORD) + && ($token->keyword == 'SELECT')) { + /* CREATE TABLE ... SELECT */ + $this->select = new SelectStatement($parser, $list); + } elseif (($token->type == Token::TYPE_KEYWORD) && ($token->keyword == 'AS') + && ($list->tokens[$nextidx]->type == Token::TYPE_KEYWORD) + && ($list->tokens[$nextidx]->value == 'SELECT')) { + /* CREATE TABLE ... AS SELECT */ + $list->idx = $nextidx; + $this->select = new SelectStatement($parser, $list); + } elseif ($token->type == Token::TYPE_KEYWORD + && $token->keyword == 'LIKE') { + /* CREATE TABLE `new_tbl` LIKE 'orig_tbl' */ + $list->idx = $nextidx; + $this->like = Expression::parse( + $parser, + $list, + array( + 'parseField' => 'table', + 'breakOnAlias' => true, + ) ); - } - ++$list->idx; + // The 'LIKE' keyword was found, but no table_name was found next to it + if ($this->like == null) { + $parser->error( + 'A table name was expected.', + $list->tokens[$list->idx] + ); + } + } else { + $this->fields = CreateDefinition::parse($parser, $list); + if (empty($this->fields)) { + $parser->error( + 'At least one column definition was expected.', + $list->tokens[$list->idx] + ); + } + ++$list->idx; - $this->entityOptions = OptionsArray::parse( - $parser, - $list, - static::$TABLE_OPTIONS - ); + $this->entityOptions = OptionsArray::parse( + $parser, + $list, + static::$TABLE_OPTIONS + ); - /** - * The field that is being filled (`partitionBy` or - * `subpartitionBy`). - * - * @var string - */ - $field = null; - - /** - * The number of brackets. `false` means no bracket was found - * previously. At least one bracket is required to validate the - * expression. - * - * @var int|bool - */ - $brackets = false; - - /* - * Handles partitions. - */ - for (; $list->idx < $list->count; ++$list->idx) { /** - * Token parsed at this moment. + * The field that is being filled (`partitionBy` or + * `subpartitionBy`). * - * @var Token + * @var string */ - $token = $list->tokens[$list->idx]; + $field = null; - // End of statement. - if ($token->type === Token::TYPE_DELIMITER) { - break; - } - - // Skipping comments. - if ($token->type === Token::TYPE_COMMENT) { - continue; - } + /** + * The number of brackets. `false` means no bracket was found + * previously. At least one bracket is required to validate the + * expression. + * + * @var int|bool + */ + $brackets = false; - if (($token->type === Token::TYPE_KEYWORD) && ($token->keyword === 'PARTITION BY')) { - $field = 'partitionBy'; - $brackets = false; - } elseif (($token->type === Token::TYPE_KEYWORD) && ($token->keyword === 'SUBPARTITION BY')) { - $field = 'subpartitionBy'; - $brackets = false; - } elseif (($token->type === Token::TYPE_KEYWORD) && ($token->keyword === 'PARTITIONS')) { - $token = $list->getNextOfType(Token::TYPE_NUMBER); - --$list->idx; // `getNextOfType` also advances one position. - $this->partitionsNum = $token->value; - } elseif (($token->type === Token::TYPE_KEYWORD) && ($token->keyword === 'SUBPARTITIONS')) { - $token = $list->getNextOfType(Token::TYPE_NUMBER); - --$list->idx; // `getNextOfType` also advances one position. - $this->subpartitionsNum = $token->value; - } elseif (!empty($field)) { - /* - * Handling the content of `PARTITION BY` and `SUBPARTITION BY`. + /* + * Handles partitions. + */ + for (; $list->idx < $list->count; ++$list->idx) { + /** + * Token parsed at this moment. + * + * @var Token */ + $token = $list->tokens[$list->idx]; - // Counting brackets. - if (($token->type === Token::TYPE_OPERATOR) && ($token->value === '(')) { - // This is used instead of `++$brackets` because, - // initially, `$brackets` is `false` cannot be - // incremented. - $brackets = $brackets + 1; - } elseif (($token->type === Token::TYPE_OPERATOR) && ($token->value === ')')) { - --$brackets; + // End of statement. + if ($token->type === Token::TYPE_DELIMITER) { + break; } - // Building the expression used for partitioning. - $this->$field .= ($token->type === Token::TYPE_WHITESPACE) ? ' ' : $token->token; - - // Last bracket was read, the expression ended. - // Comparing with `0` and not `false`, because `false` means - // that no bracket was found and at least one must is - // required. - if ($brackets === 0) { - $this->$field = trim($this->$field); - $field = null; + // Skipping comments. + if ($token->type === Token::TYPE_COMMENT) { + continue; } - } elseif (($token->type === Token::TYPE_OPERATOR) && ($token->value === '(')) { - if (!empty($this->partitionBy)) { - $this->partitions = ArrayObj::parse( - $parser, - $list, - array( - 'type' => 'PhpMyAdmin\\SqlParser\\Components\\PartitionDefinition', - ) - ); + + if (($token->type === Token::TYPE_KEYWORD) && ($token->keyword === 'PARTITION BY')) { + $field = 'partitionBy'; + $brackets = false; + } elseif (($token->type === Token::TYPE_KEYWORD) && ($token->keyword === 'SUBPARTITION BY')) { + $field = 'subpartitionBy'; + $brackets = false; + } elseif (($token->type === Token::TYPE_KEYWORD) && ($token->keyword === 'PARTITIONS')) { + $token = $list->getNextOfType(Token::TYPE_NUMBER); + --$list->idx; // `getNextOfType` also advances one position. + $this->partitionsNum = $token->value; + } elseif (($token->type === Token::TYPE_KEYWORD) && ($token->keyword === 'SUBPARTITIONS')) { + $token = $list->getNextOfType(Token::TYPE_NUMBER); + --$list->idx; // `getNextOfType` also advances one position. + $this->subpartitionsNum = $token->value; + } elseif (!empty($field)) { + /* + * Handling the content of `PARTITION BY` and `SUBPARTITION BY`. + */ + + // Counting brackets. + if ($token->type === Token::TYPE_OPERATOR) { + if ($token->value === '(') { + // This is used instead of `++$brackets` because, + // initially, `$brackets` is `false` cannot be + // incremented. + ++$brackets; + } elseif ($token->value === ')') { + --$brackets; + } + } + + // Building the expression used for partitioning. + $this->$field .= ($token->type === Token::TYPE_WHITESPACE) ? ' ' : $token->token; + + // Last bracket was read, the expression ended. + // Comparing with `0` and not `false`, because `false` means + // that no bracket was found and at least one must is + // required. + if ($brackets === 0) { + $this->$field = trim($this->$field); + $field = null; + } + } elseif (($token->type === Token::TYPE_OPERATOR) && ($token->value === '(')) { + if (!empty($this->partitionBy)) { + $this->partitions = ArrayObj::parse( + $parser, + $list, + array( + 'type' => 'PhpMyAdmin\\SqlParser\\Components\\PartitionDefinition', + ) + ); + } + break; } - break; } } - } elseif (($this->options->has('PROCEDURE')) - || ($this->options->has('FUNCTION')) + } elseif ($this->options->has('PROCEDURE') + || $this->options->has('FUNCTION') ) { $this->parameters = ParameterDefinition::parse($parser, $list); if ($this->options->has('FUNCTION')) { diff --git a/src/Statements/DeleteStatement.php b/src/Statements/DeleteStatement.php index bf47cd8..20b6bcf 100644 --- a/src/Statements/DeleteStatement.php +++ b/src/Statements/DeleteStatement.php @@ -221,87 +221,83 @@ class DeleteStatement extends Statement } if ($state === 0) { - if ($token->type === Token::TYPE_KEYWORD - && $token->keyword !== 'FROM' - ) { - $parser->error('Unexpected keyword.', $token); - break; - } elseif ($token->type === Token::TYPE_KEYWORD - && $token->keyword === 'FROM' - ) { - ++$list->idx; // Skip 'FROM' - $this->from = ExpressionArray::parse($parser, $list); + if ($token->type === Token::TYPE_KEYWORD) { + if ($token->keyword !== 'FROM') { + $parser->error('Unexpected keyword.', $token); + break; + } else { + ++$list->idx; // Skip 'FROM' + $this->from = ExpressionArray::parse($parser, $list); - $state = 2; + $state = 2; + } } else { $this->columns = ExpressionArray::parse($parser, $list); $state = 1; } } elseif ($state === 1) { - if ($token->type === Token::TYPE_KEYWORD - && $token->keyword !== 'FROM' - ) { - $parser->error('Unexpected keyword.', $token); - break; - } elseif ($token->type === Token::TYPE_KEYWORD - && $token->keyword === 'FROM' - ) { - ++$list->idx; // Skip 'FROM' - $this->from = ExpressionArray::parse($parser, $list); + if ($token->type === Token::TYPE_KEYWORD) { + if ($token->keyword !== 'FROM') { + $parser->error('Unexpected keyword.', $token); + break; + } else { + ++$list->idx; // Skip 'FROM' + $this->from = ExpressionArray::parse($parser, $list); - $state = 2; + $state = 2; + } } else { $parser->error('Unexpected token.', $token); break; } } elseif ($state === 2) { - if ($token->type === Token::TYPE_KEYWORD - && stripos($token->keyword, 'JOIN') !== false - ) { - ++$list->idx; - $this->join = JoinKeyword::parse($parser, $list); + if ($token->type === Token::TYPE_KEYWORD) { + if (stripos($token->keyword, 'JOIN') !== false) { + ++$list->idx; + $this->join = JoinKeyword::parse($parser, $list); - // remain in state = 2 - } elseif ($token->type === Token::TYPE_KEYWORD - && $token->keyword === 'USING' - ) { - ++$list->idx; // Skip 'USING' - $this->using = ExpressionArray::parse($parser, $list); - $state = 3; + // remain in state = 2 + } + else { + switch($token->keyword) { + case 'USING': + ++$list->idx; // Skip 'USING' + $this->using = ExpressionArray::parse($parser, $list); + $state = 3; - $multiTable = true; - } elseif ($token->type === Token::TYPE_KEYWORD - && $token->keyword === 'WHERE' - ) { - ++$list->idx; // Skip 'WHERE' - $this->where = Condition::parse($parser, $list); - $state = 4; - } elseif ($token->type === Token::TYPE_KEYWORD - && $token->keyword === 'ORDER BY' - ) { - ++$list->idx; // Skip 'ORDER BY' - $this->order = OrderKeyword::parse($parser, $list); - $state = 5; - } elseif ($token->type === Token::TYPE_KEYWORD - && $token->keyword === 'LIMIT' - ) { - ++$list->idx; // Skip 'LIMIT' - $this->limit = Limit::parse($parser, $list); - $state = 6; - } elseif ($token->type === Token::TYPE_KEYWORD) { - $parser->error('Unexpected keyword.', $token); - break; + $multiTable = true; + break; + case 'WHERE': + ++$list->idx; // Skip 'WHERE' + $this->where = Condition::parse($parser, $list); + $state = 4; + break; + case 'ORDER BY': + ++$list->idx; // Skip 'ORDER BY' + $this->order = OrderKeyword::parse($parser, $list); + $state = 5; + break; + case 'LIMIT': + ++$list->idx; // Skip 'LIMIT' + $this->limit = Limit::parse($parser, $list); + $state = 6; + break; + default: + $parser->error('Unexpected keyword.', $token); + break 2; + } + } } } elseif ($state === 3) { - if ($token->type === Token::TYPE_KEYWORD - && $token->keyword === 'WHERE' - ) { - ++$list->idx; // Skip 'WHERE' - $this->where = Condition::parse($parser, $list); - $state = 4; - } elseif ($token->type === Token::TYPE_KEYWORD) { - $parser->error('Unexpected keyword.', $token); - break; + if ($token->type === Token::TYPE_KEYWORD) { + if ($token->keyword === 'WHERE') { + ++$list->idx; // Skip 'WHERE' + $this->where = Condition::parse($parser, $list); + $state = 4; + } else { + $parser->error('Unexpected keyword.', $token); + break; + } } else { $parser->error('Unexpected token.', $token); break; @@ -317,32 +313,33 @@ class DeleteStatement extends Statement break; } - if ($token->type === Token::TYPE_KEYWORD - && $token->keyword === 'ORDER BY' - ) { - ++$list->idx; // Skip 'ORDER BY' - $this->order = OrderKeyword::parse($parser, $list); - $state = 5; - } elseif ($token->type === Token::TYPE_KEYWORD - && $token->keyword === 'LIMIT' - ) { - ++$list->idx; // Skip 'LIMIT' - $this->limit = Limit::parse($parser, $list); - $state = 6; - } elseif ($token->type === Token::TYPE_KEYWORD) { - $parser->error('Unexpected keyword.', $token); - break; + if ($token->type === Token::TYPE_KEYWORD) { + switch($token->keyword) { + case 'ORDER BY': + ++$list->idx; // Skip 'ORDER BY' + $this->order = OrderKeyword::parse($parser, $list); + $state = 5; + break; + case 'LIMIT': + ++$list->idx; // Skip 'LIMIT' + $this->limit = Limit::parse($parser, $list); + $state = 6; + break; + default: + $parser->error('Unexpected keyword.', $token); + break 2; + } } } elseif ($state === 5) { - if ($token->type === Token::TYPE_KEYWORD - && $token->keyword === 'LIMIT' - ) { - ++$list->idx; // Skip 'LIMIT' - $this->limit = Limit::parse($parser, $list); - $state = 6; - } elseif ($token->type === Token::TYPE_KEYWORD) { - $parser->error('Unexpected keyword.', $token); - break; + if ($token->type === Token::TYPE_KEYWORD) { + if ($token->keyword === 'LIMIT') { + ++$list->idx; // Skip 'LIMIT' + $this->limit = Limit::parse($parser, $list); + $state = 6; + } else { + $parser->error('Unexpected keyword.', $token); + break; + } } } } diff --git a/src/Statements/LoadStatement.php b/src/Statements/LoadStatement.php index 13b68e7..45ad05f 100644 --- a/src/Statements/LoadStatement.php +++ b/src/Statements/LoadStatement.php @@ -270,15 +270,13 @@ class LoadStatement extends Statement ); $state = 1; } elseif ($state === 1) { - if (($token->type === Token::TYPE_KEYWORD) - && ($token->keyword === 'REPLACE' - || $token->keyword === 'IGNORE') - ) { - $this->replace_ignore = trim($token->keyword); - } elseif ($token->type === Token::TYPE_KEYWORD - && $token->keyword === 'INTO' - ) { - $state = 2; + if ($token->type === Token::TYPE_KEYWORD) { + if ($token->keyword === 'REPLACE' + || $token->keyword === 'IGNORE') { + $this->replace_ignore = trim($token->keyword); + } elseif ($token->keyword === 'INTO') { + $state = 2; + } } } elseif ($state === 2) { if ($token->type === Token::TYPE_KEYWORD diff --git a/src/Statements/SelectStatement.php b/src/Statements/SelectStatement.php index cd1d628..f2f94a8 100644 --- a/src/Statements/SelectStatement.php +++ b/src/Statements/SelectStatement.php @@ -226,8 +226,7 @@ class SelectStatement extends Statement // statement. if (!empty($this->union)) { $clauses = static::$CLAUSES; - unset($clauses['ORDER BY']); - unset($clauses['LIMIT']); + unset($clauses['ORDER BY'], $clauses['LIMIT']); $clauses['ORDER BY'] = array('ORDER BY', 3); $clauses['LIMIT'] = array('LIMIT', 3); diff --git a/src/Statements/TransactionStatement.php b/src/Statements/TransactionStatement.php index c2bb303..cfdc25c 100644 --- a/src/Statements/TransactionStatement.php +++ b/src/Statements/TransactionStatement.php @@ -82,12 +82,12 @@ class TransactionStatement extends Statement parent::parse($parser, $list); // Checks the type of this query. - if (($this->options->has('START TRANSACTION')) - || ($this->options->has('BEGIN')) + if ($this->options->has('START TRANSACTION') + || $this->options->has('BEGIN') ) { $this->type = self::TYPE_BEGIN; - } elseif (($this->options->has('COMMIT')) - || ($this->options->has('ROLLBACK')) + } elseif ($this->options->has('COMMIT') + || $this->options->has('ROLLBACK') ) { $this->type = self::TYPE_END; } diff --git a/src/Token.php b/src/Token.php index 4d028bf..077c2c2 100644 --- a/src/Token.php +++ b/src/Token.php @@ -295,20 +295,20 @@ class Token return $str; case self::TYPE_SYMBOL: $str = $this->token; - if ((isset($str[0])) && ($str[0] === '@')) { + if (isset($str[0]) && ($str[0] === '@')) { // `mb_strlen($str)` must be used instead of `null` because // in PHP 5.3- the `null` parameter isn't handled correctly. $str = mb_substr( $str, - ((!empty($str[1])) && ($str[1] === '@')) ? 2 : 1, + (!empty($str[1]) && ($str[1] === '@')) ? 2 : 1, mb_strlen($str), 'UTF-8' ); } - if ((isset($str[0])) && ($str[0] === ':')) { + if (isset($str[0]) && ($str[0] === ':')) { $str = mb_substr($str, 1, mb_strlen($str), 'UTF-8'); } - if ((isset($str[0])) && (($str[0] === '`') + if (isset($str[0]) && (($str[0] === '`') || ($str[0] === '"') || ($str[0] === '\'')) ) { $quote = $str[0]; diff --git a/src/Utils/BufferedQuery.php b/src/Utils/BufferedQuery.php index b333fa3..9b0cda7 100644 --- a/src/Utils/BufferedQuery.php +++ b/src/Utils/BufferedQuery.php @@ -261,22 +261,21 @@ class BufferedQuery $this->status = static::STATUS_COMMENT_BASH; $this->current .= $this->query[$i]; continue; - } elseif (($i + 2 < $len) - && ($this->query[$i] === '-') - && ($this->query[$i + 1] === '-') - && (Context::isWhitespace($this->query[$i + 2])) - ) { - $this->status = static::STATUS_COMMENT_SQL; - $this->current .= $this->query[$i]; - continue; - } elseif (($i + 2 < $len) - && ($this->query[$i] === '/') - && ($this->query[$i + 1] === '*') - && ($this->query[$i + 2] !== '!') - ) { - $this->status = static::STATUS_COMMENT_C; - $this->current .= $this->query[$i]; - continue; + } elseif ($i + 2 < $len) { + if (($this->query[$i] === '-') + && ($this->query[$i + 1] === '-') + && Context::isWhitespace($this->query[$i + 2])) { + $this->status = static::STATUS_COMMENT_SQL; + $this->current .= $this->query[$i]; + continue; + } + elseif (($this->query[$i] === '/') + && ($this->query[$i + 1] === '*') + && ($this->query[$i + 2] !== '!')) { + $this->status = static::STATUS_COMMENT_C; + $this->current .= $this->query[$i]; + continue; + } } /* @@ -301,7 +300,7 @@ class BufferedQuery && (($this->query[$i + 6] === 'T') || ($this->query[$i + 6] === 't')) && (($this->query[$i + 7] === 'E') || ($this->query[$i + 7] === 'e')) && (($this->query[$i + 8] === 'R') || ($this->query[$i + 8] === 'r')) - && (Context::isWhitespace($this->query[$i + 9])) + && Context::isWhitespace($this->query[$i + 9]) ) { // Saving the current index to be able to revert any parsing // done in this block. @@ -309,7 +308,7 @@ class BufferedQuery $i += 9; // Skipping `DELIMITER`. // Skipping whitespaces. - while (($i < $len) && (Context::isWhitespace($this->query[$i]))) { + while (($i < $len) && Context::isWhitespace($this->query[$i])) { ++$i; } @@ -321,8 +320,8 @@ class BufferedQuery // Checking if the delimiter definition ended. if (($delimiter != '') - && ((($i < $len) && (Context::isWhitespace($this->query[$i]))) - || (($i === $len) && ($end))) + && ((($i < $len) && Context::isWhitespace($this->query[$i])) + || (($i === $len) && $end)) ) { // Saving the delimiter. $this->setDelimiter($delimiter); @@ -393,7 +392,7 @@ class BufferedQuery $this->current .= $this->query[$i]; } - if (($end) && ($i === $len)) { + if ($end && ($i === $len)) { // If the end of the buffer was reached, the buffer is emptied and // the current statement that was extracted is returned. $ret = $this->current; diff --git a/src/Utils/Formatter.php b/src/Utils/Formatter.php index df260e7..4bfdc26 100644 --- a/src/Utils/Formatter.php +++ b/src/Utils/Formatter.php @@ -412,8 +412,8 @@ class Formatter } // Checking if this clause ended. - if ($tmp = static::isClause($curr)) { - if (($tmp == 2 || $this->options['clause_newline']) && empty(self::$SHORT_CLAUSES[$lastClause])) { + if ($isClause = static::isClause($curr)) { + if (($isClause === 2 || $this->options['clause_newline']) && empty(self::$SHORT_CLAUSES[$lastClause])) { $lineEnded = true; if ($this->options['parts_newline'] && $indent > 0) { --$indent; @@ -424,8 +424,8 @@ class Formatter // Inline JOINs if (($prev->type === Token::TYPE_KEYWORD && isset(JoinKeyword::$JOINS[$prev->value])) || (in_array($curr->value, array('ON', 'USING'), true) && isset(JoinKeyword::$JOINS[$list->tokens[$list->idx - 2]->value])) - || (isset($list->tokens[$list->idx - 4]) && isset(JoinKeyword::$JOINS[$list->tokens[$list->idx - 4]->value])) - || (isset($list->tokens[$list->idx - 6]) && isset(JoinKeyword::$JOINS[$list->tokens[$list->idx - 6]->value])) + || isset($list->tokens[$list->idx - 4], JoinKeyword::$JOINS[$list->tokens[$list->idx - 4]->value]) + || isset($list->tokens[$list->idx - 6], JoinKeyword::$JOINS[$list->tokens[$list->idx - 6]->value]) ) { $lineEnded = false; } @@ -433,7 +433,7 @@ class Formatter // Indenting BEGIN ... END blocks. if ($prev->type === Token::TYPE_KEYWORD && $prev->keyword === 'BEGIN') { $lineEnded = true; - array_push($blocksIndentation, $indent); + $blocksIndentation[] = $indent; ++$indent; } elseif ($curr->type === Token::TYPE_KEYWORD && $curr->keyword === 'END') { $lineEnded = true; @@ -460,14 +460,14 @@ class Formatter // Brackets are indented only if the length of the fragment between // them is longer than 30 characters. if ($prev->type === Token::TYPE_OPERATOR && $prev->value === '(') { - array_push($blocksIndentation, $indent); + $blocksIndentation[] = $indent; $shortGroup = true; if (static::getGroupLength($list) > 30) { ++$indent; $lineEnded = true; $shortGroup = false; } - array_push($blocksLineEndings, $lineEnded); + $blocksLineEndings[] = $lineEnded; } elseif ($curr->type === Token::TYPE_OPERATOR && $curr->value === ')') { $indent = array_pop($blocksIndentation); $lineEnded |= array_pop($blocksLineEndings); diff --git a/src/Utils/Misc.php b/src/Utils/Misc.php index 9374db3..387600f 100644 --- a/src/Utils/Misc.php +++ b/src/Utils/Misc.php @@ -29,8 +29,8 @@ class Misc public static function getAliases($statement, $database) { if (!($statement instanceof SelectStatement) - || (empty($statement->expr)) - || (empty($statement->from)) + || empty($statement->expr) + || empty($statement->from) ) { return array(); } @@ -55,11 +55,11 @@ class Misc } foreach ($expressions as $expr) { - if ((!isset($expr->table)) || ($expr->table === '')) { + if (!isset($expr->table) || ($expr->table === '')) { continue; } - $thisDb = ((isset($expr->database)) && ($expr->database !== '')) ? + $thisDb = (isset($expr->database) && ($expr->database !== '')) ? $expr->database : $database; if (!isset($retval[$thisDb])) { @@ -71,7 +71,7 @@ class Misc if (!isset($retval[$thisDb]['tables'][$expr->table])) { $retval[$thisDb]['tables'][$expr->table] = array( - 'alias' => ((isset($expr->alias)) && ($expr->alias !== '')) ? + 'alias' => (isset($expr->alias) && ($expr->alias !== '')) ? $expr->alias : null, 'columns' => array(), ); @@ -84,16 +84,15 @@ class Misc } foreach ($statement->expr as $expr) { - if ((!isset($expr->column)) || ($expr->column === '') - || (!isset($expr->alias)) || ($expr->alias === '') + if (!isset($expr->column, $expr->alias) || ($expr->column === '') || ($expr->alias === '') ) { continue; } - $thisDb = ((isset($expr->database)) && ($expr->database !== '')) ? + $thisDb = (isset($expr->database) && ($expr->database !== '')) ? $expr->database : $database; - if ((isset($expr->table)) && ($expr->table !== '')) { + if (isset($expr->table) && ($expr->table !== '')) { $thisTable = isset($tables[$thisDb][$expr->table]) ? $tables[$thisDb][$expr->table] : $expr->table; $retval[$thisDb]['tables'][$thisTable]['columns'][$expr->column] = $expr->alias; diff --git a/src/Utils/Query.php b/src/Utils/Query.php index 0115bbf..7a8f462 100644 --- a/src/Utils/Query.php +++ b/src/Utils/Query.php @@ -228,11 +228,11 @@ class Query $flags['distinct'] = true; } - if ((!empty($statement->group)) || (!empty($statement->having))) { + if (!empty($statement->group) || !empty($statement->having)) { $flags['is_group'] = true; } - if ((!empty($statement->into)) + if (!empty($statement->into) && ($statement->into->type === 'OUTFILE') ) { $flags['is_export'] = true; @@ -258,7 +258,7 @@ class Query } } - if ((!empty($statement->procedure)) + if (!empty($statement->procedure) && ($statement->procedure->name === 'ANALYSE') ) { $flags['is_analyse'] = true; @@ -330,8 +330,8 @@ class Query $flags['querytype'] = 'DROP'; $flags['reload'] = true; - if (($statement->options->has('DATABASE') - || ($statement->options->has('SCHEMA'))) + if ($statement->options->has('DATABASE') + || $statement->options->has('SCHEMA') ) { $flags['drop_database'] = true; } @@ -415,8 +415,7 @@ class Query // Finding tables' aliases and their associated real names. $tableAliases = array(); foreach ($statement->from as $expr) { - if ((isset($expr->table)) && ($expr->table !== '') - && (isset($expr->alias)) && ($expr->alias !== '') + if (isset($expr->table, $expr->alias) && ($expr->table !== '') && ($expr->alias !== '') ) { $tableAliases[$expr->alias] = array( $expr->table, @@ -429,13 +428,13 @@ class Query // Sometimes, this is not possible because the tables aren't defined // explicitly (e.g. SELECT * FROM film, SELECT film_id FROM film). foreach ($statement->expr as $expr) { - if ((isset($expr->table)) && ($expr->table !== '')) { + if (isset($expr->table) && ($expr->table !== '')) { if (isset($tableAliases[$expr->table])) { $arr = $tableAliases[$expr->table]; } else { $arr = array( $expr->table, - ((isset($expr->database)) && ($expr->database !== '')) ? + (isset($expr->database) && ($expr->database !== '')) ? $expr->database : null, ); } @@ -452,10 +451,10 @@ class Query // extracted from the FROM clause. if (empty($ret['select_tables'])) { foreach ($statement->from as $expr) { - if ((isset($expr->table)) && ($expr->table !== '')) { + if (isset($expr->table) && ($expr->table !== '')) { $arr = array( $expr->table, - ((isset($expr->database)) && ($expr->database !== '')) ? + (isset($expr->database) && ($expr->database !== '')) ? $expr->database : null, ); if (!in_array($arr, $ret['select_tables'])) { @@ -627,14 +626,14 @@ class Query } } - if ($brackets == 0) { + if ($brackets === 0) { // Checking if the section was changed. if (($token->type === Token::TYPE_KEYWORD) - && (isset($clauses[$token->keyword])) + && isset($clauses[$token->keyword]) && ($clauses[$token->keyword] >= $currIdx) ) { $currIdx = $clauses[$token->keyword]; - if (($skipFirst) && ($currIdx == $clauseIdx)) { + if ($skipFirst && ($currIdx == $clauseIdx)) { // This token is skipped (not added to the old // clause) because it will be replaced. continue; @@ -727,7 +726,7 @@ class Query $ret .= static::getClause($statement, $list, $ops[0][0], -1) . ' '; // Doing replacements. - for ($i = 0; $i < $count; ++$i) { + foreach ($ops as $i => $iValue) { $ret .= $ops[$i][1] . ' '; // Adding everything between this and next replacement. @@ -780,7 +779,7 @@ class Query $statement .= $token->token; - if (($token->type === Token::TYPE_DELIMITER) && (!empty($token->token))) { + if (($token->type === Token::TYPE_DELIMITER) && !empty($token->token)) { $delimiter = $token->token; $fullStatement = true; break; @@ -846,7 +845,7 @@ class Query if ($brackets == 0) { if (($token->type === Token::TYPE_KEYWORD) - && (isset($clauses[$token->keyword])) + && isset($clauses[$token->keyword]) && ($clause === $token->keyword) ) { return $i; diff --git a/src/Utils/Table.php b/src/Utils/Table.php index 7624974..b90c17a 100644 --- a/src/Utils/Table.php +++ b/src/Utils/Table.php @@ -26,7 +26,7 @@ class Table */ public static function getForeignKeys($statement) { - if ((empty($statement->fields)) + if (empty($statement->fields) || (!is_array($statement->fields)) || (!$statement->options->has('TABLE')) ) { @@ -36,7 +36,7 @@ class Table $ret = array(); foreach ($statement->fields as $field) { - if ((empty($field->key)) || ($field->key->type !== 'FOREIGN KEY')) { + if (empty($field->key) || ($field->key->type !== 'FOREIGN KEY')) { continue; } @@ -55,11 +55,11 @@ class Table $tmp['ref_table_name'] = $field->references->table->table; $tmp['ref_index_list'] = $field->references->columns; - if (($opt = $field->references->options->has('ON UPDATE'))) { + if ($opt = $field->references->options->has('ON UPDATE')) { $tmp['on_update'] = str_replace(' ', '_', $opt); } - if (($opt = $field->references->options->has('ON DELETE'))) { + if ($opt = $field->references->options->has('ON DELETE')) { $tmp['on_delete'] = str_replace(' ', '_', $opt); } @@ -83,7 +83,7 @@ class Table */ public static function getFields($statement) { - if ((empty($statement->fields)) + if (empty($statement->fields) || (!is_array($statement->fields)) || (!$statement->options->has('TABLE')) ) { @@ -110,20 +110,20 @@ class Table } } - if (($option = $field->options->has('DEFAULT'))) { + if ($option = $field->options->has('DEFAULT')) { $ret[$field->name]['default_value'] = $option; if ($option === 'CURRENT_TIMESTAMP') { $ret[$field->name]['default_current_timestamp'] = true; } } - if (($option = $field->options->has('ON UPDATE'))) { + if ($option = $field->options->has('ON UPDATE')) { if ($option === 'CURRENT_TIMESTAMP') { $ret[$field->name]['on_update_current_timestamp'] = true; } } - if (($option = $field->options->has('AS'))) { + if ($option = $field->options->has('AS')) { $ret[$field->name]['generated'] = true; $ret[$field->name]['expr'] = $option; } diff --git a/src/Utils/Tokens.php b/src/Utils/Tokens.php index 6520a79..76ab14c 100644 --- a/src/Utils/Tokens.php +++ b/src/Utils/Tokens.php @@ -30,34 +30,34 @@ class Tokens public static function match(Token $token, array $pattern) { // Token. - if ((isset($pattern['token'])) + if (isset($pattern['token']) && ($pattern['token'] !== $token->token) ) { return false; } // Value. - if ((isset($pattern['value'])) + if (isset($pattern['value']) && ($pattern['value'] !== $token->value) ) { return false; } - if ((isset($pattern['value_str'])) - && (strcasecmp($pattern['value_str'], $token->value)) + if (isset($pattern['value_str']) + && strcasecmp($pattern['value_str'], $token->value) ) { return false; } // Type. - if ((isset($pattern['type'])) + if (isset($pattern['type']) && ($pattern['type'] !== $token->type) ) { return false; } // Flags. - if ((isset($pattern['flags'])) + if (isset($pattern['flags']) && (($pattern['flags'] & $token->flags) === 0) ) { return false; diff --git a/tests/Components/CreateDefinitionTest.php b/tests/Components/CreateDefinitionTest.php index b14708c..3683e29 100644 --- a/tests/Components/CreateDefinitionTest.php +++ b/tests/Components/CreateDefinitionTest.php @@ -38,7 +38,7 @@ class CreateDefinitionTest extends TestCase public function testParseErr2() { $parser = new Parser(); - $component = CreateDefinition::parse( + CreateDefinition::parse( $parser, $this->getTokensList(')') ); diff --git a/tests/Components/LimitTest.php b/tests/Components/LimitTest.php index 466a199..e78f5ff 100644 --- a/tests/Components/LimitTest.php +++ b/tests/Components/LimitTest.php @@ -3,7 +3,6 @@ namespace PhpMyAdmin\SqlParser\Tests\Components; use PhpMyAdmin\SqlParser\Components\Limit; -use PhpMyAdmin\SqlParser\Parser; use PhpMyAdmin\SqlParser\Tests\TestCase; class LimitTest extends TestCase diff --git a/tests/Utils/BufferedQueryTest.php b/tests/Utils/BufferedQueryTest.php index 182c1ab..731fc0e 100644 --- a/tests/Utils/BufferedQueryTest.php +++ b/tests/Utils/BufferedQueryTest.php @@ -39,7 +39,7 @@ class BufferedQueryTest extends TestCase // Feeding chunks and extracting queries. $i = 0; while ($i < $count) { - if (($stmt = $bq->extract())) { + if ($stmt = $bq->extract()) { $statements[] = $stmt; } else { $bq->query .= $chunks[$i++]; @@ -47,7 +47,7 @@ class BufferedQueryTest extends TestCase } // Feeding ended, extracting remaining queries. - while (($stmt = $bq->extract(true))) { + while ($stmt = $bq->extract(true)) { $statements[] = $stmt; } |