Complex classes like Tokenizer 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 Tokenizer, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 10 | class Tokenizer |
||
| 11 | { |
||
| 12 | protected $source; |
||
| 13 | protected $pos = 0; |
||
| 14 | protected $line = 1; |
||
| 15 | protected $lineStart = 0; |
||
| 16 | |||
| 17 | /** @var Token */ |
||
| 18 | protected $lookAhead; |
||
| 19 | |||
| 20 | 45 | public function setSource($source) |
|
| 25 | |||
| 26 | 45 | protected function next() |
|
| 41 | |||
| 42 | 45 | protected function skipWhitespace() |
|
| 43 | { |
||
| 44 | 45 | while ($this->pos < strlen($this->source)) { |
|
| 45 | 45 | $ch = $this->source[$this->pos]; |
|
| 46 | 45 | if ($ch === ' ' || $ch === "\t") { |
|
| 47 | 43 | $this->pos++; |
|
| 48 | 45 | } elseif($ch === '#') { |
|
| 49 | 1 | $this->pos++; |
|
| 50 | while ( |
||
| 51 | 1 | $this->pos < strlen($this->source) && |
|
| 52 | 1 | ($code = ord($this->source[$this->pos])) && |
|
| 53 | 1 | $code !== 10 && $code !== 13 && $code !== 0x2028 && $code !== 0x2029 |
|
| 54 | ) { |
||
| 55 | 1 | $this->pos++; |
|
| 56 | } |
||
| 57 | 45 | } elseif ($ch === "\r") { |
|
| 58 | $this->pos++; |
||
| 59 | if ($this->source[$this->pos] === "\n") { |
||
| 60 | $this->pos++; |
||
| 61 | } |
||
| 62 | $this->line++; |
||
| 63 | $this->lineStart = $this->pos; |
||
| 64 | 45 | } elseif ($ch === "\n") { |
|
| 65 | 18 | $this->pos++; |
|
| 66 | 18 | $this->line++; |
|
| 67 | 18 | $this->lineStart = $this->pos; |
|
| 68 | } else { |
||
| 69 | 45 | break; |
|
| 70 | } |
||
| 71 | } |
||
| 72 | 45 | } |
|
| 73 | |||
| 74 | /** |
||
| 75 | * @return Token |
||
| 76 | */ |
||
| 77 | 45 | protected function scan() |
|
| 157 | |||
| 158 | 8 | protected function checkFragment() |
|
| 176 | |||
| 177 | 44 | protected function scanWord() |
|
| 178 | { |
||
| 179 | 44 | $start = $this->pos; |
|
| 180 | 44 | $this->pos++; |
|
| 181 | |||
| 182 | 44 | while ($this->pos < strlen($this->source)) { |
|
| 183 | 44 | $ch = $this->source[$this->pos]; |
|
| 184 | |||
| 185 | 44 | if ($ch === '_' || $ch === '$' || 'a' <= $ch && $ch <= ('z') || 'A' <= $ch && $ch <= 'Z' || '0' <= $ch && $ch <= '9') { |
|
| 186 | 44 | $this->pos++; |
|
| 187 | } else { |
||
| 188 | 43 | break; |
|
| 189 | } |
||
| 190 | } |
||
| 191 | |||
| 192 | 44 | $value = substr($this->source, $start, $this->pos - $start); |
|
| 193 | |||
| 194 | 44 | return new Token($this->getKeyword($value), $value); |
|
| 195 | } |
||
| 196 | |||
| 197 | 44 | protected function getKeyword($name) |
|
| 198 | { |
||
| 199 | switch ($name) { |
||
| 200 | 44 | case 'null': |
|
| 201 | 2 | return Token::TYPE_NULL; |
|
| 202 | |||
| 203 | case 'true': |
||
| 204 | 2 | return Token::TYPE_TRUE; |
|
| 205 | |||
| 206 | case 'false': |
||
| 207 | 1 | return Token::TYPE_FALSE; |
|
| 208 | |||
| 209 | case 'query': |
||
| 210 | 11 | return Token::TYPE_QUERY; |
|
| 211 | |||
| 212 | case 'fragment': |
||
| 213 | 3 | return Token::TYPE_FRAGMENT; |
|
| 214 | |||
| 215 | case 'mutation': |
||
| 216 | 5 | return Token::TYPE_MUTATION; |
|
| 217 | |||
| 218 | 43 | case 'on': |
|
| 219 | 6 | return Token::TYPE_ON; |
|
| 220 | } |
||
| 221 | |||
| 222 | 43 | return Token::TYPE_IDENTIFIER; |
|
| 223 | } |
||
| 224 | |||
| 225 | 8 | protected function scanNumber() |
|
| 226 | { |
||
| 227 | 8 | $start = $this->pos; |
|
| 228 | |||
| 229 | |||
| 230 | 8 | $this->skipInteger(); |
|
| 231 | |||
| 232 | 8 | if ($this->source[$this->pos] === '.') { |
|
| 233 | 1 | $this->pos++; |
|
| 234 | 1 | $this->skipInteger(); |
|
| 235 | } |
||
| 236 | |||
| 237 | 8 | $value = substr($this->source, $start, $this->pos - $start); |
|
| 238 | |||
| 239 | 8 | if(strpos($value, '.') === false){ |
|
| 240 | 8 | $value = (int) $value; |
|
| 241 | } else { |
||
| 242 | 1 | $value = (float) $value; |
|
| 243 | } |
||
| 244 | |||
| 245 | 8 | return new Token(Token::TYPE_NUMBER, $value); |
|
| 246 | } |
||
| 247 | |||
| 248 | 8 | protected function skipInteger() |
|
| 249 | { |
||
| 250 | 8 | $start = $this->pos; |
|
| 251 | |||
| 252 | 8 | while ($this->pos < strlen($this->source)) { |
|
| 253 | 8 | $ch = $this->source[$this->pos]; |
|
| 254 | 8 | if ('0' <= $ch && $ch <= '9') { |
|
| 255 | 8 | $this->pos++; |
|
| 256 | } else { |
||
| 257 | 8 | break; |
|
| 258 | } |
||
| 259 | } |
||
| 260 | |||
| 261 | 8 | if ($this->pos - $start === 0) { |
|
| 262 | throw $this->createIllegal(); |
||
| 263 | } |
||
| 264 | 8 | } |
|
| 265 | |||
| 266 | protected function createIllegal() |
||
| 272 | |||
| 273 | 3 | protected function createError($message) |
|
| 277 | |||
| 278 | 3 | protected function getColumn() |
|
| 282 | |||
| 283 | 9 | protected function scanString() |
|
| 284 | { |
||
| 285 | 9 | $this->pos++; |
|
| 286 | |||
| 287 | 9 | $value = ''; |
|
| 288 | 9 | while ($this->pos < strlen($this->source)) { |
|
| 289 | 9 | $ch = $this->source[$this->pos]; |
|
| 290 | 9 | if ($ch === '"') { |
|
| 291 | 9 | $this->pos++; |
|
| 292 | |||
| 293 | 9 | return new Token(Token::TYPE_STRING, $value); |
|
| 294 | } |
||
| 295 | |||
| 296 | 9 | if ($ch === "\r" || $ch === "\n") { |
|
| 297 | break; |
||
| 298 | } |
||
| 299 | |||
| 300 | 9 | $value .= $ch; |
|
| 301 | 9 | $this->pos++; |
|
| 302 | } |
||
| 303 | |||
| 304 | throw $this->createIllegal(); |
||
| 305 | } |
||
| 306 | |||
| 307 | 45 | protected function end() |
|
| 311 | |||
| 312 | 45 | protected function peek() |
|
| 316 | |||
| 317 | 44 | protected function lex() |
|
| 324 | |||
| 325 | 7 | protected function createUnexpected(Token $token) |
|
| 326 | { |
||
| 327 | 7 | switch ($token->getType()) { |
|
| 328 | 7 | case Token::TYPE_END: |
|
| 329 | 1 | return $this->createError('Unexpected end of input'); |
|
| 330 | 6 | case Token::TYPE_NUMBER: |
|
| 340 | } |