Skip to content

Commit 0efc011

Browse files
committed
Use early exit when possible
Signed-off-by: Maurício Meneghini Fauth <mauricio@fauth.dev>
1 parent c6dd2c1 commit 0efc011

36 files changed

+545
-413
lines changed

src/Components/Array2d.php

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -76,33 +76,33 @@ public static function parse(Parser $parser, TokensList $list, array $options =
7676
}
7777

7878
if ($state === 0) {
79-
if ($token->value === '(') {
80-
$arr = ArrayObj::parse($parser, $list, $options);
81-
$arrCount = count($arr->values);
82-
if ($count === -1) {
83-
$count = $arrCount;
84-
} elseif ($arrCount !== $count) {
85-
$parser->error(
86-
sprintf(
87-
Translator::gettext('%1$d values were expected, but found %2$d.'),
88-
$count,
89-
$arrCount
90-
),
91-
$token
92-
);
93-
}
94-
95-
$ret[] = $arr;
96-
$state = 1;
97-
} else {
79+
if ($token->value !== '(') {
9880
break;
9981
}
82+
83+
$arr = ArrayObj::parse($parser, $list, $options);
84+
$arrCount = count($arr->values);
85+
if ($count === -1) {
86+
$count = $arrCount;
87+
} elseif ($arrCount !== $count) {
88+
$parser->error(
89+
sprintf(
90+
Translator::gettext('%1$d values were expected, but found %2$d.'),
91+
$count,
92+
$arrCount
93+
),
94+
$token
95+
);
96+
}
97+
98+
$ret[] = $arr;
99+
$state = 1;
100100
} elseif ($state === 1) {
101-
if ($token->value === ',') {
102-
$state = 0;
103-
} else {
101+
if ($token->value !== ',') {
104102
break;
105103
}
104+
105+
$state = 0;
106106
}
107107
}
108108

src/Components/ArrayObj.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,9 @@ public static function build($component, array $options = [])
179179
{
180180
if (is_array($component)) {
181181
return implode(', ', $component);
182-
} elseif (! empty($component->raw)) {
182+
}
183+
184+
if (! empty($component->raw)) {
183185
return '(' . implode(', ', $component->raw) . ')';
184186
}
185187

src/Components/Condition.php

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -194,16 +194,20 @@ public static function parse(Parser $parser, TokensList $list, array $options =
194194
}
195195

196196
$expr->expr .= $token->token;
197-
if (($token->type === Token::TYPE_NONE)
198-
|| (($token->type === Token::TYPE_KEYWORD)
199-
&& (! ($token->flags & Token::FLAG_KEYWORD_RESERVED)))
200-
|| ($token->type === Token::TYPE_STRING)
201-
|| ($token->type === Token::TYPE_SYMBOL)
197+
if (($token->type !== Token::TYPE_NONE)
198+
&& (($token->type !== Token::TYPE_KEYWORD)
199+
|| ($token->flags & Token::FLAG_KEYWORD_RESERVED))
200+
&& ($token->type !== Token::TYPE_STRING)
201+
&& ($token->type !== Token::TYPE_SYMBOL)
202202
) {
203-
if (! in_array($token->value, $expr->identifiers)) {
204-
$expr->identifiers[] = $token->value;
205-
}
203+
continue;
204+
}
205+
206+
if (in_array($token->value, $expr->identifiers)) {
207+
continue;
206208
}
209+
210+
$expr->identifiers[] = $token->value;
207211
}
208212

209213
// Last iteration was not processed.

src/Components/CreateDefinition.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -226,16 +226,16 @@ public static function parse(Parser $parser, TokensList $list, array $options =
226226
}
227227

228228
if ($state === 0) {
229-
if (($token->type === Token::TYPE_OPERATOR) && ($token->value === '(')) {
230-
$state = 1;
231-
} else {
229+
if (($token->type !== Token::TYPE_OPERATOR) || ($token->value !== '(')) {
232230
$parser->error(
233231
'An opening bracket was expected.',
234232
$token
235233
);
236234

237235
break;
238236
}
237+
238+
$state = 1;
239239
} elseif ($state === 1) {
240240
if ($token->type === Token::TYPE_KEYWORD && $token->keyword === 'CONSTRAINT') {
241241
$expr->isConstraint = true;

src/Components/Expression.php

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -266,7 +266,9 @@ public static function parse(Parser $parser, TokensList $list, array $options =
266266

267267
$alias = true;
268268
continue;
269-
} elseif ($token->keyword === 'CASE') {
269+
}
270+
271+
if ($token->keyword === 'CASE') {
270272
// For a use of CASE like
271273
// 'SELECT a = CASE .... END, b=1, `id`, ... FROM ...'
272274
$tempCaseExpr = CaseExpression::parse($parser, $list);
@@ -322,21 +324,21 @@ public static function parse(Parser $parser, TokensList $list, array $options =
322324
if ($brackets === 0) {
323325
// Not our bracket
324326
break;
325-
} else {
326-
--$brackets;
327-
if ($brackets === 0) {
328-
if (! empty($options['parenthesesDelimited'])) {
329-
// The current token is the last bracket, the next
330-
// one will be outside the expression.
331-
$ret->expr .= $token->token;
332-
++$list->idx;
333-
break;
334-
}
335-
} elseif ($brackets < 0) {
336-
// $parser->error('Unexpected closing bracket.', $token);
337-
// $brackets = 0;
327+
}
328+
329+
--$brackets;
330+
if ($brackets === 0) {
331+
if (! empty($options['parenthesesDelimited'])) {
332+
// The current token is the last bracket, the next
333+
// one will be outside the expression.
334+
$ret->expr .= $token->token;
335+
++$list->idx;
338336
break;
339337
}
338+
} elseif ($brackets < 0) {
339+
// $parser->error('Unexpected closing bracket.', $token);
340+
// $brackets = 0;
341+
break;
340342
}
341343
} elseif ($token->value === ',') {
342344
// Expressions are comma-delimited.

src/Components/ExpressionArray.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -96,11 +96,11 @@ public static function parse(Parser $parser, TokensList $list, array $options =
9696
$ret[] = $expr;
9797
$state = 1;
9898
} elseif ($state === 1) {
99-
if ($token->value === ',') {
100-
$state = 0;
101-
} else {
99+
if ($token->value !== ',') {
102100
break;
103101
}
102+
103+
$state = 0;
104104
}
105105
}
106106

src/Components/IndexHint.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -119,12 +119,12 @@ public static function parse(Parser $parser, TokensList $list, array $options =
119119
switch ($state) {
120120
case 0:
121121
if ($token->type === Token::TYPE_KEYWORD) {
122-
if ($token->keyword === 'USE' || $token->keyword === 'IGNORE' || $token->keyword === 'FORCE') {
123-
$expr->type = $token->keyword;
124-
$state = 1;
125-
} else {
122+
if ($token->keyword !== 'USE' && $token->keyword !== 'IGNORE' && $token->keyword !== 'FORCE') {
126123
break 2;
127124
}
125+
126+
$expr->type = $token->keyword;
127+
$state = 1;
128128
}
129129

130130
break;

src/Components/IntoKeyword.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -281,7 +281,9 @@ public static function build($component, array $options = [])
281281
$columns = ! empty($component->columns) ? '(`' . implode('`, `', $component->columns) . '`)' : '';
282282

283283
return $component->dest . $columns;
284-
} elseif (isset($component->values)) {
284+
}
285+
286+
if (isset($component->values)) {
285287
return ExpressionArray::build($component->values);
286288
}
287289

src/Components/JoinKeyword.php

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -147,14 +147,14 @@ public static function parse(Parser $parser, TokensList $list, array $options =
147147
}
148148

149149
if ($state === 0) {
150-
if (($token->type === Token::TYPE_KEYWORD)
151-
&& ! empty(static::$JOINS[$token->keyword])
150+
if (($token->type !== Token::TYPE_KEYWORD)
151+
|| empty(static::$JOINS[$token->keyword])
152152
) {
153-
$expr->type = static::$JOINS[$token->keyword];
154-
$state = 1;
155-
} else {
156153
break;
157154
}
155+
156+
$expr->type = static::$JOINS[$token->keyword];
157+
$state = 1;
158158
} elseif ($state === 1) {
159159
$expr->expr = Expression::parse($parser, $list, ['field' => 'table']);
160160
$state = 2;
@@ -168,17 +168,17 @@ public static function parse(Parser $parser, TokensList $list, array $options =
168168
$state = 4;
169169
break;
170170
default:
171-
if (! empty(static::$JOINS[$token->keyword])
171+
if (empty(static::$JOINS[$token->keyword])
172172
) {
173-
$ret[] = $expr;
174-
$expr = new static();
175-
$expr->type = static::$JOINS[$token->keyword];
176-
$state = 1;
177-
} else {
178173
/* Next clause is starting */
179174
break 2;
180175
}
181176

177+
$ret[] = $expr;
178+
$expr = new static();
179+
$expr->type = static::$JOINS[$token->keyword];
180+
$state = 1;
181+
182182
break;
183183
}
184184
}

src/Components/LockExpression.php

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -178,21 +178,21 @@ private static function parseLockType(Parser $parser, TokensList $list)
178178

179179
$lockType .= $token->keyword;
180180
} elseif ($state === 1) {
181-
if ($token->keyword === 'LOCAL') {
182-
$lockType .= ' ' . $token->keyword;
183-
$state = 3;
184-
} else {
181+
if ($token->keyword !== 'LOCAL') {
185182
$parser->error('Unexpected keyword.', $token);
186183
break;
187184
}
185+
186+
$lockType .= ' ' . $token->keyword;
187+
$state = 3;
188188
} elseif ($state === 2) {
189-
if ($token->keyword === 'WRITE') {
190-
$lockType .= ' ' . $token->keyword;
191-
$state = 3; // parsing over
192-
} else {
189+
if ($token->keyword !== 'WRITE') {
193190
$parser->error('Unexpected keyword.', $token);
194191
break;
195192
}
193+
194+
$lockType .= ' ' . $token->keyword;
195+
$state = 3; // parsing over
196196
}
197197

198198
$prevToken = $token;

0 commit comments

Comments
 (0)