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 | * Determine if the model or given attribute(s) have been modified. |
||
| 26 | * Implemented in \Illuminate\Database\Eloquent\Model |
||
| 27 | * |
||
| 28 | * @param array|string|null $attributes = null |
||
| 29 | * |
||
| 30 | * @return bool |
||
| 31 | */ |
||
| 32 | abstract public function isDirty($attributes = null); |
||
| 33 | |||
| 34 | /** |
||
| 35 | * Get the model's original attribute values. |
||
| 36 | * Implemented in \Illuminate\Database\Eloquent\Model |
||
| 37 | * |
||
| 38 | * @param string|null $key = null |
||
| 39 | * @param mixed $default = null |
||
| 40 | * |
||
| 41 | * @return mixed|array |
||
| 42 | */ |
||
| 43 | abstract public function getOriginal($key = null, $default = null); |
||
| 44 | |||
| 45 | /** |
||
| 46 | * Boot the trait. |
||
| 47 | */ |
||
| 48 | public static function bootUploadable() |
||
| 55 | |||
| 56 | /** |
||
| 57 | * Bind create model events. |
||
| 58 | */ |
||
| 59 | protected static function bindCreateEvents() |
||
| 66 | |||
| 67 | /** |
||
| 68 | * Bind save model events. |
||
| 69 | */ |
||
| 70 | protected static function bindSaveEvents() |
||
| 76 | |||
| 77 | /** |
||
| 78 | * Bind update model events. |
||
| 79 | */ |
||
| 80 | protected static function bindUpdateEvents() |
||
| 87 | |||
| 88 | /** |
||
| 89 | * Bind delete model events. |
||
| 90 | */ |
||
| 91 | protected static function bindDeleteEvents() |
||
| 104 | |||
| 105 | /** |
||
| 106 | * Retrive a specifice UploadOptions for this model, or return default UploadOptions |
||
| 107 | * @return UploadOptions |
||
| 108 | */ |
||
| 109 | public function getUploadOptionsOrDefault() : UploadOptions |
||
| 125 | |||
| 126 | /** |
||
| 127 | * This function will throw an exception when any of the options is missing or invalid. |
||
| 128 | * @throws InvalidOption |
||
| 129 | */ |
||
| 130 | public function guardAgainstInvalidUploadOptions() |
||
| 139 | |||
| 140 | /** |
||
| 141 | * Handle file upload. |
||
| 142 | */ |
||
| 143 | public function uploadFiles() |
||
| 156 | |||
| 157 | /** |
||
| 158 | * Upload a file releted to a passed attribute name |
||
| 159 | * @param string $uploadField |
||
| 160 | */ |
||
| 161 | public function uploadFile(string $uploadField) |
||
| 187 | |||
| 188 | /** |
||
| 189 | * Get an UploadedFile, generate new name, and save it in destination path. |
||
| 190 | * Return empty string if it fails, otherwise return the saved file name. |
||
| 191 | * @param UploadedFile $uploadedFile |
||
| 192 | * @param string $uploadAttribute |
||
| 193 | * @return string |
||
| 194 | */ |
||
| 195 | public function doUpload(UploadedFile $uploadedFile, $uploadAttribute) : string |
||
| 227 | |||
| 228 | /** |
||
| 229 | * Check request for valid files and server for correct paths defined in model |
||
| 230 | * @return bool |
||
| 231 | */ |
||
| 232 | public function requestHasValidFilesAndCorrectPaths() : bool |
||
| 246 | |||
| 247 | /** |
||
| 248 | * Generate a new file name for uploaded file. |
||
| 249 | * Return empty string if uploadedFile is null, otherwise return the new file name.. |
||
| 250 | * @param UploadedFile $uploadedFile |
||
| 251 | * @return string |
||
| 252 | * @internal param string $uploadField |
||
| 253 | */ |
||
| 254 | public function generateNewUploadFileName(UploadedFile $uploadedFile) : string |
||
| 269 | |||
| 270 | /** |
||
| 271 | * Check if file need a new name and return it, otherwise return empty string. |
||
| 272 | * @param UploadedFile $uploadedFile |
||
| 273 | * @return string |
||
| 274 | */ |
||
| 275 | public function calcolateNewUploadFileName(UploadedFile $uploadedFile) : string |
||
| 288 | |||
| 289 | /** |
||
| 290 | * delete all Uploaded Files |
||
| 291 | */ |
||
| 292 | public function deleteUploadedFiles() |
||
| 299 | |||
| 300 | /** |
||
| 301 | * Delete upload file related to passed attribute name |
||
| 302 | * @param string $uploadField |
||
| 303 | */ |
||
| 304 | public function deleteUploadedFile(string $uploadField) |
||
| 326 | |||
| 327 | /** |
||
| 328 | * Reset model attribute and update db field |
||
| 329 | * @param string $uploadField |
||
| 330 | */ |
||
| 331 | public function setBlanckAttributeAndDB(string $uploadField) |
||
| 348 | |||
| 349 | /** |
||
| 350 | * Return true If All Upload atrributes Are Empty or |
||
| 351 | * if the uploads array is not set. |
||
| 352 | * @return bool |
||
| 353 | */ |
||
| 354 | public function checkIfAllUploadFieldsAreEmpty() : bool |
||
| 365 | |||
| 366 | /** |
||
| 367 | * Check all attributes upload path, and try to create dir if not already exists. |
||
| 368 | * Return false if it fails to create all founded dirs. |
||
| 369 | * @return bool |
||
| 370 | */ |
||
| 371 | public function checkOrCreateAllUploadBasePaths() : bool |
||
| 381 | |||
| 382 | /** |
||
| 383 | * Check uploads property and return a uploads class field |
||
| 384 | * or empty array if somethings wrong. |
||
| 385 | * @return array |
||
| 386 | */ |
||
| 387 | public function getUploadsAttributesSafe() : array |
||
| 395 | |||
| 396 | /** |
||
| 397 | * Check attribute upload path, and try to create dir if not already exists. |
||
| 398 | * Return false if it fails to create the dir. |
||
| 399 | * @param string $uploadField |
||
| 400 | * @return bool |
||
| 401 | */ |
||
| 402 | public function checkOrCreateUploadBasePath(string $uploadField) : bool |
||
| 415 | |||
| 416 | /** |
||
| 417 | * Return the upload path for the passed attribute and try to create it if not exists. |
||
| 418 | * Returns empty string if dir if not exists and fails to create it. |
||
| 419 | * @param string $uploadField |
||
| 420 | * @return string |
||
| 421 | */ |
||
| 422 | public function getUploadFileBasePath(string $uploadField) : string |
||
| 445 | |||
| 446 | /** |
||
| 447 | * Return the specific upload path (by uploadPaths prop) for the passed attribute if exists, |
||
| 448 | * otherwise return empty string. |
||
| 449 | * If uploadPaths for this field is relative, a public_path was appended. |
||
| 450 | * @param string $uploadField |
||
| 451 | * @return string |
||
| 452 | */ |
||
| 453 | public function getUploadFileBasePathSpecific(string $uploadField) : string |
||
| 466 | |||
| 467 | /** |
||
| 468 | * Check if there is a specified upload path setted for this field |
||
| 469 | * @param string $uploadField |
||
| 470 | * @return bool |
||
| 471 | */ |
||
| 472 | public function hasUploadPathSpecifiedForThisField(string $uploadField) : bool |
||
| 479 | |||
| 480 | /** |
||
| 481 | * Return the full (path+filename) upload abs path for the passed attribute. |
||
| 482 | * Returns empty string if dir if not exists. |
||
| 483 | * @param string $uploadField |
||
| 484 | * @return string |
||
| 485 | */ |
||
| 486 | public |
||
| 500 | |||
| 501 | /** |
||
| 502 | * Return the full url (base url + filename) for the passed attribute. |
||
| 503 | * Returns empty string if dir if not exists. |
||
| 504 | * Ex.: http://localhost/laravel/public/upload/news/pippo.jpg |
||
| 505 | * @param string $uploadField |
||
| 506 | * @return string |
||
| 507 | */ |
||
| 508 | View Code Duplication | public |
|
| 517 | |||
| 518 | /** |
||
| 519 | * get a path and remove public_path. |
||
| 520 | * @param string $path |
||
| 521 | * @return string |
||
| 522 | */ |
||
| 523 | public |
||
| 542 | |||
| 543 | /** |
||
| 544 | * Return the base url (without filename) for the passed attribute. |
||
| 545 | * Returns empty string if dir if not exists. |
||
| 546 | * Ex.: http://localhost/laravel/public/upload/ |
||
| 547 | * @param string $uploadField |
||
| 548 | * @return string |
||
| 549 | */ |
||
| 550 | View Code Duplication | public |
|
| 565 | |||
| 566 | /** |
||
| 567 | * Calcolate the new name for ALL uploaded files and set relative upload attributes |
||
| 568 | */ |
||
| 569 | public |
||
| 576 | |||
| 577 | /** |
||
| 578 | * Calcolate the new name for uploaded file relative to passed attribute name and set the upload attribute |
||
| 579 | * @param string $uploadField |
||
| 580 | */ |
||
| 581 | public |
||
| 602 | |||
| 603 | /** |
||
| 604 | * @param string $uploadField |
||
| 605 | * @param string $newName |
||
| 606 | */ |
||
| 607 | public |
||
| 619 | |||
| 620 | /** |
||
| 621 | * @param string $path |
||
| 622 | * @return bool |
||
| 623 | */ |
||
| 624 | public |
||
| 631 | |||
| 632 | /** |
||
| 633 | * Check if all uploadedFields are changed, so there is an old value |
||
| 634 | * for attribute and if true try to unlink old file. |
||
| 635 | */ |
||
| 636 | public |
||
| 643 | |||
| 644 | /** |
||
| 645 | * Check if $uploadField are changed, so there is an old value |
||
| 646 | * for $uploadField attribute and if true try to unlink old file. |
||
| 647 | * @param string $uploadField |
||
| 648 | */ |
||
| 649 | public |
||
| 665 | } |
||
| 666 |
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
Idableprovides a methodequalsIdthat 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.