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::SPACE, |
||
26 | Token::COMMENT, |
||
27 | Token::NEWLINE, |
||
28 | ]; |
||
29 | |||
30 | /** |
||
31 | * Tag map. |
||
32 | */ |
||
33 | private const TAG_MAP = [ |
||
34 | '%%' => Token::MARK, |
||
35 | '%{' => Token::BEGININC, |
||
36 | '%}' => Token::ENDINC, |
||
37 | '%token' => Token::TOKEN, |
||
38 | '%term' => Token::TOKEN, |
||
39 | '%left' => Token::LEFT, |
||
40 | '%right' => Token::RIGHT, |
||
41 | '%nonassoc' => Token::NONASSOC, |
||
42 | '%prec' => Token::PRECTOK, |
||
43 | '%type' => Token::TYPE, |
||
44 | '%union' => Token::UNION, |
||
45 | '%start' => Token::START, |
||
46 | '%expect' => Token::EXPECT, |
||
47 | '%pure_parser' => Token::PURE_PARSER, |
||
48 | ]; |
||
49 | |||
50 | /** |
||
51 | * @var string |
||
52 | */ |
||
53 | protected $buffer; |
||
54 | |||
55 | /** |
||
56 | * @var string |
||
57 | */ |
||
58 | protected $filename; |
||
59 | |||
60 | /** |
||
61 | * @var int |
||
62 | */ |
||
63 | protected $line; |
||
64 | |||
65 | /** |
||
66 | * @var int |
||
67 | */ |
||
68 | protected $offset; |
||
69 | |||
70 | /** |
||
71 | * @var Token |
||
72 | */ |
||
73 | protected $currentToken; |
||
74 | |||
75 | /** |
||
76 | * @var Token |
||
77 | */ |
||
78 | protected $backToken; |
||
79 | |||
80 | /** |
||
81 | * @var string |
||
82 | */ |
||
83 | protected $backChar; |
||
84 | |||
85 | /** |
||
86 | * @var bool |
||
87 | */ |
||
88 | protected $prevIsDollar; |
||
89 | |||
90 | /** |
||
91 | * @var string |
||
92 | */ |
||
93 | protected $char; |
||
94 | |||
95 | /** |
||
96 | * @var string |
||
97 | */ |
||
98 | protected $value; |
||
99 | |||
100 | /** |
||
101 | * @param string $code |
||
102 | * @param string $filename |
||
103 | */ |
||
104 | public function startLexing(string $code, string $filename = '') |
||
111 | |||
112 | /** |
||
113 | * @return void |
||
114 | */ |
||
115 | protected function reset() |
||
123 | |||
124 | /** |
||
125 | * @throws LexingException |
||
126 | * @throws ParseException |
||
127 | * |
||
128 | * @return Token |
||
129 | */ |
||
130 | public function getToken(): Token |
||
140 | |||
141 | /** |
||
142 | * @throws LexingException |
||
143 | */ |
||
144 | public function ungetToken() |
||
152 | |||
153 | /** |
||
154 | * @throws LexingException |
||
155 | * @throws ParseException |
||
156 | * |
||
157 | * @return Token |
||
158 | */ |
||
159 | public function peek(): Token |
||
166 | |||
167 | /** |
||
168 | * @throws LexingException |
||
169 | * @throws ParseException |
||
170 | * |
||
171 | * @return Token |
||
172 | */ |
||
173 | public function getRawToken() |
||
211 | |||
212 | /** |
||
213 | * @return bool |
||
214 | */ |
||
215 | protected function isWhitespace(): bool |
||
229 | |||
230 | /** |
||
231 | * @return bool |
||
232 | */ |
||
233 | protected function isNewline(): bool |
||
242 | |||
243 | /** |
||
244 | * @throws ParseException |
||
245 | * |
||
246 | * @return bool |
||
247 | */ |
||
248 | protected function isComment(): bool |
||
291 | |||
292 | /** |
||
293 | * @return bool |
||
294 | */ |
||
295 | protected function isEof(): bool |
||
304 | |||
305 | /** |
||
306 | * @throws ParseException |
||
307 | * |
||
308 | * @return int |
||
309 | */ |
||
310 | protected function detectToken() |
||
390 | |||
391 | /** |
||
392 | * @return string |
||
393 | */ |
||
394 | protected function getChar(): string |
||
415 | |||
416 | /** |
||
417 | * @param string $char |
||
418 | * |
||
419 | * @throws LexingException |
||
420 | */ |
||
421 | protected function ungetChar(string $char) |
||
433 | |||
434 | /** |
||
435 | * @param int $type |
||
436 | * @param string $value |
||
437 | * |
||
438 | * @return Token |
||
439 | */ |
||
440 | protected function token(int $type, string $value): Token |
||
444 | } |
||
445 |
When comparing the result of a bit operation, we suggest to add explicit parenthesis and not to rely on PHP’s built-in operator precedence to ensure the code behaves as intended and to make it more readable.
Let’s take a look at these examples: