Complex classes like UploadedFile 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 UploadedFile, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
20 | final class UploadedFile extends \SplFileInfo |
||
21 | { |
||
22 | use FileTrait; |
||
23 | |||
24 | /** |
||
25 | * Error message mapping. |
||
26 | * |
||
27 | * @var array |
||
28 | */ |
||
29 | private static $errorMessages = array( |
||
30 | UPLOAD_ERR_INI_SIZE => 'The file "%s" exceeds your upload_max_filesize ini directive (limit is %d KiB).', |
||
31 | UPLOAD_ERR_FORM_SIZE => 'The file "%s" exceeds the upload limit defined in your form.', |
||
32 | UPLOAD_ERR_PARTIAL => 'The file "%s" was only partially uploaded.', |
||
33 | UPLOAD_ERR_NO_FILE => 'No file was uploaded.', |
||
34 | UPLOAD_ERR_CANT_WRITE => 'The file "%s" could not be written on disk.', |
||
35 | UPLOAD_ERR_NO_TMP_DIR => 'File could not be uploaded: missing temporary directory.', |
||
36 | UPLOAD_ERR_EXTENSION => 'File upload was stopped by a PHP extension.', |
||
37 | ); |
||
38 | |||
39 | /** |
||
40 | * @var string |
||
41 | */ |
||
42 | private $originalName; |
||
43 | |||
44 | /** |
||
45 | * @var string |
||
46 | */ |
||
47 | private $mimeType; |
||
48 | |||
49 | /** |
||
50 | * @var int |
||
51 | */ |
||
52 | private $size; |
||
53 | |||
54 | /** |
||
55 | * @var int |
||
56 | */ |
||
57 | private $error; |
||
58 | |||
59 | /** |
||
60 | * @var bool |
||
61 | */ |
||
62 | private $test; |
||
63 | |||
64 | /** |
||
65 | * @var bool |
||
66 | */ |
||
67 | private $mimeTypeValid; |
||
68 | |||
69 | /** |
||
70 | * @var AcceptedMimeType |
||
71 | */ |
||
72 | private $acceptedMimeTypes; |
||
73 | |||
74 | /** |
||
75 | * Constructor. |
||
76 | * |
||
77 | * @param string $file |
||
78 | * @param string $originalName |
||
79 | * @param string $mimeType |
||
80 | * @param int $size |
||
81 | * @param int $error |
||
82 | * @param bool $test |
||
83 | */ |
||
84 | public function __construct($file, $originalName, $mimeType = null, $size = null, $error = null, $test = false) |
||
99 | |||
100 | /** |
||
101 | * Factory create UploadedFile from array. |
||
102 | * |
||
103 | * @param array $uploadedFile |
||
104 | * |
||
105 | * @return UploadedFile |
||
106 | */ |
||
107 | public static function fromArray(array $uploadedFile) |
||
117 | |||
118 | /** |
||
119 | * Get maximum file size of an uploaded file base on php.ini |
||
120 | * |
||
121 | * @return int |
||
122 | */ |
||
123 | public static function getMaxFileSize() |
||
133 | |||
134 | /** |
||
135 | * Convert file size from string to int. |
||
136 | * |
||
137 | * @param string $iniMax |
||
138 | * @param int $max |
||
139 | * |
||
140 | * @return int |
||
141 | */ |
||
142 | private static function convertFileSize($iniMax, $max) |
||
163 | |||
164 | /** |
||
165 | * Define file size. |
||
166 | * |
||
167 | * @param string $iniMax |
||
168 | * |
||
169 | * @return int|string |
||
170 | */ |
||
171 | private static function defineFileSize($iniMax) |
||
184 | |||
185 | /** |
||
186 | * Set test state. |
||
187 | * |
||
188 | * @param bool $tested |
||
189 | * |
||
190 | * @return $this |
||
191 | */ |
||
192 | public function setTest($tested) |
||
198 | |||
199 | /** |
||
200 | * Check if file is tested. |
||
201 | * |
||
202 | * @return bool |
||
203 | */ |
||
204 | public function isTested() |
||
208 | |||
209 | /** |
||
210 | * Get client original name. |
||
211 | * |
||
212 | * @return string |
||
213 | */ |
||
214 | public function getOriginalName() |
||
218 | |||
219 | /** |
||
220 | * Get client original extension. |
||
221 | * |
||
222 | * @return string |
||
223 | */ |
||
224 | public function getOriginalExtension() |
||
228 | |||
229 | /** |
||
230 | * Get file mime type. |
||
231 | * |
||
232 | * @return string|null |
||
233 | */ |
||
234 | public function getMimeType() |
||
238 | |||
239 | /** |
||
240 | * Get file size. |
||
241 | * |
||
242 | * @return int|null |
||
243 | */ |
||
244 | public function getSize() |
||
248 | |||
249 | /** |
||
250 | * Get accepted mime type. |
||
251 | * |
||
252 | * @return AcceptedMimeType |
||
253 | */ |
||
254 | public function getAcceptedMimeType() |
||
258 | |||
259 | /** |
||
260 | * Get upload error. |
||
261 | * |
||
262 | * @return int |
||
263 | */ |
||
264 | public function getError() |
||
268 | |||
269 | /** |
||
270 | * Check file was uploaded successfully. |
||
271 | * |
||
272 | * @return bool |
||
273 | */ |
||
274 | public function isValid() |
||
284 | |||
285 | /** |
||
286 | * Move file to new location. |
||
287 | * |
||
288 | * @param string $directory Destination folder |
||
289 | * @param string|null $name New file name if needed |
||
290 | * |
||
291 | * @return UploadedFile |
||
292 | * |
||
293 | * @throws FileException |
||
294 | */ |
||
295 | public function move($directory, $name = null) |
||
321 | |||
322 | /** |
||
323 | * Copy file to new location. |
||
324 | * |
||
325 | * @param string $directory |
||
326 | * @param string|null $name |
||
327 | * |
||
328 | * @return UploadedFile |
||
329 | * |
||
330 | * @throws FileException |
||
331 | */ |
||
332 | public function copy($directory, $name = null) |
||
354 | |||
355 | /** |
||
356 | * Remove uploaded files. |
||
357 | * |
||
358 | * @return $this |
||
359 | */ |
||
360 | public function remove() |
||
376 | |||
377 | /** |
||
378 | * Get error message based on error code. |
||
379 | * |
||
380 | * @return string |
||
381 | */ |
||
382 | public function getErrorMessage() |
||
401 | |||
402 | /** |
||
403 | * Get error message format. |
||
404 | * |
||
405 | * @return string |
||
406 | */ |
||
407 | private function getMessageFormat() |
||
417 | |||
418 | /** |
||
419 | * Prepare file. |
||
420 | * |
||
421 | * @param string $directory |
||
422 | * @param string|null $name |
||
423 | * |
||
424 | * @return UploadedFile |
||
425 | */ |
||
426 | private function prepare($directory, $name = null) |
||
436 | |||
437 | /** |
||
438 | * Get file name. |
||
439 | * |
||
440 | * @param string $name |
||
441 | * |
||
442 | * @return string |
||
443 | */ |
||
444 | private function getName($name) |
||
451 | } |
||
452 |