Total Complexity | 104 |
Total Lines | 562 |
Duplicated Lines | 1.42 % |
Changes | 0 |
Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
Complex classes like ExpressionLexer often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use ExpressionLexer, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
28 | class ExpressionLexer |
||
29 | { |
||
30 | /** |
||
31 | * Suffix for single literals. |
||
32 | * |
||
33 | * @var char |
||
34 | */ |
||
35 | const SINGLE_SUFFIX_LOWER = 'f'; |
||
36 | |||
37 | /** |
||
38 | * Suffix for single literals. |
||
39 | * |
||
40 | * @var char |
||
41 | */ |
||
42 | const SINGLE_SUFFIX_UPPER = 'F'; |
||
43 | |||
44 | /** |
||
45 | * Text being parsed. |
||
46 | * |
||
47 | * @var string |
||
48 | */ |
||
49 | private $text; |
||
50 | |||
51 | /** |
||
52 | * Length of text being parsed. |
||
53 | * |
||
54 | * @var int |
||
55 | */ |
||
56 | private $textLen; |
||
57 | |||
58 | /** |
||
59 | * Position on text being parsed. |
||
60 | * |
||
61 | * @var int |
||
62 | */ |
||
63 | private $textPos; |
||
64 | |||
65 | /** |
||
66 | * Character being processed. |
||
67 | * |
||
68 | * @var string |
||
69 | */ |
||
70 | private $ch; |
||
71 | |||
72 | /** |
||
73 | * ExpressionToken being processed. |
||
74 | * |
||
75 | * @var ExpressionToken |
||
76 | */ |
||
77 | private $token; |
||
78 | |||
79 | /** |
||
80 | * Initialize a new instance of ExpressionLexer. |
||
81 | * |
||
82 | * @param string $expression Expression to parse |
||
83 | */ |
||
84 | public function __construct($expression) |
||
85 | { |
||
86 | $this->text = $expression; |
||
87 | $this->textLen = strlen($this->text); |
||
88 | $this->token = new ExpressionToken(); |
||
89 | $this->setTextPos(0); |
||
90 | $this->nextToken(); |
||
91 | } |
||
92 | |||
93 | /** |
||
94 | * To get the expression token being processed. |
||
95 | * |
||
96 | * @return ExpressionToken |
||
97 | */ |
||
98 | public function getCurrentToken() |
||
99 | { |
||
100 | return $this->token; |
||
101 | } |
||
102 | |||
103 | /** |
||
104 | * To set the token being processed. |
||
105 | * |
||
106 | * @param ExpressionToken $token The expression token to set as current |
||
107 | */ |
||
108 | public function setCurrentToken(ExpressionToken $token) |
||
109 | { |
||
110 | $this->token = $token; |
||
111 | } |
||
112 | |||
113 | /** |
||
114 | * To get the text being parsed. |
||
115 | * |
||
116 | * @return string |
||
117 | */ |
||
118 | public function getExpressionText() |
||
119 | { |
||
120 | return $this->text; |
||
121 | } |
||
122 | |||
123 | /** |
||
124 | * Position of the current token in the text being parsed. |
||
125 | * |
||
126 | * @return int |
||
127 | */ |
||
128 | public function getPosition() |
||
129 | { |
||
130 | return $this->token->Position; |
||
131 | } |
||
132 | |||
133 | /** |
||
134 | * Whether the specified token identifier is a numeric literal. |
||
135 | * |
||
136 | * @param ExpressionTokenId $id Token identifier to check |
||
137 | * |
||
138 | * @return bool true if it's a numeric literal; false otherwise |
||
139 | */ |
||
140 | public static function isNumeric($id) |
||
141 | { |
||
142 | return |
||
143 | $id == ExpressionTokenId::INTEGER_LITERAL |
||
144 | || $id == ExpressionTokenId::DECIMAL_LITERAL |
||
145 | || $id == ExpressionTokenId::DOUBLE_LITERAL |
||
146 | || $id == ExpressionTokenId::INT64_LITERAL |
||
147 | || $id == ExpressionTokenId::SINGLE_LITERAL; |
||
148 | } |
||
149 | |||
150 | /** |
||
151 | * Reads the next token, skipping whitespace as necessary. |
||
152 | */ |
||
153 | public function nextToken() |
||
154 | { |
||
155 | while (Char::isWhiteSpace($this->ch)) { |
||
156 | $this->nextChar(); |
||
157 | } |
||
158 | |||
159 | $t = null; |
||
160 | $tokenPos = $this->textPos; |
||
161 | switch ($this->ch) { |
||
162 | case '(': |
||
163 | $this->nextChar(); |
||
164 | $t = ExpressionTokenId::OPENPARAM; |
||
165 | break; |
||
166 | case ')': |
||
167 | $this->nextChar(); |
||
168 | $t = ExpressionTokenId::CLOSEPARAM; |
||
169 | break; |
||
170 | case ',': |
||
171 | $this->nextChar(); |
||
172 | $t = ExpressionTokenId::COMMA; |
||
173 | break; |
||
174 | case '-': |
||
175 | $hasNext = $this->textPos + 1 < $this->textLen; |
||
176 | if ($hasNext && Char::isDigit($this->text[$this->textPos + 1])) { |
||
177 | $this->nextChar(); |
||
178 | $t = $this->parseFromDigit(); |
||
179 | if (self::isNumeric($t)) { |
||
180 | break; |
||
181 | } |
||
182 | } elseif ($hasNext && $this->text[$tokenPos + 1] == 'I') { |
||
183 | $this->nextChar(); |
||
184 | $this->parseIdentifier(); |
||
185 | $currentIdentifier = substr($this->text, $tokenPos + 1, $this->textPos - $tokenPos - 1); |
||
186 | |||
187 | if (self::isInfinityLiteralDouble($currentIdentifier)) { |
||
188 | $t = ExpressionTokenId::DOUBLE_LITERAL; |
||
189 | break; |
||
190 | } elseif (self::isInfinityLiteralSingle($currentIdentifier)) { |
||
191 | $t = ExpressionTokenId::SINGLE_LITERAL; |
||
192 | break; |
||
193 | } |
||
194 | |||
195 | // If it looked like '-INF' but wasn't we'll rewind and fall through to a simple '-' token. |
||
196 | } |
||
197 | $this->setTextPos($tokenPos); |
||
198 | $this->nextChar(); |
||
199 | $t = ExpressionTokenId::MINUS; |
||
200 | break; |
||
201 | case '=': |
||
202 | $this->nextChar(); |
||
203 | $t = ExpressionTokenId::EQUAL; |
||
204 | break; |
||
205 | case '/': |
||
206 | $this->nextChar(); |
||
207 | $t = ExpressionTokenId::SLASH; |
||
208 | break; |
||
209 | case '?': |
||
210 | $this->nextChar(); |
||
211 | $t = ExpressionTokenId::QUESTION; |
||
212 | break; |
||
213 | case '.': |
||
214 | $this->nextChar(); |
||
215 | $t = ExpressionTokenId::DOT; |
||
216 | break; |
||
217 | case '\'': |
||
218 | $quote = $this->ch; |
||
219 | do { |
||
220 | $this->nextChar(); |
||
221 | while ($this->textPos < $this->textLen && $this->ch != $quote) { |
||
222 | $this->nextChar(); |
||
223 | } |
||
224 | |||
225 | if ($this->textPos == $this->textLen) { |
||
226 | $this->parseError( |
||
227 | Messages::expressionLexerUnterminatedStringLiteral( |
||
228 | $this->textPos, |
||
229 | $this->text |
||
230 | ) |
||
231 | ); |
||
232 | } |
||
233 | |||
234 | $this->nextChar(); |
||
235 | } while ($this->ch == $quote); |
||
236 | $t = ExpressionTokenId::STRING_LITERAL; |
||
237 | break; |
||
238 | case '*': |
||
239 | $this->nextChar(); |
||
240 | $t = ExpressionTokenId::STAR; |
||
241 | break; |
||
242 | default: |
||
243 | if (Char::isLetter($this->ch) || $this->ch == '_') { |
||
244 | $this->parseIdentifier(); |
||
245 | $t = ExpressionTokenId::IDENTIFIER; |
||
246 | break; |
||
247 | } |
||
248 | |||
249 | if (Char::isDigit($this->ch)) { |
||
250 | $t = $this->parseFromDigit(); |
||
251 | break; |
||
252 | } |
||
253 | |||
254 | if ($this->textPos == $this->textLen) { |
||
255 | $t = ExpressionTokenId::END; |
||
256 | break; |
||
257 | } |
||
258 | |||
259 | $this->parseError( |
||
260 | Messages::expressionLexerInvalidCharacter( |
||
261 | $this->ch, |
||
262 | $this->textPos |
||
263 | ) |
||
264 | ); |
||
265 | } |
||
266 | |||
267 | $this->token->Id = $t; |
||
268 | $this->token->Text = substr($this->text, $tokenPos, $this->textPos - $tokenPos); |
||
269 | $this->token->Position = $tokenPos; |
||
270 | |||
271 | // Handle type-prefixed literals such as binary, datetime or guid. |
||
272 | $this->handleTypePrefixedLiterals(); |
||
273 | |||
274 | // Handle keywords. |
||
275 | if ($this->token->Id == ExpressionTokenId::IDENTIFIER) { |
||
276 | if (self::isInfinityOrNaNDouble($this->token->Text)) { |
||
277 | $this->token->Id = ExpressionTokenId::DOUBLE_LITERAL; |
||
278 | } elseif (self::isInfinityOrNanSingle($this->token->Text)) { |
||
279 | $this->token->Id = ExpressionTokenId::SINGLE_LITERAL; |
||
280 | } elseif ($this->token->Text == ODataConstants::KEYWORD_TRUE |
||
281 | || $this->token->Text == ODataConstants::KEYWORD_FALSE |
||
282 | ) { |
||
283 | $this->token->Id = ExpressionTokenId::BOOLEAN_LITERAL; |
||
284 | } elseif ($this->token->Text == ODataConstants::KEYWORD_NULL) { |
||
285 | $this->token->Id = ExpressionTokenId::NULL_LITERAL; |
||
286 | } |
||
287 | } |
||
288 | } |
||
289 | |||
290 | /** |
||
291 | * Returns the next token without advancing the lexer to next token. |
||
292 | * |
||
293 | * @return ExpressionToken |
||
294 | */ |
||
295 | public function peekNextToken() |
||
310 | } |
||
311 | |||
312 | /** |
||
313 | * Validates the current token is of the specified kind. |
||
314 | * |
||
315 | * @param ExpressionTokenId $tokenId Expected token kind |
||
316 | * |
||
317 | * @throws ODataException if current token is not of the |
||
318 | * specified kind |
||
319 | */ |
||
320 | public function validateToken($tokenId) |
||
321 | { |
||
322 | if ($this->token->Id != $tokenId) { |
||
323 | $this->parseError(Messages::expressionLexerSyntaxError($this->textPos)); |
||
324 | } |
||
325 | } |
||
326 | |||
327 | /** |
||
328 | * Starting from an identifier, reads alternate sequence of dots and identifiers |
||
329 | * and returns the text for it. |
||
330 | * |
||
331 | * @return string The dotted identifier starting at the current identifier |
||
332 | */ |
||
333 | public function readDottedIdentifier() |
||
334 | { |
||
335 | $this->validateToken(ExpressionTokenId::IDENTIFIER); |
||
336 | $identifier = $this->token->Text; |
||
337 | $this->nextToken(); |
||
338 | while ($this->token->Id == ExpressionTokenId::DOT) { |
||
339 | $this->nextToken(); |
||
340 | $this->validateToken(ExpressionTokenId::IDENTIFIER); |
||
341 | $identifier = $identifier . '.' . $this->token->Text; |
||
342 | $this->nextToken(); |
||
343 | } |
||
344 | |||
345 | return $identifier; |
||
346 | } |
||
347 | |||
348 | /** |
||
349 | * Check if the parameter ($tokenText) is INF or NaN. |
||
350 | * |
||
351 | * @param string $tokenText Text to look in |
||
352 | * |
||
353 | * @return bool true if match found, false otherwise |
||
354 | */ |
||
355 | private static function isInfinityOrNaNDouble($tokenText) |
||
356 | { |
||
357 | if (strlen($tokenText) == 3) { |
||
358 | if ($tokenText[0] == 'I') { |
||
359 | return self::isInfinityLiteralDouble($tokenText); |
||
360 | } elseif ($tokenText[0] == 'N') { |
||
361 | return strncmp($tokenText, ODataConstants::XML_NAN_LITERAL, 3) == 0; |
||
362 | } |
||
363 | } |
||
364 | |||
365 | return false; |
||
366 | } |
||
367 | |||
368 | /** |
||
369 | * Check if the parameter ($text) is INF. |
||
370 | * |
||
371 | * @param string $text Text to look in |
||
372 | * |
||
373 | * @return bool true if match found, false otherwise |
||
374 | */ |
||
375 | private static function isInfinityLiteralDouble($text) |
||
376 | { |
||
377 | return strcmp($text, ODataConstants::XML_INFINITY_LITERAL) == 0; |
||
378 | } |
||
379 | |||
380 | /** |
||
381 | * Checks if the parameter ($tokenText) is INFf/INFF or NaNf/NaNF. |
||
382 | * |
||
383 | * @param string $tokenText Input token |
||
384 | * |
||
385 | * @return bool true if match found, false otherwise |
||
386 | */ |
||
387 | private static function isInfinityOrNanSingle($tokenText) |
||
388 | { |
||
389 | if (strlen($tokenText) == 4) { |
||
390 | if ($tokenText[0] == 'I') { |
||
391 | return self::isInfinityLiteralSingle($tokenText); |
||
392 | } elseif ($tokenText[0] == 'N') { |
||
393 | return ($tokenText[3] == self::SINGLE_SUFFIX_LOWER |
||
394 | || $tokenText[3] == self::SINGLE_SUFFIX_UPPER) |
||
395 | && strncmp($tokenText, ODataConstants::XML_NAN_LITERAL, 3) == 0; |
||
396 | } |
||
397 | } |
||
398 | |||
399 | return false; |
||
400 | } |
||
401 | |||
402 | /** |
||
403 | * Checks whether parameter ($text) EQUALS to 'INFf' or 'INFF' at position. |
||
404 | * |
||
405 | * @param string $text Text to look in |
||
406 | * |
||
407 | * @return bool true if the substring is equal using an ordinal comparison; |
||
408 | * false otherwise |
||
409 | */ |
||
410 | private static function isInfinityLiteralSingle($text) |
||
411 | { |
||
412 | return strlen($text) == 4 |
||
413 | && ($text[3] == self::SINGLE_SUFFIX_LOWER |
||
414 | || $text[3] == self::SINGLE_SUFFIX_UPPER) |
||
415 | && strncmp($text, ODataConstants::XML_INFINITY_LITERAL, 3) == 0; |
||
416 | } |
||
417 | |||
418 | /** |
||
419 | * Handles the literals that are prefixed by types. |
||
420 | * This method modified the token field as necessary. |
||
421 | * |
||
422 | * |
||
423 | * @throws ODataException |
||
424 | */ |
||
425 | private function handleTypePrefixedLiterals() |
||
426 | { |
||
427 | $id = $this->token->Id; |
||
428 | if ($id != ExpressionTokenId::IDENTIFIER) { |
||
429 | return; |
||
430 | } |
||
431 | |||
432 | $quoteFollows = $this->ch == '\''; |
||
433 | if (!$quoteFollows) { |
||
434 | return; |
||
435 | } |
||
436 | |||
437 | $tokenText = $this->token->Text; |
||
438 | |||
439 | if (strcasecmp('datetime', $tokenText) == 0) { |
||
440 | $id = ExpressionTokenId::DATETIME_LITERAL; |
||
441 | } elseif (strcasecmp('guid', $tokenText) == 0) { |
||
442 | $id = ExpressionTokenId::GUID_LITERAL; |
||
443 | } elseif (strcasecmp('binary', $tokenText) == 0 |
||
444 | || strcasecmp('X', $tokenText) == 0 |
||
445 | || strcasecmp('x', $tokenText) == 0 |
||
446 | ) { |
||
447 | $id = ExpressionTokenId::BINARY_LITERAL; |
||
448 | } else { |
||
449 | return; |
||
450 | } |
||
451 | |||
452 | $tokenPos = $this->token->Position; |
||
453 | do { |
||
454 | $this->nextChar(); |
||
455 | } while ($this->ch != '\0' && $this->ch != '\''); |
||
456 | |||
457 | if ($this->ch == '\0') { |
||
458 | $this->parseError( |
||
459 | Messages::expressionLexerUnterminatedStringLiteral( |
||
460 | $this->textPos, |
||
461 | $this->text |
||
462 | ) |
||
463 | ); |
||
464 | } |
||
465 | |||
466 | $this->nextChar(); |
||
467 | $this->token->Id = $id; |
||
468 | $this->token->Text |
||
469 | = substr($this->text, $tokenPos, $this->textPos - $tokenPos); |
||
470 | } |
||
471 | |||
472 | /** |
||
473 | * Parses a token that starts with a digit. |
||
474 | * |
||
475 | * @return ExpressionTokenId The kind of token recognized |
||
476 | */ |
||
477 | private function parseFromDigit() |
||
531 | } |
||
532 | |||
533 | /** |
||
534 | * Parses an identifier by advancing the current character. |
||
535 | */ |
||
536 | private function parseIdentifier() |
||
537 | { |
||
538 | do { |
||
539 | $this->nextChar(); |
||
540 | } while (Char::isLetterOrDigit($this->ch) || $this->ch == '_'); |
||
541 | } |
||
542 | |||
543 | /** |
||
544 | * Advance to next character. |
||
545 | */ |
||
546 | private function nextChar() |
||
547 | { |
||
548 | if ($this->textPos < $this->textLen) { |
||
549 | ++$this->textPos; |
||
550 | } |
||
551 | |||
552 | $nextChar = $this->textPos < $this->textLen ? $this->text[$this->textPos] : '\0'; |
||
553 | assert(2 >= strlen($nextChar)); |
||
554 | $this->ch = $nextChar; |
||
555 | } |
||
556 | |||
557 | /** |
||
558 | * Set the text position. |
||
559 | * |
||
560 | * @param int $pos Value to position |
||
561 | */ |
||
562 | private function setTextPos($pos) |
||
563 | { |
||
564 | $this->textPos = $pos; |
||
565 | $nextChar = $this->textPos < $this->textLen ? $this->text[$this->textPos] : '\0'; |
||
566 | assert(2 >= strlen($nextChar)); |
||
567 | $this->ch = $nextChar; |
||
568 | } |
||
569 | |||
570 | /** |
||
571 | * Validate current character is a digit. |
||
572 | */ |
||
573 | private function validateDigit() |
||
577 | } |
||
578 | } |
||
579 | |||
580 | /** |
||
581 | * Throws parser error. |
||
582 | * |
||
583 | * @param string $message The error message |
||
584 | * |
||
585 | * @throws ODataException |
||
586 | */ |
||
587 | private function parseError($message) |
||
590 | } |
||
591 | } |
||
592 |
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"]
, you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths