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 |
||
15 | class Parser |
||
16 | { |
||
17 | /** |
||
18 | * Convert tokens tree to abstract syntax tree. |
||
19 | * |
||
20 | * @param Tokens\Token[] $tokens |
||
21 | * @return Nodes\Node |
||
22 | */ |
||
23 | public function parse(array $tokens): Nodes\Node |
||
29 | |||
30 | /** |
||
31 | * @param $token |
||
32 | * @return Nodes\Identifier|Nodes\Literal|Nodes\Node |
||
33 | * @throws ParserException |
||
34 | */ |
||
35 | private function parseToken($token) |
||
51 | |||
52 | /** |
||
53 | * @param array $tokens |
||
54 | * @return Nodes\Node |
||
55 | * @throws ParserException |
||
56 | */ |
||
57 | private function parseExpression(array $tokens): Nodes\Node |
||
85 | |||
86 | /** |
||
87 | * @param array $tokens |
||
88 | * @return Nodes\Node |
||
89 | */ |
||
90 | private function parseDeclaration($tokens): Nodes\Node |
||
100 | |||
101 | /** |
||
102 | * @param $tokens |
||
103 | * @return Nodes\Node |
||
104 | * @throws ParserException |
||
105 | */ |
||
106 | private function parseIfExpression($tokens): Nodes\Node |
||
116 | |||
117 | /** |
||
118 | * @param $tokens |
||
119 | * @return Nodes\Node |
||
120 | * @throws ParserException |
||
121 | */ |
||
122 | View Code Duplication | private function parseOrExpression($tokens): Nodes\Node |
|
132 | |||
133 | /** |
||
134 | * @param $tokens |
||
135 | * @return Nodes\Node |
||
136 | * @throws ParserException |
||
137 | */ |
||
138 | View Code Duplication | private function parseAndExpression($tokens): Nodes\Node |
|
148 | |||
149 | /** |
||
150 | * @param $tokens |
||
151 | * @return Nodes\Node |
||
152 | * @throws ParserException |
||
153 | */ |
||
154 | private function parseLambdaDeclaration($tokens): Nodes\Node |
||
170 | |||
171 | /** |
||
172 | * @param $tokens |
||
173 | * @return Nodes\Node |
||
174 | * @throws ParserException |
||
175 | */ |
||
176 | private function parseFunctionDeclaration($tokens): Nodes\Node |
||
198 | |||
199 | /** |
||
200 | * @param array $tokens |
||
201 | * @return Nodes\Assign |
||
202 | * @throws ParserException |
||
203 | */ |
||
204 | private function parseAssignDeclaration(array $tokens): Nodes\Assign |
||
221 | |||
222 | /** |
||
223 | * @param array $tokens |
||
224 | * @return Nodes\Node |
||
225 | */ |
||
226 | private function parseCallExpression(array $tokens): Nodes\Node |
||
235 | |||
236 | /** |
||
237 | * @param array $tokens |
||
238 | * @return Nodes\Program |
||
239 | */ |
||
240 | private function parseProgram(array $tokens): Nodes\Program |
||
244 | |||
245 | /** |
||
246 | * @param array $tokens |
||
247 | * @return Nodes\SequenceExpression |
||
248 | */ |
||
249 | private function parseSequence(array $tokens): Nodes\SequenceExpression |
||
253 | |||
254 | /** |
||
255 | * @param Tokens\IdentifierToken $token |
||
256 | * @return Nodes\Identifier |
||
257 | */ |
||
258 | private function parseIdentifier(Tokens\IdentifierToken $token): Nodes\Identifier |
||
262 | |||
263 | /** |
||
264 | * @param Tokens\StringToken $token |
||
265 | * @return Nodes\Literal |
||
266 | */ |
||
267 | private function parseString(Tokens\StringToken $token): Nodes\Literal |
||
271 | |||
272 | /** |
||
273 | * @param Tokens\NumericToken $token |
||
274 | * @return Nodes\Literal |
||
275 | */ |
||
276 | private function parseNumeric(Tokens\NumericToken $token): Nodes\Literal |
||
280 | |||
281 | /** |
||
282 | * Convert list of tokens into token tree. |
||
283 | * |
||
284 | * @param Tokens\Token[] $tokens |
||
285 | * @return mixed |
||
286 | */ |
||
287 | private function deflate(array $tokens) |
||
309 | |||
310 | /** |
||
311 | * @param array $tokens |
||
312 | * @return mixed |
||
313 | */ |
||
314 | private function findPairClosingBracketIndex(array $tokens) |
||
334 | } |
||
335 |
This checks looks for assignemnts to variables using the
list(...)
function, where not all assigned variables are subsequently used.Consider the following code example.
Only the variables
$a
and$c
are used. There was no need to assign$b
.Instead, the list call could have been.