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 |
||
19 | class Parser |
||
20 | { |
||
21 | /** @var Lexer */ |
||
22 | private $lexer; |
||
23 | |||
24 | /** @var Version */ |
||
25 | private $version; |
||
26 | |||
27 | /** @var Mode */ |
||
28 | private $mode; |
||
29 | |||
30 | /** @var bool */ |
||
31 | private $inValuePath; |
||
32 | |||
33 | /** |
||
34 | * @param Mode $mode |
||
35 | * @param Version $version |
||
36 | */ |
||
37 | public function __construct(Mode $mode = null, Version $version = null) |
||
46 | |||
47 | /** |
||
48 | * @return Mode |
||
49 | */ |
||
50 | public function getMode() |
||
54 | |||
55 | /** |
||
56 | * @param Mode $mode |
||
57 | */ |
||
58 | public function setMode(Mode $mode) |
||
62 | |||
63 | /** |
||
64 | * @return Version |
||
65 | */ |
||
66 | public function getVersion() |
||
70 | |||
71 | /** |
||
72 | * @param Version $version |
||
73 | */ |
||
74 | public function setVersion(Version $version) |
||
78 | |||
79 | /** |
||
80 | * @param string $input |
||
81 | * |
||
82 | * @return Ast\Node |
||
83 | */ |
||
84 | public function parse($input) |
||
101 | |||
102 | /** |
||
103 | * @return Ast\Path |
||
104 | */ |
||
105 | private function path() |
||
120 | |||
121 | /** |
||
122 | * @return Ast\Term|Ast\Disjunction |
||
123 | */ |
||
124 | View Code Duplication | private function disjunction() |
|
125 | { |
||
126 | /** @var Ast\Term[] $terms */ |
||
127 | $terms = []; |
||
128 | $terms[] = $this->conjunction(); |
||
129 | |||
130 | if ($this->lexer->isNextToken(Tokens::T_SP)) { |
||
131 | $nextToken = $this->lexer->glimpse(); |
||
132 | if ($this->isName('or', $nextToken)) { |
||
133 | $this->match(Tokens::T_SP); |
||
134 | $this->match(Tokens::T_NAME); |
||
135 | $this->match(Tokens::T_SP); |
||
136 | $terms[] = $this->conjunction(); |
||
137 | } |
||
138 | } |
||
139 | |||
140 | if (count($terms) == 1) { |
||
141 | return $terms[0]; |
||
142 | } |
||
143 | |||
144 | return new Ast\Disjunction($terms); |
||
145 | } |
||
146 | |||
147 | /** |
||
148 | * @return Ast\Conjunction|Ast\Factor |
||
149 | */ |
||
150 | View Code Duplication | private function conjunction() |
|
151 | { |
||
152 | $factors = []; |
||
153 | $factors[] = $this->factor(); |
||
154 | |||
155 | if ($this->lexer->isNextToken(Tokens::T_SP)) { |
||
156 | $nextToken = $this->lexer->glimpse(); |
||
157 | if ($this->isName('and', $nextToken)) { |
||
158 | $this->match(Tokens::T_SP); |
||
159 | $this->match(Tokens::T_NAME); |
||
160 | $this->match(Tokens::T_SP); |
||
161 | $factors[] = $this->factor(); |
||
162 | } |
||
163 | } |
||
164 | |||
165 | if (count($factors) == 1) { |
||
166 | return $factors[0]; |
||
167 | } |
||
168 | |||
169 | return new Ast\Conjunction($factors); |
||
170 | } |
||
171 | |||
172 | /** |
||
173 | * @return Ast\Filter |
||
174 | */ |
||
175 | private function factor() |
||
203 | |||
204 | /** |
||
205 | * @return Ast\ValuePath |
||
206 | */ |
||
207 | private function valuePath() |
||
218 | |||
219 | /** |
||
220 | * @return Ast\ComparisonExpression |
||
221 | */ |
||
222 | private function comparisionExpression() |
||
237 | |||
238 | /** |
||
239 | * @return Ast\AttributePath |
||
240 | */ |
||
241 | private function attributePath() |
||
286 | |||
287 | /** |
||
288 | * @return string |
||
289 | */ |
||
290 | private function comparisonOperator() |
||
300 | |||
301 | /** |
||
302 | * @return mixed |
||
303 | */ |
||
304 | private function compareValue() |
||
335 | |||
336 | /** |
||
337 | * @return bool |
||
338 | */ |
||
339 | private function isValuePathIncoming() |
||
346 | |||
347 | /** |
||
348 | * @param string|string[] $value |
||
349 | * @param Token|null $token |
||
350 | * |
||
351 | * @return bool |
||
352 | */ |
||
353 | private function isName($value, $token) |
||
374 | |||
375 | private function match($tokenName) |
||
389 | |||
390 | private function syntaxError($expected = '', Token $token = null) |
||
409 | } |
||
410 |
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVar
assignment in line 1 and the$higher
assignment in line 2 are dead. The first because$myVar
is never used and the second because$higher
is always overwritten for every possible time line.