Conditions | 68 |
Paths | 3760 |
Total Lines | 247 |
Code Lines | 130 |
Lines | 25 |
Ratio | 10.12 % |
Tests | 149 |
CRAP Score | 68.0106 |
Changes | 13 | ||
Bugs | 2 | Features | 2 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
1 | <?php |
||
153 | 134 | public static function parse(Parser $parser, TokensList $list, array $options = array()) |
|
154 | { |
||
155 | 134 | $ret = new Expression(); |
|
156 | |||
157 | /** |
||
158 | * Whether current tokens make an expression or a table reference. |
||
159 | * |
||
160 | * @var bool $isExpr |
||
161 | */ |
||
162 | 134 | $isExpr = false; |
|
163 | |||
164 | /** |
||
165 | * Whether a period was previously found. |
||
166 | * |
||
167 | * @var bool $dot |
||
168 | */ |
||
169 | 134 | $dot = false; |
|
170 | |||
171 | /** |
||
172 | * Whether an alias is expected. Is 2 if `AS` keyword was found. |
||
173 | * |
||
174 | * @var bool $alias |
||
175 | */ |
||
176 | 134 | $alias = false; |
|
177 | |||
178 | /** |
||
179 | * Counts brackets. |
||
180 | * |
||
181 | * @var int $brackets |
||
182 | */ |
||
183 | 134 | $brackets = 0; |
|
184 | |||
185 | /** |
||
186 | * Keeps track of the last two previous tokens. |
||
187 | * |
||
188 | * @var Token[] $prev |
||
189 | */ |
||
190 | 134 | $prev = array(null, null); |
|
191 | |||
192 | // When a field is parsed, no parentheses are expected. |
||
193 | 134 | if (!empty($options['parseField'])) { |
|
194 | 113 | $options['breakOnParentheses'] = true; |
|
195 | 113 | $options['field'] = $options['parseField']; |
|
196 | 113 | } |
|
197 | |||
198 | 134 | for (; $list->idx < $list->count; ++$list->idx) { |
|
199 | |||
200 | /** |
||
201 | * Token parsed at this moment. |
||
202 | * |
||
203 | * @var Token $token |
||
204 | */ |
||
205 | 134 | $token = $list->tokens[$list->idx]; |
|
206 | |||
207 | // End of statement. |
||
208 | 134 | if ($token->type === Token::TYPE_DELIMITER) { |
|
209 | 47 | break; |
|
210 | } |
||
211 | |||
212 | // Skipping whitespaces and comments. |
||
213 | 133 | if (($token->type === Token::TYPE_WHITESPACE) |
|
214 | 133 | || ($token->type === Token::TYPE_COMMENT) |
|
215 | 133 | ) { |
|
216 | 93 | if ($isExpr) { |
|
217 | 48 | $ret->expr .= $token->token; |
|
218 | 48 | } |
|
219 | 93 | continue; |
|
220 | } |
||
221 | |||
222 | 133 | if ($token->type === Token::TYPE_KEYWORD) { |
|
223 | 80 | if (($brackets > 0) && (empty($ret->subquery)) |
|
224 | 80 | && (!empty(Parser::$STATEMENT_PARSERS[$token->value])) |
|
225 | 80 | ) { |
|
226 | // A `(` was previously found and this keyword is the |
||
227 | // beginning of a statement, so this is a subquery. |
||
228 | 6 | $ret->subquery = $token->value; |
|
229 | 80 | } elseif (($token->flags & Token::FLAG_KEYWORD_FUNCTION) |
|
230 | 79 | && (empty($options['parseField'])) |
|
231 | 79 | ) { |
|
232 | 13 | $isExpr = true; |
|
233 | 79 | } elseif (($token->flags & Token::FLAG_KEYWORD_RESERVED) |
|
234 | 75 | && ($brackets === 0) |
|
235 | 75 | ) { |
|
236 | 72 | if (empty(self::$ALLOWED_KEYWORDS[$token->value])) { |
|
237 | // A reserved keyword that is not allowed in the |
||
238 | // expression was found so the expression must have |
||
239 | // ended and a new clause is starting. |
||
240 | 68 | break; |
|
241 | } |
||
242 | 14 | View Code Duplication | if ($token->value === 'AS') { |
1 ignored issue
–
show
|
|||
243 | 9 | if (!empty($options['breakOnAlias'])) { |
|
244 | 1 | break; |
|
245 | } |
||
246 | 8 | if (!empty($ret->alias)) { |
|
247 | 1 | $parser->error( |
|
248 | 1 | __('An alias was previously found.'), |
|
249 | $token |
||
250 | 1 | ); |
|
251 | 1 | break; |
|
252 | } |
||
253 | 8 | $alias = true; |
|
254 | 8 | continue; |
|
255 | } |
||
256 | 5 | $isExpr = true; |
|
257 | 5 | } |
|
258 | 26 | } |
|
259 | |||
260 | 131 | if (($token->type === Token::TYPE_NUMBER) |
|
261 | 127 | || ($token->type === Token::TYPE_BOOL) |
|
262 | 127 | || (($token->type === Token::TYPE_SYMBOL) |
|
263 | 127 | && ($token->flags & Token::FLAG_SYMBOL_VARIABLE)) |
|
264 | 127 | || (($token->type === Token::TYPE_OPERATOR) |
|
265 | 127 | && ($token->value !== '.')) |
|
266 | 131 | ) { |
|
267 | 96 | if (!empty($options['parseField'])) { |
|
268 | 46 | break; |
|
269 | } |
||
270 | |||
271 | // Numbers, booleans and operators (except dot) are usually part |
||
272 | // of expressions. |
||
273 | 67 | $isExpr = true; |
|
274 | 67 | } |
|
275 | |||
276 | 131 | if ($token->type === Token::TYPE_OPERATOR) { |
|
277 | 63 | if ((!empty($options['breakOnParentheses'])) |
|
278 | 63 | && (($token->value === '(') || ($token->value === ')')) |
|
279 | 63 | ) { |
|
280 | // No brackets were expected. |
||
281 | 2 | break; |
|
282 | } |
||
283 | 62 | if ($token->value === '(') { |
|
284 | 21 | ++$brackets; |
|
285 | 21 | if ((empty($ret->function)) && ($prev[1] !== null) |
|
286 | 21 | && (($prev[1]->type === Token::TYPE_NONE) |
|
287 | 7 | || ($prev[1]->type === Token::TYPE_SYMBOL) |
|
288 | 7 | || (($prev[1]->type === Token::TYPE_KEYWORD) |
|
289 | 7 | && ($prev[1]->flags & Token::FLAG_KEYWORD_FUNCTION))) |
|
290 | 21 | ) { |
|
291 | 7 | $ret->function = $prev[1]->value; |
|
292 | 7 | } |
|
293 | 62 | } elseif ($token->value === ')') { |
|
294 | 24 | --$brackets; |
|
295 | 24 | if ($brackets === 0) { |
|
296 | 21 | if (!empty($options['parenthesesDelimited'])) { |
|
297 | // The current token is the last bracket, the next |
||
298 | // one will be outside the expression. |
||
299 | 7 | $ret->expr .= $token->token; |
|
300 | 7 | ++$list->idx; |
|
301 | 7 | break; |
|
302 | } |
||
303 | 19 | } elseif ($brackets < 0) { |
|
304 | // $parser->error(__('Unexpected closing bracket.'), $token); |
||
1 ignored issue
–
show
|
|||
305 | // $brackets = 0; |
||
1 ignored issue
–
show
|
|||
306 | 3 | break; |
|
307 | } |
||
308 | 56 | } elseif ($token->value === ',') { |
|
309 | // Expressions are comma-delimited. |
||
310 | 29 | if ($brackets === 0) { |
|
311 | 25 | break; |
|
312 | } |
||
313 | 4 | } |
|
314 | 48 | } |
|
315 | |||
316 | // Saving the previous tokens. |
||
317 | 130 | $prev[0] = $prev[1]; |
|
318 | 130 | $prev[1] = $token; |
|
319 | |||
320 | 130 | if ($alias) { |
|
321 | // An alias is expected (the keyword `AS` was previously found). |
||
322 | 8 | if (!empty($ret->alias)) { |
|
323 | $parser->error(__('An alias was previously found.'), $token); |
||
324 | break; |
||
325 | } |
||
326 | 8 | $ret->alias = $token->value; |
|
327 | 8 | $alias = false; |
|
328 | 130 | } elseif ($isExpr) { |
|
329 | // Handling aliases. |
||
330 | 60 | if (/* (empty($ret->alias)) && */ ($brackets === 0) |
|
1 ignored issue
–
show
|
|||
331 | 60 | && (($prev[0] === null) |
|
332 | 20 | || ((($prev[0]->type !== Token::TYPE_OPERATOR) |
|
333 | 10 | || ($prev[0]->token === ')')) |
|
334 | 20 | && (($prev[0]->type !== Token::TYPE_KEYWORD) |
|
335 | 20 | || (!($prev[0]->flags & Token::FLAG_KEYWORD_RESERVED))))) |
|
336 | 60 | && (($prev[1]->type === Token::TYPE_STRING) |
|
337 | 52 | || (($prev[1]->type === Token::TYPE_SYMBOL) |
|
338 | 52 | && (!($prev[1]->flags & Token::FLAG_SYMBOL_VARIABLE))) |
|
339 | 52 | || ($prev[1]->type === Token::TYPE_NONE)) |
|
340 | 60 | ) { |
|
341 | 2 | if (!empty($ret->alias)) { |
|
342 | 1 | $parser->error(__('An alias was previously found.'), $token); |
|
343 | 1 | break; |
|
344 | } |
||
345 | 2 | $ret->alias = $prev[1]->value; |
|
346 | 2 | } else { |
|
347 | 60 | $ret->expr .= $token->token; |
|
348 | } |
||
349 | 130 | } elseif (!$isExpr) { |
|
350 | 115 | if (($token->type === Token::TYPE_OPERATOR) && ($token->value === '.')) { |
|
351 | // Found a `.` which means we expect a column name and |
||
352 | // the column name we parsed is actually the table name |
||
353 | // and the table name is actually a database name. |
||
354 | 13 | if ((!empty($ret->database)) || ($dot)) { |
|
355 | 2 | $parser->error(__('Unexpected dot.'), $token); |
|
356 | 2 | } |
|
357 | 13 | $ret->database = $ret->table; |
|
358 | 13 | $ret->table = $ret->column; |
|
359 | 13 | $ret->column = null; |
|
360 | 13 | $dot = true; |
|
361 | 13 | $ret->expr .= $token->token; |
|
362 | 13 | } else { |
|
363 | 115 | $field = empty($options['field']) ? 'column' : $options['field']; |
|
364 | 115 | if (empty($ret->$field)) { |
|
365 | 115 | $ret->$field = $token->value; |
|
366 | 115 | $ret->expr .= $token->token; |
|
367 | 115 | $dot = false; |
|
368 | 115 | View Code Duplication | } else { |
1 ignored issue
–
show
|
|||
369 | // No alias is expected. |
||
370 | 10 | if (!empty($options['breakOnAlias'])) { |
|
371 | 2 | break; |
|
372 | } |
||
373 | 8 | if (!empty($ret->alias)) { |
|
374 | 1 | $parser->error(__('An alias was previously found.'), $token); |
|
375 | 1 | break; |
|
376 | } |
||
377 | 8 | $ret->alias = $token->value; |
|
378 | } |
||
379 | } |
||
380 | 115 | } |
|
381 | 130 | } |
|
382 | |||
383 | 134 | if ($alias) { |
|
384 | 1 | $parser->error( |
|
385 | 1 | __('An alias was expected.'), |
|
386 | 1 | $list->tokens[$list->idx - 1] |
|
387 | 1 | ); |
|
388 | 1 | } |
|
389 | |||
390 | // White-spaces might be added at the end. |
||
391 | 134 | $ret->expr = trim($ret->expr); |
|
392 | |||
393 | 134 | if (empty($ret->expr)) { |
|
394 | 11 | return null; |
|
395 | } |
||
396 | |||
397 | 130 | --$list->idx; |
|
398 | 130 | return $ret; |
|
399 | } |
||
400 | |||
436 |
This check marks property names that have not been written in camelCase.
In camelCase names are written without any punctuation, the start of each new word being marked by a capital letter. Thus the name database connection string becomes
databaseConnectionString
.