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 | * Boot the trait. |
||
27 | */ |
||
28 | public static function bootUploadable() |
||
63 | |||
64 | /** |
||
65 | * Retrive a specifice UploadOptions for this model, or return default UploadOptions |
||
66 | * @return UploadOptions |
||
67 | */ |
||
68 | public function getUploadOptionsOrDefault() : UploadOptions |
||
84 | |||
85 | /** |
||
86 | * This function will throw an exception when any of the options is missing or invalid. |
||
87 | * @throws InvalidOption |
||
88 | */ |
||
89 | public function guardAgainstInvalidUploadOptions() |
||
98 | |||
99 | /** |
||
100 | * Handle file upload. |
||
101 | */ |
||
102 | public function uploadFiles() |
||
119 | |||
120 | /** |
||
121 | * Upload a file releted to a passed attribute name |
||
122 | * @param string $uploadField |
||
123 | */ |
||
124 | public function uploadFile(string $uploadField) |
||
142 | |||
143 | /** |
||
144 | * Get an UploadedFile, generate new name, and save it in destination path. |
||
145 | * Return empty string if it fails, otherwise return the saved file name. |
||
146 | * @param UploadedFile $uploadedFile |
||
147 | * @param string $uploadAttribute |
||
148 | * @return string |
||
149 | */ |
||
150 | public function doUpload(UploadedFile $uploadedFile, $uploadAttribute) : string |
||
175 | |||
176 | /** |
||
177 | * Check request for valid files and server for correct paths defined in model |
||
178 | * @return bool |
||
179 | */ |
||
180 | protected function requestHasValidFilesAndCorrectPaths() : bool |
||
194 | |||
195 | /** |
||
196 | * Generate a new file name for uploaded file. |
||
197 | * Return empty string if uploadedFile is null, otherwise return the new file name.. |
||
198 | * @param UploadedFile $uploadedFile |
||
199 | * @param string $uploadField |
||
200 | * @return string |
||
201 | */ |
||
202 | public function generateNewUploadFileName(UploadedFile $uploadedFile, string $uploadField) : string |
||
220 | |||
221 | /** |
||
222 | * Check if file need a new name and return it, otherwise return empty string. |
||
223 | * @param UploadedFile $uploadedFile |
||
224 | * @return string |
||
225 | */ |
||
226 | protected function calcolateNewUploadFileName(UploadedFile $uploadedFile) : string |
||
239 | |||
240 | /** |
||
241 | * delete all Uploaded Files |
||
242 | */ |
||
243 | public function deleteUploadedFiles() |
||
250 | |||
251 | /** |
||
252 | * Delete upload file related to passed attribute name |
||
253 | * @param string $uploadField |
||
254 | */ |
||
255 | public function deleteUploadedFile(string $uploadField) |
||
275 | |||
276 | /** |
||
277 | * Reset model attribute and update db field |
||
278 | * @param string $uploadField |
||
279 | */ |
||
280 | protected function setBlanckAttributeAndDB(string $uploadField) |
||
290 | |||
291 | /** |
||
292 | * Return true If All Upload atrributes Are Empty or |
||
293 | * if the uploads array is not set. |
||
294 | * @return bool |
||
295 | */ |
||
296 | public function checkIfAllUploadFieldsAreEmpty() : bool |
||
307 | |||
308 | /** |
||
309 | * Check all attributes upload path, and try to create dir if not already exists. |
||
310 | * Return false if it fails to create all founded dirs. |
||
311 | * @return bool |
||
312 | */ |
||
313 | public function checkOrCreateAllUploadBasePaths() : bool |
||
323 | |||
324 | /** |
||
325 | * Check uploads property and return a uploads class field |
||
326 | * or empty array if somethings wrong. |
||
327 | * @return array |
||
328 | */ |
||
329 | public function getUploadsAttributesSafe() : array |
||
337 | |||
338 | /** |
||
339 | * Check attribute upload path, and try to create dir if not already exists. |
||
340 | * Return false if it fails to create the dir. |
||
341 | * @param string $uploadField |
||
342 | * @return bool |
||
343 | */ |
||
344 | public function checkOrCreateUploadBasePath(string $uploadField) : bool |
||
351 | |||
352 | /** |
||
353 | * Return the upload path for the passed attribute and try to create it if not exists. |
||
354 | * Returns empty string if dir if not exists and fails to create it. |
||
355 | * @param string $uploadField |
||
356 | * @return string |
||
357 | */ |
||
358 | public function getUploadFileBasePath(string $uploadField) : string |
||
378 | |||
379 | /** |
||
380 | * Return the specific upload path (by uploadPaths prop) for the passed attribute if exists, otherwise return empty string. |
||
381 | * @param string $uploadField |
||
382 | * @return string |
||
383 | */ |
||
384 | public function getUploadFileBasePathSpecific(string $uploadField) : string |
||
395 | |||
396 | /** |
||
397 | * Return the full (path+filename) upload abs path for the passed attribute. |
||
398 | * Returns empty string if dir if not exists. |
||
399 | * @param string $uploadField |
||
400 | * @return string |
||
401 | */ |
||
402 | public function getUploadFileFullPath(string $uploadField) : string |
||
413 | |||
414 | /** |
||
415 | * Return the full url (base url + filename) for the passed attribute. |
||
416 | * Returns empty string if dir if not exists. |
||
417 | * Ex.: http://localhost/laravel/public/upload/news/pippo.jpg |
||
418 | * @param string $uploadField |
||
419 | * @return string |
||
420 | */ |
||
421 | public function getUploadFileUrl(string $uploadField) : string |
||
429 | |||
430 | /** |
||
431 | * get a path and remove public_path. |
||
432 | * @param string $path |
||
433 | * @return string |
||
434 | */ |
||
435 | public function removePublicPath(string $path) : string |
||
448 | |||
449 | /** |
||
450 | * Return the base url (without filename) for the passed attribute. |
||
451 | * Returns empty string if dir if not exists. |
||
452 | * Ex.: http://localhost/laravel/public/upload/ |
||
453 | * @param string $uploadField |
||
454 | * @return string |
||
455 | */ |
||
456 | public function getUploadFileBaseUrl(string $uploadField) : string |
||
468 | |||
469 | /** |
||
470 | * Calcolate the new name for ALL uploaded files and set relative upload attributes |
||
471 | */ |
||
472 | public function generateAllNewUploadFileNameAndSetAttribute() |
||
478 | |||
479 | /** |
||
480 | * Calcolate the new name for uploaded file relative to passed attribute name and set the upload attribute |
||
481 | * @param string $uploadField |
||
482 | */ |
||
483 | public function generateNewUploadFileNameAndSetAttribute(string $uploadField) |
||
502 | } |
||
503 |
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.