Skip to content

Commit 513ed81

Browse files
committed
Cleanup and improve readability:
Avoid duplicate if conditions Use switch/case instead of ifs when possible
1 parent 8e37bb3 commit 513ed81

28 files changed

+457
-473
lines changed

src/Components/CaseExpression.php

Lines changed: 56 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -112,84 +112,79 @@ public static function parse(Parser $parser, TokensList $list, array $options =
112112
}
113113

114114
if ($state === 0) {
115-
if ($token->type === Token::TYPE_KEYWORD
116-
&& $token->keyword === 'WHEN'
117-
) {
118-
++$list->idx; // Skip 'WHEN'
119-
$new_condition = Condition::parse($parser, $list);
120-
$type = 1;
121-
$state = 1;
122-
$ret->conditions[] = $new_condition;
123-
} elseif ($token->type === Token::TYPE_KEYWORD
124-
&& $token->keyword === 'ELSE'
125-
) {
126-
++$list->idx; // Skip 'ELSE'
127-
$ret->else_result = Expression::parse($parser, $list);
128-
$state = 0; // last clause of CASE expression
129-
} elseif ($token->type === Token::TYPE_KEYWORD
130-
&& $token->keyword === 'END'
131-
) {
132-
$state = 3; // end of CASE expression
133-
++$list->idx;
134-
break;
135-
} elseif ($token->type === Token::TYPE_KEYWORD) {
136-
$parser->error('Unexpected keyword.', $token);
137-
break;
115+
if ($token->type === Token::TYPE_KEYWORD) {
116+
switch($token->keyword) {
117+
case 'WHEN':
118+
++$list->idx; // Skip 'WHEN'
119+
$new_condition = Condition::parse($parser, $list);
120+
$type = 1;
121+
$state = 1;
122+
$ret->conditions[] = $new_condition;
123+
break;
124+
case 'ELSE':
125+
++$list->idx; // Skip 'ELSE'
126+
$ret->else_result = Expression::parse($parser, $list);
127+
$state = 0; // last clause of CASE expression
128+
break;
129+
case 'END':
130+
$state = 3; // end of CASE expression
131+
++$list->idx;
132+
break;
133+
default:
134+
$parser->error('Unexpected keyword.', $token);
135+
break;
136+
}
138137
} else {
139138
$ret->value = Expression::parse($parser, $list);
140139
$type = 0;
141140
$state = 1;
142141
}
143142
} elseif ($state === 1) {
144143
if ($type === 0) {
145-
if ($token->type === Token::TYPE_KEYWORD
146-
&& $token->keyword === 'WHEN'
147-
) {
148-
++$list->idx; // Skip 'WHEN'
149-
$new_value = Expression::parse($parser, $list);
150-
$state = 2;
151-
$ret->compare_values[] = $new_value;
152-
} elseif ($token->type === Token::TYPE_KEYWORD
153-
&& $token->keyword === 'ELSE'
154-
) {
155-
++$list->idx; // Skip 'ELSE'
156-
$ret->else_result = Expression::parse($parser, $list);
157-
$state = 0; // last clause of CASE expression
158-
} elseif ($token->type === Token::TYPE_KEYWORD
159-
&& $token->keyword === 'END'
160-
) {
161-
$state = 3; // end of CASE expression
162-
++$list->idx;
163-
break;
164-
} elseif ($token->type === Token::TYPE_KEYWORD) {
165-
$parser->error('Unexpected keyword.', $token);
166-
break;
144+
if ($token->type === Token::TYPE_KEYWORD) {
145+
switch($token->keyword) {
146+
case 'WHEN':
147+
++$list->idx; // Skip 'WHEN'
148+
$new_value = Expression::parse($parser, $list);
149+
$state = 2;
150+
$ret->compare_values[] = $new_value;
151+
break;
152+
case 'ELSE':
153+
++$list->idx; // Skip 'ELSE'
154+
$ret->else_result = Expression::parse($parser, $list);
155+
$state = 0; // last clause of CASE expression
156+
break;
157+
case 'END':
158+
$state = 3; // end of CASE expression
159+
++$list->idx;
160+
break;
161+
default:
162+
$parser->error('Unexpected keyword.', $token);
163+
break;
164+
}
167165
}
168-
} else {
169-
if ($token->type === Token::TYPE_KEYWORD
170-
&& $token->keyword === 'THEN'
171-
) {
166+
} else if ($token->type === Token::TYPE_KEYWORD) {
167+
if ($token->keyword === 'THEN') {
172168
++$list->idx; // Skip 'THEN'
173169
$new_result = Expression::parse($parser, $list);
174170
$state = 0;
175171
$ret->results[] = $new_result;
176-
} elseif ($token->type === Token::TYPE_KEYWORD) {
172+
} else {
177173
$parser->error('Unexpected keyword.', $token);
178-
break;
179174
}
175+
break;
180176
}
181177
} elseif ($state === 2) {
182178
if ($type === 0) {
183-
if ($token->type === Token::TYPE_KEYWORD
184-
&& $token->keyword === 'THEN'
185-
) {
186-
++$list->idx; // Skip 'THEN'
187-
$new_result = Expression::parse($parser, $list);
188-
$ret->results[] = $new_result;
189-
$state = 1;
190-
} elseif ($token->type === Token::TYPE_KEYWORD) {
191-
$parser->error('Unexpected keyword.', $token);
192-
break;
179+
if ($token->type === Token::TYPE_KEYWORD) {
180+
if ($token->keyword === 'THEN') {
181+
++$list->idx; // Skip 'THEN'
182+
$new_result = Expression::parse($parser, $list);
183+
$ret->results[] = $new_result;
184+
$state = 1;
185+
} else {
186+
$parser->error('Unexpected keyword.', $token);
187+
}
193188
}
194189
}
195190
}

src/Components/Condition.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,7 @@ public static function parse(Parser $parser, TokensList $list, array $options =
142142

143143
// Conditions are delimited by logical operators.
144144
if (in_array($token->value, static::$DELIMITERS, true)) {
145-
if (($betweenBefore) && ($token->value === 'AND')) {
145+
if ($betweenBefore && ($token->value === 'AND')) {
146146
// The syntax of keyword `BETWEEN` is hard-coded.
147147
$betweenBefore = false;
148148
} else {
@@ -170,7 +170,7 @@ public static function parse(Parser $parser, TokensList $list, array $options =
170170
if ($token->value === 'BETWEEN') {
171171
$betweenBefore = true;
172172
}
173-
if (($brackets === 0) && (empty(static::$ALLOWED_KEYWORDS[$token->value]))) {
173+
if (($brackets === 0) && empty(static::$ALLOWED_KEYWORDS[$token->value])) {
174174
break;
175175
}
176176
}

src/Components/CreateDefinition.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -259,7 +259,7 @@ public static function parse(Parser $parser, TokensList $list, array $options =
259259
}
260260
$state = 5;
261261
} elseif ($state === 5) {
262-
if ((!empty($expr->type)) || (!empty($expr->key))) {
262+
if (!empty($expr->type) || !empty($expr->key)) {
263263
$ret[] = $expr;
264264
}
265265
$expr = new self();
@@ -281,7 +281,7 @@ public static function parse(Parser $parser, TokensList $list, array $options =
281281
}
282282

283283
// Last iteration was not saved.
284-
if ((!empty($expr->type)) || (!empty($expr->key))) {
284+
if (!empty($expr->type) || !empty($expr->key)) {
285285
$ret[] = $expr;
286286
}
287287

@@ -315,7 +315,7 @@ public static function build($component, array $options = array())
315315
$tmp .= 'CONSTRAINT ';
316316
}
317317

318-
if ((isset($component->name)) && ($component->name !== '')) {
318+
if (isset($component->name) && ($component->name !== '')) {
319319
$tmp .= Context::escape($component->name) . ' ';
320320
}
321321

src/Components/DataType.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,7 @@ public static function parse(Parser $parser, TokensList $list, array $options =
153153
*/
154154
public static function build($component, array $options = array())
155155
{
156-
$name = (empty($options['lowercase'])) ?
156+
$name = empty($options['lowercase']) ?
157157
$component->name : strtolower($component->name);
158158

159159
$parameters = '';

src/Components/Expression.php

Lines changed: 24 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -215,8 +215,8 @@ public static function parse(Parser $parser, TokensList $list, array $options =
215215
}
216216

217217
if ($token->type === Token::TYPE_KEYWORD) {
218-
if (($brackets > 0) && (empty($ret->subquery))
219-
&& (!empty(Parser::$STATEMENT_PARSERS[$token->keyword]))
218+
if (($brackets > 0) && empty($ret->subquery)
219+
&& !empty(Parser::$STATEMENT_PARSERS[$token->keyword])
220220
) {
221221
// A `(` was previously found and this keyword is the
222222
// beginning of a statement, so this is a subquery.
@@ -282,39 +282,41 @@ public static function parse(Parser $parser, TokensList $list, array $options =
282282
}
283283

284284
if ($token->type === Token::TYPE_OPERATOR) {
285-
if ((!empty($options['breakOnParentheses']))
285+
if (!empty($options['breakOnParentheses'])
286286
&& (($token->value === '(') || ($token->value === ')'))
287287
) {
288288
// No brackets were expected.
289289
break;
290290
}
291291
if ($token->value === '(') {
292292
++$brackets;
293-
if ((empty($ret->function)) && ($prev[1] !== null)
293+
if (empty($ret->function) && ($prev[1] !== null)
294294
&& (($prev[1]->type === Token::TYPE_NONE)
295295
|| ($prev[1]->type === Token::TYPE_SYMBOL)
296296
|| (($prev[1]->type === Token::TYPE_KEYWORD)
297297
&& ($prev[1]->flags & Token::FLAG_KEYWORD_FUNCTION)))
298298
) {
299299
$ret->function = $prev[1]->value;
300300
}
301-
} elseif ($token->value === ')' && $brackets == 0) {
302-
// Not our bracket
303-
break;
304301
} elseif ($token->value === ')') {
305-
--$brackets;
306-
if ($brackets === 0) {
307-
if (!empty($options['parenthesesDelimited'])) {
308-
// The current token is the last bracket, the next
309-
// one will be outside the expression.
310-
$ret->expr .= $token->token;
311-
++$list->idx;
302+
if ($brackets == 0) {
303+
// Not our bracket
304+
break;
305+
} else {
306+
--$brackets;
307+
if ($brackets === 0) {
308+
if (!empty($options['parenthesesDelimited'])) {
309+
// The current token is the last bracket, the next
310+
// one will be outside the expression.
311+
$ret->expr .= $token->token;
312+
++$list->idx;
313+
break;
314+
}
315+
} elseif ($brackets < 0) {
316+
// $parser->error('Unexpected closing bracket.', $token);
317+
// $brackets = 0;
312318
break;
313319
}
314-
} elseif ($brackets < 0) {
315-
// $parser->error('Unexpected closing bracket.', $token);
316-
// $brackets = 0;
317-
break;
318320
}
319321
} elseif ($token->value === ',') {
320322
// Expressions are comma-delimited.
@@ -362,7 +364,7 @@ public static function parse(Parser $parser, TokensList $list, array $options =
362364
// Found a `.` which means we expect a column name and
363365
// the column name we parsed is actually the table name
364366
// and the table name is actually a database name.
365-
if ((!empty($ret->database)) || ($dot)) {
367+
if (!empty($ret->database) || $dot) {
366368
$parser->error('Unexpected dot.', $token);
367369
}
368370
$ret->database = $ret->table;
@@ -426,13 +428,13 @@ public static function build($component, array $options = array())
426428
$ret = $component->expr;
427429
} else {
428430
$fields = array();
429-
if ((isset($component->database)) && ($component->database !== '')) {
431+
if (isset($component->database) && ($component->database !== '')) {
430432
$fields[] = $component->database;
431433
}
432-
if ((isset($component->table)) && ($component->table !== '')) {
434+
if (isset($component->table) && ($component->table !== '')) {
433435
$fields[] = $component->table;
434436
}
435-
if ((isset($component->column)) && ($component->column !== '')) {
437+
if (isset($component->column) && ($component->column !== '')) {
436438
$fields[] = $component->column;
437439
}
438440
$ret = implode('.', Context::escape($fields));

src/Components/IntoKeyword.php

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -237,11 +237,7 @@ public function parseFileOptions(Parser $parser, TokensList $list, $keyword = 'F
237237
static::$FIELDS_OPTIONS
238238
);
239239

240-
if ($keyword === 'FIELDS') {
241-
$this->fields_keyword = true;
242-
} else {
243-
$this->fields_keyword = false;
244-
}
240+
$this->fields_keyword = ($keyword === 'FIELDS');
245241
} else {
246242
// parse line options
247243
$this->lines_options = OptionsArray::parse(
@@ -272,7 +268,7 @@ public static function build($component, array $options = array())
272268

273269
$fields_options_str = OptionsArray::build($component->fields_options);
274270
if (trim($fields_options_str) !== '') {
275-
$ret .= ($component->fields_keyword) ? ' FIELDS' : ' COLUMNS';
271+
$ret .= $component->fields_keyword ? ' FIELDS' : ' COLUMNS';
276272
$ret .= ' ' . $fields_options_str;
277273
}
278274

src/Components/JoinKeyword.php

Lines changed: 20 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,7 @@ public static function parse(Parser $parser, TokensList $list, array $options =
151151

152152
if ($state === 0) {
153153
if (($token->type === Token::TYPE_KEYWORD)
154-
&& (!empty(static::$JOINS[$token->keyword]))
154+
&& !empty(static::$JOINS[$token->keyword])
155155
) {
156156
$expr->type = static::$JOINS[$token->keyword];
157157
$state = 1;
@@ -163,22 +163,25 @@ public static function parse(Parser $parser, TokensList $list, array $options =
163163
$state = 2;
164164
} elseif ($state === 2) {
165165
if ($token->type === Token::TYPE_KEYWORD) {
166-
if ($token->keyword === 'ON') {
167-
$state = 3;
168-
} elseif ($token->keyword === 'USING') {
169-
$state = 4;
170-
} else {
171-
if (($token->type === Token::TYPE_KEYWORD)
172-
&& (!empty(static::$JOINS[$token->keyword]))
173-
) {
174-
$ret[] = $expr;
175-
$expr = new self();
176-
$expr->type = static::$JOINS[$token->keyword];
177-
$state = 1;
178-
} else {
179-
/* Next clause is starting */
180-
break;
181-
}
166+
switch($token->keyword) {
167+
case 'ON':
168+
$state = 3;
169+
break;
170+
case 'USING':
171+
$state = 4;
172+
break;
173+
default:
174+
if (!empty(static::$JOINS[$token->keyword])
175+
) {
176+
$ret[] = $expr;
177+
$expr = new self();
178+
$expr->type = static::$JOINS[$token->keyword];
179+
$state = 1;
180+
} else {
181+
/* Next clause is starting */
182+
break;
183+
}
184+
break;
182185
}
183186
}
184187
} elseif ($state === 3) {

src/Components/ParameterDefinition.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ public static function parse(Parser $parser, TokensList $list, array $options =
138138
}
139139

140140
// Last iteration was not saved.
141-
if ((isset($expr->name)) && ($expr->name !== '')) {
141+
if (isset($expr->name) && ($expr->name !== '')) {
142142
$ret[] = $expr;
143143
}
144144

src/Components/UnionKeyword.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,8 @@ class UnionKeyword extends Component
2626
public static function build($component, array $options = array())
2727
{
2828
$tmp = array();
29-
foreach ($component as $component) {
30-
$tmp[] = $component[0] . ' ' . $component[1];
29+
foreach ($component as $componentPart) {
30+
$tmp[] = $componentPart[0] . ' ' . $componentPart[1];
3131
}
3232

3333
return implode(' ', $tmp);

0 commit comments

Comments
 (0)