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 |
||
21 | class Lexer |
||
22 | { |
||
23 | private $language; |
||
24 | private $lines; |
||
25 | private $linesCount; |
||
26 | private $line; |
||
27 | private $trimmedLine; |
||
28 | private $lineNumber; |
||
29 | private $eos; |
||
30 | private $keywords; |
||
31 | private $keywordsCache = array(); |
||
32 | private $stepKeywordTypesCache = array(); |
||
33 | private $deferredObjects = array(); |
||
34 | private $deferredObjectsCount = 0; |
||
35 | private $stashedToken; |
||
36 | private $inPyString = false; |
||
37 | private $pyStringSwallow = 0; |
||
38 | private $featureStarted = false; |
||
39 | private $allowMultilineArguments = false; |
||
40 | private $allowSteps = false; |
||
41 | |||
42 | /** |
||
43 | * Initializes lexer. |
||
44 | * |
||
45 | * @param KeywordsInterface $keywords Keywords holder |
||
46 | */ |
||
47 | 244 | public function __construct(KeywordsInterface $keywords) |
|
51 | |||
52 | /** |
||
53 | * Sets lexer input. |
||
54 | * |
||
55 | * @param string $input Input string |
||
56 | * @param string $language Language name |
||
57 | * |
||
58 | * @throws Exception\LexerException |
||
59 | */ |
||
60 | 242 | public function analyse($input, $language = 'en') |
|
90 | |||
91 | /** |
||
92 | * Returns current lexer language. |
||
93 | * |
||
94 | * @return string |
||
95 | */ |
||
96 | 241 | public function getLanguage() |
|
100 | |||
101 | /** |
||
102 | * Returns next token or previously stashed one. |
||
103 | * |
||
104 | * @return array |
||
105 | */ |
||
106 | 242 | public function getAdvancedToken() |
|
110 | |||
111 | /** |
||
112 | * Defers token. |
||
113 | * |
||
114 | * @param array $token Token to defer |
||
115 | */ |
||
116 | public function deferToken(array $token) |
||
122 | |||
123 | /** |
||
124 | * Predicts for number of tokens. |
||
125 | * |
||
126 | * @return array |
||
127 | */ |
||
128 | 242 | public function predictToken() |
|
136 | |||
137 | /** |
||
138 | * Constructs token with specified parameters. |
||
139 | * |
||
140 | * @param string $type Token type |
||
141 | * @param string $value Token value |
||
142 | * |
||
143 | * @return array |
||
144 | */ |
||
145 | 242 | public function takeToken($type, $value = null) |
|
154 | |||
155 | /** |
||
156 | * Consumes line from input & increments line counter. |
||
157 | */ |
||
158 | 242 | protected function consumeLine() |
|
171 | |||
172 | /** |
||
173 | * Returns trimmed version of line. |
||
174 | * |
||
175 | * @return string |
||
176 | */ |
||
177 | 242 | protected function getTrimmedLine() |
|
181 | |||
182 | /** |
||
183 | * Returns stashed token or null if hasn't. |
||
184 | * |
||
185 | * @return array|null |
||
186 | */ |
||
187 | 242 | protected function getStashedToken() |
|
194 | |||
195 | /** |
||
196 | * Returns deferred token or null if hasn't. |
||
197 | * |
||
198 | * @return array|null |
||
199 | */ |
||
200 | 242 | protected function getDeferredToken() |
|
210 | |||
211 | /** |
||
212 | * Returns next token from input. |
||
213 | * |
||
214 | * @return array |
||
215 | */ |
||
216 | 242 | protected function getNextToken() |
|
236 | |||
237 | /** |
||
238 | * Scans for token with specified regex. |
||
239 | * |
||
240 | * @param string $regex Regular expression |
||
241 | * @param string $type Expected token type |
||
242 | * |
||
243 | * @return null|array |
||
244 | */ |
||
245 | 195 | protected function scanInput($regex, $type) |
|
256 | |||
257 | /** |
||
258 | * Scans for token with specified keywords. |
||
259 | * |
||
260 | * @param string $keywords Keywords (splitted with |) |
||
261 | * @param string $type Expected token type |
||
262 | * |
||
263 | * @return null|array |
||
264 | */ |
||
265 | 242 | protected function scanInputForKeywords($keywords, $type) |
|
296 | |||
297 | /** |
||
298 | * Scans EOS from input & returns it if found. |
||
299 | * |
||
300 | * @return null|array |
||
301 | */ |
||
302 | 242 | protected function scanEOS() |
|
310 | |||
311 | /** |
||
312 | * Returns keywords for provided type. |
||
313 | * |
||
314 | * @param string $type Keyword type |
||
315 | * |
||
316 | * @return string |
||
317 | */ |
||
318 | 242 | protected function getKeywords($type) |
|
340 | |||
341 | /** |
||
342 | * Scans Feature from input & returns it if found. |
||
343 | * |
||
344 | * @return null|array |
||
345 | */ |
||
346 | 242 | protected function scanFeature() |
|
350 | |||
351 | /** |
||
352 | * Scans Background from input & returns it if found. |
||
353 | * |
||
354 | * @return null|array |
||
355 | */ |
||
356 | 242 | protected function scanBackground() |
|
360 | |||
361 | /** |
||
362 | * Scans Rule from input & returns it if found. |
||
363 | * |
||
364 | * @return null|array |
||
365 | */ |
||
366 | 242 | protected function scanRule() |
|
370 | |||
371 | /** |
||
372 | * Scans Scenario from input & returns it if found. |
||
373 | * |
||
374 | * @return null|array |
||
375 | */ |
||
376 | 242 | protected function scanScenario() |
|
380 | |||
381 | /** |
||
382 | * Scans Scenario Outline from input & returns it if found. |
||
383 | * |
||
384 | * @return null|array |
||
385 | */ |
||
386 | 242 | protected function scanOutline() |
|
390 | |||
391 | /** |
||
392 | * Scans Scenario Outline Examples from input & returns it if found. |
||
393 | * |
||
394 | * @return null|array |
||
395 | */ |
||
396 | 242 | protected function scanExamples() |
|
400 | |||
401 | /** |
||
402 | * Scans Step from input & returns it if found. |
||
403 | * |
||
404 | * @return null|array |
||
405 | */ |
||
406 | 242 | protected function scanStep() |
|
427 | |||
428 | /** |
||
429 | * Scans PyString from input & returns it if found. |
||
430 | * |
||
431 | * @return null|array |
||
432 | */ |
||
433 | 242 | protected function scanPyStringOp() |
|
451 | |||
452 | /** |
||
453 | * Scans PyString content. |
||
454 | * |
||
455 | * @return null|array |
||
456 | */ |
||
457 | 242 | protected function scanPyStringContent() |
|
469 | |||
470 | /** |
||
471 | * Scans Table Row from input & returns it if found. |
||
472 | * |
||
473 | * @return null|array |
||
474 | */ |
||
475 | 240 | protected function scanTableRow() |
|
497 | |||
498 | /** |
||
499 | * Scans Tags from input & returns it if found. |
||
500 | * |
||
501 | * @return null|array |
||
502 | */ |
||
503 | 241 | protected function scanTags() |
|
519 | |||
520 | /** |
||
521 | * Scans Language specifier from input & returns it if found. |
||
522 | * |
||
523 | * @return null|array |
||
524 | */ |
||
525 | 242 | protected function scanLanguage() |
|
541 | |||
542 | /** |
||
543 | * Scans Comment from input & returns it if found. |
||
544 | * |
||
545 | * @return null|array |
||
546 | */ |
||
547 | 242 | protected function scanComment() |
|
563 | |||
564 | /** |
||
565 | * Scans Newline from input & returns it if found. |
||
566 | * |
||
567 | * @return null|array |
||
568 | */ |
||
569 | 240 | protected function scanNewline() |
|
580 | |||
581 | /** |
||
582 | * Scans text from input & returns it if found. |
||
583 | * |
||
584 | * @return null|array |
||
585 | */ |
||
586 | 224 | protected function scanText() |
|
593 | |||
594 | /** |
||
595 | * Returns step type keyword (Given, When, Then, etc.). |
||
596 | * |
||
597 | * @param string $native Step keyword in provided language |
||
598 | * @return string |
||
599 | */ |
||
600 | 229 | private function getStepKeywordType($native) |
|
625 | } |
||
626 |