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 |
||
| 20 | trait Uploadable |
||
| 21 | { |
||
| 22 | /** @var UploadOptions */ |
||
| 23 | protected $uploadOptions; |
||
| 24 | |||
| 25 | /** |
||
| 26 | * Determine if the model or given attribute(s) have been modified. |
||
| 27 | * Implemented in \Illuminate\Database\Eloquent\Model |
||
| 28 | * |
||
| 29 | * @param array|string|null $attributes = null |
||
| 30 | * |
||
| 31 | * @return bool |
||
| 32 | */ |
||
| 33 | abstract public function isDirty($attributes = null); |
||
| 34 | |||
| 35 | /** |
||
| 36 | * Get the model's original attribute values. |
||
| 37 | * Implemented in \Illuminate\Database\Eloquent\Model |
||
| 38 | * |
||
| 39 | * @param string|null $key = null |
||
| 40 | * @param mixed $default = null |
||
| 41 | * |
||
| 42 | * @return mixed|array |
||
| 43 | */ |
||
| 44 | abstract public function getOriginal($key = null, $default = null); |
||
| 45 | |||
| 46 | /** |
||
| 47 | * Boot the trait. |
||
| 48 | */ |
||
| 49 | public static function bootUploadable() |
||
| 56 | |||
| 57 | /** |
||
| 58 | * Bind create model events. |
||
| 59 | */ |
||
| 60 | protected static function bindCreateEvents() |
||
| 67 | |||
| 68 | /** |
||
| 69 | * Bind save model events. |
||
| 70 | */ |
||
| 71 | protected static function bindSaveEvents() |
||
| 77 | |||
| 78 | /** |
||
| 79 | * Bind update model events. |
||
| 80 | */ |
||
| 81 | protected static function bindUpdateEvents() |
||
| 88 | |||
| 89 | /** |
||
| 90 | * Bind delete model events. |
||
| 91 | */ |
||
| 92 | protected static function bindDeleteEvents() |
||
| 105 | |||
| 106 | /** |
||
| 107 | * Retrive a specifice UploadOptions for this model, or return default UploadOptions |
||
| 108 | * @return UploadOptions |
||
| 109 | */ |
||
| 110 | public function getUploadOptionsOrDefault() : UploadOptions |
||
| 126 | |||
| 127 | /** |
||
| 128 | * This function will throw an exception when any of the options is missing or invalid. |
||
| 129 | * @throws InvalidOption |
||
| 130 | */ |
||
| 131 | public function guardAgainstInvalidUploadOptions() |
||
| 140 | |||
| 141 | /** |
||
| 142 | * Handle file upload. |
||
| 143 | */ |
||
| 144 | 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) |
||
| 185 | |||
| 186 | /** |
||
| 187 | * Get an UploadedFile, generate new name, and save it in destination path. |
||
| 188 | * Return empty string if it fails, otherwise return the saved file name. |
||
| 189 | * @param UploadedFile $uploadedFile |
||
| 190 | * @param string $uploadAttribute |
||
| 191 | * @return string |
||
| 192 | */ |
||
| 193 | public function doUpload(UploadedFile $uploadedFile, $uploadAttribute) : string |
||
| 218 | |||
| 219 | /** |
||
| 220 | * Check request for valid files and server for correct paths defined in model |
||
| 221 | * @return bool |
||
| 222 | */ |
||
| 223 | public function requestHasValidFilesAndCorrectPaths() : bool |
||
| 237 | |||
| 238 | /** |
||
| 239 | * Generate a new file name for uploaded file. |
||
| 240 | * Return empty string if uploadedFile is null, otherwise return the new file name.. |
||
| 241 | * @param UploadedFile $uploadedFile |
||
| 242 | * @return string |
||
| 243 | * @internal param string $uploadField |
||
| 244 | */ |
||
| 245 | public function generateNewUploadFileName(UploadedFile $uploadedFile) : string |
||
| 260 | |||
| 261 | /** |
||
| 262 | * Check if file need a new name and return it, otherwise return empty string. |
||
| 263 | * @param UploadedFile $uploadedFile |
||
| 264 | * @return string |
||
| 265 | */ |
||
| 266 | public function calcolateNewUploadFileName(UploadedFile $uploadedFile) : string |
||
| 279 | |||
| 280 | /** |
||
| 281 | * delete all Uploaded Files |
||
| 282 | */ |
||
| 283 | public function deleteUploadedFiles() |
||
| 290 | |||
| 291 | /** |
||
| 292 | * Delete upload file related to passed attribute name |
||
| 293 | * @param string $uploadField |
||
| 294 | */ |
||
| 295 | public function deleteUploadedFile(string $uploadField) |
||
| 315 | |||
| 316 | /** |
||
| 317 | * Reset model attribute and update db field |
||
| 318 | * @param string $uploadField |
||
| 319 | */ |
||
| 320 | public function setBlanckAttributeAndDB(string $uploadField) |
||
| 332 | |||
| 333 | /** |
||
| 334 | * Return true If All Upload atrributes Are Empty or |
||
| 335 | * if the uploads array is not set. |
||
| 336 | * @return bool |
||
| 337 | */ |
||
| 338 | public function checkIfAllUploadFieldsAreEmpty() : bool |
||
| 349 | |||
| 350 | /** |
||
| 351 | * Check all attributes upload path, and try to create dir if not already exists. |
||
| 352 | * Return false if it fails to create all founded dirs. |
||
| 353 | * @return bool |
||
| 354 | */ |
||
| 355 | public function checkOrCreateAllUploadBasePaths() : bool |
||
| 365 | |||
| 366 | /** |
||
| 367 | * Check uploads property and return a uploads class field |
||
| 368 | * or empty array if somethings wrong. |
||
| 369 | * @return array |
||
| 370 | */ |
||
| 371 | public function getUploadsAttributesSafe() : array |
||
| 379 | |||
| 380 | /** |
||
| 381 | * Check attribute upload path, and try to create dir if not already exists. |
||
| 382 | * Return false if it fails to create the dir. |
||
| 383 | * @param string $uploadField |
||
| 384 | * @return bool |
||
| 385 | */ |
||
| 386 | public function checkOrCreateUploadBasePath(string $uploadField) : bool |
||
| 393 | |||
| 394 | /** |
||
| 395 | * Return the upload path for the passed attribute and try to create it if not exists. |
||
| 396 | * Returns empty string if dir if not exists and fails to create it. |
||
| 397 | * @param string $uploadField |
||
| 398 | * @return string |
||
| 399 | */ |
||
| 400 | public function getUploadFileBasePath(string $uploadField) : string |
||
| 420 | |||
| 421 | /** |
||
| 422 | * Return the specific upload path (by uploadPaths prop) for the passed attribute if exists, |
||
| 423 | * otherwise return empty string. |
||
| 424 | * If uploadPaths for this field is relative, a public_path was appended. |
||
| 425 | * @param string $uploadField |
||
| 426 | * @return string |
||
| 427 | */ |
||
| 428 | public function getUploadFileBasePathSpecific(string $uploadField) : string |
||
| 444 | |||
| 445 | /** |
||
| 446 | * Check if there is a specified upload path setted for this field |
||
| 447 | * @param string $uploadField |
||
| 448 | * @return bool |
||
| 449 | */ |
||
| 450 | public function hasUploadPathSpecifiedForThisField(string $uploadField) : bool |
||
| 457 | |||
| 458 | /** |
||
| 459 | * Return the full (path+filename) upload abs path for the passed attribute. |
||
| 460 | * Returns empty string if dir if not exists. |
||
| 461 | * @param string $uploadField |
||
| 462 | * @return string |
||
| 463 | */ |
||
| 464 | public |
||
| 478 | |||
| 479 | /** |
||
| 480 | * Return the full url (base url + filename) for the passed attribute. |
||
| 481 | * Returns empty string if dir if not exists. |
||
| 482 | * Ex.: http://localhost/laravel/public/upload/news/pippo.jpg |
||
| 483 | * @param string $uploadField |
||
| 484 | * @return string |
||
| 485 | */ |
||
| 486 | public |
||
| 497 | |||
| 498 | /** |
||
| 499 | * get a path and remove public_path. |
||
| 500 | * @param string $path |
||
| 501 | * @return string |
||
| 502 | */ |
||
| 503 | public |
||
| 518 | |||
| 519 | /** |
||
| 520 | * Return the base url (without filename) for the passed attribute. |
||
| 521 | * Returns empty string if dir if not exists. |
||
| 522 | * Ex.: http://localhost/laravel/public/upload/ |
||
| 523 | * @param string $uploadField |
||
| 524 | * @return string |
||
| 525 | */ |
||
| 526 | public |
||
| 541 | |||
| 542 | /** |
||
| 543 | * Calcolate the new name for ALL uploaded files and set relative upload attributes |
||
| 544 | */ |
||
| 545 | public |
||
| 552 | |||
| 553 | /** |
||
| 554 | * Calcolate the new name for uploaded file relative to passed attribute name and set the upload attribute |
||
| 555 | * @param string $uploadField |
||
| 556 | */ |
||
| 557 | public |
||
| 578 | |||
| 579 | /** |
||
| 580 | * @param string $uploadField |
||
| 581 | * @param string $newName |
||
| 582 | */ |
||
| 583 | public |
||
| 595 | |||
| 596 | /** |
||
| 597 | * @param string $path |
||
| 598 | * @return bool |
||
| 599 | */ |
||
| 600 | public |
||
| 607 | |||
| 608 | /** |
||
| 609 | * Check if all uploadedFields are changed, so there is an old value |
||
| 610 | * for attribute and if true try to unlink old file. |
||
| 611 | */ |
||
| 612 | public |
||
| 619 | |||
| 620 | /** |
||
| 621 | * Check if $uploadField are changed, so there is an old value |
||
| 622 | * for $uploadField attribute and if true try to unlink old file. |
||
| 623 | * @param string $uploadField |
||
| 624 | */ |
||
| 625 | public |
||
| 640 | } |
||
| 641 |
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.