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 ArrayDataSource 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 ArrayDataSource, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
23 | 1 | class ArrayDataSource implements IDataSource |
|
24 | { |
||
25 | |||
26 | /** |
||
27 | * @var array |
||
28 | */ |
||
29 | protected $data = []; |
||
30 | |||
31 | /** |
||
32 | * @var int |
||
33 | */ |
||
34 | protected $count = 0; |
||
35 | |||
36 | |||
37 | /** |
||
38 | * @param array $data_source |
||
39 | */ |
||
40 | public function __construct(array $data_source) |
||
44 | |||
45 | |||
46 | /******************************************************************************** |
||
47 | * IDataSource implementation * |
||
48 | ********************************************************************************/ |
||
49 | |||
50 | |||
51 | /** |
||
52 | * Get count of data |
||
53 | * @return int |
||
54 | */ |
||
55 | public function getCount() |
||
59 | |||
60 | |||
61 | /** |
||
62 | * Get the data |
||
63 | * @return array |
||
64 | */ |
||
65 | public function getData() |
||
69 | |||
70 | |||
71 | /** |
||
72 | * Set the data |
||
73 | * @param array $data_source |
||
74 | * @return static |
||
75 | */ |
||
76 | private function setData(array $data_source) |
||
82 | |||
83 | |||
84 | /** |
||
85 | * Filter data |
||
86 | * @param Filter[] $filters |
||
87 | * @return static |
||
88 | */ |
||
89 | public function filter(array $filters) |
||
110 | |||
111 | |||
112 | /** |
||
113 | * Filter data - get one row |
||
114 | * @param array $condition |
||
115 | * @return ArrayDataSource |
||
116 | */ |
||
117 | public function filterOne(array $condition) |
||
133 | |||
134 | |||
135 | /** |
||
136 | * Apply limit and offset on data |
||
137 | * @param int $offset |
||
138 | * @param int $limit |
||
139 | * @return static |
||
140 | */ |
||
141 | public function limit($offset, $limit) |
||
148 | |||
149 | |||
150 | /** |
||
151 | * Apply fitler and tell whether row passes conditions or not |
||
152 | * @param mixed $row |
||
153 | * @param Filter $filter |
||
154 | * @return mixed |
||
155 | */ |
||
156 | protected function applyFilter($row, Filter $filter) |
||
194 | |||
195 | |||
196 | /** |
||
197 | * Filter by multi select value |
||
198 | * @param mixed $row |
||
199 | * @param FilterMultiSelect $filter |
||
200 | * @return void |
||
201 | */ |
||
202 | public function applyFilterMultiSelect($row, FilterMultiSelect $filter) |
||
209 | |||
210 | |||
211 | /** |
||
212 | * @param mixed $row |
||
213 | * @param FilterRange $filter |
||
214 | * @return void |
||
215 | */ |
||
216 | public function applyFilterRange($row, FilterRange $filter) |
||
235 | |||
236 | |||
237 | /** |
||
238 | * @param mixed $row |
||
239 | * @param FilterDateRange $filter |
||
240 | * @return void |
||
241 | */ |
||
242 | public function applyFilterDateRange($row, FilterDateRange $filter) |
||
297 | |||
298 | |||
299 | /** |
||
300 | * Apply fitler date and tell whether row value matches or not |
||
301 | * @param mixed $row |
||
302 | * @param Filter $filter |
||
303 | * @return mixed |
||
304 | */ |
||
305 | protected function applyFilterDate($row, FilterDate $filter) |
||
332 | |||
333 | |||
334 | /** |
||
335 | * Sort data |
||
336 | * @param Sorting $sorting |
||
337 | * @return static |
||
338 | */ |
||
339 | public function sort(Sorting $sorting) |
||
390 | } |
||
391 |
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.