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 | 39 | public function __construct(InputStream $input) { |
|
| 28 | |||
| 29 | /** |
||
| 30 | * @return Token |
||
| 31 | */ |
||
| 32 | 39 | public function next() { |
|
| 40 | |||
| 41 | /** |
||
| 42 | * @return bool |
||
| 43 | */ |
||
| 44 | 22 | public function eof() { |
|
| 47 | |||
| 48 | /** |
||
| 49 | * @return Token |
||
|
|
|||
| 50 | */ |
||
| 51 | 23 | 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 | 39 | protected function readNext() { |
|
| 102 | |||
| 103 | 5 | protected function skipComment() { |
|
| 111 | |||
| 112 | /** |
||
| 113 | * @return Token |
||
| 114 | */ |
||
| 115 | 13 | protected function readDoubleQuotedString() { |
|
| 118 | |||
| 119 | /** |
||
| 120 | * @return Token |
||
| 121 | */ |
||
| 122 | 1 | protected function readSingleQuotedString() { |
|
| 125 | |||
| 126 | /** |
||
| 127 | * @return Token |
||
| 128 | */ |
||
| 129 | 12 | protected function readDoubleBracketString() { |
|
| 180 | |||
| 181 | /** |
||
| 182 | * @param string $end |
||
| 183 | * |
||
| 184 | * @return string |
||
| 185 | */ |
||
| 186 | 14 | protected function readEscaped($end) { |
|
| 225 | |||
| 226 | /** |
||
| 227 | * @return Token |
||
| 228 | */ |
||
| 229 | 10 | protected function readNumber() { |
|
| 245 | |||
| 246 | /** |
||
| 247 | * @return Token |
||
| 248 | */ |
||
| 249 | 16 | protected function readIdentifier() { |
|
| 265 | |||
| 266 | /** |
||
| 267 | * @return Token |
||
| 268 | */ |
||
| 269 | 14 | protected function readPunctuation() { |
|
| 272 | |||
| 273 | /** |
||
| 274 | * @param callable $predicate |
||
| 275 | * |
||
| 276 | * @return string |
||
| 277 | */ |
||
| 278 | 39 | protected function readWhile(callable $predicate) { |
|
| 285 | |||
| 286 | /** |
||
| 287 | * @param string $char |
||
| 288 | * |
||
| 289 | * @return bool |
||
| 290 | */ |
||
| 291 | 39 | protected function isWhitespace($char) { |
|
| 294 | |||
| 295 | /** |
||
| 296 | * @param string $char |
||
| 297 | * |
||
| 298 | * @return bool |
||
| 299 | */ |
||
| 300 | 24 | protected function isDigit($char) { |
|
| 303 | |||
| 304 | /** |
||
| 305 | * @return bool |
||
| 306 | */ |
||
| 307 | 33 | protected function isDoubleBracketString() { |
|
| 310 | |||
| 311 | /** |
||
| 312 | * @return bool |
||
| 313 | */ |
||
| 314 | 39 | protected function isComment() { |
|
| 317 | |||
| 318 | /** |
||
| 319 | * @param string $char |
||
| 320 | * |
||
| 321 | * @return bool |
||
| 322 | */ |
||
| 323 | 20 | protected function isStartIdentifierCharacter($char) { |
|
| 326 | |||
| 327 | /** |
||
| 328 | * @param string $char |
||
| 329 | * |
||
| 330 | * @return bool |
||
| 331 | */ |
||
| 332 | 16 | protected function isIdentifierCharacter($char) { |
|
| 335 | |||
| 336 | /** |
||
| 337 | * @param string $char |
||
| 338 | * |
||
| 339 | * @return bool |
||
| 340 | */ |
||
| 341 | 15 | protected function isPunctuation($char) { |
|
| 344 | |||
| 345 | /** |
||
| 346 | * @param string $text |
||
| 347 | * |
||
| 348 | * @return bool |
||
| 349 | */ |
||
| 350 | 16 | protected function isKeyword($text) { |
|
| 353 | } |
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.