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 |
||
| 24 | class Parser extends Tokenizer |
||
| 25 | { |
||
| 26 | |||
| 27 | 45 | public function parse() |
|
| 28 | { |
||
| 29 | 45 | $data = ['queries' => [], 'mutations' => [], 'fragments' => []]; |
|
| 30 | |||
| 31 | 45 | while (!$this->end()) { |
|
| 32 | 45 | $tokenType = $this->peek()->getType(); |
|
| 33 | |||
| 34 | switch ($tokenType) { |
||
| 35 | 45 | case Token::TYPE_LBRACE: |
|
| 36 | 16 | case Token::TYPE_QUERY: |
|
| 37 | 39 | $data = array_merge($data, ['queries' => $this->parseBody()]); |
|
| 38 | 33 | break; |
|
| 39 | |||
| 40 | 9 | case Token::TYPE_MUTATION: |
|
| 41 | 5 | $data = array_merge($data, ['mutations' => $this->parseBody(Token::TYPE_MUTATION)]); |
|
| 42 | 3 | break; |
|
| 43 | |||
| 44 | 4 | case Token::TYPE_FRAGMENT: |
|
| 45 | 3 | $data['fragments'][] = $this->parseFragment(); |
|
| 46 | 3 | break; |
|
| 47 | |||
| 48 | default: |
||
| 49 | 1 | throw new SyntaxErrorException(); |
|
| 50 | } |
||
| 51 | } |
||
| 52 | |||
| 53 | 36 | return $data; |
|
| 54 | } |
||
| 55 | |||
| 56 | 44 | protected function parseBody($token = Token::TYPE_QUERY, $highLevel = true) |
|
| 57 | { |
||
| 58 | 44 | $fields = []; |
|
| 59 | 44 | $first = true; |
|
| 60 | |||
| 61 | 44 | if ($this->peek()->getType() == $token) { |
|
| 62 | 14 | $this->lex(); |
|
| 63 | 14 | $this->eat(Token::TYPE_IDENTIFIER); |
|
| 64 | } |
||
| 65 | |||
| 66 | 44 | $this->lex(); |
|
| 67 | |||
| 68 | 44 | while (!$this->match(Token::TYPE_RBRACE) && !$this->end()) { |
|
| 69 | 41 | if ($first) { |
|
| 70 | 41 | $first = false; |
|
| 71 | } else { |
||
| 72 | 18 | $this->eatMulti([Token::TYPE_COMMA]); |
|
| 73 | } |
||
| 74 | |||
| 75 | 41 | if ($this->match(Token::TYPE_FRAGMENT_REFERENCE)) { |
|
| 76 | 6 | $this->lex(); |
|
| 77 | |||
| 78 | 6 | if ($this->eat(Token::TYPE_ON)) { |
|
| 79 | 3 | $fields[] = $this->parseBodyItem(Token::TYPE_TYPED_FRAGMENT, $highLevel); |
|
| 80 | } else { |
||
| 81 | 6 | $fields[] = $this->parseFragmentReference(); |
|
| 82 | } |
||
| 83 | } else { |
||
| 84 | 41 | $fields[] = $this->parseBodyItem($token, $highLevel); |
|
| 85 | } |
||
| 86 | } |
||
| 87 | |||
| 88 | 37 | $this->expect(Token::TYPE_RBRACE); |
|
| 89 | |||
| 90 | 37 | return $fields; |
|
| 91 | } |
||
| 92 | |||
| 93 | 42 | protected function expect($type) |
|
| 101 | |||
| 102 | 41 | protected function expectMulti($types) |
|
| 110 | |||
| 111 | 4 | protected function parseReference() |
|
| 121 | |||
| 122 | 3 | protected function parseFragmentReference() |
|
| 128 | |||
| 129 | 41 | protected function parseIdentifier() |
|
| 138 | |||
| 139 | 41 | protected function parseBodyItem($type = Token::TYPE_QUERY, $highLevel = true) |
|
| 140 | { |
||
| 141 | 41 | $name = $this->parseIdentifier(); |
|
| 142 | 41 | $alias = null; |
|
| 143 | |||
| 144 | 41 | if ($this->eat(Token::TYPE_COLON)) { |
|
| 145 | 10 | $alias = $name; |
|
| 146 | 10 | $name = $this->parseIdentifier(); |
|
| 147 | } |
||
| 148 | |||
| 149 | 41 | $arguments = $this->match(Token::TYPE_LPAREN) ? $this->parseArgumentList() : []; |
|
| 150 | |||
| 151 | 36 | if ($this->match(Token::TYPE_LBRACE)) { |
|
| 152 | 34 | $fields = $this->parseBody($type, false); |
|
| 153 | |||
| 154 | 32 | if ($type == Token::TYPE_QUERY) { |
|
| 155 | 31 | return new Query($name, $alias, $arguments, $fields); |
|
| 156 | 4 | } elseif ($type == Token::TYPE_TYPED_FRAGMENT) { |
|
| 157 | 3 | return new TypedFragmentReference($name, $fields); |
|
| 158 | } else { |
||
| 159 | 1 | return new Mutation($name, $alias, $arguments, $fields); |
|
| 160 | } |
||
| 161 | } else { |
||
| 162 | 36 | if ($highLevel && $type == Token::TYPE_MUTATION) { |
|
| 163 | 2 | return new Mutation($name, $alias, $arguments); |
|
| 164 | } |
||
| 165 | |||
| 166 | 34 | return new Field($name, $alias); |
|
| 167 | } |
||
| 168 | } |
||
| 169 | |||
| 170 | 20 | protected function parseArgumentList() |
|
| 171 | { |
||
| 172 | 20 | $args = []; |
|
| 173 | 20 | $first = true; |
|
| 174 | |||
| 175 | 20 | $this->expect(Token::TYPE_LPAREN); |
|
| 176 | |||
| 177 | 20 | while (!$this->match(Token::TYPE_RPAREN) && !$this->end()) { |
|
| 178 | 20 | if ($first) { |
|
| 179 | 20 | $first = false; |
|
| 180 | } else { |
||
| 181 | 3 | $this->expect(Token::TYPE_COMMA); |
|
| 182 | } |
||
| 183 | |||
| 184 | 20 | $args[] = $this->parseArgument(); |
|
| 185 | } |
||
| 186 | |||
| 187 | 15 | $this->expect(Token::TYPE_RPAREN); |
|
| 188 | |||
| 189 | 15 | return $args; |
|
| 190 | } |
||
| 191 | |||
| 192 | 20 | protected function parseArgument() |
|
| 200 | |||
| 201 | 20 | protected function parseValue() |
|
| 202 | { |
||
| 203 | 20 | switch ($this->lookAhead->getType()) { |
|
| 204 | 20 | case Token::TYPE_LSQUARE_BRACE: |
|
| 230 | |||
| 231 | 4 | protected function parseList($createType = true) |
|
| 248 | |||
| 249 | 6 | protected function parseListValue() |
|
| 250 | { |
||
| 251 | 6 | switch ($this->lookAhead->getType()) { |
|
| 252 | 6 | case Token::TYPE_NUMBER: |
|
| 253 | 5 | return $this->expect(Token::TYPE_NUMBER)->getData(); |
|
| 254 | |||
| 255 | 4 | case Token::TYPE_STRING: |
|
| 256 | 2 | return $this->expect(Token::TYPE_STRING)->getData(); |
|
| 257 | |||
| 258 | 4 | case Token::TYPE_LBRACE: |
|
| 259 | 1 | return $this->parseObject(false); |
|
| 260 | |||
| 261 | 4 | case Token::TYPE_LSQUARE_BRACE: |
|
| 262 | 1 | return $this->parseList(false); |
|
| 263 | |||
| 264 | 3 | case Token::TYPE_TRUE: |
|
| 265 | 1 | return $this->expect(Token::TYPE_TRUE)->getData(); |
|
| 266 | |||
| 267 | 3 | case Token::TYPE_FALSE: |
|
| 268 | 1 | return $this->expect(Token::TYPE_FALSE)->getData(); |
|
| 269 | |||
| 270 | 3 | case Token::TYPE_NULL: |
|
| 271 | 2 | return $this->expect(Token::TYPE_NULL)->getData(); |
|
| 272 | |||
| 273 | 1 | case Token::TYPE_IDENTIFIER: |
|
| 274 | return $this->expect(Token::TYPE_IDENTIFIER)->getData(); |
||
| 275 | } |
||
| 276 | |||
| 277 | 1 | throw new SyntaxErrorException('Can\'t parse argument'); |
|
| 278 | } |
||
| 279 | |||
| 280 | 3 | protected function parseObject($createType = true) |
|
| 281 | { |
||
| 282 | 3 | $this->eat(Token::TYPE_LBRACE); |
|
| 283 | |||
| 284 | 3 | $object = []; |
|
| 285 | 3 | while (!$this->match(Token::TYPE_RBRACE) && !$this->end()) { |
|
| 286 | 3 | $key = $this->expect(Token::TYPE_STRING)->getData(); |
|
| 287 | 3 | $this->expect(Token::TYPE_COLON); |
|
| 288 | 3 | $value = $this->parseListValue(); |
|
| 289 | |||
| 290 | 3 | if ($this->peek()->getType() != Token::TYPE_RBRACE) { |
|
| 291 | 3 | $this->expect(Token::TYPE_COMMA); |
|
| 292 | } |
||
| 293 | |||
| 294 | 3 | $object[$key] = $value; |
|
| 295 | } |
||
| 296 | |||
| 297 | 1 | $this->eat(Token::TYPE_RBRACE); |
|
| 298 | |||
| 299 | 1 | return $createType ? new InputObject($object) : $object; |
|
| 300 | } |
||
| 301 | |||
| 302 | 1 | protected function parseVariable() |
|
| 310 | |||
| 311 | 3 | protected function parseFragment() |
|
| 323 | |||
| 324 | 43 | protected function eat($type) |
|
| 332 | |||
| 333 | 18 | protected function eatMulti($types) |
|
| 341 | |||
| 342 | 41 | protected function matchMulti($types) |
|
| 343 | { |
||
| 344 | 41 | foreach ($types as $type) { |
|
| 345 | 41 | if ($this->peek()->getType() == $type) { |
|
| 346 | 41 | return true; |
|
| 347 | } |
||
| 348 | } |
||
| 349 | |||
| 350 | 4 | return false; |
|
| 351 | } |
||
| 352 | |||
| 353 | 44 | protected function match($type) |
|
| 357 | } |