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 | 27 | public function __construct(InputStream $input) { |
|
| 28 | |||
| 29 | /** |
||
| 30 | * @return Token |
||
| 31 | */ |
||
| 32 | 27 | public function next() { |
|
| 33 | 27 | $token = $this->current; |
|
| 34 | 27 | $this->current = null; |
|
| 35 | 27 | if ($token) { |
|
| 36 | 19 | return $token; |
|
| 37 | } |
||
| 38 | 8 | return $this->readNext(); |
|
| 39 | } |
||
| 40 | |||
| 41 | /** |
||
| 42 | * @return bool |
||
| 43 | */ |
||
| 44 | 9 | public function eof() { |
|
| 47 | |||
| 48 | /** |
||
| 49 | * @return Token |
||
|
|
|||
| 50 | */ |
||
| 51 | 19 | 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 | 27 | protected function readNext() { |
|
| 73 | 27 | $this->readWhile([$this, 'isWhitespace']); |
|
| 74 | 27 | if ($this->input->eof()) { |
|
| 75 | return null; |
||
| 76 | } |
||
| 77 | 27 | $char = $this->input->peek(); |
|
| 78 | 27 | if ($this->isComment()) { |
|
| 79 | 3 | $this->skipComment(); |
|
| 80 | 3 | return $this->readNext(); |
|
| 81 | } |
||
| 82 | 27 | if ($char == '"') { |
|
| 83 | 10 | return $this->readDoubleQuotedString(); |
|
| 84 | } |
||
| 85 | 23 | if ($char == '\'') { |
|
| 86 | 1 | return $this->readSingleQuotedString(); |
|
| 87 | } |
||
| 88 | 22 | if ($this->isDoubleBracketString()) { |
|
| 89 | 2 | return $this->readDoubleBracketString(); |
|
| 90 | } |
||
| 91 | 20 | if ($this->isDigit($char)) { |
|
| 92 | 8 | return $this->readNumber(); |
|
| 93 | } |
||
| 94 | 16 | if ($this->isStartIdentifierCharacter($char)) { |
|
| 95 | 12 | return $this->readIdentifier(); |
|
| 96 | } |
||
| 97 | 12 | if ($this->isPunctuation($char)) { |
|
| 98 | 11 | return $this->readPunctuation(); |
|
| 99 | } |
||
| 100 | 1 | $this->input->error('Cannot handle character: ' . $char . ' (ord: ' . ord($char) . ')'); |
|
| 101 | } |
||
| 102 | |||
| 103 | 3 | protected function skipComment() { |
|
| 104 | 3 | $this->readWhile( |
|
| 105 | function ($char) { |
||
| 106 | 3 | return $char != "\n"; |
|
| 107 | } |
||
| 108 | 3 | ); |
|
| 109 | 3 | $this->input->next(); |
|
| 110 | 3 | } |
|
| 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 | 2 | protected function readDoubleBracketString() { |
|
| 155 | |||
| 156 | /** |
||
| 157 | * @param string $end |
||
| 158 | * |
||
| 159 | * @return string |
||
| 160 | */ |
||
| 161 | 11 | protected function readEscaped($end) { |
|
| 184 | |||
| 185 | /** |
||
| 186 | * @return Token |
||
| 187 | */ |
||
| 188 | 8 | protected function readNumber() { |
|
| 204 | |||
| 205 | /** |
||
| 206 | * @return Token |
||
| 207 | */ |
||
| 208 | 12 | protected function readIdentifier() { |
|
| 224 | |||
| 225 | /** |
||
| 226 | * @return Token |
||
| 227 | */ |
||
| 228 | 11 | protected function readPunctuation() { |
|
| 231 | |||
| 232 | /** |
||
| 233 | * @param callable $predicate |
||
| 234 | * |
||
| 235 | * @return string |
||
| 236 | */ |
||
| 237 | 27 | protected function readWhile(callable $predicate) { |
|
| 244 | |||
| 245 | /** |
||
| 246 | * @param string $char |
||
| 247 | * |
||
| 248 | * @return bool |
||
| 249 | */ |
||
| 250 | 27 | protected function isWhitespace($char) { |
|
| 253 | |||
| 254 | /** |
||
| 255 | * @param string $char |
||
| 256 | * |
||
| 257 | * @return bool |
||
| 258 | */ |
||
| 259 | 20 | protected function isDigit($char) { |
|
| 262 | |||
| 263 | /** |
||
| 264 | * @return bool |
||
| 265 | */ |
||
| 266 | 22 | protected function isDoubleBracketString() { |
|
| 269 | |||
| 270 | /** |
||
| 271 | * @return bool |
||
| 272 | */ |
||
| 273 | 27 | protected function isComment() { |
|
| 276 | |||
| 277 | /** |
||
| 278 | * @param string $char |
||
| 279 | * |
||
| 280 | * @return bool |
||
| 281 | */ |
||
| 282 | 16 | protected function isStartIdentifierCharacter($char) { |
|
| 285 | |||
| 286 | /** |
||
| 287 | * @param string $char |
||
| 288 | * |
||
| 289 | * @return bool |
||
| 290 | */ |
||
| 291 | 12 | protected function isIdentifierCharacter($char) { |
|
| 294 | |||
| 295 | /** |
||
| 296 | * @param string $char |
||
| 297 | * |
||
| 298 | * @return bool |
||
| 299 | */ |
||
| 300 | 12 | protected function isPunctuation($char) { |
|
| 303 | |||
| 304 | /** |
||
| 305 | * @param string $text |
||
| 306 | * |
||
| 307 | * @return bool |
||
| 308 | */ |
||
| 309 | 12 | protected function isKeyword($text) { |
|
| 312 | } |
This check compares the return type specified in the
@returnannotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.