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 | * @param Xhgui_Storage_Filter $filter |
||
66 | * @param bool $projections |
||
67 | * @return Xhgui_Storage_ResultSet |
||
68 | */ |
||
69 | public function find(Xhgui_Storage_Filter $filter, $projections = false) |
||
146 | |||
147 | /** |
||
148 | * @param Xhgui_Storage_Filter $filter |
||
149 | * @return int |
||
150 | */ |
||
151 | public function count(Xhgui_Storage_Filter $filter) |
||
159 | |||
160 | /** |
||
161 | * @param $id |
||
162 | * @return mixed |
||
163 | */ |
||
164 | public function findOne($id) |
||
171 | |||
172 | /** |
||
173 | * @param $id |
||
174 | * @return bool |
||
175 | */ |
||
176 | public function remove($id) |
||
188 | |||
189 | /** |
||
190 | * |
||
191 | */ |
||
192 | public function drop() |
||
197 | |||
198 | /** |
||
199 | * @param $match |
||
200 | * @param $col |
||
201 | * @param int $percentile |
||
202 | * @return array |
||
203 | */ |
||
204 | public function aggregate(Xhgui_Storage_Filter $filter, $col, $percentile = 1) |
||
231 | |||
232 | |||
233 | /** |
||
234 | * @param $a |
||
235 | * @param $b |
||
236 | * @return int |
||
237 | */ |
||
238 | public function sortByColumn($a, $b) |
||
293 | |||
294 | |||
295 | |||
296 | /** |
||
297 | * @param $file |
||
298 | * @return mixed |
||
299 | */ |
||
300 | protected function getMetafileNameFromProfileName($file) |
||
305 | |||
306 | |||
307 | /** |
||
308 | * @param $timestamp |
||
309 | * @return bool|DateTime |
||
310 | */ |
||
311 | protected function getDateTimeFromStringOrTimestamp($timestamp) |
||
336 | |||
337 | /** |
||
338 | * @param $data |
||
339 | */ |
||
340 | public function insert(array $data) |
||
349 | |||
350 | /** |
||
351 | * @param $id |
||
352 | * @param $data |
||
353 | */ |
||
354 | public function update($id, array $data) |
||
358 | |||
359 | /** |
||
360 | * @param $path |
||
361 | * @param bool $meta |
||
362 | * @return mixed |
||
363 | */ |
||
364 | protected function importFile($path, $meta = false) |
||
401 | |||
402 | /** |
||
403 | * @return array |
||
404 | */ |
||
405 | public function getWatchedFunctions() |
||
414 | |||
415 | /** |
||
416 | * @param $name |
||
417 | * @return bool |
||
418 | */ |
||
419 | View Code Duplication | public function addWatchedFunction($name) |
|
429 | |||
430 | /** |
||
431 | * @param $id |
||
432 | * @param $name |
||
433 | * @return bool |
||
434 | */ |
||
435 | View Code Duplication | public function updateWatchedFunction($id, $name) |
|
445 | |||
446 | /** |
||
447 | * @param $id |
||
448 | */ |
||
449 | public function removeWatchedFunction($id) |
||
455 | |||
456 | /** |
||
457 | * @param $fileName |
||
458 | * @return bool|DateTime |
||
459 | */ |
||
460 | public function getRequestTimeFromFilename($fileName) |
||
472 | } |
||
473 |