Complex classes like TokenStream 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 TokenStream, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
13 | class TokenStream { |
||
14 | private $current = null; |
||
15 | /** |
||
16 | * @var InputStream |
||
17 | */ |
||
18 | private $input; |
||
19 | |||
20 | /** |
||
21 | * TokenStream constructor. |
||
22 | * |
||
23 | * @param InputStream $input |
||
24 | */ |
||
25 | 40 | public function __construct(InputStream $input) { |
|
28 | |||
29 | /** |
||
30 | * @return Token |
||
31 | */ |
||
32 | 40 | public function next() { |
|
40 | |||
41 | /** |
||
42 | * @return bool |
||
43 | */ |
||
44 | 23 | public function eof() { |
|
47 | |||
48 | /** |
||
49 | * @return Token |
||
|
|||
50 | */ |
||
51 | 24 | public function peek() { |
|
58 | |||
59 | /** |
||
60 | * @param string $msg |
||
61 | * |
||
62 | * @throws ParseException |
||
63 | */ |
||
64 | 4 | public function error($msg) { |
|
67 | |||
68 | /** |
||
69 | * @return Token |
||
70 | * @throws ParseException |
||
71 | */ |
||
72 | 40 | protected function readNext() { |
|
106 | |||
107 | 6 | protected function skipComment() { |
|
117 | |||
118 | /** |
||
119 | * @return Token |
||
120 | */ |
||
121 | 14 | protected function readDoubleQuotedString() { |
|
124 | |||
125 | /** |
||
126 | * @return Token |
||
127 | */ |
||
128 | 1 | protected function readSingleQuotedString() { |
|
131 | |||
132 | /** |
||
133 | * @return Token |
||
134 | */ |
||
135 | 13 | protected function readDoubleBracketString() { |
|
186 | |||
187 | /** |
||
188 | * @param string $end |
||
189 | * |
||
190 | * @return string |
||
191 | */ |
||
192 | 15 | protected function readEscaped($end) { |
|
231 | |||
232 | /** |
||
233 | * @return Token |
||
234 | */ |
||
235 | 10 | protected function readNumber() { |
|
251 | |||
252 | /** |
||
253 | * @return Token |
||
254 | */ |
||
255 | 17 | protected function readIdentifier() { |
|
271 | |||
272 | /** |
||
273 | * @return Token |
||
274 | */ |
||
275 | 15 | protected function readPunctuation() { |
|
278 | |||
279 | /** |
||
280 | * @param callable $predicate |
||
281 | * |
||
282 | * @return string |
||
283 | */ |
||
284 | 40 | protected function readWhile(callable $predicate) { |
|
291 | |||
292 | /** |
||
293 | * @param string $char |
||
294 | * |
||
295 | * @return bool |
||
296 | */ |
||
297 | 40 | protected function isWhitespace($char) { |
|
300 | |||
301 | /** |
||
302 | * @param string $char |
||
303 | * |
||
304 | * @return bool |
||
305 | */ |
||
306 | 25 | protected function isDigit($char) { |
|
309 | |||
310 | /** |
||
311 | * @return bool |
||
312 | */ |
||
313 | 34 | protected function isDoubleBracketString() { |
|
316 | |||
317 | /** |
||
318 | * @return bool |
||
319 | */ |
||
320 | 40 | protected function isComment() { |
|
323 | |||
324 | /** |
||
325 | * @param string $char |
||
326 | * |
||
327 | * @return bool |
||
328 | */ |
||
329 | 21 | protected function isStartIdentifierCharacter($char) { |
|
332 | |||
333 | /** |
||
334 | * @param string $char |
||
335 | * |
||
336 | * @return bool |
||
337 | */ |
||
338 | 17 | protected function isIdentifierCharacter($char) { |
|
341 | |||
342 | /** |
||
343 | * @param string $char |
||
344 | * |
||
345 | * @return bool |
||
346 | */ |
||
347 | 16 | protected function isPunctuation($char) { |
|
350 | |||
351 | /** |
||
352 | * @param string $text |
||
353 | * |
||
354 | * @return bool |
||
355 | */ |
||
356 | 17 | protected function isKeyword($text) { |
|
359 | } |
This check compares the return type specified in the
@return
annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.