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() |
||
| 270 | |||
| 271 | /** |
||
| 272 | * @return string |
||
| 273 | */ |
||
| 274 | private function comparisonOperator() |
||
| 284 | |||
| 285 | /** |
||
| 286 | * @return mixed |
||
| 287 | */ |
||
| 288 | private function compareValue() |
||
| 319 | |||
| 320 | /** |
||
| 321 | * @return bool |
||
| 322 | */ |
||
| 323 | private function isValuePathIncoming() |
||
| 330 | |||
| 331 | /** |
||
| 332 | * @param string|string[] $value |
||
| 333 | * @param Token|null $token |
||
| 334 | * |
||
| 335 | * @return bool |
||
| 336 | */ |
||
| 337 | private function isName($value, $token) |
||
| 358 | |||
| 359 | private function match($tokenName) |
||
| 373 | |||
| 374 | private function syntaxError($expected = '', Token $token = null) |
||
| 393 | } |
||
| 394 |
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVarassignment in line 1 and the$higherassignment in line 2 are dead. The first because$myVaris never used and the second because$higheris always overwritten for every possible time line.