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 CollectionDataTable 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 CollectionDataTable, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
10 | class CollectionDataTable extends DataTableAbstract |
||
11 | { |
||
12 | /** |
||
13 | * Collection object |
||
14 | * |
||
15 | * @var \Illuminate\Support\Collection |
||
16 | */ |
||
17 | public $collection; |
||
18 | |||
19 | /** |
||
20 | * Collection object |
||
21 | * |
||
22 | * @var \Illuminate\Support\Collection |
||
23 | */ |
||
24 | public $original; |
||
25 | |||
26 | /** |
||
27 | * Can the DataTable engine be created with these parameters. |
||
28 | * |
||
29 | * @param mixed $source |
||
30 | * @return boolean |
||
31 | */ |
||
32 | public static function canCreate($source) |
||
36 | |||
37 | /** |
||
38 | * Factory method, create and return an instance for the DataTable engine. |
||
39 | * |
||
40 | * @param array|\Illuminate\Support\Collection $source |
||
41 | * @return CollectionDataTable|DataTableAbstract |
||
42 | */ |
||
43 | public static function create($source) |
||
51 | |||
52 | /** |
||
53 | * CollectionEngine constructor. |
||
54 | * |
||
55 | * @param \Illuminate\Support\Collection $collection |
||
56 | */ |
||
57 | public function __construct(Collection $collection) |
||
65 | |||
66 | /** |
||
67 | * Serialize collection. |
||
68 | * |
||
69 | * @param mixed $collection |
||
70 | * @return mixed|null |
||
71 | */ |
||
72 | protected function serialize($collection) |
||
76 | |||
77 | /** |
||
78 | * Count results. |
||
79 | * |
||
80 | * @return integer |
||
81 | */ |
||
82 | public function count() |
||
86 | |||
87 | /** |
||
88 | * Perform column search. |
||
89 | * |
||
90 | * @return void |
||
91 | */ |
||
92 | public function columnSearch() |
||
127 | |||
128 | /** |
||
129 | * Perform pagination. |
||
130 | * |
||
131 | * @return void |
||
132 | */ |
||
133 | public function paging() |
||
140 | |||
141 | /** |
||
142 | * Organizes works. |
||
143 | * |
||
144 | * @param bool $mDataSupport |
||
145 | * @return \Illuminate\Http\JsonResponse |
||
146 | */ |
||
147 | public function make($mDataSupport = true) |
||
170 | |||
171 | /** |
||
172 | * Count total items. |
||
173 | * |
||
174 | * @return integer |
||
175 | */ |
||
176 | public function totalCount() |
||
180 | |||
181 | /** |
||
182 | * Get results. |
||
183 | * |
||
184 | * @return mixed |
||
185 | */ |
||
186 | public function results() |
||
190 | |||
191 | /** |
||
192 | * Revert transformed DT_Row_Index back to it's original values. |
||
193 | * |
||
194 | * @param bool $mDataSupport |
||
195 | */ |
||
196 | private function revertIndexColumn($mDataSupport) |
||
208 | |||
209 | /** |
||
210 | * Perform global search for the given keyword. |
||
211 | * |
||
212 | * @param string $keyword |
||
213 | */ |
||
214 | protected function globalSearch($keyword) |
||
244 | |||
245 | /** |
||
246 | * Perform default query orderBy clause. |
||
247 | */ |
||
248 | protected function defaultOrdering() |
||
269 | |||
270 | /** |
||
271 | * Get array sorter closure. |
||
272 | * |
||
273 | * @param array $criteria |
||
274 | * @return \Closure |
||
275 | */ |
||
276 | protected function getSorter(array $criteria) |
||
305 | |||
306 | /** |
||
307 | * Resolve callback parameter instance. |
||
308 | * |
||
309 | * @return $this |
||
310 | */ |
||
311 | protected function resolveCallbackParameter() |
||
315 | } |
||
316 |
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.