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 | 24 | public function __construct(InputStream $input) { |
|
28 | |||
29 | /** |
||
30 | * @return Token |
||
31 | */ |
||
32 | 24 | public function next() { |
|
33 | 24 | $token = $this->current; |
|
34 | 24 | $this->current = null; |
|
35 | 24 | if ($token) { |
|
36 | 16 | return $token; |
|
37 | } |
||
38 | 8 | return $this->readNext(); |
|
39 | } |
||
40 | |||
41 | /** |
||
42 | * @return bool |
||
43 | */ |
||
44 | 6 | public function eof() { |
|
47 | |||
48 | /** |
||
49 | * @return Token |
||
|
|||
50 | */ |
||
51 | 16 | 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 | 24 | protected function readNext() { |
|
102 | |||
103 | protected function skipComment() { |
||
111 | |||
112 | /** |
||
113 | * @return Token |
||
114 | */ |
||
115 | 9 | protected function readDoubleQuotedString() { |
|
118 | |||
119 | /** |
||
120 | * @return Token |
||
121 | */ |
||
122 | 1 | protected function readSingleQuotedString() { |
|
125 | |||
126 | /** |
||
127 | * @return Token |
||
128 | */ |
||
129 | 2 | protected function readDoubleBracketString() { |
|
155 | |||
156 | /** |
||
157 | * @param string $end |
||
158 | * |
||
159 | * @return string |
||
160 | */ |
||
161 | 10 | protected function readEscaped($end) { |
|
184 | |||
185 | /** |
||
186 | * @return Token |
||
187 | */ |
||
188 | 6 | protected function readNumber() { |
|
204 | |||
205 | /** |
||
206 | * @return Token |
||
207 | */ |
||
208 | 9 | protected function readIdentifier() { |
|
224 | |||
225 | /** |
||
226 | * @return Token |
||
227 | */ |
||
228 | 8 | protected function readPunctuation() { |
|
231 | |||
232 | /** |
||
233 | * @param callable $predicate |
||
234 | * |
||
235 | * @return string |
||
236 | */ |
||
237 | 24 | protected function readWhile(callable $predicate) { |
|
244 | |||
245 | /** |
||
246 | * @param string $char |
||
247 | * |
||
248 | * @return bool |
||
249 | */ |
||
250 | 24 | protected function isWhitespace($char) { |
|
253 | |||
254 | /** |
||
255 | * @param string $char |
||
256 | * |
||
257 | * @return bool |
||
258 | */ |
||
259 | 17 | protected function isDigit($char) { |
|
262 | |||
263 | /** |
||
264 | * @return bool |
||
265 | */ |
||
266 | 19 | protected function isDoubleBracketString() { |
|
269 | |||
270 | /** |
||
271 | * @param string $char |
||
272 | * |
||
273 | * @return bool |
||
274 | */ |
||
275 | 13 | protected function isStartIdentifierCharacter($char) { |
|
278 | |||
279 | /** |
||
280 | * @param string $char |
||
281 | * |
||
282 | * @return bool |
||
283 | */ |
||
284 | 9 | protected function isIdentifierCharacter($char) { |
|
287 | |||
288 | /** |
||
289 | * @param string $char |
||
290 | * |
||
291 | * @return bool |
||
292 | */ |
||
293 | 9 | protected function isPunctuation($char) { |
|
296 | |||
297 | /** |
||
298 | * @param string $text |
||
299 | * |
||
300 | * @return bool |
||
301 | */ |
||
302 | 9 | protected function isKeyword($text) { |
|
305 | } |
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.