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 | 37 | public function setSource($source) |
|
| 25 | |||
| 26 | 37 | protected function next() |
|
| 41 | |||
| 42 | 37 | protected function skipWhitespace() |
|
| 43 | { |
||
| 44 | 37 | while ($this->pos < strlen($this->source)) { |
|
| 45 | 37 | $ch = $this->source[$this->pos]; |
|
| 46 | 37 | if ($ch === ' ' || $ch === "\t") { |
|
| 47 | 35 | $this->pos++; |
|
| 48 | 37 | } elseif ($ch === "\r") { |
|
| 49 | $this->pos++; |
||
| 50 | if ($this->source[$this->pos] === "\n") { |
||
| 51 | $this->pos++; |
||
| 52 | } |
||
| 53 | $this->line++; |
||
| 54 | $this->lineStart = $this->pos; |
||
| 55 | 37 | } elseif ($ch === "\n") { |
|
| 56 | 10 | $this->pos++; |
|
| 57 | 10 | $this->line++; |
|
| 58 | 10 | $this->lineStart = $this->pos; |
|
| 59 | } else { |
||
| 60 | 37 | break; |
|
| 61 | } |
||
| 62 | } |
||
| 63 | 37 | } |
|
| 64 | |||
| 65 | /** |
||
| 66 | * @return Token |
||
| 67 | */ |
||
| 68 | 37 | protected function scan() |
|
| 148 | |||
| 149 | 3 | protected function checkFragment() |
|
| 167 | |||
| 168 | 36 | protected function scanWord() |
|
| 169 | { |
||
| 170 | 36 | $start = $this->pos; |
|
| 171 | 36 | $this->pos++; |
|
| 172 | |||
| 173 | 36 | while ($this->pos < strlen($this->source)) { |
|
| 174 | 36 | $ch = $this->source[$this->pos]; |
|
| 175 | |||
| 176 | 36 | if ($ch === '_' || $ch === '$' || 'a' <= $ch && $ch <= ('z') || 'A' <= $ch && $ch <= 'Z' || '0' <= $ch && $ch <= '9') { |
|
| 177 | 36 | $this->pos++; |
|
| 178 | } else { |
||
| 179 | 35 | break; |
|
| 180 | } |
||
| 181 | } |
||
| 182 | |||
| 183 | 36 | $value = substr($this->source, $start, $this->pos - $start); |
|
| 184 | |||
| 185 | 36 | return new Token($this->getKeyword($value), $value); |
|
| 186 | } |
||
| 187 | |||
| 188 | 36 | protected function getKeyword($name) |
|
| 189 | { |
||
| 190 | switch ($name) { |
||
| 191 | 36 | case 'null': |
|
| 192 | 2 | return Token::TYPE_NULL; |
|
| 193 | |||
| 194 | case 'true': |
||
| 195 | 2 | return Token::TYPE_TRUE; |
|
| 196 | |||
| 197 | case 'false': |
||
| 198 | 1 | return Token::TYPE_FALSE; |
|
| 199 | |||
| 200 | case 'query': |
||
| 201 | 8 | return Token::TYPE_QUERY; |
|
| 202 | |||
| 203 | case 'fragment': |
||
| 204 | 1 | return Token::TYPE_FRAGMENT; |
|
| 205 | |||
| 206 | case 'mutation': |
||
| 207 | 5 | return Token::TYPE_MUTATION; |
|
| 208 | |||
| 209 | 35 | case 'on': |
|
| 210 | 1 | return Token::TYPE_ON; |
|
| 211 | } |
||
| 212 | |||
| 213 | 35 | return Token::TYPE_IDENTIFIER; |
|
| 214 | } |
||
| 215 | |||
| 216 | 8 | protected function scanNumber() |
|
| 217 | { |
||
| 218 | 8 | $start = $this->pos; |
|
| 219 | |||
| 220 | |||
| 221 | 8 | $this->skipInteger(); |
|
| 222 | |||
| 223 | 8 | if ($this->source[$this->pos] === '.') { |
|
| 224 | 1 | $this->pos++; |
|
| 225 | 1 | $this->skipInteger(); |
|
| 226 | } |
||
| 227 | |||
| 228 | 8 | $value = substr($this->source, $start, $this->pos - $start); |
|
| 229 | |||
| 230 | 8 | if(strpos($value, '.') === false){ |
|
| 231 | 8 | $value = (int) $value; |
|
| 232 | } else { |
||
| 233 | 1 | $value = (float) $value; |
|
| 234 | } |
||
| 235 | |||
| 236 | 8 | return new Token(Token::TYPE_NUMBER, $value); |
|
| 237 | } |
||
| 238 | |||
| 239 | 8 | protected function skipInteger() |
|
| 240 | { |
||
| 241 | 8 | $start = $this->pos; |
|
| 242 | |||
| 243 | 8 | while ($this->pos < strlen($this->source)) { |
|
| 244 | 8 | $ch = $this->source[$this->pos]; |
|
| 245 | 8 | if ('0' <= $ch && $ch <= '9') { |
|
| 246 | 8 | $this->pos++; |
|
| 247 | } else { |
||
| 248 | 8 | break; |
|
| 249 | } |
||
| 250 | } |
||
| 251 | |||
| 252 | 8 | if ($this->pos - $start === 0) { |
|
| 253 | throw $this->createIllegal(); |
||
| 254 | } |
||
| 255 | 8 | } |
|
| 256 | |||
| 257 | protected function createIllegal() |
||
| 263 | |||
| 264 | 3 | protected function createError($message) |
|
| 268 | |||
| 269 | 3 | protected function getColumn() |
|
| 273 | |||
| 274 | 8 | protected function scanString() |
|
| 275 | { |
||
| 276 | 8 | $this->pos++; |
|
| 277 | |||
| 278 | 8 | $value = ''; |
|
| 279 | 8 | while ($this->pos < strlen($this->source)) { |
|
| 280 | 8 | $ch = $this->source[$this->pos]; |
|
| 281 | 8 | if ($ch === '"') { |
|
| 282 | 8 | $this->pos++; |
|
| 283 | |||
| 284 | 8 | return new Token(Token::TYPE_STRING, $value); |
|
| 285 | } |
||
| 286 | |||
| 287 | 8 | if ($ch === "\r" || $ch === "\n") { |
|
| 288 | break; |
||
| 289 | } |
||
| 290 | |||
| 291 | 8 | $value .= $ch; |
|
| 292 | 8 | $this->pos++; |
|
| 293 | } |
||
| 294 | |||
| 295 | throw $this->createIllegal(); |
||
| 296 | } |
||
| 297 | |||
| 298 | 37 | protected function end() |
|
| 302 | |||
| 303 | 37 | protected function peek() |
|
| 307 | |||
| 308 | 36 | protected function lex() |
|
| 315 | |||
| 316 | 7 | protected function createUnexpected(Token $token) |
|
| 317 | { |
||
| 318 | 7 | switch ($token->getType()) { |
|
| 319 | 7 | case Token::TYPE_END: |
|
| 320 | 1 | return $this->createError('Unexpected end of input'); |
|
| 321 | 6 | case Token::TYPE_NUMBER: |
|
| 331 | } |