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 |
||
17 | class CollectionEngine extends BaseEngine |
||
18 | { |
||
19 | /** |
||
20 | * Collection object |
||
21 | * |
||
22 | * @var \Illuminate\Support\Collection |
||
23 | */ |
||
24 | public $collection; |
||
25 | |||
26 | /** |
||
27 | * Collection object |
||
28 | * |
||
29 | * @var \Illuminate\Support\Collection |
||
30 | */ |
||
31 | public $original_collection; |
||
32 | |||
33 | /** |
||
34 | * CollectionEngine constructor. |
||
35 | * |
||
36 | * @param \Illuminate\Support\Collection $collection |
||
37 | * @param \Yajra\Datatables\Request $request |
||
38 | */ |
||
39 | public function __construct(Collection $collection, Request $request) |
||
46 | |||
47 | /** |
||
48 | * Serialize collection |
||
49 | * |
||
50 | * @param mixed $collection |
||
51 | * @return mixed|null |
||
52 | */ |
||
53 | protected function serialize($collection) |
||
57 | |||
58 | /** |
||
59 | * Set auto filter off and run your own filter. |
||
60 | * Overrides global search. |
||
61 | * |
||
62 | * @param callable $callback |
||
63 | * @param bool $globalSearch |
||
64 | * @return $this |
||
65 | */ |
||
66 | public function filter(callable $callback, $globalSearch = false) |
||
72 | |||
73 | /** |
||
74 | * Append debug parameters on output. |
||
75 | * |
||
76 | * @param array $output |
||
77 | * @return array |
||
78 | */ |
||
79 | public function showDebugger(array $output) |
||
85 | |||
86 | /** |
||
87 | * Count total items. |
||
88 | * |
||
89 | * @return integer |
||
90 | */ |
||
91 | public function totalCount() |
||
95 | |||
96 | /** |
||
97 | * Count results. |
||
98 | * |
||
99 | * @return integer |
||
100 | */ |
||
101 | public function count() |
||
105 | |||
106 | /** |
||
107 | * Perform sorting of columns. |
||
108 | * |
||
109 | * @return void |
||
110 | */ |
||
111 | public function ordering() |
||
138 | |||
139 | /** |
||
140 | * Perform global search. |
||
141 | * |
||
142 | * @return void |
||
143 | */ |
||
144 | public function filtering() |
||
156 | |||
157 | /** |
||
158 | * Perform multi-term search by splitting keyword into |
||
159 | * individual words and searches for each of them. |
||
160 | * |
||
161 | * @param string $keyword |
||
162 | */ |
||
163 | private function smartGlobalSearch($keyword) |
||
171 | |||
172 | /** |
||
173 | * Perform global search for the given keyword. |
||
174 | * |
||
175 | * @param string $keyword |
||
176 | */ |
||
177 | private function globalSearch($keyword) |
||
212 | |||
213 | /** |
||
214 | * Perform column search. |
||
215 | * |
||
216 | * @return void |
||
217 | */ |
||
218 | public function columnSearch() |
||
253 | |||
254 | /** |
||
255 | * Perform pagination. |
||
256 | * |
||
257 | * @return void |
||
258 | */ |
||
259 | public function paging() |
||
266 | |||
267 | /** |
||
268 | * Get results. |
||
269 | * |
||
270 | * @return mixed |
||
271 | */ |
||
272 | public function results() |
||
276 | |||
277 | /** |
||
278 | * Organizes works. |
||
279 | * |
||
280 | * @param bool $mDataSupport |
||
281 | * @return \Illuminate\Http\JsonResponse |
||
282 | */ |
||
283 | public function make($mDataSupport = false) |
||
303 | } |
||
304 |
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: