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:
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 | * CollectionEngine constructor. |
||
28 | * |
||
29 | * @param \Illuminate\Support\Collection $collection |
||
30 | */ |
||
31 | public function __construct(Collection $collection) |
||
39 | |||
40 | /** |
||
41 | * Serialize collection |
||
42 | * |
||
43 | * @param mixed $collection |
||
44 | * @return mixed|null |
||
45 | */ |
||
46 | protected function serialize($collection) |
||
50 | |||
51 | /** |
||
52 | * Append debug parameters on output. |
||
53 | * |
||
54 | * @param array $output |
||
55 | * @return array |
||
56 | */ |
||
57 | public function showDebugger(array $output) |
||
63 | |||
64 | /** |
||
65 | * Count results. |
||
66 | * |
||
67 | * @return integer |
||
68 | */ |
||
69 | public function count() |
||
73 | |||
74 | /** |
||
75 | * Perform column search. |
||
76 | * |
||
77 | * @return void |
||
78 | */ |
||
79 | public function columnSearch() |
||
114 | |||
115 | /** |
||
116 | * Perform pagination. |
||
117 | * |
||
118 | * @return void |
||
119 | */ |
||
120 | public function paging() |
||
127 | |||
128 | /** |
||
129 | * Organizes works. |
||
130 | * |
||
131 | * @param bool $mDataSupport |
||
132 | * @return \Illuminate\Http\JsonResponse |
||
133 | */ |
||
134 | public function make($mDataSupport = true) |
||
155 | |||
156 | /** |
||
157 | * Count total items. |
||
158 | * |
||
159 | * @return integer |
||
160 | */ |
||
161 | public function totalCount() |
||
165 | |||
166 | /** |
||
167 | * Get results. |
||
168 | * |
||
169 | * @return mixed |
||
170 | */ |
||
171 | public function results() |
||
175 | |||
176 | /** |
||
177 | * Perform global search for the given keyword. |
||
178 | * |
||
179 | * @param string $keyword |
||
180 | */ |
||
181 | protected function globalSearch($keyword) |
||
206 | |||
207 | /** |
||
208 | * Perform default query orderBy clause. |
||
209 | */ |
||
210 | protected function defaultOrdering() |
||
240 | |||
241 | /** |
||
242 | * Resolve callback parameter instance. |
||
243 | * |
||
244 | * @return $this |
||
245 | */ |
||
246 | protected function resolveCallbackParameter() |
||
250 | } |
||
251 |
If you implement
__call
and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.This is often the case, when
__call
is implemented by a parent class and only the child class knows which methods exist: