Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
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 |
||
| 26 | class Parser extends Tokenizer |
||
| 27 | { |
||
| 28 | |||
| 29 | /** @var array */ |
||
| 30 | private $data = []; |
||
| 31 | |||
| 32 | 95 | public function parse($source = null) |
|
| 33 | { |
||
| 34 | 95 | $this->init($source); |
|
| 35 | |||
| 36 | 95 | while (!$this->end()) { |
|
| 37 | 94 | $tokenType = $this->peek()->getType(); |
|
| 38 | |||
| 39 | switch ($tokenType) { |
||
| 40 | 94 | case Token::TYPE_LBRACE: |
|
| 41 | 94 | case Token::TYPE_QUERY: |
|
| 42 | 77 | foreach ($this->parseBody() as $query) { |
|
| 43 | 70 | $this->data['queries'][] = $query; |
|
| 44 | 73 | } |
|
| 45 | |||
| 46 | 73 | break; |
|
| 47 | |||
| 48 | 25 | case Token::TYPE_MUTATION: |
|
| 49 | 16 | foreach ($this->parseBody(Token::TYPE_MUTATION) as $query) { |
|
| 50 | 11 | $this->data['mutations'][] = $query; |
|
| 51 | 11 | } |
|
| 52 | |||
| 53 | 11 | break; |
|
| 54 | |||
| 55 | 9 | case Token::TYPE_FRAGMENT: |
|
| 56 | 8 | $this->data['fragments'][] = $this->parseFragment(); |
|
| 57 | |||
| 58 | 8 | break; |
|
| 59 | |||
| 60 | 1 | default: |
|
| 61 | 1 | throw new SyntaxErrorException('Incorrect request syntax', $this->getLocation()); |
|
| 62 | 1 | } |
|
| 63 | 84 | } |
|
| 64 | |||
| 65 | 85 | return $this->data; |
|
| 66 | } |
||
| 67 | |||
| 68 | 95 | private function init($source = null) |
|
| 69 | { |
||
| 70 | 95 | $this->initTokenizer($source); |
|
| 71 | |||
| 72 | 95 | $this->data = [ |
|
| 73 | 95 | 'queries' => [], |
|
| 74 | 95 | 'mutations' => [], |
|
| 75 | 95 | 'fragments' => [], |
|
| 76 | 95 | 'fragmentReferences' => [], |
|
| 77 | 95 | 'variables' => [], |
|
| 78 | 95 | 'variableReferences' => [] |
|
| 79 | 95 | ]; |
|
| 80 | 95 | } |
|
| 81 | |||
| 82 | 93 | protected function parseBody($token = Token::TYPE_QUERY, $highLevel = true) |
|
| 83 | { |
||
| 84 | 93 | $fields = []; |
|
| 85 | |||
| 86 | 93 | if ($highLevel && $this->peek()->getType() === $token) { |
|
| 87 | 40 | $this->lex(); |
|
| 88 | 40 | $this->eat(Token::TYPE_IDENTIFIER); |
|
| 89 | |||
| 90 | 40 | if ($this->match(Token::TYPE_LPAREN)) { |
|
| 91 | 11 | $this->parseVariables(); |
|
| 92 | 11 | } |
|
| 93 | 40 | } |
|
| 94 | |||
| 95 | 93 | $this->lex(); |
|
| 96 | |||
| 97 | 93 | while (!$this->match(Token::TYPE_RBRACE) && !$this->end()) { |
|
| 98 | 90 | if ($this->match(Token::TYPE_FRAGMENT_REFERENCE)) { |
|
| 99 | 10 | $this->lex(); |
|
| 100 | |||
| 101 | 10 | if ($this->eat(Token::TYPE_ON)) { |
|
| 102 | 3 | $fields[] = $this->parseBodyItem(Token::TYPE_TYPED_FRAGMENT, $highLevel); |
|
| 103 | 3 | } else { |
|
| 104 | 8 | $fields[] = $this->parseFragmentReference(); |
|
| 105 | } |
||
| 106 | 10 | } else { |
|
| 107 | 90 | $fields[] = $this->parseBodyItem($token, $highLevel); |
|
| 108 | } |
||
| 109 | 82 | } |
|
| 110 | |||
| 111 | 85 | $this->expect(Token::TYPE_RBRACE); |
|
| 112 | |||
| 113 | 85 | return $fields; |
|
| 114 | } |
||
| 115 | |||
| 116 | 11 | protected function parseVariables() |
|
| 117 | { |
||
| 118 | 11 | $this->eat(Token::TYPE_LPAREN); |
|
| 119 | |||
| 120 | 11 | while (!$this->match(Token::TYPE_RPAREN) && !$this->end()) { |
|
| 121 | 11 | $variableToken = $this->eat(Token::TYPE_VARIABLE); |
|
| 122 | 11 | $nameToken = $this->eatIdentifierToken(); |
|
| 123 | 11 | $this->eat(Token::TYPE_COLON); |
|
| 124 | |||
| 125 | 11 | $isArray = false; |
|
| 126 | 11 | $arrayElementNullable = true; |
|
| 127 | |||
| 128 | 11 | if ($this->match(Token::TYPE_LSQUARE_BRACE)) { |
|
| 129 | 2 | $isArray = true; |
|
| 130 | |||
| 131 | 2 | $this->eat(Token::TYPE_LSQUARE_BRACE); |
|
| 132 | 2 | $type = $this->eatIdentifierToken()->getData(); |
|
| 133 | |||
| 134 | 2 | if ($this->match(Token::TYPE_REQUIRED)) { |
|
| 135 | 2 | $arrayElementNullable = false; |
|
| 136 | 2 | $this->eat(Token::TYPE_REQUIRED); |
|
| 137 | 2 | } |
|
| 138 | |||
| 139 | 2 | $this->eat(Token::TYPE_RSQUARE_BRACE); |
|
| 140 | 2 | } else { |
|
| 141 | 10 | $type = $this->eatIdentifierToken()->getData(); |
|
| 142 | } |
||
| 143 | |||
| 144 | 11 | $required = false; |
|
| 145 | 11 | if ($this->match(Token::TYPE_REQUIRED)) { |
|
| 146 | 2 | $required = true; |
|
| 147 | 2 | $this->eat(Token::TYPE_REQUIRED); |
|
| 148 | 2 | } |
|
| 149 | |||
| 150 | 11 | $this->data['variables'][] = new Variable( |
|
| 151 | 11 | $nameToken->getData(), |
|
| 152 | 11 | $type, |
|
| 153 | 11 | $required, |
|
| 154 | 11 | $isArray, |
|
| 155 | 11 | new Location($variableToken->getLine(), $variableToken->getColumn()), |
|
| 156 | $arrayElementNullable |
||
| 157 | 11 | ); |
|
| 158 | 11 | } |
|
| 159 | |||
| 160 | 11 | $this->expect(Token::TYPE_RPAREN); |
|
| 161 | 11 | } |
|
| 162 | |||
| 163 | 90 | protected function expectMulti($types) |
|
| 171 | |||
| 172 | 10 | protected function parseVariableReference() |
|
| 173 | { |
||
| 174 | 10 | $startToken = $this->expectMulti([Token::TYPE_VARIABLE]); |
|
| 175 | |||
| 176 | 10 | if ($this->match(Token::TYPE_NUMBER) || $this->match(Token::TYPE_IDENTIFIER) || $this->match(Token::TYPE_QUERY)) { |
|
| 177 | 10 | $name = $this->lex()->getData(); |
|
| 178 | |||
| 179 | 10 | $variable = $this->findVariable($name); |
|
| 180 | 10 | if ($variable) { |
|
| 181 | 10 | $variable->setUsed(true); |
|
| 182 | 10 | } |
|
| 183 | |||
| 184 | 10 | $variableReference = new VariableReference($name, $variable, new Location($startToken->getLine(), $startToken->getColumn())); |
|
| 185 | |||
| 186 | 10 | $this->data['variableReferences'][] = $variableReference; |
|
| 187 | |||
| 188 | 10 | return $variableReference; |
|
| 189 | } |
||
| 190 | |||
| 191 | throw $this->createUnexpectedException($this->peek()); |
||
| 192 | } |
||
| 193 | |||
| 194 | 10 | protected function findVariable($name) |
|
| 195 | { |
||
| 196 | 10 | foreach ($this->data['variables'] as $variable) { |
|
| 197 | /** @var $variable Variable */ |
||
| 198 | 10 | if ($variable->getName() == $name) { |
|
| 199 | 10 | return $variable; |
|
| 200 | } |
||
| 201 | 2 | } |
|
| 202 | |||
| 203 | return null; |
||
| 204 | } |
||
| 205 | |||
| 206 | 8 | protected function parseFragmentReference() |
|
| 215 | |||
| 216 | 90 | protected function eatIdentifierToken() |
|
| 217 | { |
||
| 218 | 90 | return $this->expectMulti([ |
|
| 219 | 90 | Token::TYPE_IDENTIFIER, |
|
| 220 | 90 | Token::TYPE_MUTATION, |
|
| 221 | 90 | Token::TYPE_QUERY, |
|
| 222 | 90 | Token::TYPE_FRAGMENT, |
|
| 223 | 90 | ]); |
|
| 224 | } |
||
| 225 | |||
| 226 | 90 | protected function parseBodyItem($type = Token::TYPE_QUERY, $highLevel = true) |
|
| 227 | { |
||
| 228 | 90 | $nameToken = $this->eatIdentifierToken(); |
|
| 229 | 90 | $alias = null; |
|
| 230 | |||
| 231 | 90 | if ($this->eat(Token::TYPE_COLON)) { |
|
| 232 | 17 | $alias = $nameToken->getData(); |
|
| 233 | 17 | $nameToken = $this->eatIdentifierToken(); |
|
| 234 | 17 | } |
|
| 235 | |||
| 236 | 90 | $bodyLocation = new Location($nameToken->getLine(), $nameToken->getColumn()); |
|
| 237 | 90 | $arguments = $this->match(Token::TYPE_LPAREN) ? $this->parseArgumentList() : []; |
|
| 238 | |||
| 239 | 83 | if ($this->match(Token::TYPE_LBRACE)) { |
|
| 240 | 56 | $fields = $this->parseBody($type == Token::TYPE_TYPED_FRAGMENT ? Token::TYPE_QUERY : $type, false); |
|
| 241 | |||
| 242 | 55 | if (!$fields) { |
|
|
|
|||
| 243 | 3 | throw $this->createUnexpectedTokenTypeException($this->lookAhead->getType()); |
|
| 244 | } |
||
| 245 | |||
| 246 | 54 | View Code Duplication | if ($type == Token::TYPE_QUERY) { |
| 247 | 52 | return new Query($nameToken->getData(), $alias, $arguments, $fields, $bodyLocation); |
|
| 248 | 6 | } elseif ($type == Token::TYPE_TYPED_FRAGMENT) { |
|
| 249 | 3 | return new TypedFragmentReference($nameToken->getData(), $fields, $bodyLocation); |
|
| 250 | } else { |
||
| 251 | 3 | return new Mutation($nameToken->getData(), $alias, $arguments, $fields, $bodyLocation); |
|
| 252 | } |
||
| 253 | View Code Duplication | } else { |
|
| 254 | 81 | if ($highLevel && $type == Token::TYPE_MUTATION) { |
|
| 255 | 9 | return new Mutation($nameToken->getData(), $alias, $arguments, [], $bodyLocation); |
|
| 256 | 73 | } elseif ($highLevel && $type == Token::TYPE_QUERY) { |
|
| 257 | 23 | return new Query($nameToken->getData(), $alias, $arguments, [], $bodyLocation); |
|
| 258 | } |
||
| 259 | |||
| 260 | 54 | return new Field($nameToken->getData(), $alias, $arguments, $bodyLocation); |
|
| 261 | } |
||
| 262 | } |
||
| 263 | |||
| 264 | 54 | protected function parseArgumentList() |
|
| 265 | { |
||
| 266 | 54 | $args = []; |
|
| 267 | |||
| 268 | 54 | $this->expect(Token::TYPE_LPAREN); |
|
| 269 | |||
| 270 | 54 | while (!$this->match(Token::TYPE_RPAREN) && !$this->end()) { |
|
| 271 | 54 | $args[] = $this->parseArgument(); |
|
| 272 | 49 | } |
|
| 273 | |||
| 274 | 48 | $this->expect(Token::TYPE_RPAREN); |
|
| 275 | |||
| 276 | 47 | return $args; |
|
| 277 | } |
||
| 278 | |||
| 279 | 54 | protected function parseArgument() |
|
| 287 | |||
| 288 | /** |
||
| 289 | * @return array|InputList|InputObject|Literal|VariableReference |
||
| 290 | * |
||
| 291 | * @throws SyntaxErrorException |
||
| 292 | */ |
||
| 293 | 53 | protected function parseValue() |
|
| 294 | { |
||
| 295 | 53 | switch ($this->lookAhead->getType()) { |
|
| 296 | 53 | case Token::TYPE_LSQUARE_BRACE: |
|
| 297 | 7 | return $this->parseList(); |
|
| 298 | |||
| 299 | 46 | case Token::TYPE_LBRACE: |
|
| 300 | 8 | return $this->parseObject(); |
|
| 301 | |||
| 302 | 38 | case Token::TYPE_VARIABLE: |
|
| 303 | 10 | return $this->parseVariableReference(); |
|
| 304 | |||
| 305 | 32 | case Token::TYPE_NUMBER: |
|
| 306 | 32 | case Token::TYPE_STRING: |
|
| 307 | 32 | case Token::TYPE_IDENTIFIER: |
|
| 308 | 32 | case Token::TYPE_NULL: |
|
| 309 | 32 | case Token::TYPE_TRUE: |
|
| 310 | 32 | case Token::TYPE_FALSE: |
|
| 311 | 31 | $token = $this->lex(); |
|
| 312 | |||
| 313 | 31 | return new Literal($token->getData(), new Location($token->getLine(), $token->getColumn())); |
|
| 314 | 1 | } |
|
| 315 | |||
| 316 | 1 | throw $this->createUnexpectedException($this->lookAhead); |
|
| 317 | } |
||
| 318 | |||
| 319 | 9 | protected function parseList($createType = true) |
|
| 320 | { |
||
| 321 | 9 | $startToken = $this->eat(Token::TYPE_LSQUARE_BRACE); |
|
| 322 | |||
| 323 | 9 | $list = []; |
|
| 324 | 9 | while (!$this->match(Token::TYPE_RSQUARE_BRACE) && !$this->end()) { |
|
| 325 | 9 | $list[] = $this->parseListValue(); |
|
| 326 | 8 | } |
|
| 327 | |||
| 328 | 8 | $this->expect(Token::TYPE_RSQUARE_BRACE); |
|
| 329 | |||
| 330 | 8 | return $createType ? new InputList($list, new Location($startToken->getLine(), $startToken->getColumn())) : $list; |
|
| 331 | } |
||
| 332 | |||
| 333 | 15 | protected function parseListValue() |
|
| 334 | { |
||
| 335 | 15 | switch ($this->lookAhead->getType()) { |
|
| 336 | 15 | case Token::TYPE_NUMBER: |
|
| 337 | 15 | case Token::TYPE_STRING: |
|
| 338 | 15 | case Token::TYPE_TRUE: |
|
| 339 | 15 | case Token::TYPE_FALSE: |
|
| 340 | 15 | case Token::TYPE_NULL: |
|
| 341 | 15 | case Token::TYPE_IDENTIFIER: |
|
| 342 | 14 | return $this->expect($this->lookAhead->getType())->getData(); |
|
| 343 | |||
| 344 | 5 | case Token::TYPE_VARIABLE: |
|
| 345 | return $this->parseVariableReference(); |
||
| 346 | |||
| 347 | 5 | case Token::TYPE_LBRACE: |
|
| 348 | 4 | return $this->parseObject(true); |
|
| 349 | |||
| 350 | 3 | case Token::TYPE_LSQUARE_BRACE: |
|
| 351 | 2 | return $this->parseList(false); |
|
| 352 | 1 | } |
|
| 353 | |||
| 354 | 1 | throw new SyntaxErrorException('Can\'t parse argument', $this->getLocation()); |
|
| 355 | } |
||
| 356 | |||
| 357 | 9 | protected function parseObject($createType = true) |
|
| 358 | { |
||
| 359 | 9 | $startToken = $this->eat(Token::TYPE_LBRACE); |
|
| 360 | |||
| 361 | 9 | $object = []; |
|
| 362 | 9 | while (!$this->match(Token::TYPE_RBRACE) && !$this->end()) { |
|
| 363 | 9 | $key = $this->expectMulti([Token::TYPE_STRING, Token::TYPE_IDENTIFIER])->getData(); |
|
| 364 | 9 | $this->expect(Token::TYPE_COLON); |
|
| 365 | 9 | $value = $this->parseListValue(); |
|
| 366 | |||
| 367 | 9 | $object[$key] = $value; |
|
| 368 | 9 | } |
|
| 369 | |||
| 370 | 7 | $this->eat(Token::TYPE_RBRACE); |
|
| 371 | |||
| 372 | 7 | return $createType ? new InputObject($object, new Location($startToken->getLine(), $startToken->getColumn())) : $object; |
|
| 373 | } |
||
| 374 | |||
| 375 | 8 | protected function parseFragment() |
|
| 387 | |||
| 388 | 92 | protected function eat($type) |
|
| 396 | |||
| 397 | protected function eatMulti($types) |
||
| 405 | |||
| 406 | 90 | protected function matchMulti($types) |
|
| 407 | { |
||
| 408 | 90 | foreach ($types as $type) { |
|
| 409 | 90 | if ($this->peek()->getType() == $type) { |
|
| 410 | 90 | return true; |
|
| 411 | } |
||
| 412 | 11 | } |
|
| 413 | |||
| 414 | 2 | return false; |
|
| 415 | } |
||
| 416 | } |
||
| 417 |
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.