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 | 29 | public function __construct(InputStream $input) { |
|
28 | |||
29 | /** |
||
30 | * @return Token |
||
31 | */ |
||
32 | 29 | public function next() { |
|
33 | 29 | $token = $this->current; |
|
34 | 29 | $this->current = null; |
|
35 | 29 | if ($token) { |
|
36 | 21 | return $token; |
|
37 | } |
||
38 | 8 | return $this->readNext(); |
|
39 | } |
||
40 | |||
41 | /** |
||
42 | * @return bool |
||
43 | */ |
||
44 | 10 | public function eof() { |
|
47 | |||
48 | /** |
||
49 | * @return Token |
||
|
|||
50 | */ |
||
51 | 21 | public function peek() { |
|
58 | |||
59 | /** |
||
60 | * @param string $msg |
||
61 | * |
||
62 | * @throws ParseException |
||
63 | */ |
||
64 | 1 | public function error($msg) { |
|
67 | |||
68 | /** |
||
69 | * @return Token |
||
70 | * @throws ParseException |
||
71 | */ |
||
72 | 29 | protected function readNext() { |
|
73 | 29 | $this->readWhile([$this, 'isWhitespace']); |
|
74 | 29 | if ($this->input->eof()) { |
|
75 | return null; |
||
76 | } |
||
77 | 29 | $char = $this->input->peek(); |
|
78 | 29 | if ($this->isComment()) { |
|
79 | 4 | $this->skipComment(); |
|
80 | 4 | return $this->readNext(); |
|
81 | } |
||
82 | 29 | if ($char == '"') { |
|
83 | 10 | return $this->readDoubleQuotedString(); |
|
84 | } |
||
85 | 25 | if ($char == '\'') { |
|
86 | 1 | return $this->readSingleQuotedString(); |
|
87 | } |
||
88 | 24 | if ($this->isDoubleBracketString()) { |
|
89 | 3 | return $this->readDoubleBracketString(); |
|
90 | } |
||
91 | 22 | if ($this->isDigit($char)) { |
|
92 | 8 | return $this->readNumber(); |
|
93 | } |
||
94 | 18 | if ($this->isStartIdentifierCharacter($char)) { |
|
95 | 14 | return $this->readIdentifier(); |
|
96 | } |
||
97 | 13 | if ($this->isPunctuation($char)) { |
|
98 | 12 | return $this->readPunctuation(); |
|
99 | } |
||
100 | 1 | $this->input->error('Cannot handle character: ' . $char . ' (ord: ' . ord($char) . ')'); |
|
101 | } |
||
102 | |||
103 | 4 | protected function skipComment() { |
|
104 | 4 | $this->readWhile( |
|
105 | function ($char) { |
||
106 | 4 | return $char != "\n"; |
|
107 | } |
||
108 | 4 | ); |
|
109 | 4 | $this->input->next(); |
|
110 | 4 | } |
|
111 | |||
112 | /** |
||
113 | * @return Token |
||
114 | */ |
||
115 | 10 | protected function readDoubleQuotedString() { |
|
118 | |||
119 | /** |
||
120 | * @return Token |
||
121 | */ |
||
122 | 1 | protected function readSingleQuotedString() { |
|
125 | |||
126 | /** |
||
127 | * @return Token |
||
128 | */ |
||
129 | 3 | protected function readDoubleBracketString() { |
|
156 | |||
157 | /** |
||
158 | * @param string $end |
||
159 | * |
||
160 | * @return string |
||
161 | */ |
||
162 | 11 | protected function readEscaped($end) { |
|
185 | |||
186 | /** |
||
187 | * @return Token |
||
188 | */ |
||
189 | 8 | protected function readNumber() { |
|
205 | |||
206 | /** |
||
207 | * @return Token |
||
208 | */ |
||
209 | 14 | protected function readIdentifier() { |
|
225 | |||
226 | /** |
||
227 | * @return Token |
||
228 | */ |
||
229 | 12 | protected function readPunctuation() { |
|
232 | |||
233 | /** |
||
234 | * @param callable $predicate |
||
235 | * |
||
236 | * @return string |
||
237 | */ |
||
238 | 29 | protected function readWhile(callable $predicate) { |
|
245 | |||
246 | /** |
||
247 | * @param string $char |
||
248 | * |
||
249 | * @return bool |
||
250 | */ |
||
251 | 29 | protected function isWhitespace($char) { |
|
254 | |||
255 | /** |
||
256 | * @param string $char |
||
257 | * |
||
258 | * @return bool |
||
259 | */ |
||
260 | 22 | protected function isDigit($char) { |
|
263 | |||
264 | /** |
||
265 | * @return bool |
||
266 | */ |
||
267 | 24 | protected function isDoubleBracketString() { |
|
270 | |||
271 | /** |
||
272 | * @return bool |
||
273 | */ |
||
274 | 29 | protected function isComment() { |
|
277 | |||
278 | /** |
||
279 | * @param string $char |
||
280 | * |
||
281 | * @return bool |
||
282 | */ |
||
283 | 18 | protected function isStartIdentifierCharacter($char) { |
|
286 | |||
287 | /** |
||
288 | * @param string $char |
||
289 | * |
||
290 | * @return bool |
||
291 | */ |
||
292 | 14 | protected function isIdentifierCharacter($char) { |
|
295 | |||
296 | /** |
||
297 | * @param string $char |
||
298 | * |
||
299 | * @return bool |
||
300 | */ |
||
301 | 13 | protected function isPunctuation($char) { |
|
304 | |||
305 | /** |
||
306 | * @param string $text |
||
307 | * |
||
308 | * @return bool |
||
309 | */ |
||
310 | 14 | protected function isKeyword($text) { |
|
313 | } |
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.