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 | 21 | public function __construct(InputStream $input) { |
|
| 28 | |||
| 29 | /** |
||
| 30 | * @return Token |
||
| 31 | */ |
||
| 32 | 21 | public function next() { |
|
| 40 | |||
| 41 | /** |
||
| 42 | * @return bool |
||
| 43 | */ |
||
| 44 | 6 | public function eof() { |
|
| 47 | |||
| 48 | /** |
||
| 49 | * @return Token |
||
|
|
|||
| 50 | */ |
||
| 51 | 13 | 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 | 21 | protected function readNext() { |
|
| 99 | |||
| 100 | protected function skipComment() { |
||
| 108 | |||
| 109 | /** |
||
| 110 | * @return Token |
||
| 111 | */ |
||
| 112 | 8 | protected function readDoubleQuotedString() { |
|
| 115 | |||
| 116 | /** |
||
| 117 | * @return Token |
||
| 118 | */ |
||
| 119 | 1 | protected function readSingleQuotedString() { |
|
| 122 | |||
| 123 | /** |
||
| 124 | * @param string $end |
||
| 125 | * |
||
| 126 | * @return string |
||
| 127 | */ |
||
| 128 | 9 | protected function readEscaped($end) { |
|
| 151 | |||
| 152 | /** |
||
| 153 | * @return Token |
||
| 154 | */ |
||
| 155 | 6 | protected function readNumber() { |
|
| 171 | |||
| 172 | /** |
||
| 173 | * @return Token |
||
| 174 | */ |
||
| 175 | 9 | protected function readIdentifier() { |
|
| 191 | |||
| 192 | /** |
||
| 193 | * @return Token |
||
| 194 | */ |
||
| 195 | 8 | protected function readPunctuation() { |
|
| 198 | |||
| 199 | /** |
||
| 200 | * @param callable $predicate |
||
| 201 | * |
||
| 202 | * @return string |
||
| 203 | */ |
||
| 204 | 21 | protected function readWhile(callable $predicate) { |
|
| 211 | |||
| 212 | 21 | protected function isWhitespace($char) { |
|
| 215 | |||
| 216 | /** |
||
| 217 | * @param string $char |
||
| 218 | * |
||
| 219 | * @return bool |
||
| 220 | */ |
||
| 221 | 17 | protected function isDigit($char) { |
|
| 224 | |||
| 225 | /** |
||
| 226 | * @param string $char |
||
| 227 | * |
||
| 228 | * @return bool |
||
| 229 | */ |
||
| 230 | 13 | protected function isStartIdentifierCharacter($char) { |
|
| 233 | |||
| 234 | /** |
||
| 235 | * @param string $char |
||
| 236 | * |
||
| 237 | * @return bool |
||
| 238 | */ |
||
| 239 | 9 | protected function isIdentifierCharacter($char) { |
|
| 242 | |||
| 243 | /** |
||
| 244 | * @param string $char |
||
| 245 | * |
||
| 246 | * @return bool |
||
| 247 | */ |
||
| 248 | 9 | protected function isPunctuation($char) { |
|
| 251 | |||
| 252 | /** |
||
| 253 | * @param string $text |
||
| 254 | * |
||
| 255 | * @return bool |
||
| 256 | */ |
||
| 257 | 9 | protected function isKeyword($text) { |
|
| 260 | } |
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.