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 | * The original source. |
||
21 | * |
||
22 | * @var mixed |
||
23 | */ |
||
24 | public $original; |
||
25 | |||
26 | /** |
||
27 | * CollectionDataTable constructor. |
||
28 | * |
||
29 | * @param mixed $source |
||
30 | */ |
||
31 | public function __construct($source) |
||
32 | { |
||
33 | $this->request = app('datatables.request'); |
||
34 | $this->config = app('datatables.config'); |
||
35 | $this->original = $source; |
||
36 | $this->collection = $source instanceof Collection ? $source : new Collection($source); |
||
37 | $this->columns = array_keys($this->serialize($this->collection->first())); |
||
38 | } |
||
39 | |||
40 | /** |
||
41 | * Serialize collection. |
||
42 | * |
||
43 | * @param mixed $collection |
||
44 | * @return mixed|null |
||
45 | */ |
||
46 | protected function serialize($collection) |
||
50 | |||
51 | /** |
||
52 | * Count results. |
||
53 | * |
||
54 | * @return int |
||
55 | */ |
||
56 | public function count() |
||
60 | |||
61 | /** |
||
62 | * Perform column search. |
||
63 | * |
||
64 | * @return void |
||
65 | */ |
||
66 | public function columnSearch() |
||
101 | |||
102 | /** |
||
103 | * Perform pagination. |
||
104 | * |
||
105 | * @return void |
||
106 | */ |
||
107 | public function paging() |
||
114 | |||
115 | /** |
||
116 | * Organizes works. |
||
117 | * |
||
118 | * @param bool $mDataSupport |
||
119 | * @return \Illuminate\Http\JsonResponse |
||
120 | */ |
||
121 | public function make($mDataSupport = true) |
||
144 | |||
145 | /** |
||
146 | * Count total items. |
||
147 | * |
||
148 | * @return int |
||
149 | */ |
||
150 | public function totalCount() |
||
154 | |||
155 | /** |
||
156 | * Get results. |
||
157 | * |
||
158 | * @return mixed |
||
159 | */ |
||
160 | public function results() |
||
164 | |||
165 | /** |
||
166 | * Revert transformed DT_Row_Index back to it's original values. |
||
167 | * |
||
168 | * @param bool $mDataSupport |
||
169 | */ |
||
170 | private function revertIndexColumn($mDataSupport) |
||
182 | |||
183 | /** |
||
184 | * Perform global search for the given keyword. |
||
185 | * |
||
186 | * @param string $keyword |
||
187 | */ |
||
188 | protected function globalSearch($keyword) |
||
217 | |||
218 | /** |
||
219 | * Perform default query orderBy clause. |
||
220 | */ |
||
221 | protected function defaultOrdering() |
||
242 | |||
243 | /** |
||
244 | * Get array sorter closure. |
||
245 | * |
||
246 | * @param array $criteria |
||
247 | * @return \Closure |
||
248 | */ |
||
249 | protected function getSorter(array $criteria) |
||
278 | |||
279 | /** |
||
280 | * Resolve callback parameter instance. |
||
281 | * |
||
282 | * @return $this |
||
283 | */ |
||
284 | protected function resolveCallbackParameter() |
||
288 | } |
||
289 |
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.