Skip to content

Commit 3420cbb

Browse files
authored
Merge pull request #122 from bigfoot90/some-clean
Remove unuseful brackets
2 parents f06862f + 1e72109 commit 3420cbb

File tree

2 files changed

+97
-71
lines changed

2 files changed

+97
-71
lines changed

src/Lexer.php

Lines changed: 88 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -185,18 +185,16 @@ public function __construct($str, $strict = false, $delimiter = null)
185185
{
186186
// `strlen` is used instead of `mb_strlen` because the lexer needs to
187187
// parse each byte of the input.
188-
$len = ($str instanceof UtfString) ? $str->length() : strlen($str);
188+
$len = $str instanceof UtfString ? $str->length() : strlen($str);
189189

190190
// For multi-byte strings, a new instance of `UtfString` is
191191
// initialized (only if `UtfString` usage is forced.
192-
if (!($str instanceof UtfString)) {
193-
if ((USE_UTF_STRINGS) && ($len !== mb_strlen($str, 'UTF-8'))) {
194-
$str = new UtfString($str);
195-
}
192+
if (!$str instanceof UtfString && USE_UTF_STRINGS && $len !== mb_strlen($str, 'UTF-8')) {
193+
$str = new UtfString($str);
196194
}
197195

198196
$this->str = $str;
199-
$this->len = ($str instanceof UtfString) ? $str->length() : $len;
197+
$this->len = $str instanceof UtfString ? $str->length() : $len;
200198

201199
$this->strict = $strict;
202200

@@ -251,7 +249,7 @@ public function lex()
251249
$token = null;
252250

253251
foreach (static::$PARSER_METHODS as $method) {
254-
if (($token = $this->$method())) {
252+
if ($token = $this->$method()) {
255253
break;
256254
}
257255
}
@@ -264,23 +262,27 @@ public function lex()
264262
$this->str[$this->last],
265263
$this->last
266264
);
267-
} elseif (($lastToken !== null)
268-
&& ($token->type === Token::TYPE_SYMBOL)
269-
&& ($token->flags & Token::FLAG_SYMBOL_VARIABLE)
270-
&& (($lastToken->type === Token::TYPE_STRING)
271-
|| (($lastToken->type === Token::TYPE_SYMBOL)
272-
&& ($lastToken->flags & Token::FLAG_SYMBOL_BACKTICK)))
265+
} elseif ($lastToken !== null
266+
&& $token->type === Token::TYPE_SYMBOL
267+
&& $token->flags & Token::FLAG_SYMBOL_VARIABLE
268+
&& (
269+
$lastToken->type === Token::TYPE_STRING
270+
|| (
271+
$lastToken->type === Token::TYPE_SYMBOL
272+
&& $lastToken->flags & Token::FLAG_SYMBOL_BACKTICK
273+
)
274+
)
273275
) {
274276
// Handles ```... FROM 'user'@'%' ...```.
275277
$lastToken->token .= $token->token;
276278
$lastToken->type = Token::TYPE_SYMBOL;
277279
$lastToken->flags = Token::FLAG_SYMBOL_USER;
278280
$lastToken->value .= '@' . $token->value;
279281
continue;
280-
} elseif (($lastToken !== null)
281-
&& ($token->type === Token::TYPE_KEYWORD)
282-
&& ($lastToken->type === Token::TYPE_OPERATOR)
283-
&& ($lastToken->value === '.')
282+
} elseif ($lastToken !== null
283+
&& $token->type === Token::TYPE_KEYWORD
284+
&& $lastToken->type === Token::TYPE_OPERATOR
285+
&& $lastToken->value === '.'
284286
) {
285287
// Handles ```... tbl.FROM ...```. In this case, FROM is not
286288
// a reserved word.
@@ -294,7 +296,7 @@ public function lex()
294296
$list->tokens[$list->count++] = $token;
295297

296298
// Handling delimiters.
297-
if (($token->type === Token::TYPE_NONE) && ($token->value === 'DELIMITER')) {
299+
if ($token->type === Token::TYPE_NONE && $token->value === 'DELIMITER') {
298300
if ($this->last + 1 >= $this->len) {
299301
$this->error(
300302
__('Expected whitespace(s) before delimiter.'),
@@ -325,7 +327,7 @@ public function lex()
325327

326328
// Parsing the delimiter.
327329
$this->delimiter = null;
328-
while ((++$this->last < $this->len) && (!Context::isWhitespace($this->str[$this->last]))) {
330+
while (++$this->last < $this->len && !Context::isWhitespace($this->str[$this->last])) {
329331
$this->delimiter .= $this->str[$this->last];
330332
}
331333

@@ -419,16 +421,17 @@ public function parseKeyword()
419421
} else {
420422
$lastSpace = false;
421423
}
424+
422425
$token .= $this->str[$this->last];
423-
if (($this->last + 1 === $this->len) || (Context::isSeparator($this->str[$this->last + 1]))) {
424-
if (($flags = Context::isKeyword($token))) {
425-
$ret = new Token($token, Token::TYPE_KEYWORD, $flags);
426-
$iEnd = $this->last;
427-
428-
// We don't break so we find longest keyword.
429-
// For example, `OR` and `ORDER` have a common prefix `OR`.
430-
// If we stopped at `OR`, the parsing would be invalid.
431-
}
426+
if (($this->last + 1 === $this->len || Context::isSeparator($this->str[$this->last + 1]))
427+
&& $flags = Context::isKeyword($token)
428+
) {
429+
$ret = new Token($token, Token::TYPE_KEYWORD, $flags);
430+
$iEnd = $this->last;
431+
432+
// We don't break so we find longest keyword.
433+
// For example, `OR` and `ORDER` have a common prefix `OR`.
434+
// If we stopped at `OR`, the parsing would be invalid.
432435
}
433436
}
434437

@@ -542,7 +545,7 @@ public function parseWhitespace()
542545
return null;
543546
}
544547

545-
while ((++$this->last < $this->len) && (Context::isWhitespace($this->str[$this->last]))) {
548+
while (++$this->last < $this->len && Context::isWhitespace($this->str[$this->last])) {
546549
$token .= $this->str[$this->last];
547550
}
548551

@@ -563,7 +566,10 @@ public function parseComment()
563566

564567
// Bash style comments. (#comment\n)
565568
if (Context::isComment($token)) {
566-
while ((++$this->last < $this->len) && ($this->str[$this->last] !== "\n")) {
569+
while (
570+
++$this->last < $this->len
571+
&& $this->str[$this->last] !== "\n"
572+
) {
567573
$token .= $this->str[$this->last];
568574
}
569575
$token .= "\n"; // Adding the line ending.
@@ -583,13 +589,16 @@ public function parseComment()
583589
}
584590

585591
// Checking if this is a MySQL-specific command.
586-
if (($this->last + 1 < $this->len) && ($this->str[$this->last + 1] === '!')) {
592+
if ($this->last + 1 < $this->len
593+
&& $this->str[$this->last + 1] === '!'
594+
) {
587595
$flags |= Token::FLAG_COMMENT_MYSQL_CMD;
588596
$token .= $this->str[++$this->last];
589597

590-
while ((++$this->last < $this->len)
591-
&& ('0' <= $this->str[$this->last])
592-
&& ($this->str[$this->last] <= '9')
598+
while (
599+
++$this->last < $this->len
600+
&& '0' <= $this->str[$this->last]
601+
&& $this->str[$this->last] <= '9'
593602
) {
594603
$token .= $this->str[$this->last];
595604
}
@@ -601,8 +610,12 @@ public function parseComment()
601610
}
602611

603612
// Parsing the comment.
604-
while ((++$this->last < $this->len)
605-
&& (($this->str[$this->last - 1] !== '*') || ($this->str[$this->last] !== '/'))
613+
while (
614+
++$this->last < $this->len
615+
&& (
616+
$this->str[$this->last - 1] !== '*'
617+
|| $this->str[$this->last] !== '/'
618+
)
606619
) {
607620
$token .= $this->str[$this->last];
608621
}
@@ -622,7 +635,10 @@ public function parseComment()
622635
if (Context::isComment($token)) {
623636
// Checking if this comment did not end already (```--\n```).
624637
if ($this->str[$this->last] !== "\n") {
625-
while ((++$this->last < $this->len) && ($this->str[$this->last] !== "\n")) {
638+
while (
639+
++$this->last < $this->len
640+
&& $this->str[$this->last] !== "\n"
641+
) {
626642
$token .= $this->str[$this->last];
627643
}
628644
$token .= "\n"; // Adding the line ending.
@@ -719,14 +735,16 @@ public function parseNumber()
719735
if ($state === 1) {
720736
if ($this->str[$this->last] === '-') {
721737
$flags |= Token::FLAG_NUMBER_NEGATIVE;
722-
} elseif (($this->last + 1 < $this->len)
723-
&& ($this->str[$this->last] === '0')
724-
&& (($this->str[$this->last + 1] === 'x')
725-
|| ($this->str[$this->last + 1] === 'X'))
738+
} elseif ($this->last + 1 < $this->len
739+
&& $this->str[$this->last] === '0'
740+
&& (
741+
$this->str[$this->last + 1] === 'x'
742+
|| $this->str[$this->last + 1] === 'X'
743+
)
726744
) {
727745
$token .= $this->str[$this->last++];
728746
$state = 2;
729-
} elseif (($this->str[$this->last] >= '0') && ($this->str[$this->last] <= '9')) {
747+
} elseif ($this->str[$this->last] >= '0' && $this->str[$this->last] <= '9') {
730748
$state = 3;
731749
} elseif ($this->str[$this->last] === '.') {
732750
$state = 4;
@@ -738,40 +756,43 @@ public function parseNumber()
738756
}
739757
} elseif ($state === 2) {
740758
$flags |= Token::FLAG_NUMBER_HEX;
741-
if (!((($this->str[$this->last] >= '0') && ($this->str[$this->last] <= '9'))
742-
|| (($this->str[$this->last] >= 'A') && ($this->str[$this->last] <= 'F'))
743-
|| (($this->str[$this->last] >= 'a') && ($this->str[$this->last] <= 'f')))
759+
if (
760+
!(
761+
($this->str[$this->last] >= '0' && $this->str[$this->last] <= '9')
762+
|| ($this->str[$this->last] >= 'A' && $this->str[$this->last] <= 'F')
763+
|| ($this->str[$this->last] >= 'a' && $this->str[$this->last] <= 'f')
764+
)
744765
) {
745766
break;
746767
}
747768
} elseif ($state === 3) {
748769
if ($this->str[$this->last] === '.') {
749770
$state = 4;
750-
} elseif (($this->str[$this->last] === 'e') || ($this->str[$this->last] === 'E')) {
771+
} elseif ($this->str[$this->last] === 'e' || $this->str[$this->last] === 'E') {
751772
$state = 5;
752-
} elseif (($this->str[$this->last] < '0') || ($this->str[$this->last] > '9')) {
773+
} elseif ($this->str[$this->last] < '0' || $this->str[$this->last] > '9') {
753774
// Just digits and `.`, `e` and `E` are valid characters.
754775
break;
755776
}
756777
} elseif ($state === 4) {
757778
$flags |= Token::FLAG_NUMBER_FLOAT;
758-
if (($this->str[$this->last] === 'e') || ($this->str[$this->last] === 'E')) {
779+
if ($this->str[$this->last] === 'e' || $this->str[$this->last] === 'E') {
759780
$state = 5;
760-
} elseif (($this->str[$this->last] < '0') || ($this->str[$this->last] > '9')) {
781+
} elseif ($this->str[$this->last] < '0' || $this->str[$this->last] > '9') {
761782
// Just digits, `e` and `E` are valid characters.
762783
break;
763784
}
764785
} elseif ($state === 5) {
765786
$flags |= Token::FLAG_NUMBER_APPROXIMATE;
766-
if (($this->str[$this->last] === '+') || ($this->str[$this->last] === '-')
767-
|| ((($this->str[$this->last] >= '0') && ($this->str[$this->last] <= '9')))
787+
if ($this->str[$this->last] === '+' || $this->str[$this->last] === '-'
788+
|| ($this->str[$this->last] >= '0' && $this->str[$this->last] <= '9')
768789
) {
769790
$state = 6;
770791
} else {
771792
break;
772793
}
773794
} elseif ($state === 6) {
774-
if (($this->str[$this->last] < '0') || ($this->str[$this->last] > '9')) {
795+
if ($this->str[$this->last] < '0' || $this->str[$this->last] > '9') {
775796
// Just digits are valid characters.
776797
break;
777798
}
@@ -785,8 +806,8 @@ public function parseNumber()
785806
} elseif ($state === 8) {
786807
if ($this->str[$this->last] === '\'') {
787808
$state = 9;
788-
} elseif (($this->str[$this->last] !== '0')
789-
&& ($this->str[$this->last] !== '1')
809+
} elseif ($this->str[$this->last] !== '0'
810+
&& $this->str[$this->last] !== '1'
790811
) {
791812
break;
792813
}
@@ -795,9 +816,9 @@ public function parseNumber()
795816
}
796817
$token .= $this->str[$this->last];
797818
}
798-
if (($state === 2) || ($state === 3)
799-
|| (($token !== '.') && ($state === 4))
800-
|| ($state === 6) || ($state === 9)
819+
if ($state === 2 || $state === 3
820+
|| ($token !== '.' && $state === 4)
821+
|| $state === 6 || $state === 9
801822
) {
802823
--$this->last;
803824

@@ -818,15 +839,17 @@ public function parseNumber()
818839
public function parseString($quote = '')
819840
{
820841
$token = $this->str[$this->last];
821-
if ((!($flags = Context::isString($token))) && ($token !== $quote)) {
842+
if (!($flags = Context::isString($token)) && $token !== $quote) {
822843
return null;
823844
}
824845
$quote = $token;
825846

826847
while (++$this->last < $this->len) {
827-
if (($this->last + 1 < $this->len)
828-
&& ((($this->str[$this->last] === $quote) && ($this->str[$this->last + 1] === $quote))
829-
|| (($this->str[$this->last] === '\\') && ($quote !== '`')))
848+
if ($this->last + 1 < $this->len
849+
&& (
850+
($this->str[$this->last] === $quote && $this->str[$this->last + 1] === $quote)
851+
|| ($this->str[$this->last] === '\\' && $quote !== '`')
852+
)
830853
) {
831854
$token .= $this->str[$this->last] . $this->str[++$this->last];
832855
} else {
@@ -837,7 +860,7 @@ public function parseString($quote = '')
837860
}
838861
}
839862

840-
if (($this->last >= $this->len) || ($this->str[$this->last] !== $quote)) {
863+
if ($this->last >= $this->len || $this->str[$this->last] !== $quote) {
841864
$this->error(
842865
sprintf(
843866
__('Ending quote %1$s was expected.'),
@@ -907,7 +930,8 @@ public function parseUnknown()
907930
if (Context::isSeparator($token)) {
908931
return null;
909932
}
910-
while ((++$this->last < $this->len) && (!Context::isSeparator($this->str[$this->last]))) {
933+
934+
while (++$this->last < $this->len && !Context::isSeparator($this->str[$this->last])) {
911935
$token .= $this->str[$this->last];
912936
}
913937
--$this->last;
@@ -924,7 +948,7 @@ public function parseDelimiter()
924948
{
925949
$idx = 0;
926950

927-
while (($idx < $this->delimiterLen) && ($this->last + $idx < $this->len)) {
951+
while ($idx < $this->delimiterLen && $this->last + $idx < $this->len) {
928952
if ($this->delimiter[$idx] !== $this->str[$this->last + $idx]) {
929953
return null;
930954
}

src/Utils/Formatter.php

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -410,24 +410,26 @@ public function formatList($list)
410410
}
411411

412412
// Indenting BEGIN ... END blocks.
413-
if (($prev->type === Token::TYPE_KEYWORD) && ($prev->value === 'BEGIN')) {
413+
if ($prev->type === Token::TYPE_KEYWORD && $prev->value === 'BEGIN') {
414414
$lineEnded = true;
415415
array_push($blocksIndentation, $indent);
416416
++$indent;
417-
} elseif (($curr->type === Token::TYPE_KEYWORD) && ($curr->value === 'END')) {
417+
} elseif ($curr->type === Token::TYPE_KEYWORD && $curr->value === 'END') {
418418
$lineEnded = true;
419419
$indent = array_pop($blocksIndentation);
420420
}
421421

422422
// Formatting fragments delimited by comma.
423-
if (($prev->type === Token::TYPE_OPERATOR) && ($prev->value === ',')) {
423+
if ($prev->type === Token::TYPE_OPERATOR && $prev->value === ',') {
424424
// Fragments delimited by a comma are broken into multiple
425425
// pieces only if the clause is not inlined or this fragment
426426
// is between brackets that are on new line.
427-
if ((empty(self::$INLINE_CLAUSES[$lastClause])
428-
&& !$shortGroup
429-
&& $this->options['parts_newline'])
430-
|| end($blocksLineEndings) === true
427+
if (end($blocksLineEndings) === true
428+
|| (
429+
empty(self::$INLINE_CLAUSES[$lastClause])
430+
&& !$shortGroup
431+
&& $this->options['parts_newline']
432+
)
431433
) {
432434
$lineEnded = true;
433435
}

0 commit comments

Comments
 (0)