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 RecordableHistory 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 RecordableHistory, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
8 | class RecordableHistory |
||
9 | { |
||
10 | /** @var Record[] */ |
||
11 | private $records; |
||
12 | /** @var RecordDiff[] */ |
||
13 | private $diffs; |
||
14 | |||
15 | /** |
||
16 | * @param Record $initial |
||
17 | * @param Record $final |
||
18 | * @param RecordDiff[] $diffs |
||
19 | * |
||
20 | * @return RecordableHistory |
||
21 | */ |
||
22 | 2 | public static function fromInitialAndFinalAndDiffs(Record $initial, Record $final, $diffs) |
|
29 | |||
30 | /** |
||
31 | * @param Record[] $records |
||
32 | */ |
||
33 | 3 | public function __construct($records) |
|
37 | |||
38 | /** |
||
39 | * @return float |
||
40 | */ |
||
41 | public function getCompactionRate() |
||
60 | |||
61 | /** |
||
62 | * @return Record |
||
63 | */ |
||
64 | 3 | View Code Duplication | public function getInitialRecord() |
76 | |||
77 | /** |
||
78 | * @return Record |
||
79 | */ |
||
80 | 3 | View Code Duplication | public function getFinalRecord() |
91 | |||
92 | /** |
||
93 | * {@inheritdoc} |
||
94 | */ |
||
95 | 3 | public function getRecords() |
|
99 | |||
100 | /** |
||
101 | * @return RecordDiff[] |
||
102 | */ |
||
103 | 3 | public function getDiffs() |
|
111 | |||
112 | 3 | protected function calculateDiffs() |
|
144 | |||
145 | /** |
||
146 | * @param Record $before |
||
147 | * @param Record $after |
||
148 | * |
||
149 | * @return RecordDiff |
||
150 | */ |
||
151 | 3 | protected function calculateDiff(Record $before, Record $after) |
|
163 | |||
164 | /** |
||
165 | * @param RecordDiff $diff |
||
166 | * @param string $pathInArray |
||
167 | * @param mixed[] $before |
||
168 | * @param mixed[] $after |
||
169 | */ |
||
170 | 3 | View Code Duplication | private function diffRecursiveFromBeforeToAfter(RecordDiff $diff, $pathInArray, $before, $after) |
199 | |||
200 | /** |
||
201 | * @param RecordDiff $diff |
||
202 | * @param string $pathInArray |
||
203 | * @param array $before |
||
204 | * @param array $after |
||
205 | */ |
||
206 | 3 | View Code Duplication | private function diffRecursiveFromAfterToBefore(RecordDiff $diff, $pathInArray, $before, $after) |
236 | |||
237 | /** |
||
238 | * @param $value |
||
239 | * @param string $fallback |
||
240 | * |
||
241 | * @return string |
||
242 | */ |
||
243 | 3 | private function convertToString($value, $fallback = 'N/A') |
|
268 | } |
||
269 |
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.