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 |
||
63 | */ |
||
64 | protected $displayLength = false; |
||
65 | |||
66 | /** |
||
67 | * @var array |
||
68 | */ |
||
69 | protected $searchableColumns = [ |
||
70 | Text::class, |
||
71 | Link::class, |
||
72 | Email::class, |
||
73 | ]; |
||
74 | |||
75 | /** |
||
76 | * DisplayDatatablesAsync constructor. |
||
77 | * |
||
78 | * @param string|null $name |
||
79 | * @param string|null $distinct |
||
80 | */ |
||
81 | public function __construct($name = null, $distinct = null) |
||
90 | |||
91 | /** |
||
92 | * Initialize display. |
||
93 | */ |
||
94 | public function initialize() |
||
114 | |||
115 | /** |
||
116 | * @param bool $length |
||
117 | * @return $this |
||
118 | */ |
||
119 | public function setDisplayLength($length) |
||
125 | |||
126 | /** |
||
127 | * @return bool |
||
128 | */ |
||
129 | public function getDisplayLength() |
||
133 | |||
134 | /** |
||
135 | * @param $search |
||
136 | * @return $this |
||
137 | */ |
||
138 | public function setDisplaySearch($search) |
||
144 | |||
145 | /** |
||
146 | * @return bool |
||
147 | */ |
||
148 | public function getDisplaySearch() |
||
152 | |||
153 | /** |
||
154 | * @return string |
||
155 | */ |
||
156 | public function getName() |
||
160 | |||
161 | /** |
||
162 | * @param string $name |
||
163 | * |
||
164 | * @return $this |
||
165 | */ |
||
166 | public function setName($name) |
||
172 | |||
173 | /** |
||
174 | * @return mixed |
||
175 | */ |
||
176 | public function getDistinct() |
||
180 | |||
181 | /** |
||
182 | * @param mixed $distinct |
||
183 | * |
||
184 | * @return $this |
||
185 | */ |
||
186 | public function setDistinct($distinct) |
||
192 | |||
193 | /** |
||
194 | * Render async request. |
||
195 | * |
||
196 | * @param \Illuminate\Http\Request $request |
||
197 | * |
||
198 | * @return array |
||
199 | */ |
||
200 | public function renderAsync(\Illuminate\Http\Request $request) |
||
224 | |||
225 | /** |
||
226 | * Apply offset and limit to the query. |
||
227 | * |
||
228 | * @param $query |
||
229 | * @param \Illuminate\Http\Request $request |
||
230 | */ |
||
231 | protected function applyOffset($query, \Illuminate\Http\Request $request) |
||
242 | |||
243 | /** |
||
244 | * Check if column is searchable. |
||
245 | * |
||
246 | * @param ColumnInterface $column |
||
247 | * @return bool |
||
248 | */ |
||
249 | public function checkSearchableColumns(ColumnInterface $column) |
||
259 | |||
260 | /** |
||
261 | * Apply search to the query. |
||
262 | * |
||
263 | * @param Builder $query |
||
264 | * @param \Illuminate\Http\Request $request |
||
265 | */ |
||
266 | protected function applySearch(Builder $query, \Illuminate\Http\Request $request) |
||
297 | |||
298 | /** |
||
299 | * Convert collection to the datatables structure. |
||
300 | * |
||
301 | * @param \Illuminate\Http\Request $request |
||
302 | * @param array|Collection $collection |
||
303 | * @param int $totalCount |
||
304 | * @param int $filteredCount |
||
305 | * |
||
306 | * @return array |
||
307 | */ |
||
308 | protected function prepareDatatablesStructure( |
||
340 | |||
341 | /** |
||
342 | * @return Collection |
||
343 | * @throws \Exception |
||
344 | */ |
||
345 | public function getCollection() |
||
348 | |||
349 | /** |
||
350 | * @param $payload |
||
351 | */ |
||
352 | public function setPayload($payload) |
||
356 | |||
357 | /** |
||
358 | * @return mixed |
||
359 | */ |
||
360 | public function getPayload() |
||
364 | |||
365 | /** |
||
366 | * @return array |
||
367 | */ |
||
368 | public function toArray() |
||
375 | } |
||
376 |
Let’s assume that you have a directory layout like this:
and let’s assume the following content of
Bar.php
:If both files
OtherDir/Foo.php
andSomeDir/Foo.php
are loaded in the same runtime, you will see a PHP error such as the following:PHP Fatal error: Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php
However, as
OtherDir/Foo.php
does not necessarily have to be loaded and the error is only triggered if it is loaded beforeOtherDir/Bar.php
, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias: