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. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
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 |
||
30 | class ExpressionLexer |
||
31 | { |
||
32 | /** |
||
33 | * Suffix for single literals |
||
34 | * |
||
35 | * @var char |
||
36 | */ |
||
37 | const SINGLE_SUFFIX_LOWER = 'f'; |
||
38 | |||
39 | /** |
||
40 | * Suffix for single literals |
||
41 | * |
||
42 | * @var char |
||
43 | */ |
||
44 | const SINGLE_SUFFIX_UPPER = 'F'; |
||
45 | |||
46 | /** |
||
47 | * Text being parsed |
||
48 | * |
||
49 | * @var char[] |
||
50 | */ |
||
51 | private $_text; |
||
52 | |||
53 | /** |
||
54 | * Length of text being parsed |
||
55 | * |
||
56 | * @var int |
||
57 | */ |
||
58 | private $_textLen; |
||
59 | |||
60 | /** |
||
61 | * Position on text being parsed |
||
62 | * |
||
63 | * @var int |
||
64 | */ |
||
65 | private $_textPos; |
||
66 | |||
67 | /** |
||
68 | * Character being processed |
||
69 | * |
||
70 | * @var char |
||
71 | */ |
||
72 | private $_ch; |
||
73 | |||
74 | /** |
||
75 | * ExpressionToken being processed |
||
76 | * |
||
77 | * @var ExpressionToken |
||
78 | */ |
||
79 | private $_token; |
||
80 | |||
81 | /** |
||
82 | * Initialize a new instance of ExpressionLexer |
||
83 | * |
||
84 | * @param string $expression Expression to parse |
||
85 | */ |
||
86 | public function __construct($expression) |
||
94 | |||
95 | /** |
||
96 | * To get the expression token being processed |
||
97 | * |
||
98 | * @return ExpressionToken |
||
99 | */ |
||
100 | public function getCurrentToken() |
||
104 | |||
105 | /** |
||
106 | * To set the token being processed |
||
107 | * |
||
108 | * @param ExpressionToken $token The expression token to set as current |
||
109 | * |
||
110 | * @return void |
||
111 | */ |
||
112 | public function setCurrentToken($token) |
||
116 | |||
117 | /** |
||
118 | * To get the text being parsed |
||
119 | * |
||
120 | * @return string |
||
121 | */ |
||
122 | public function getExpressionText() |
||
126 | |||
127 | /** |
||
128 | * Position of the current token in the text being parsed |
||
129 | * |
||
130 | * @return int |
||
131 | */ |
||
132 | public function getPosition() |
||
136 | |||
137 | /** |
||
138 | * Whether the specified token identifier is a numeric literal |
||
139 | * |
||
140 | * @param ExpressionTokenId $id Token identifier to check |
||
141 | * |
||
142 | * @return bool true if it's a numeric literal; false otherwise |
||
143 | */ |
||
144 | public static function isNumeric($id) |
||
153 | |||
154 | /** |
||
155 | * Reads the next token, skipping whitespace as necessary. |
||
156 | * |
||
157 | * @return void |
||
158 | */ |
||
159 | public function nextToken() |
||
298 | |||
299 | /** |
||
300 | * Returns the next token without advancing the lexer to next token |
||
301 | * |
||
302 | * @return ExpressionToken |
||
303 | */ |
||
304 | public function peekNextToken() |
||
318 | |||
319 | /** |
||
320 | * Validates the current token is of the specified kind |
||
321 | * |
||
322 | * @param ExpressionTokenId $tokenId Expected token kind |
||
323 | * |
||
324 | * @return void |
||
325 | * |
||
326 | * @throws ODataException if current token is not of the |
||
327 | * specified kind. |
||
328 | */ |
||
329 | public function validateToken($tokenId) |
||
339 | |||
340 | /** |
||
341 | * Starting from an identifier, reads alternate sequence of dots and identifiers |
||
342 | * and returns the text for it |
||
343 | * |
||
344 | * @return string The dotted identifier starting at the current identifier |
||
345 | */ |
||
346 | public function readDottedIdentifier() |
||
360 | |||
361 | /** |
||
362 | * Check if the parameter ($tokenText) is INF or NaN |
||
363 | * |
||
364 | * @param string $tokenText Text to look in |
||
365 | * |
||
366 | * @return boolean true if match found, false otherwise |
||
367 | */ |
||
368 | private static function _isInfinityOrNaNDouble($tokenText) |
||
380 | |||
381 | /** |
||
382 | * Check if the parameter ($text) is INF |
||
383 | * |
||
384 | * @param string $text Text to look in |
||
385 | * |
||
386 | * @return boolean true if match found, false otherwise |
||
387 | */ |
||
388 | private static function _isInfinityLiteralDouble($text) |
||
392 | |||
393 | /** |
||
394 | * Checks if the parameter ($tokenText) is INFf/INFF or NaNf/NaNF. |
||
395 | * |
||
396 | * @param string $tokenText Input token |
||
397 | * |
||
398 | * @return bool true if match found, false otherwise |
||
399 | */ |
||
400 | private static function _isInfinityOrNanSingle($tokenText) |
||
414 | |||
415 | /** |
||
416 | * Checks whether parameter ($text) EQUALS to 'INFf' or 'INFF' at position |
||
417 | * |
||
418 | * @param string $text Text to look in |
||
419 | * |
||
420 | * @return bool true if the substring is equal using an ordinal comparison; |
||
421 | * false otherwise |
||
422 | */ |
||
423 | private static function _isInfinityLiteralSingle($text) |
||
430 | |||
431 | /** |
||
432 | * Handles the literals that are prefixed by types. |
||
433 | * This method modified the token field as necessary. |
||
434 | * |
||
435 | * @return void |
||
436 | * |
||
437 | * @throws ODataException |
||
438 | */ |
||
439 | private function _handleTypePrefixedLiterals() |
||
484 | |||
485 | /** |
||
486 | * Parses a token that starts with a digit |
||
487 | * |
||
488 | * @return ExpressionTokenId The kind of token recognized. |
||
489 | */ |
||
490 | private function _parseFromDigit() |
||
546 | |||
547 | /** |
||
548 | * Parses an identifier by advancing the current character. |
||
549 | * |
||
550 | * @return void |
||
551 | */ |
||
552 | private function _parseIdentifier() |
||
558 | |||
559 | /** |
||
560 | * Advance to next character. |
||
561 | * |
||
562 | * @return void |
||
563 | */ |
||
564 | private function _nextChar() |
||
574 | |||
575 | /** |
||
576 | * Set the text position. |
||
577 | * |
||
578 | * @param int $pos Value to position. |
||
579 | * |
||
580 | * @return void |
||
581 | */ |
||
582 | private function _setTextPos($pos) |
||
589 | |||
590 | /** |
||
591 | * Validate current character is a digit. |
||
592 | * |
||
593 | * @return void |
||
594 | */ |
||
595 | private function _validateDigit() |
||
605 | |||
606 | /** |
||
607 | * Throws parser error. |
||
608 | * |
||
609 | * @param string $message The error message. |
||
610 | * |
||
611 | * @return void |
||
612 | * |
||
613 | * @throws ODataException |
||
614 | */ |
||
615 | private function _parseError($message) |
||
619 | } |
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..