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 Logical 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 Logical, and based on these observations, apply Extract Interface, too.
1 | <?php namespace Cornford\Logical; |
||
10 | class Logical extends LogicalAbstract implements LogicalInterface |
||
11 | { |
||
12 | /** |
||
13 | * The temporary logic method. |
||
14 | * |
||
15 | * @var string |
||
16 | */ |
||
17 | protected $tempLogic; |
||
18 | |||
19 | /** |
||
20 | * The temporary logic method. |
||
21 | * |
||
22 | * @var string |
||
23 | */ |
||
24 | protected $tempMethod; |
||
25 | |||
26 | /** |
||
27 | * The temporary logic expected. |
||
28 | * |
||
29 | * @var string|array |
||
30 | */ |
||
31 | protected $tempExpected; |
||
32 | |||
33 | /** |
||
34 | * The temporary logic field. |
||
35 | * |
||
36 | * @var string |
||
37 | */ |
||
38 | protected $tempField; |
||
39 | |||
40 | /** |
||
41 | * The temporary results. |
||
42 | * |
||
43 | * @var array |
||
44 | */ |
||
45 | protected $tempResults; |
||
46 | |||
47 | /** |
||
48 | * Decode the logic string into an array of logical statements. |
||
49 | * |
||
50 | * @return boolean |
||
51 | */ |
||
52 | protected function decodeLogicStatements() |
||
141 | |||
142 | /** |
||
143 | * Execute a logic statement method on an input value against an expected value. |
||
144 | * |
||
145 | * @param string $method |
||
146 | * @param string|integer|boolean $input |
||
147 | * @param string|integer|boolean $expected |
||
148 | * |
||
149 | * @throws LogicalExecutionException |
||
150 | * |
||
151 | * @return boolean |
||
152 | */ |
||
153 | protected function executeLogicStatement($method, $input, $expected = null) |
||
172 | |||
173 | /** |
||
174 | * Decodes logic input into statements and executes each statement removing un-matching results. |
||
175 | * |
||
176 | * @throws LogicalDecodingException |
||
177 | * @throws LogicalFieldValueException |
||
178 | * @throws LogicalExecutionException |
||
179 | * |
||
180 | * @return self |
||
181 | */ |
||
182 | public function execute() |
||
222 | } |
||
223 |
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.