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) |
||
127 | |||
128 | /** |
||
129 | * Get an UploadedFile, generate new name, and save it in destination path. |
||
130 | * Return empty string if it fails, otherwise return the saved file name. |
||
131 | * @param UploadedFile $uploadedFile |
||
132 | * @param string $uploadAttribute |
||
133 | * @return string |
||
134 | */ |
||
135 | public function doUpload(UploadedFile $uploadedFile, $uploadAttribute) : string |
||
160 | |||
161 | /** |
||
162 | * Check request for valid files and server for correct paths defined in model |
||
163 | * @return bool |
||
164 | */ |
||
165 | protected function requestHasValidFilesAndCorrectPaths() : bool |
||
179 | |||
180 | /** |
||
181 | * Generate a new file name for uploaded file. |
||
182 | * Return empty string if uploadedFile is null, otherwise return the new file name.. |
||
183 | * @param UploadedFile $uploadedFile |
||
184 | * @param string $uploadField |
||
185 | * @return string |
||
186 | */ |
||
187 | public function generateNewUploadFileName(UploadedFile $uploadedFile, string $uploadField) : string |
||
205 | |||
206 | /** |
||
207 | * Check if file need a new name and return it, otherwise return empty string. |
||
208 | * @param UploadedFile $uploadedFile |
||
209 | * @return string |
||
210 | */ |
||
211 | protected function calcolateNewUploadFileName(UploadedFile $uploadedFile) : string |
||
224 | |||
225 | /** |
||
226 | * delete all Uploaded Files |
||
227 | */ |
||
228 | public function deleteUploadedFiles() |
||
235 | |||
236 | /** |
||
237 | * Delete upload file related to passed attribute name |
||
238 | * @param string $uploadField |
||
239 | */ |
||
240 | public function deleteUploadedFile(string $uploadField) |
||
262 | |||
263 | /** |
||
264 | * Reset model attribute and update db field |
||
265 | * @param string $uploadField |
||
266 | */ |
||
267 | protected function setBlanckAttributeAndDB(string $uploadField) |
||
277 | |||
278 | /** |
||
279 | * Return true If All Upload atrributes Are Empty or |
||
280 | * if the uploads array is not set. |
||
281 | * @return bool |
||
282 | */ |
||
283 | public function checkIfAllUploadFieldsAreEmpty() : bool |
||
294 | |||
295 | /** |
||
296 | * Check all attributes upload path, and try to create dir if not already exists. |
||
297 | * Return false if it fails to create all founded dirs. |
||
298 | * @return bool |
||
299 | */ |
||
300 | public function checkOrCreateAllUploadBasePaths() : bool |
||
310 | |||
311 | /** |
||
312 | * Check uploads property and return a uploads class field |
||
313 | * or empty array if somethings wrong. |
||
314 | * @return array |
||
315 | */ |
||
316 | public function getUploadsAttributesSafe() : array |
||
324 | |||
325 | /** |
||
326 | * Check attribute upload path, and try to create dir if not already exists. |
||
327 | * Return false if it fails to create the dir. |
||
328 | * @param string $uploadField |
||
329 | * @return bool |
||
330 | */ |
||
331 | public function checkOrCreateUploadBasePath(string $uploadField) : bool |
||
337 | |||
338 | /** |
||
339 | * Return the upload path for the passed attribute and try to create it if not exists. |
||
340 | * Returns empty string if dir if not exists and fails to create it. |
||
341 | * @param string $uploadField |
||
342 | * @return string |
||
343 | */ |
||
344 | public function getUploadFilePath(string $uploadField) : string |
||
362 | |||
363 | /** |
||
364 | * Return the specific upload path (by uploadPaths prop) for the passed attribute if exists. |
||
365 | * @param string $uploadField |
||
366 | * @return string |
||
367 | */ |
||
368 | public function getUploadFilePathSpecific(string $uploadField) : string |
||
379 | |||
380 | /** |
||
381 | * Calcolate the new name for ALL uploaded files and set relative upload attributes |
||
382 | */ |
||
383 | public function generateAllNewUploadFileNameAndSetAttribute() |
||
389 | |||
390 | /** |
||
391 | * Calcolate the new name for uploaded file relative to passed attribute name and set the upload attribute |
||
392 | * @param string $uploadField |
||
393 | */ |
||
394 | 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
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.