diff options
Diffstat (limited to 'src/Utils')
-rw-r--r-- | src/Utils/BufferedQuery.php | 45 | ||||
-rw-r--r-- | src/Utils/CLI.php | 2 | ||||
-rw-r--r-- | src/Utils/Formatter.php | 20 | ||||
-rw-r--r-- | src/Utils/Misc.php | 17 | ||||
-rw-r--r-- | src/Utils/Query.php | 39 | ||||
-rw-r--r-- | src/Utils/Table.php | 16 | ||||
-rw-r--r-- | src/Utils/Tokens.php | 12 |
7 files changed, 74 insertions, 77 deletions
diff --git a/src/Utils/BufferedQuery.php b/src/Utils/BufferedQuery.php index b333fa3..ee0dca4 100644 --- a/src/Utils/BufferedQuery.php +++ b/src/Utils/BufferedQuery.php @@ -191,7 +191,7 @@ class BufferedQuery * treated differently, because of the preceding backslash, it will * be ignored. */ - if ((($this->status & static::STATUS_COMMENT) == 0) && ($this->query[$i] === '\\')) { + if ((($this->status & static::STATUS_COMMENT) === 0) && ($this->query[$i] === '\\')) { $this->current .= $this->query[$i] . $this->query[++$i]; continue; } @@ -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; } @@ -320,9 +319,9 @@ class BufferedQuery } // Checking if the delimiter definition ended. - if (($delimiter != '') - && ((($i < $len) && (Context::isWhitespace($this->query[$i]))) - || (($i === $len) && ($end))) + if (($delimiter !== '') + && ((($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/CLI.php b/src/Utils/CLI.php index d412789..3dc48a6 100644 --- a/src/Utils/CLI.php +++ b/src/Utils/CLI.php @@ -120,7 +120,7 @@ class CLI $lexer = new Lexer($params['q'], false); $parser = new Parser($lexer->list); $errors = Error::get(array($lexer, $parser)); - if (count($errors) == 0) { + if (count($errors) === 0) { return 0; } $output = Error::format($errors); diff --git a/src/Utils/Formatter.php b/src/Utils/Formatter.php index df260e7..c41c69c 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); @@ -554,7 +554,7 @@ class Formatter if ($this->options['type'] === 'html') { return '<span ' . $format['html'] . '>' . htmlspecialchars($text, ENT_NOQUOTES) . '</span>'; } elseif ($this->options['type'] === 'cli') { - if ($prev != $format['cli']) { + if ($prev !== $format['cli']) { $prev = $format['cli']; return $format['cli'] . $this->escapeConsole($text); @@ -568,7 +568,7 @@ class Formatter } if ($this->options['type'] === 'cli') { - if ($prev != "\x1b[39m") { + if ($prev !== "\x1b[39m") { $prev = "\x1b[39m"; return "\x1b[39m" . $this->escapeConsole($text); @@ -633,7 +633,7 @@ class Formatter ++$count; } elseif ($list->tokens[$idx]->value === ')') { --$count; - if ($count == 0) { + if ($count === 0) { break; } } 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..b43a35e 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,12 +726,12 @@ class Query $ret .= static::getClause($statement, $list, $ops[0][0], -1) . ' '; // Doing replacements. - for ($i = 0; $i < $count; ++$i) { - $ret .= $ops[$i][1] . ' '; + foreach ($ops as $i => $clause) { + $ret .= $clause[1] . ' '; // Adding everything between this and next replacement. if ($i + 1 !== $count) { - $ret .= static::getClause($statement, $list, $ops[$i][0], $ops[$i + 1][0]) . ' '; + $ret .= static::getClause($statement, $list, $clause[0], $ops[$i + 1][0]) . ' '; } } @@ -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; @@ -844,9 +843,9 @@ class Query } } - if ($brackets == 0) { + 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; |