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:
Complex classes like Uploadable often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use Uploadable, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
19 | trait Uploadable |
||
20 | { |
||
21 | /** @var UploadOptions */ |
||
22 | protected $uploadOptions; |
||
23 | |||
24 | /** |
||
25 | * Boot the trait. |
||
26 | */ |
||
27 | public static function bootUploadable() |
||
62 | |||
63 | /** |
||
64 | * Retrive a specifice UploadOptions for this model, or return default UploadOptions |
||
65 | * @return UploadOptions |
||
66 | */ |
||
67 | public function getUploadOptionsOrDefault() : UploadOptions |
||
83 | |||
84 | /** |
||
85 | * This function will throw an exception when any of the options is missing or invalid. |
||
86 | * @throws InvalidOption |
||
87 | */ |
||
88 | public function guardAgainstInvalidUploadOptions() |
||
97 | |||
98 | /** |
||
99 | * Handle file upload. |
||
100 | */ |
||
101 | public function uploadFiles() |
||
118 | |||
119 | /** |
||
120 | * Upload a file releted to a passed attribute name |
||
121 | * @param string $uploadField |
||
122 | */ |
||
123 | public function uploadFile(string $uploadField) |
||
139 | |||
140 | /** |
||
141 | * Get an UploadedFile, generate new name, and save it in destination path. |
||
142 | * Return empty string if it fails, otherwise return the saved file name. |
||
143 | * @param UploadedFile $uploadedFile |
||
144 | * @param string $uploadAttribute |
||
145 | * @return string |
||
146 | */ |
||
147 | public function doUpload(UploadedFile $uploadedFile, $uploadAttribute) : string |
||
172 | |||
173 | /** |
||
174 | * Check request for valid files and server for correct paths defined in model |
||
175 | * @return bool |
||
176 | */ |
||
177 | protected function requestHasValidFilesAndCorrectPaths() : bool |
||
191 | |||
192 | /** |
||
193 | * Generate a new file name for uploaded file. |
||
194 | * Return empty string if uploadedFile is null, otherwise return the new file name.. |
||
195 | * @param UploadedFile $uploadedFile |
||
196 | * @param string $uploadField |
||
197 | * @return string |
||
198 | */ |
||
199 | public function generateNewUploadFileName(UploadedFile $uploadedFile, string $uploadField) : string |
||
217 | |||
218 | /** |
||
219 | * Check if file need a new name and return it, otherwise return empty string. |
||
220 | * @param UploadedFile $uploadedFile |
||
221 | * @return string |
||
222 | */ |
||
223 | protected function calcolateNewUploadFileName(UploadedFile $uploadedFile) : string |
||
236 | |||
237 | /** |
||
238 | * delete all Uploaded Files |
||
239 | */ |
||
240 | public function deleteUploadedFiles() |
||
247 | |||
248 | /** |
||
249 | * Delete upload file related to passed attribute name |
||
250 | * @param string $uploadField |
||
251 | */ |
||
252 | public function deleteUploadedFile(string $uploadField) |
||
272 | |||
273 | /** |
||
274 | * Reset model attribute and update db field |
||
275 | * @param string $uploadField |
||
276 | */ |
||
277 | protected function setBlanckAttributeAndDB(string $uploadField) |
||
287 | |||
288 | /** |
||
289 | * Return true If All Upload atrributes Are Empty or |
||
290 | * if the uploads array is not set. |
||
291 | * @return bool |
||
292 | */ |
||
293 | public function checkIfAllUploadFieldsAreEmpty() : bool |
||
304 | |||
305 | /** |
||
306 | * Check all attributes upload path, and try to create dir if not already exists. |
||
307 | * Return false if it fails to create all founded dirs. |
||
308 | * @return bool |
||
309 | */ |
||
310 | public function checkOrCreateAllUploadBasePaths() : bool |
||
320 | |||
321 | /** |
||
322 | * Check uploads property and return a uploads class field |
||
323 | * or empty array if somethings wrong. |
||
324 | * @return array |
||
325 | */ |
||
326 | public function getUploadsAttributesSafe() : array |
||
334 | |||
335 | /** |
||
336 | * Check attribute upload path, and try to create dir if not already exists. |
||
337 | * Return false if it fails to create the dir. |
||
338 | * @param string $uploadField |
||
339 | * @return bool |
||
340 | */ |
||
341 | public function checkOrCreateUploadBasePath(string $uploadField) : bool |
||
348 | |||
349 | /** |
||
350 | * Return the upload path for the passed attribute and try to create it if not exists. |
||
351 | * Returns empty string if dir if not exists and fails to create it. |
||
352 | * @param string $uploadField |
||
353 | * @return string |
||
354 | */ |
||
355 | public function getUploadFileBasePath(string $uploadField) : string |
||
375 | |||
376 | /** |
||
377 | * Return the specific upload path (by uploadPaths prop) for the passed attribute if exists, otherwise return empty string. |
||
378 | * @param string $uploadField |
||
379 | * @return string |
||
380 | */ |
||
381 | public function getUploadFileBasePathSpecific(string $uploadField) : string |
||
392 | |||
393 | /** |
||
394 | * Return the full (path+filename) upload abs path for the passed attribute. |
||
395 | * Returns empty string if dir if not exists. |
||
396 | * @param string $uploadField |
||
397 | * @return string |
||
398 | */ |
||
399 | public function getUploadFileFullPath(string $uploadField) : string |
||
410 | |||
411 | /** |
||
412 | * Return the full url (base url + filename) for the passed attribute. |
||
413 | * Returns empty string if dir if not exists. |
||
414 | * Ex.: http://localhost/laravel/public/upload/news/pippo.jpg |
||
415 | * @param string $uploadField |
||
416 | * @return string |
||
417 | */ |
||
418 | View Code Duplication | public function getUploadFileUrl(string $uploadField) : string |
|
436 | /** |
||
437 | * Return the base url (without filename) for the passed attribute. |
||
438 | * Returns empty string if dir if not exists. |
||
439 | * Ex.: http://localhost/laravel/public/upload/ |
||
440 | * @param string $uploadField |
||
441 | * @return string |
||
442 | */ |
||
443 | View Code Duplication | public function getUploadFileBaseUrl(string $uploadField) : string |
|
461 | |||
462 | /** |
||
463 | * Calcolate the new name for ALL uploaded files and set relative upload attributes |
||
464 | */ |
||
465 | public function generateAllNewUploadFileNameAndSetAttribute() |
||
471 | |||
472 | /** |
||
473 | * Calcolate the new name for uploaded file relative to passed attribute name and set the upload attribute |
||
474 | * @param string $uploadField |
||
475 | */ |
||
476 | public function generateNewUploadFileNameAndSetAttribute(string $uploadField) |
||
495 | } |
||
496 |
This check looks for methods that are used by a trait but not required by it.
To illustrate, let’s look at the following code example
The trait
Idable
provides a methodequalsId
that in turn relies on the methodgetId()
. If this method does not exist on a class mixing in this trait, the method will fail.Adding the
getId()
as an abstract method to the trait will make sure it is available.