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 |
||
| 18 | trait Uploadable |
||
| 19 | { |
||
| 20 | /** @var UploadOptions */ |
||
| 21 | protected $uploadOptions; |
||
| 22 | |||
| 23 | /** |
||
| 24 | * Boot the trait. |
||
| 25 | */ |
||
| 26 | public static function bootUploadable() |
||
| 61 | |||
| 62 | /** |
||
| 63 | * Retrive a specifice UploadOptions for this model, or return default UploadOptions |
||
| 64 | * @return UploadOptions |
||
| 65 | */ |
||
| 66 | public function getUploadOptionsOrDefault() : UploadOptions |
||
| 76 | |||
| 77 | /** |
||
| 78 | * This function will throw an exception when any of the options is missing or invalid. |
||
| 79 | * @throws InvalidOption |
||
| 80 | */ |
||
| 81 | public function guardAgainstInvalidUploadOptions() |
||
| 90 | |||
| 91 | /** |
||
| 92 | * Handle file upload. |
||
| 93 | */ |
||
| 94 | public function uploadFiles() |
||
| 111 | |||
| 112 | /** |
||
| 113 | * Upload a file releted to a passed attribute name |
||
| 114 | * @param string $uploadField |
||
| 115 | */ |
||
| 116 | public function uploadFile(string $uploadField) |
||
| 126 | |||
| 127 | /** |
||
| 128 | * Get an UploadedFile, generate new name, and save it in destination path. |
||
| 129 | * Return empty string if it fails, otherwise return the saved file name. |
||
| 130 | * @param UploadedFile $uploadedFile |
||
| 131 | * @param string $uploadAttribute |
||
| 132 | * @return string |
||
| 133 | */ |
||
| 134 | public function doUpload(UploadedFile $uploadedFile, $uploadAttribute) : string |
||
| 159 | |||
| 160 | /** |
||
| 161 | * Check request for valid files and server for correct paths defined in model |
||
| 162 | * @return bool |
||
| 163 | */ |
||
| 164 | protected function requestHasValidFilesAndCorrectPaths() : bool |
||
| 178 | |||
| 179 | /** |
||
| 180 | * Generate a new file name for uploaded file. |
||
| 181 | * Return empty string if uploadedFile is null, otherwise return the new file name.. |
||
| 182 | * @param UploadedFile $uploadedFile |
||
| 183 | * @param string $uploadField |
||
| 184 | * @return string |
||
| 185 | */ |
||
| 186 | public function generateNewUploadFileName(UploadedFile $uploadedFile, string $uploadField) : string |
||
| 204 | |||
| 205 | /** |
||
| 206 | * Check if file need a new name and return it, otherwise return empty string. |
||
| 207 | * @param UploadedFile $uploadedFile |
||
| 208 | * @return string |
||
| 209 | */ |
||
| 210 | protected function calcolateNewUploadFileName(UploadedFile $uploadedFile) : string |
||
| 223 | |||
| 224 | /** |
||
| 225 | * delete all Uploaded Files |
||
| 226 | */ |
||
| 227 | public function deleteUploadedFiles() |
||
| 234 | |||
| 235 | /** |
||
| 236 | * Delete upload file related to passed attribute name |
||
| 237 | * @param string $uploadField |
||
| 238 | */ |
||
| 239 | public function deleteUploadedFile(string $uploadField) |
||
| 259 | |||
| 260 | /** |
||
| 261 | * Reset model attribute and update db field |
||
| 262 | * @param string $uploadField |
||
| 263 | */ |
||
| 264 | protected function setBlanckAttributeAndDB(string $uploadField) |
||
| 274 | |||
| 275 | /** |
||
| 276 | * Return true If All Upload atrributes Are Empty or |
||
| 277 | * if the uploads array is not set. |
||
| 278 | * @return bool |
||
| 279 | */ |
||
| 280 | public function checkIfAllUploadFieldsAreEmpty() : bool |
||
| 291 | |||
| 292 | /** |
||
| 293 | * Check all attributes upload path, and try to create dir if not already exists. |
||
| 294 | * Return false if it fails to create all founded dirs. |
||
| 295 | * @return bool |
||
| 296 | */ |
||
| 297 | public function checkOrCreateAllUploadBasePaths() : bool |
||
| 307 | |||
| 308 | /** |
||
| 309 | * Check uploads property and return a uploads class field |
||
| 310 | * or empty array if somethings wrong. |
||
| 311 | * @return array |
||
| 312 | */ |
||
| 313 | public function getUploadsAttributesSafe() : array |
||
| 321 | |||
| 322 | /** |
||
| 323 | * Check attribute upload path, and try to create dir if not already exists. |
||
| 324 | * Return false if it fails to create the dir. |
||
| 325 | * @param string $uploadField |
||
| 326 | * @return bool |
||
| 327 | */ |
||
| 328 | public function checkOrCreateUploadBasePath(string $uploadField) : bool |
||
| 334 | |||
| 335 | /** |
||
| 336 | * Return the upload path for the passed attribute and try to create it if not exists. |
||
| 337 | * Returns empty string if dir if not exists and fails to create it. |
||
| 338 | * @param string $uploadField |
||
| 339 | * @return string |
||
| 340 | */ |
||
| 341 | public function getUploadFilePath(string $uploadField) : string |
||
| 359 | |||
| 360 | /** |
||
| 361 | * Return the specific upload path (by uploadPaths prop) for the passed attribute if exists. |
||
| 362 | * @param string $uploadField |
||
| 363 | * @return string |
||
| 364 | */ |
||
| 365 | public function getUploadFilePathSpecific(string $uploadField) : string |
||
| 376 | |||
| 377 | /** |
||
| 378 | * Calcolate the new name for ALL uploaded files and set relative upload attributes |
||
| 379 | */ |
||
| 380 | public function generateAllNewUploadFileNameAndSetAttribute() |
||
| 386 | |||
| 387 | /** |
||
| 388 | * Calcolate the new name for uploaded file relative to passed attribute name and set the upload attribute |
||
| 389 | * @param string $uploadField |
||
| 390 | */ |
||
| 391 | public function generateNewUploadFileNameAndSetAttribute(string $uploadField) |
||
| 410 | } |
||
| 411 |
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.