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 |
||
17 | trait Uploadable |
||
18 | { |
||
19 | /** @var UploadOptions */ |
||
20 | protected $uploadOptions; |
||
21 | |||
22 | /** |
||
23 | * Boot the trait. |
||
24 | */ |
||
25 | public static function bootUploadable() |
||
60 | |||
61 | /** |
||
62 | * Retrive a specifice UploadOptions for this model, or return default UploadOptions |
||
63 | * @return UploadOptions |
||
64 | */ |
||
65 | public function getUploadOptionsOrDefault() : UploadOptions |
||
75 | |||
76 | /** |
||
77 | * This function will throw an exception when any of the options is missing or invalid. |
||
78 | * @throws InvalidOption |
||
79 | */ |
||
80 | public function guardAgainstInvalidUploadOptions() |
||
89 | |||
90 | /** |
||
91 | * Handle file upload. |
||
92 | */ |
||
93 | public function uploadFiles() |
||
115 | |||
116 | /** |
||
117 | * Upload a file releted to a passed attribute name |
||
118 | * @param string $uploadField |
||
119 | */ |
||
120 | public function uploadFile(string $uploadField) |
||
131 | |||
132 | /** |
||
133 | * Get an UploadedFile, generate new name, and save it in destination path. |
||
134 | * Return empty string if it fails, otherwise return the saved file name. |
||
135 | * @param UploadedFile $uploadedFile |
||
136 | * @param string $uploadAttribute |
||
137 | * @return string |
||
138 | */ |
||
139 | public function doUpload(UploadedFile $uploadedFile, $uploadAttribute) : string |
||
168 | |||
169 | /** |
||
170 | * Generate a new file name for uploaded file. |
||
171 | * Return empty string if uploadedFile is null, otherwise return the new file name.. |
||
172 | * @param UploadedFile $uploadedFile |
||
173 | * @param string $uploadField |
||
174 | * @return string |
||
175 | */ |
||
176 | public function generateNewUploadFileName(UploadedFile $uploadedFile, string $uploadField) : string |
||
194 | |||
195 | /** |
||
196 | * Check if file need a new name and return it, otherwise return empty string. |
||
197 | * @param UploadedFile $uploadedFile |
||
198 | * @return string |
||
199 | */ |
||
200 | protected function calcolateNewUploadFileName(UploadedFile $uploadedFile) : string |
||
213 | |||
214 | /** |
||
215 | * delete all Uploaded Files |
||
216 | */ |
||
217 | public function deleteUploadedFiles() |
||
224 | |||
225 | /** |
||
226 | * Delete upload file related to passed attribute name |
||
227 | * @param string $uploadField |
||
228 | */ |
||
229 | public function deleteUploadedFile(string $uploadField) |
||
251 | |||
252 | /** |
||
253 | * Reset model attribute and update db field |
||
254 | * @param string $uploadField |
||
255 | */ |
||
256 | protected function setBlanckAttributeAndDB(string $uploadField) |
||
266 | |||
267 | /** |
||
268 | * Return true If All Upload atrributes Are Empty or |
||
269 | * if the uploads array is not set. |
||
270 | * @return bool |
||
271 | */ |
||
272 | public function checkIfAllUploadFieldsAreEmpty() : bool |
||
283 | |||
284 | /** |
||
285 | * Check all attributes upload path, and try to create dir if not already exists. |
||
286 | * Return false if it fails to create all founded dirs. |
||
287 | * @return bool |
||
288 | */ |
||
289 | public function checkOrCreateAllUploadBasePaths() : bool |
||
299 | |||
300 | /** |
||
301 | * Check uploads property and return a uploads class field |
||
302 | * or empty array if somethings wrong. |
||
303 | * @return array |
||
304 | */ |
||
305 | public function getUploadsAttributesSafe() : array |
||
313 | |||
314 | /** |
||
315 | * Check attribute upload path, and try to create dir if not already exists. |
||
316 | * Return false if it fails to create the dir. |
||
317 | * @param string $uploadField |
||
318 | * @return bool |
||
319 | */ |
||
320 | public function checkOrCreateUploadBasePath(string $uploadField) : bool |
||
326 | |||
327 | /** |
||
328 | * Return the upload path for the passed attribute and try to create it if not exists. |
||
329 | * Returns empty string if dir if not exists and fails to create it. |
||
330 | * @param string $uploadField |
||
331 | * @return string |
||
332 | */ |
||
333 | public function getUploadFilePath(string $uploadField) : string |
||
351 | |||
352 | /** |
||
353 | * Return the specific upload path (by uploadPaths prop) for the passed attribute if exists. |
||
354 | * @param string $uploadField |
||
355 | * @return string |
||
356 | */ |
||
357 | public function getUploadFilePathSpecific(string $uploadField) : string |
||
368 | |||
369 | /** |
||
370 | * Calcolate the new name for ALL uploaded files and set relative upload attributes |
||
371 | */ |
||
372 | public function generateAllNewUploadFileNameAndSetAttribute() |
||
378 | |||
379 | /** |
||
380 | * Calcolate the new name for uploaded file relative to passed attribute name and set the upload attribute |
||
381 | * @param string $uploadField |
||
382 | */ |
||
383 | public function generateNewUploadFileNameAndSetAttribute(string $uploadField) |
||
399 | } |
||
400 |
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.