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 Xhgui_Storage_File 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 Xhgui_Storage_File, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
3 | class Xhgui_Storage_File implements Xhgui_StorageInterface, Xhgui_WatchedFunctionsStorageInterface |
||
4 | { |
||
5 | |||
6 | /** |
||
7 | * @var string |
||
8 | */ |
||
9 | protected $path = '../data/'; |
||
10 | |||
11 | /** |
||
12 | * @var string |
||
13 | */ |
||
14 | protected $prefix = 'xhgui.data'; |
||
15 | |||
16 | /** |
||
17 | * @var bool|mixed |
||
18 | */ |
||
19 | protected $separateMeta = true; |
||
20 | |||
21 | /** |
||
22 | * @var mixed |
||
23 | */ |
||
24 | protected $dataSerializer; |
||
25 | |||
26 | /** |
||
27 | * @var mixed |
||
28 | */ |
||
29 | protected $metaSerializer; |
||
30 | |||
31 | /** |
||
32 | * @var string |
||
33 | */ |
||
34 | protected $watchedFunctionsPathPrefix = '../watched_functions/'; |
||
35 | |||
36 | /** |
||
37 | * @var int[] |
||
38 | */ |
||
39 | protected $countCache; |
||
40 | |||
41 | /** |
||
42 | * @var Xhgui_Storage_Filter |
||
43 | */ |
||
44 | private $filter; |
||
45 | |||
46 | /** |
||
47 | * Xhgui_Storage_File constructor. |
||
48 | * @param $config |
||
49 | */ |
||
50 | public function __construct($config) |
||
63 | |||
64 | /** |
||
65 | * @inheritDoc |
||
66 | * @param Xhgui_Storage_Filter $filter |
||
67 | * @param bool $projections |
||
68 | * @return Xhgui_Storage_ResultSet |
||
69 | */ |
||
70 | public function find(Xhgui_Storage_Filter $filter, $projections = false) |
||
150 | |||
151 | /** |
||
152 | * @inheritDoc |
||
153 | * @param Xhgui_Storage_Filter $filter |
||
154 | * @return int |
||
155 | */ |
||
156 | public function count(Xhgui_Storage_Filter $filter) |
||
164 | |||
165 | /** |
||
166 | * @inheritDoc |
||
167 | * @param $id |
||
168 | * @return mixed |
||
169 | */ |
||
170 | public function findOne($id) |
||
177 | |||
178 | /** |
||
179 | * @inheritDoc |
||
180 | * @param $id |
||
181 | * @return bool |
||
182 | */ |
||
183 | public function remove($id) |
||
195 | |||
196 | /** |
||
197 | * @inheritDoc |
||
198 | */ |
||
199 | public function drop() |
||
204 | |||
205 | /** |
||
206 | * @inheritDoc |
||
207 | * @param $match |
||
208 | * @param $col |
||
209 | * @param int $percentile |
||
210 | * @return array |
||
211 | */ |
||
212 | public function aggregate(Xhgui_Storage_Filter $filter, $col, $percentile = 1) |
||
241 | |||
242 | |||
243 | /** |
||
244 | * Column sorter |
||
245 | * |
||
246 | * @param $a |
||
247 | * @param $b |
||
248 | * @return int |
||
249 | */ |
||
250 | public function sortByColumn($a, $b) |
||
305 | |||
306 | /** |
||
307 | * Generate meta profile name from profile file name. |
||
308 | * |
||
309 | * In most cases just add .meta extension |
||
310 | * |
||
311 | * @param $file |
||
312 | * @return mixed |
||
313 | */ |
||
314 | protected function getMetafileNameFromProfileName($file) |
||
319 | |||
320 | |||
321 | /** |
||
322 | * Try to parse string or unix timestamp ane return \DateTime |
||
323 | * |
||
324 | * @param $timestamp |
||
325 | * @return bool|DateTime |
||
326 | */ |
||
327 | protected function getDateTimeFromStringOrTimestamp($timestamp) |
||
352 | |||
353 | /** |
||
354 | * @inheritDoc |
||
355 | * @param $data |
||
356 | */ |
||
357 | public function insert(array $data) |
||
366 | |||
367 | /** |
||
368 | * @inheritDoc |
||
369 | * @param $id |
||
370 | * @param $data |
||
371 | */ |
||
372 | public function update($id, array $data) |
||
376 | |||
377 | /** |
||
378 | * Load profile file from disk, prepare it and return parsed array |
||
379 | * |
||
380 | * @param $path |
||
381 | * @param bool $meta |
||
382 | * @return mixed |
||
383 | */ |
||
384 | protected function importFile($path, $meta = false) |
||
421 | |||
422 | /** |
||
423 | * @inheritDoc |
||
424 | * @return array |
||
425 | */ |
||
426 | public function getWatchedFunctions() |
||
435 | |||
436 | /** |
||
437 | * @inheritDoc |
||
438 | * @param $name |
||
439 | * @return bool |
||
440 | */ |
||
441 | View Code Duplication | public function addWatchedFunction($name) |
|
454 | |||
455 | /** |
||
456 | * @inheritDoc |
||
457 | * @param $id |
||
458 | * @param $name |
||
459 | * @return bool |
||
460 | */ |
||
461 | View Code Duplication | public function updateWatchedFunction($id, $name) |
|
474 | |||
475 | /** |
||
476 | * @inheritDoc |
||
477 | * @param $id |
||
478 | */ |
||
479 | public function removeWatchedFunction($id) |
||
485 | |||
486 | /** |
||
487 | * Parse filename and try to get request time from filename |
||
488 | * |
||
489 | * @param $fileName |
||
490 | * @return bool|DateTime |
||
491 | */ |
||
492 | public function getRequestTimeFromFilename($fileName) |
||
504 | } |
||
505 |
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.