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 DisplayDatatablesAsync extends DisplayDatatables implements WithRoutesInterface |
||
18 | { |
||
19 | /** |
||
20 | * Register display routes. |
||
21 | * |
||
22 | * @param Router $router |
||
23 | * |
||
24 | * @throws \Symfony\Component\HttpKernel\Exception\NotFoundHttpException |
||
25 | */ |
||
26 | 285 | View Code Duplication | public static function registerRoutes(Router $router) |
44 | |||
45 | protected $payload; |
||
46 | /** |
||
47 | * @var string |
||
48 | */ |
||
49 | protected $name; |
||
50 | |||
51 | /** |
||
52 | * @param string|null $name |
||
53 | */ |
||
54 | protected $distinct; |
||
55 | |||
56 | /** |
||
57 | * @var |
||
58 | */ |
||
59 | protected $displaySearch = false; |
||
60 | |||
61 | /** |
||
62 | * @var array |
||
63 | */ |
||
64 | protected $searchableColumns = [ |
||
65 | Text::class, |
||
66 | Link::class, |
||
67 | Email::class, |
||
68 | ]; |
||
69 | |||
70 | /** |
||
71 | * DisplayDatatablesAsync constructor. |
||
72 | * |
||
73 | * @param string|null $name |
||
74 | * @param string|null $distinct |
||
75 | */ |
||
76 | public function __construct($name = null, $distinct = null) |
||
85 | |||
86 | /** |
||
87 | * Initialize display. |
||
88 | */ |
||
89 | public function initialize() |
||
105 | |||
106 | /** |
||
107 | * @param $searching |
||
108 | * @return $this |
||
109 | */ |
||
110 | public function setSearching($searching) |
||
116 | |||
117 | /** |
||
118 | * @return bool |
||
119 | */ |
||
120 | public function getSearching() |
||
124 | |||
125 | /** |
||
126 | * @return string |
||
127 | */ |
||
128 | public function getName() |
||
132 | |||
133 | /** |
||
134 | * @param string $name |
||
135 | * |
||
136 | * @return $this |
||
137 | */ |
||
138 | public function setName($name) |
||
144 | |||
145 | /** |
||
146 | * @return mixed |
||
147 | */ |
||
148 | public function getDistinct() |
||
152 | |||
153 | /** |
||
154 | * @param mixed $distinct |
||
155 | * |
||
156 | * @return $this |
||
157 | */ |
||
158 | public function setDistinct($distinct) |
||
164 | |||
165 | /** |
||
166 | * Render async request. |
||
167 | * |
||
168 | * @param \Illuminate\Http\Request $request |
||
169 | * |
||
170 | * @return array |
||
171 | */ |
||
172 | public function renderAsync(\Illuminate\Http\Request $request) |
||
194 | |||
195 | /** |
||
196 | * Apply offset and limit to the query. |
||
197 | * |
||
198 | * @param $query |
||
199 | * @param \Illuminate\Http\Request $request |
||
200 | */ |
||
201 | protected function applyOffset($query, \Illuminate\Http\Request $request) |
||
212 | |||
213 | /** |
||
214 | * Apply search to the query. |
||
215 | * |
||
216 | * @param Builder $query |
||
217 | * @param \Illuminate\Http\Request $request |
||
218 | */ |
||
219 | protected function applySearch(Builder $query, \Illuminate\Http\Request $request) |
||
250 | |||
251 | /** |
||
252 | * Convert collection to the datatables structure. |
||
253 | * |
||
254 | * @param \Illuminate\Http\Request $request |
||
255 | * @param array|Collection $collection |
||
256 | * @param int $totalCount |
||
257 | * @param int $filteredCount |
||
258 | * |
||
259 | * @return array |
||
260 | */ |
||
261 | protected function prepareDatatablesStructure( |
||
293 | |||
294 | /** |
||
295 | * @return Collection |
||
296 | * @throws \Exception |
||
297 | */ |
||
298 | public function getCollection() |
||
301 | |||
302 | /** |
||
303 | * @param $payload |
||
304 | */ |
||
305 | public function setPayload($payload) |
||
309 | |||
310 | /** |
||
311 | * @return mixed |
||
312 | */ |
||
313 | public function getPayload() |
||
317 | |||
318 | /** |
||
319 | * @return array |
||
320 | */ |
||
321 | public function toArray() |
||
328 | } |
||
329 |
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.