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 |
||
16 | class CollectionEngine extends BaseEngine |
||
17 | { |
||
18 | /** |
||
19 | * Collection object |
||
20 | * |
||
21 | * @var \Illuminate\Support\Collection |
||
22 | */ |
||
23 | public $collection; |
||
24 | |||
25 | /** |
||
26 | * Collection object |
||
27 | * |
||
28 | * @var \Illuminate\Support\Collection |
||
29 | */ |
||
30 | public $original; |
||
31 | |||
32 | /** |
||
33 | * CollectionEngine constructor. |
||
34 | * |
||
35 | * @param \Illuminate\Support\Collection $collection |
||
36 | */ |
||
37 | public function __construct(Collection $collection) |
||
45 | |||
46 | /** |
||
47 | * Serialize collection |
||
48 | * |
||
49 | * @param mixed $collection |
||
50 | * @return mixed|null |
||
51 | */ |
||
52 | protected function serialize($collection) |
||
56 | |||
57 | /** |
||
58 | * Set auto filter off and run your own filter. |
||
59 | * Overrides global search. |
||
60 | * |
||
61 | * @param callable $callback |
||
62 | * @param bool $globalSearch |
||
63 | * @return $this |
||
64 | */ |
||
65 | public function filter(callable $callback, $globalSearch = false) |
||
71 | |||
72 | /** |
||
73 | * Append debug parameters on output. |
||
74 | * |
||
75 | * @param array $output |
||
76 | * @return array |
||
77 | */ |
||
78 | public function showDebugger(array $output) |
||
84 | |||
85 | /** |
||
86 | * Count results. |
||
87 | * |
||
88 | * @return integer |
||
89 | */ |
||
90 | public function count() |
||
94 | |||
95 | /** |
||
96 | * Perform global search for the given keyword. |
||
97 | * |
||
98 | * @param string $keyword |
||
99 | */ |
||
100 | protected function globalSearch($keyword) |
||
135 | |||
136 | /** |
||
137 | * Perform column search. |
||
138 | * |
||
139 | * @return void |
||
140 | */ |
||
141 | public function columnSearch() |
||
176 | |||
177 | /** |
||
178 | * Perform pagination. |
||
179 | * |
||
180 | * @return void |
||
181 | */ |
||
182 | public function paging() |
||
189 | |||
190 | /** |
||
191 | * Get results. |
||
192 | * |
||
193 | * @return mixed |
||
194 | */ |
||
195 | public function results() |
||
199 | |||
200 | /** |
||
201 | * Organizes works. |
||
202 | * |
||
203 | * @param bool $mDataSupport |
||
204 | * @return \Illuminate\Http\JsonResponse |
||
205 | */ |
||
206 | public function make($mDataSupport = false) |
||
226 | |||
227 | /** |
||
228 | * Count total items. |
||
229 | * |
||
230 | * @return integer |
||
231 | */ |
||
232 | public function totalCount() |
||
236 | |||
237 | /** |
||
238 | * Perform sorting of columns. |
||
239 | * |
||
240 | * @return void |
||
241 | */ |
||
242 | public function ordering() |
||
269 | } |
||
270 |
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.