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 |
||
5 | final class Parser |
||
6 | { |
||
7 | /** |
||
8 | * @var ScannerTokens |
||
9 | */ |
||
10 | private $scanner; |
||
11 | private $lexer; |
||
12 | |||
13 | 99 | public function __construct(Lexer $lexer) |
|
17 | |||
18 | 21 | private function getParseError($message) |
|
19 | { |
||
20 | 21 | $token = $this->scanner->getLastToken(); |
|
21 | 21 | if ($this->scanner->eof() === false) { |
|
22 | 12 | $token = $this->scanner->peek(); |
|
23 | 12 | } |
|
24 | |||
25 | 21 | return new ParseError($message . " (line {$token->location->line}, column {$token->location->column})"); |
|
26 | } |
||
27 | |||
28 | 66 | private function expect($tokenType) |
|
29 | { |
||
30 | 66 | $token = $this->scanner->next(); |
|
31 | |||
32 | 66 | if ($token->type !== $tokenType) { |
|
33 | 6 | $expectedToken = Token::getNameFor($tokenType); |
|
34 | 6 | throw $this->getParseError("Expected \"{$expectedToken}\" but instead found \"{$token->getName()}\" with value \"{$token->value}\""); |
|
35 | } |
||
36 | |||
37 | 66 | return $token; |
|
38 | } |
||
39 | |||
40 | 63 | View Code Duplication | private function accept($tokenType) |
50 | |||
51 | 66 | View Code Duplication | private function is($tokenType) |
61 | |||
62 | 3 | private function parseObject() |
|
63 | { |
||
64 | 3 | $location = $this->expect(Token::T_BRACE_LEFT)->location; |
|
65 | 3 | $fields = array(); |
|
66 | |||
67 | 3 | while (true) { |
|
68 | 3 | if ($this->scanner->eof()) { |
|
69 | throw $this->getParseError('Unclosed brace of object value'); |
||
70 | } |
||
71 | |||
72 | 3 | if ($this->accept(Token::T_BRACE_RIGHT)) { |
|
73 | 3 | break; |
|
74 | } |
||
75 | |||
76 | 3 | $nameToken = $this->expect(Token::T_NAME); |
|
77 | 3 | $this->expect(Token::T_COLON); |
|
78 | 3 | $fields[] = new ValueObjectField($nameToken->value, $this->parseValue(), $nameToken->location); |
|
79 | 3 | $this->accept(Token::T_COMMA); |
|
80 | 3 | } |
|
81 | |||
82 | 3 | return new ValueObject($fields, $location); |
|
83 | } |
||
84 | |||
85 | 6 | View Code Duplication | private function parseList() |
86 | { |
||
87 | 6 | $location = $this->expect(Token::T_BRACKET_LEFT)->location; |
|
88 | 6 | $items = array(); |
|
89 | |||
90 | 6 | while (true) { |
|
91 | 6 | if ($this->scanner->eof()) { |
|
92 | throw $this->getParseError('Unclosed bracket of list'); |
||
93 | } |
||
94 | |||
95 | 6 | if ($this->accept(Token::T_BRACKET_RIGHT)) { |
|
96 | 3 | break; |
|
97 | } |
||
98 | |||
99 | 6 | $items[] = $this->parseValue(); |
|
100 | 3 | $this->accept(Token::T_COMMA); |
|
101 | 3 | } |
|
102 | |||
103 | 3 | return new ValueList($items, $location); |
|
104 | } |
||
105 | |||
106 | 15 | private function parseVariable() |
|
113 | |||
114 | 30 | private function parseValue() |
|
115 | { |
||
116 | 30 | if ($this->is(Token::T_DOLLAR)) { |
|
117 | 12 | return $this->parseVariable(); |
|
118 | } |
||
119 | |||
120 | 24 | if ($string = $this->accept(Token::T_STRING)) { |
|
121 | 18 | return new ValueString($string->value, $string->location); |
|
122 | } |
||
123 | |||
124 | 21 | if ($true = $this->accept(Token::T_TRUE)) { |
|
125 | 3 | return new ValueBoolean(true, $true->location); |
|
126 | } |
||
127 | |||
128 | 21 | if ($false = $this->accept(Token::T_FALSE)) { |
|
129 | 3 | return new ValueBoolean(false, $false->location); |
|
130 | } |
||
131 | |||
132 | 21 | if ($null = $this->accept(Token::T_NULL)) { |
|
133 | 3 | return new ValueNull($null->location); |
|
134 | } |
||
135 | |||
136 | 21 | if ($int = $this->accept(Token::T_INT)) { |
|
137 | 18 | return new ValueInt($int->value, $int->location); |
|
138 | } |
||
139 | |||
140 | 6 | if ($float = $this->accept(Token::T_FLOAT)) { |
|
141 | 3 | return new ValueFloat($float->value, $float->location); |
|
142 | } |
||
143 | |||
144 | 6 | if ($name = $this->accept(Token::T_NAME)) { |
|
145 | 3 | return new ValueEnum($name->value, $name->location); |
|
146 | } |
||
147 | |||
148 | 6 | if ($this->is(Token::T_BRACKET_LEFT)) { |
|
149 | 6 | return $this->parseList(); |
|
150 | } |
||
151 | |||
152 | 6 | if ($this->is(Token::T_BRACE_LEFT)) { |
|
153 | 3 | return $this->parseObject(); |
|
154 | } |
||
155 | |||
156 | 3 | $message = 'Expected a value'; |
|
157 | |||
158 | 3 | if ($this->scanner->eof()) { |
|
159 | throw $this->getParseError($message . ' but instead reached end'); |
||
160 | } |
||
161 | |||
162 | 3 | $token = $this->scanner->peek(); |
|
163 | 3 | throw $this->getParseError($message . " but instead found \"{$token->getName()}\" with value \"{$token->value}\""); |
|
164 | } |
||
165 | |||
166 | 36 | private function parseArgument() |
|
174 | |||
175 | 60 | View Code Duplication | private function parseArgumentList() |
176 | { |
||
177 | 60 | $arguments = array(); |
|
178 | |||
179 | 60 | if ($this->is(Token::T_PAREN_LEFT) === false) { |
|
200 | |||
201 | 60 | private function parseField() |
|
224 | |||
225 | 6 | private function parseFragment() |
|
241 | |||
242 | 66 | private function parseSelection() |
|
243 | { |
||
244 | 66 | if ($this->is(Token::T_SPREAD)) { |
|
245 | 9 | return $this->parseFragment(); |
|
246 | } |
||
247 | |||
248 | 66 | if ($this->is(Token::T_NAME)) { |
|
249 | 60 | return $this->parseField(); |
|
250 | } |
||
251 | |||
252 | 3 | $message = 'Expected a field, a fragment spread or an inline fragment'; |
|
253 | |||
254 | 3 | if ($this->scanner->eof()) { |
|
255 | throw $this->getParseError($message . ' but instead reached end'); |
||
256 | } |
||
257 | |||
258 | 3 | $token = $this->scanner->peek(); |
|
259 | 3 | throw $this->getParseError($message . " but instead found \"{$token->getName()}\" with value \"{$token->value}\""); |
|
260 | } |
||
261 | |||
262 | 66 | View Code Duplication | private function parseSelectionSet() |
282 | |||
283 | 6 | private function parseTypeCondition() |
|
296 | |||
297 | 3 | private function parseListType() |
|
298 | { |
||
299 | 3 | $location = $this->expect(Token::T_BRACKET_LEFT)->location; |
|
300 | 3 | $type = $this->parseType(); |
|
301 | 3 | $this->accept(Token::T_BRACKET_RIGHT); |
|
302 | |||
303 | 3 | return new TypeList($type, $location); |
|
304 | } |
||
305 | |||
306 | 18 | private function parseNamedType() |
|
307 | { |
||
308 | 18 | $nameToken = $this->expect(Token::T_NAME); |
|
309 | 18 | $type = new TypeNamed($nameToken->value, $nameToken->location); |
|
310 | |||
311 | 18 | return $type; |
|
312 | } |
||
313 | |||
314 | 12 | private function parseType() |
|
315 | { |
||
316 | 12 | $type = null; |
|
317 | 12 | if ($this->is(Token::T_BRACKET_LEFT)) { |
|
318 | 3 | $type = $this->parseListType(); |
|
319 | 12 | } elseif ($this->is(Token::T_NAME)) { |
|
320 | 12 | $type = $this->parseNamedType(); |
|
321 | 12 | } |
|
322 | |||
323 | 12 | if ($type !== null) { |
|
324 | 12 | if ($this->accept(Token::T_EXCLAMATION)) { |
|
325 | 6 | return new TypeNonNull($type, $type->location); |
|
326 | } |
||
327 | 9 | return $type; |
|
328 | } |
||
329 | |||
330 | $message = 'Expected a type'; |
||
331 | |||
332 | if ($this->scanner->eof()) { |
||
333 | throw $this->getParseError($message . ' but instead reached end'); |
||
334 | } |
||
335 | |||
336 | $token = $this->scanner->peek(); |
||
337 | throw $this->getParseError($message . " but instead found \"{$token->getName()}\" with value \"{$token->value}\""); |
||
338 | } |
||
339 | |||
340 | 6 | private function parseDirective() |
|
341 | { |
||
342 | 6 | $location = $this->expect(Token::T_AT)->location; |
|
343 | 6 | $name = $this->expect(Token::T_NAME)->value; |
|
344 | 6 | $arguments = $this->parseArgumentList(); |
|
345 | |||
346 | 6 | return new Directive($name, $arguments, $location); |
|
347 | } |
||
348 | |||
349 | 51 | private function parseDirectiveList() |
|
350 | { |
||
351 | 51 | $directives = array(); |
|
352 | 51 | while ($this->is(Token::T_AT)) { |
|
353 | 6 | $directives[] = $this->parseDirective(); |
|
354 | 6 | } |
|
355 | |||
356 | 51 | return $directives; |
|
357 | } |
||
358 | |||
359 | 12 | private function parseVariableDefinition() |
|
360 | { |
||
361 | 12 | $variable = $this->parseVariable(); |
|
362 | 12 | $this->expect(Token::T_COLON); |
|
363 | 12 | $type = $this->parseType(); |
|
364 | 12 | $defaultValue = null; |
|
365 | |||
366 | 12 | if ($this->accept(Token::T_EQUAL)) { |
|
367 | 3 | $defaultValue = $this->parseValue(); |
|
368 | 3 | } |
|
369 | |||
370 | 12 | return new VariableDefinition($variable, $type, $defaultValue, $variable->location); |
|
371 | } |
||
372 | |||
373 | 18 | View Code Duplication | private function parseVariableDefinitionList() |
398 | |||
399 | 66 | private function parseDefinition() |
|
400 | { |
||
401 | 66 | if ($this->is(Token::T_FRAGMENT)) { |
|
402 | 3 | $location = $this->expect(Token::T_FRAGMENT)->location; |
|
403 | |||
404 | 3 | $name = $this->expect(Token::T_NAME)->value; |
|
466 | |||
467 | 69 | private function parseDocument() |
|
477 | |||
478 | 96 | public function parse($query) |
|
487 | } |
||
488 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.