Complex classes like Parser 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 Parser, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 28 | class Parser extends Tokenizer |
||
| 29 | { |
||
| 30 | |||
| 31 | /** @var array */ |
||
| 32 | protected $variablesTypes = []; |
||
| 33 | |||
| 34 | /** @var array */ |
||
| 35 | protected $variableTypeUsage = []; |
||
| 36 | |||
| 37 | 49 | public function parse($source = null) |
|
| 38 | { |
||
| 39 | 49 | if ($source) { |
|
| 40 | 19 | $this->setSource($source); |
|
| 41 | 19 | } |
|
| 42 | 49 | $data = ['queries' => [], 'mutations' => [], 'fragments' => []]; |
|
| 43 | |||
| 44 | 49 | while (!$this->end()) { |
|
| 45 | 49 | $tokenType = $this->peek()->getType(); |
|
| 46 | |||
| 47 | switch ($tokenType) { |
||
| 48 | 49 | case Token::TYPE_LBRACE: |
|
| 49 | 49 | case Token::TYPE_QUERY: |
|
| 50 | 43 | $data = array_merge($data, ['queries' => $this->parseBody()]); |
|
| 51 | 36 | break; |
|
| 52 | |||
| 53 | 9 | case Token::TYPE_MUTATION: |
|
| 54 | 5 | $data = array_merge($data, ['mutations' => $this->parseBody(Token::TYPE_MUTATION)]); |
|
| 55 | 3 | break; |
|
| 56 | |||
| 57 | 4 | case Token::TYPE_FRAGMENT: |
|
| 58 | 3 | $data['fragments'][] = $this->parseFragment(); |
|
| 59 | 3 | break; |
|
| 60 | |||
| 61 | 1 | default: |
|
| 62 | 1 | throw new SyntaxErrorException(); |
|
| 63 | 1 | } |
|
| 64 | 39 | } |
|
| 65 | |||
| 66 | 39 | $this->checkVariableUsage(); |
|
| 67 | |||
| 68 | 38 | return $data; |
|
| 69 | } |
||
| 70 | |||
| 71 | 48 | protected function parseBody($token = Token::TYPE_QUERY, $highLevel = true) |
|
| 72 | { |
||
| 73 | 48 | $fields = []; |
|
| 74 | 48 | $first = true; |
|
| 75 | |||
| 76 | 48 | if ($this->peek()->getType() == $token && $highLevel) { |
|
| 77 | 20 | $this->lex(); |
|
| 78 | 20 | $this->eat(Token::TYPE_IDENTIFIER); |
|
| 79 | |||
| 80 | 20 | if ($this->match(Token::TYPE_LPAREN)) { |
|
| 81 | 6 | $this->variablesTypes = $this->parseVariableTypes(); |
|
| 82 | 5 | } |
|
| 83 | 19 | } |
|
| 84 | |||
| 85 | 47 | $this->lex(); |
|
| 86 | |||
| 87 | 47 | while (!$this->match(Token::TYPE_RBRACE) && !$this->end()) { |
|
| 88 | 44 | if ($first) { |
|
| 89 | 44 | $first = false; |
|
| 90 | 44 | } else { |
|
| 91 | 18 | $this->eatMulti([Token::TYPE_COMMA]); |
|
| 92 | } |
||
| 93 | |||
| 94 | 44 | if ($this->match(Token::TYPE_FRAGMENT_REFERENCE)) { |
|
| 95 | 6 | $this->lex(); |
|
| 96 | |||
| 97 | 6 | if ($this->eat(Token::TYPE_ON)) { |
|
| 98 | 3 | $fields[] = $this->parseBodyItem(Token::TYPE_TYPED_FRAGMENT, $highLevel); |
|
| 99 | 3 | } else { |
|
| 100 | 3 | $fields[] = $this->parseFragmentReference(); |
|
| 101 | } |
||
| 102 | 6 | } else { |
|
| 103 | 44 | $fields[] = $this->parseBodyItem($token, $highLevel); |
|
| 104 | } |
||
| 105 | 38 | } |
|
| 106 | |||
| 107 | 39 | $this->expect(Token::TYPE_RBRACE); |
|
| 108 | |||
| 109 | 39 | return $fields; |
|
| 110 | } |
||
| 111 | |||
| 112 | 39 | protected function checkVariableUsage() |
|
| 118 | |||
| 119 | 6 | protected function parseVariableTypes() |
|
| 120 | { |
||
| 121 | 6 | $types = []; |
|
| 122 | 6 | $first = true; |
|
| 123 | |||
| 124 | 6 | $this->eat(Token::TYPE_LPAREN); |
|
| 125 | |||
| 126 | 6 | while (!$this->match(Token::TYPE_RPAREN) && !$this->end()) { |
|
| 127 | 6 | if ($first) { |
|
| 128 | 6 | $first = false; |
|
| 129 | 6 | } else { |
|
| 130 | 3 | $this->expect(Token::TYPE_COMMA); |
|
| 131 | } |
||
| 132 | |||
| 133 | 6 | $this->eat(Token::TYPE_VARIABLE); |
|
| 134 | 6 | $name = $this->parseIdentifier(); |
|
| 135 | 6 | $this->eat(Token::TYPE_COLON); |
|
| 136 | 6 | $type = $this->parseIdentifier(); |
|
| 137 | |||
| 138 | 6 | if (array_key_exists($name, $types)) { |
|
| 139 | 1 | throw new DuplicationVariableException(sprintf('"%s" variable duplication', $name)); |
|
| 140 | } |
||
| 141 | |||
| 142 | 6 | $types[$name] = $type; |
|
| 143 | 6 | } |
|
| 144 | |||
| 145 | 5 | $this->expect(Token::TYPE_RPAREN); |
|
| 146 | |||
| 147 | 5 | return $types; |
|
| 148 | } |
||
| 149 | |||
| 150 | 46 | protected function expect($type) |
|
| 158 | |||
| 159 | 45 | protected function expectMulti($types) |
|
| 167 | |||
| 168 | 6 | protected function parseReference() |
|
| 186 | |||
| 187 | 3 | protected function parseFragmentReference() |
|
| 193 | |||
| 194 | 45 | protected function parseIdentifier() |
|
| 203 | |||
| 204 | 44 | protected function parseBodyItem($type = Token::TYPE_QUERY, $highLevel = true) |
|
| 205 | { |
||
| 206 | 44 | $name = $this->parseIdentifier(); |
|
| 207 | 44 | $alias = null; |
|
| 208 | |||
| 209 | 44 | if ($this->eat(Token::TYPE_COLON)) { |
|
| 210 | 10 | $alias = $name; |
|
| 211 | 10 | $name = $this->parseIdentifier(); |
|
| 212 | 10 | } |
|
| 213 | |||
| 214 | 44 | $arguments = $this->match(Token::TYPE_LPAREN) ? $this->parseArgumentList() : []; |
|
| 215 | |||
| 216 | 39 | if ($this->match(Token::TYPE_LBRACE)) { |
|
| 217 | 37 | $fields = $this->parseBody($type == Token::TYPE_TYPED_FRAGMENT ? Token::TYPE_QUERY : $type, false); |
|
| 218 | |||
| 219 | 34 | if ($type == Token::TYPE_QUERY) { |
|
| 220 | 33 | return new Query($name, $alias, $arguments, $fields); |
|
| 221 | 4 | } elseif ($type == Token::TYPE_TYPED_FRAGMENT) { |
|
| 222 | 3 | return new TypedFragmentReference($name, $fields); |
|
| 223 | } else { |
||
| 224 | 1 | return new Mutation($name, $alias, $arguments, $fields); |
|
| 225 | } |
||
| 226 | } else { |
||
| 227 | 38 | if ($highLevel && $type == Token::TYPE_MUTATION) { |
|
| 228 | 2 | return new Mutation($name, $alias, $arguments); |
|
| 229 | } |
||
| 230 | |||
| 231 | 36 | return new Field($name, $alias, $arguments); |
|
| 232 | } |
||
| 233 | } |
||
| 234 | |||
| 235 | 23 | protected function parseArgumentList() |
|
| 236 | { |
||
| 237 | 23 | $args = []; |
|
| 238 | 23 | $first = true; |
|
| 239 | |||
| 240 | 23 | $this->expect(Token::TYPE_LPAREN); |
|
| 241 | |||
| 242 | 23 | while (!$this->match(Token::TYPE_RPAREN) && !$this->end()) { |
|
| 243 | 23 | if ($first) { |
|
| 244 | 23 | $first = false; |
|
| 245 | 23 | } else { |
|
| 246 | 4 | $this->expect(Token::TYPE_COMMA); |
|
| 247 | } |
||
| 248 | |||
| 249 | 23 | $args[] = $this->parseArgument(); |
|
| 250 | 19 | } |
|
| 251 | |||
| 252 | 18 | $this->expect(Token::TYPE_RPAREN); |
|
| 253 | |||
| 254 | 18 | return $args; |
|
| 255 | } |
||
| 256 | |||
| 257 | 23 | protected function parseArgument() |
|
| 265 | |||
| 266 | 23 | protected function parseValue() |
|
| 267 | { |
||
| 268 | 23 | switch ($this->lookAhead->getType()) { |
|
| 269 | 23 | case Token::TYPE_LSQUARE_BRACE: |
|
| 270 | 3 | return $this->parseList(); |
|
| 271 | |||
| 272 | 20 | case Token::TYPE_LBRACE: |
|
| 273 | 2 | return $this->parseObject(); |
|
| 274 | |||
| 275 | 18 | case Token::TYPE_AMP: |
|
| 276 | 18 | case Token::TYPE_VARIABLE: |
|
| 277 | 6 | return $this->parseReference(); |
|
| 278 | |||
| 279 | 14 | case Token::TYPE_NUMBER: |
|
| 280 | 14 | case Token::TYPE_STRING: |
|
| 281 | 14 | case Token::TYPE_IDENTIFIER: |
|
| 282 | 13 | return new Literal($this->lex()->getData()); |
|
| 283 | |||
| 284 | 3 | case Token::TYPE_NULL: |
|
| 285 | 3 | case Token::TYPE_TRUE: |
|
| 286 | 3 | case Token::TYPE_FALSE: |
|
| 287 | 1 | return new Literal($this->lex()->getData()); |
|
| 288 | 2 | } |
|
| 289 | |||
| 290 | 2 | throw $this->createUnexpected($this->lookAhead); |
|
| 291 | } |
||
| 292 | |||
| 293 | 4 | protected function parseList($createType = true) |
|
| 294 | { |
||
| 295 | 4 | $this->eat(Token::TYPE_LSQUARE_BRACE); |
|
| 296 | |||
| 297 | 4 | $list = []; |
|
| 298 | 4 | while (!$this->match(Token::TYPE_RSQUARE_BRACE) && !$this->end()) { |
|
| 299 | 4 | $list[] = $this->parseListValue(); |
|
| 300 | |||
| 301 | 3 | if ($this->lookAhead->getType() != Token::TYPE_RSQUARE_BRACE) { |
|
| 302 | 3 | $this->expect(Token::TYPE_COMMA); |
|
| 303 | 3 | } |
|
| 304 | 3 | } |
|
| 305 | |||
| 306 | 3 | $this->expect(Token::TYPE_RSQUARE_BRACE); |
|
| 307 | |||
| 308 | 3 | return $createType ? new InputList($list) : $list; |
|
| 309 | } |
||
| 310 | |||
| 311 | 5 | protected function parseListValue() |
|
| 312 | { |
||
| 313 | 5 | switch ($this->lookAhead->getType()) { |
|
| 314 | 5 | case Token::TYPE_NUMBER: |
|
| 315 | 4 | return $this->expect(Token::TYPE_NUMBER)->getData(); |
|
| 316 | |||
| 317 | 4 | case Token::TYPE_STRING: |
|
| 318 | 2 | return $this->expect(Token::TYPE_STRING)->getData(); |
|
| 319 | |||
| 320 | 4 | case Token::TYPE_LBRACE: |
|
| 321 | 1 | return $this->parseObject(false); |
|
| 322 | |||
| 323 | 4 | case Token::TYPE_LSQUARE_BRACE: |
|
| 324 | 1 | return $this->parseList(false); |
|
| 325 | |||
| 326 | 3 | case Token::TYPE_TRUE: |
|
| 327 | 1 | return $this->expect(Token::TYPE_TRUE)->getData(); |
|
| 328 | |||
| 329 | 3 | case Token::TYPE_FALSE: |
|
| 330 | 1 | return $this->expect(Token::TYPE_FALSE)->getData(); |
|
| 331 | |||
| 332 | 4 | case Token::TYPE_NULL: |
|
| 333 | 2 | return $this->expect(Token::TYPE_NULL)->getData(); |
|
| 334 | |||
| 335 | 1 | case Token::TYPE_IDENTIFIER: |
|
| 336 | return $this->expect(Token::TYPE_IDENTIFIER)->getData(); |
||
| 337 | 1 | } |
|
| 338 | |||
| 339 | 1 | throw new SyntaxErrorException('Can\'t parse argument'); |
|
| 340 | } |
||
| 341 | |||
| 342 | 2 | protected function parseObject($createType = true) |
|
| 343 | { |
||
| 344 | 2 | $this->eat(Token::TYPE_LBRACE); |
|
| 345 | |||
| 346 | 2 | $object = []; |
|
| 347 | 2 | while (!$this->match(Token::TYPE_RBRACE) && !$this->end()) { |
|
| 348 | 2 | $key = $this->expectMulti([Token::TYPE_STRING, Token::TYPE_IDENTIFIER])->getData(); |
|
| 349 | 2 | $this->expect(Token::TYPE_COLON); |
|
| 350 | 2 | $value = $this->parseListValue(); |
|
| 351 | |||
| 352 | 2 | if ($this->peek()->getType() != Token::TYPE_RBRACE) { |
|
| 353 | 2 | $this->expect(Token::TYPE_COMMA); |
|
| 354 | 2 | } |
|
| 355 | |||
| 356 | 2 | $object[$key] = $value; |
|
| 357 | 2 | } |
|
| 358 | |||
| 359 | 1 | $this->eat(Token::TYPE_RBRACE); |
|
| 360 | |||
| 361 | 1 | return $createType ? new InputObject($object) : $object; |
|
| 362 | } |
||
| 363 | |||
| 364 | 3 | protected function parseFragment() |
|
| 376 | |||
| 377 | 47 | protected function eat($type) |
|
| 385 | |||
| 386 | 18 | protected function eatMulti($types) |
|
| 394 | |||
| 395 | 45 | protected function matchMulti($types) |
|
| 396 | { |
||
| 397 | 45 | foreach ($types as $type) { |
|
| 398 | 45 | if ($this->peek()->getType() == $type) { |
|
| 399 | 45 | return true; |
|
| 400 | } |
||
| 401 | 11 | } |
|
| 402 | |||
| 403 | 4 | return false; |
|
| 404 | } |
||
| 405 | |||
| 406 | 48 | protected function match($type) |
|
| 410 | } |
||
| 411 |
This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.
Consider making the comparison explicit by using
empty(..)or! empty(...)instead.