summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMichal Čihař <michal@cihar.com>2017-01-17 17:19:21 +0100
committerGitHub <noreply@github.com>2017-01-17 17:19:21 +0100
commit3420cbbb52e785270afcbdf7f5b311e5ad53a75b (patch)
tree02de514f6150e22a16d28dba37f31841a01c448d
parentf06862f070d81d4b13ad9022d3489300503850cc (diff)
parent1e72109c817007db4d30f493c83842e19cf45d0e (diff)
downloadsql-parser-3420cbbb52e785270afcbdf7f5b311e5ad53a75b.zip
sql-parser-3420cbbb52e785270afcbdf7f5b311e5ad53a75b.tar.gz
sql-parser-3420cbbb52e785270afcbdf7f5b311e5ad53a75b.tar.bz2
Merge pull request #122 from bigfoot90/some-clean
Remove unuseful brackets
-rw-r--r--src/Lexer.php152
-rw-r--r--src/Utils/Formatter.php16
2 files changed, 97 insertions, 71 deletions
diff --git a/src/Lexer.php b/src/Lexer.php
index 73c4100..bb85778 100644
--- a/src/Lexer.php
+++ b/src/Lexer.php
@@ -185,18 +185,16 @@ class Lexer
{
// `strlen` is used instead of `mb_strlen` because the lexer needs to
// parse each byte of the input.
- $len = ($str instanceof UtfString) ? $str->length() : strlen($str);
+ $len = $str instanceof UtfString ? $str->length() : strlen($str);
// For multi-byte strings, a new instance of `UtfString` is
// initialized (only if `UtfString` usage is forced.
- if (!($str instanceof UtfString)) {
- if ((USE_UTF_STRINGS) && ($len !== mb_strlen($str, 'UTF-8'))) {
- $str = new UtfString($str);
- }
+ if (!$str instanceof UtfString && USE_UTF_STRINGS && $len !== mb_strlen($str, 'UTF-8')) {
+ $str = new UtfString($str);
}
$this->str = $str;
- $this->len = ($str instanceof UtfString) ? $str->length() : $len;
+ $this->len = $str instanceof UtfString ? $str->length() : $len;
$this->strict = $strict;
@@ -251,7 +249,7 @@ class Lexer
$token = null;
foreach (static::$PARSER_METHODS as $method) {
- if (($token = $this->$method())) {
+ if ($token = $this->$method()) {
break;
}
}
@@ -264,12 +262,16 @@ class Lexer
$this->str[$this->last],
$this->last
);
- } elseif (($lastToken !== null)
- && ($token->type === Token::TYPE_SYMBOL)
- && ($token->flags & Token::FLAG_SYMBOL_VARIABLE)
- && (($lastToken->type === Token::TYPE_STRING)
- || (($lastToken->type === Token::TYPE_SYMBOL)
- && ($lastToken->flags & Token::FLAG_SYMBOL_BACKTICK)))
+ } elseif ($lastToken !== null
+ && $token->type === Token::TYPE_SYMBOL
+ && $token->flags & Token::FLAG_SYMBOL_VARIABLE
+ && (
+ $lastToken->type === Token::TYPE_STRING
+ || (
+ $lastToken->type === Token::TYPE_SYMBOL
+ && $lastToken->flags & Token::FLAG_SYMBOL_BACKTICK
+ )
+ )
) {
// Handles ```... FROM 'user'@'%' ...```.
$lastToken->token .= $token->token;
@@ -277,10 +279,10 @@ class Lexer
$lastToken->flags = Token::FLAG_SYMBOL_USER;
$lastToken->value .= '@' . $token->value;
continue;
- } elseif (($lastToken !== null)
- && ($token->type === Token::TYPE_KEYWORD)
- && ($lastToken->type === Token::TYPE_OPERATOR)
- && ($lastToken->value === '.')
+ } elseif ($lastToken !== null
+ && $token->type === Token::TYPE_KEYWORD
+ && $lastToken->type === Token::TYPE_OPERATOR
+ && $lastToken->value === '.'
) {
// Handles ```... tbl.FROM ...```. In this case, FROM is not
// a reserved word.
@@ -294,7 +296,7 @@ class Lexer
$list->tokens[$list->count++] = $token;
// Handling delimiters.
- if (($token->type === Token::TYPE_NONE) && ($token->value === 'DELIMITER')) {
+ if ($token->type === Token::TYPE_NONE && $token->value === 'DELIMITER') {
if ($this->last + 1 >= $this->len) {
$this->error(
__('Expected whitespace(s) before delimiter.'),
@@ -325,7 +327,7 @@ class Lexer
// Parsing the delimiter.
$this->delimiter = null;
- while ((++$this->last < $this->len) && (!Context::isWhitespace($this->str[$this->last]))) {
+ while (++$this->last < $this->len && !Context::isWhitespace($this->str[$this->last])) {
$this->delimiter .= $this->str[$this->last];
}
@@ -419,16 +421,17 @@ class Lexer
} else {
$lastSpace = false;
}
+
$token .= $this->str[$this->last];
- if (($this->last + 1 === $this->len) || (Context::isSeparator($this->str[$this->last + 1]))) {
- if (($flags = Context::isKeyword($token))) {
- $ret = new Token($token, Token::TYPE_KEYWORD, $flags);
- $iEnd = $this->last;
-
- // We don't break so we find longest keyword.
- // For example, `OR` and `ORDER` have a common prefix `OR`.
- // If we stopped at `OR`, the parsing would be invalid.
- }
+ if (($this->last + 1 === $this->len || Context::isSeparator($this->str[$this->last + 1]))
+ && $flags = Context::isKeyword($token)
+ ) {
+ $ret = new Token($token, Token::TYPE_KEYWORD, $flags);
+ $iEnd = $this->last;
+
+ // We don't break so we find longest keyword.
+ // For example, `OR` and `ORDER` have a common prefix `OR`.
+ // If we stopped at `OR`, the parsing would be invalid.
}
}
@@ -542,7 +545,7 @@ class Lexer
return null;
}
- while ((++$this->last < $this->len) && (Context::isWhitespace($this->str[$this->last]))) {
+ while (++$this->last < $this->len && Context::isWhitespace($this->str[$this->last])) {
$token .= $this->str[$this->last];
}
@@ -563,7 +566,10 @@ class Lexer
// Bash style comments. (#comment\n)
if (Context::isComment($token)) {
- while ((++$this->last < $this->len) && ($this->str[$this->last] !== "\n")) {
+ while (
+ ++$this->last < $this->len
+ && $this->str[$this->last] !== "\n"
+ ) {
$token .= $this->str[$this->last];
}
$token .= "\n"; // Adding the line ending.
@@ -583,13 +589,16 @@ class Lexer
}
// Checking if this is a MySQL-specific command.
- if (($this->last + 1 < $this->len) && ($this->str[$this->last + 1] === '!')) {
+ if ($this->last + 1 < $this->len
+ && $this->str[$this->last + 1] === '!'
+ ) {
$flags |= Token::FLAG_COMMENT_MYSQL_CMD;
$token .= $this->str[++$this->last];
- while ((++$this->last < $this->len)
- && ('0' <= $this->str[$this->last])
- && ($this->str[$this->last] <= '9')
+ while (
+ ++$this->last < $this->len
+ && '0' <= $this->str[$this->last]
+ && $this->str[$this->last] <= '9'
) {
$token .= $this->str[$this->last];
}
@@ -601,8 +610,12 @@ class Lexer
}
// Parsing the comment.
- while ((++$this->last < $this->len)
- && (($this->str[$this->last - 1] !== '*') || ($this->str[$this->last] !== '/'))
+ while (
+ ++$this->last < $this->len
+ && (
+ $this->str[$this->last - 1] !== '*'
+ || $this->str[$this->last] !== '/'
+ )
) {
$token .= $this->str[$this->last];
}
@@ -622,7 +635,10 @@ class Lexer
if (Context::isComment($token)) {
// Checking if this comment did not end already (```--\n```).
if ($this->str[$this->last] !== "\n") {
- while ((++$this->last < $this->len) && ($this->str[$this->last] !== "\n")) {
+ while (
+ ++$this->last < $this->len
+ && $this->str[$this->last] !== "\n"
+ ) {
$token .= $this->str[$this->last];
}
$token .= "\n"; // Adding the line ending.
@@ -719,14 +735,16 @@ class Lexer
if ($state === 1) {
if ($this->str[$this->last] === '-') {
$flags |= Token::FLAG_NUMBER_NEGATIVE;
- } elseif (($this->last + 1 < $this->len)
- && ($this->str[$this->last] === '0')
- && (($this->str[$this->last + 1] === 'x')
- || ($this->str[$this->last + 1] === 'X'))
+ } elseif ($this->last + 1 < $this->len
+ && $this->str[$this->last] === '0'
+ && (
+ $this->str[$this->last + 1] === 'x'
+ || $this->str[$this->last + 1] === 'X'
+ )
) {
$token .= $this->str[$this->last++];
$state = 2;
- } elseif (($this->str[$this->last] >= '0') && ($this->str[$this->last] <= '9')) {
+ } elseif ($this->str[$this->last] >= '0' && $this->str[$this->last] <= '9') {
$state = 3;
} elseif ($this->str[$this->last] === '.') {
$state = 4;
@@ -738,40 +756,43 @@ class Lexer
}
} elseif ($state === 2) {
$flags |= Token::FLAG_NUMBER_HEX;
- if (!((($this->str[$this->last] >= '0') && ($this->str[$this->last] <= '9'))
- || (($this->str[$this->last] >= 'A') && ($this->str[$this->last] <= 'F'))
- || (($this->str[$this->last] >= 'a') && ($this->str[$this->last] <= 'f')))
+ if (
+ !(
+ ($this->str[$this->last] >= '0' && $this->str[$this->last] <= '9')
+ || ($this->str[$this->last] >= 'A' && $this->str[$this->last] <= 'F')
+ || ($this->str[$this->last] >= 'a' && $this->str[$this->last] <= 'f')
+ )
) {
break;
}
} elseif ($state === 3) {
if ($this->str[$this->last] === '.') {
$state = 4;
- } elseif (($this->str[$this->last] === 'e') || ($this->str[$this->last] === 'E')) {
+ } elseif ($this->str[$this->last] === 'e' || $this->str[$this->last] === 'E') {
$state = 5;
- } elseif (($this->str[$this->last] < '0') || ($this->str[$this->last] > '9')) {
+ } elseif ($this->str[$this->last] < '0' || $this->str[$this->last] > '9') {
// Just digits and `.`, `e` and `E` are valid characters.
break;
}
} elseif ($state === 4) {
$flags |= Token::FLAG_NUMBER_FLOAT;
- if (($this->str[$this->last] === 'e') || ($this->str[$this->last] === 'E')) {
+ if ($this->str[$this->last] === 'e' || $this->str[$this->last] === 'E') {
$state = 5;
- } elseif (($this->str[$this->last] < '0') || ($this->str[$this->last] > '9')) {
+ } elseif ($this->str[$this->last] < '0' || $this->str[$this->last] > '9') {
// Just digits, `e` and `E` are valid characters.
break;
}
} elseif ($state === 5) {
$flags |= Token::FLAG_NUMBER_APPROXIMATE;
- if (($this->str[$this->last] === '+') || ($this->str[$this->last] === '-')
- || ((($this->str[$this->last] >= '0') && ($this->str[$this->last] <= '9')))
+ if ($this->str[$this->last] === '+' || $this->str[$this->last] === '-'
+ || ($this->str[$this->last] >= '0' && $this->str[$this->last] <= '9')
) {
$state = 6;
} else {
break;
}
} elseif ($state === 6) {
- if (($this->str[$this->last] < '0') || ($this->str[$this->last] > '9')) {
+ if ($this->str[$this->last] < '0' || $this->str[$this->last] > '9') {
// Just digits are valid characters.
break;
}
@@ -785,8 +806,8 @@ class Lexer
} elseif ($state === 8) {
if ($this->str[$this->last] === '\'') {
$state = 9;
- } elseif (($this->str[$this->last] !== '0')
- && ($this->str[$this->last] !== '1')
+ } elseif ($this->str[$this->last] !== '0'
+ && $this->str[$this->last] !== '1'
) {
break;
}
@@ -795,9 +816,9 @@ class Lexer
}
$token .= $this->str[$this->last];
}
- if (($state === 2) || ($state === 3)
- || (($token !== '.') && ($state === 4))
- || ($state === 6) || ($state === 9)
+ if ($state === 2 || $state === 3
+ || ($token !== '.' && $state === 4)
+ || $state === 6 || $state === 9
) {
--$this->last;
@@ -818,15 +839,17 @@ class Lexer
public function parseString($quote = '')
{
$token = $this->str[$this->last];
- if ((!($flags = Context::isString($token))) && ($token !== $quote)) {
+ if (!($flags = Context::isString($token)) && $token !== $quote) {
return null;
}
$quote = $token;
while (++$this->last < $this->len) {
- if (($this->last + 1 < $this->len)
- && ((($this->str[$this->last] === $quote) && ($this->str[$this->last + 1] === $quote))
- || (($this->str[$this->last] === '\\') && ($quote !== '`')))
+ if ($this->last + 1 < $this->len
+ && (
+ ($this->str[$this->last] === $quote && $this->str[$this->last + 1] === $quote)
+ || ($this->str[$this->last] === '\\' && $quote !== '`')
+ )
) {
$token .= $this->str[$this->last] . $this->str[++$this->last];
} else {
@@ -837,7 +860,7 @@ class Lexer
}
}
- if (($this->last >= $this->len) || ($this->str[$this->last] !== $quote)) {
+ if ($this->last >= $this->len || $this->str[$this->last] !== $quote) {
$this->error(
sprintf(
__('Ending quote %1$s was expected.'),
@@ -907,7 +930,8 @@ class Lexer
if (Context::isSeparator($token)) {
return null;
}
- while ((++$this->last < $this->len) && (!Context::isSeparator($this->str[$this->last]))) {
+
+ while (++$this->last < $this->len && !Context::isSeparator($this->str[$this->last])) {
$token .= $this->str[$this->last];
}
--$this->last;
@@ -924,7 +948,7 @@ class Lexer
{
$idx = 0;
- while (($idx < $this->delimiterLen) && ($this->last + $idx < $this->len)) {
+ while ($idx < $this->delimiterLen && $this->last + $idx < $this->len) {
if ($this->delimiter[$idx] !== $this->str[$this->last + $idx]) {
return null;
}
diff --git a/src/Utils/Formatter.php b/src/Utils/Formatter.php
index 5b6da32..22effca 100644
--- a/src/Utils/Formatter.php
+++ b/src/Utils/Formatter.php
@@ -410,24 +410,26 @@ class Formatter
}
// Indenting BEGIN ... END blocks.
- if (($prev->type === Token::TYPE_KEYWORD) && ($prev->value === 'BEGIN')) {
+ if ($prev->type === Token::TYPE_KEYWORD && $prev->value === 'BEGIN') {
$lineEnded = true;
array_push($blocksIndentation, $indent);
++$indent;
- } elseif (($curr->type === Token::TYPE_KEYWORD) && ($curr->value === 'END')) {
+ } elseif ($curr->type === Token::TYPE_KEYWORD && $curr->value === 'END') {
$lineEnded = true;
$indent = array_pop($blocksIndentation);
}
// Formatting fragments delimited by comma.
- if (($prev->type === Token::TYPE_OPERATOR) && ($prev->value === ',')) {
+ if ($prev->type === Token::TYPE_OPERATOR && $prev->value === ',') {
// Fragments delimited by a comma are broken into multiple
// pieces only if the clause is not inlined or this fragment
// is between brackets that are on new line.
- if ((empty(self::$INLINE_CLAUSES[$lastClause])
- && !$shortGroup
- && $this->options['parts_newline'])
- || end($blocksLineEndings) === true
+ if (end($blocksLineEndings) === true
+ || (
+ empty(self::$INLINE_CLAUSES[$lastClause])
+ && !$shortGroup
+ && $this->options['parts_newline']
+ )
) {
$lineEnded = true;
}