Complex classes like Lexer 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 Lexer, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 19 | class Lexer |
||
| 20 | { |
||
| 21 | /** |
||
| 22 | * Whitespace tokens. |
||
| 23 | */ |
||
| 24 | private const SPACE_TOKENS = [ |
||
| 25 | Token::T_SPACE, |
||
| 26 | Token::T_COMMENT, |
||
| 27 | Token::T_NEWLINE, |
||
| 28 | ]; |
||
| 29 | |||
| 30 | /** |
||
| 31 | * Tag map. |
||
| 32 | */ |
||
| 33 | private const TAG_MAP = [ |
||
| 34 | ':' => Token::T_COLON, |
||
| 35 | ';' => Token::T_SEMICOLON, |
||
| 36 | '$' => Token::T_DOLLAR, |
||
| 37 | '%%' => Token::T_MARK, |
||
| 38 | '%{' => Token::T_BEGIN_INC, |
||
| 39 | '%}' => Token::T_END_INC, |
||
| 40 | '%token' => Token::T_TOKEN, |
||
| 41 | '%term' => Token::T_TOKEN, |
||
| 42 | '%left' => Token::T_LEFT, |
||
| 43 | '%right' => Token::T_RIGHT, |
||
| 44 | '%nonassoc' => Token::T_NON_ASSOC, |
||
| 45 | '%prec' => Token::T_PRECTOK, |
||
| 46 | '%type' => Token::T_TYPE, |
||
| 47 | '%union' => Token::T_UNION, |
||
| 48 | '%start' => Token::T_START, |
||
| 49 | '%expect' => Token::T_EXPECT, |
||
| 50 | '%pure_parser' => Token::T_PURE_PARSER, |
||
| 51 | ]; |
||
| 52 | |||
| 53 | /** |
||
| 54 | * @var string |
||
| 55 | */ |
||
| 56 | protected $buffer; |
||
| 57 | |||
| 58 | /** |
||
| 59 | * @var string |
||
| 60 | */ |
||
| 61 | protected $fileName; |
||
| 62 | |||
| 63 | /** |
||
| 64 | * @var int |
||
| 65 | */ |
||
| 66 | protected $line; |
||
| 67 | |||
| 68 | /** |
||
| 69 | * @var int |
||
| 70 | */ |
||
| 71 | protected $offset; |
||
| 72 | |||
| 73 | /** |
||
| 74 | * @var Token |
||
| 75 | */ |
||
| 76 | protected $currentToken; |
||
| 77 | |||
| 78 | /** |
||
| 79 | * @var Token |
||
| 80 | */ |
||
| 81 | protected $backToken; |
||
| 82 | |||
| 83 | /** |
||
| 84 | * @var string |
||
| 85 | */ |
||
| 86 | protected $backChar; |
||
| 87 | |||
| 88 | /** |
||
| 89 | * @var bool |
||
| 90 | */ |
||
| 91 | protected $prevIsDollar; |
||
| 92 | |||
| 93 | /** |
||
| 94 | * @var string |
||
| 95 | */ |
||
| 96 | protected $char; |
||
| 97 | |||
| 98 | /** |
||
| 99 | * @var string |
||
| 100 | */ |
||
| 101 | protected $value; |
||
| 102 | |||
| 103 | /** |
||
| 104 | * @param string $code |
||
| 105 | * @param string $filename |
||
| 106 | */ |
||
| 107 | public function startLexing(string $code, string $filename = '') |
||
| 114 | |||
| 115 | /** |
||
| 116 | * @return void |
||
| 117 | */ |
||
| 118 | protected function reset() |
||
| 126 | |||
| 127 | /** |
||
| 128 | * @throws LexingException |
||
| 129 | * @throws ParseException |
||
| 130 | * |
||
| 131 | * @return Token |
||
| 132 | */ |
||
| 133 | public function getToken(): Token |
||
| 143 | |||
| 144 | /** |
||
| 145 | * @throws LexingException |
||
| 146 | */ |
||
| 147 | public function ungetToken() |
||
| 155 | |||
| 156 | /** |
||
| 157 | * @throws LexingException |
||
| 158 | * @throws ParseException |
||
| 159 | * |
||
| 160 | * @return Token |
||
| 161 | */ |
||
| 162 | public function peek(): Token |
||
| 169 | |||
| 170 | /** |
||
| 171 | * @throws LexingException |
||
| 172 | * @throws ParseException |
||
| 173 | * |
||
| 174 | * @return Token |
||
| 175 | */ |
||
| 176 | public function getRawToken() |
||
| 208 | |||
| 209 | /** |
||
| 210 | * @throws LexingException |
||
| 211 | * |
||
| 212 | * @return bool |
||
| 213 | */ |
||
| 214 | protected function isWhitespace(): bool |
||
| 228 | |||
| 229 | /** |
||
| 230 | * @return bool |
||
| 231 | */ |
||
| 232 | protected function isNewline(): bool |
||
| 242 | |||
| 243 | /** |
||
| 244 | * @throws LexingException |
||
| 245 | * @throws ParseException |
||
| 246 | * |
||
| 247 | * @return bool |
||
| 248 | */ |
||
| 249 | protected function isComment(): bool |
||
| 292 | |||
| 293 | /** |
||
| 294 | * @return bool |
||
| 295 | */ |
||
| 296 | protected function isEof(): bool |
||
| 306 | |||
| 307 | /** |
||
| 308 | * @throws LexingException |
||
| 309 | * @throws ParseException |
||
| 310 | * |
||
| 311 | * @return int |
||
| 312 | */ |
||
| 313 | protected function detectToken() |
||
| 393 | |||
| 394 | /** |
||
| 395 | * @return string |
||
| 396 | */ |
||
| 397 | protected function getChar(): string |
||
| 418 | |||
| 419 | /** |
||
| 420 | * @param string $char |
||
| 421 | * |
||
| 422 | * @throws LexingException |
||
| 423 | */ |
||
| 424 | protected function ungetChar(string $char) |
||
| 436 | |||
| 437 | /** |
||
| 438 | * @param int $type |
||
| 439 | * @param string $value |
||
| 440 | * |
||
| 441 | * @return Token |
||
| 442 | */ |
||
| 443 | protected function token(int $type, string $value): Token |
||
| 447 | } |
||
| 448 |