Complex classes like UploadFileBag 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 UploadFileBag, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
24 | class UploadFileBag extends ParameterBag |
||
25 | { |
||
26 | use FileTrait; |
||
27 | |||
28 | /** |
||
29 | * Global accepted file mime types. |
||
30 | * |
||
31 | * @var AcceptedMimeType |
||
32 | */ |
||
33 | public static $acceptedMimeTypes; |
||
34 | |||
35 | /** |
||
36 | * Upload file keys mapping. |
||
37 | * |
||
38 | * @var array |
||
39 | */ |
||
40 | private static $fileKeys = array('error', 'name', 'size', 'tmp_name', 'type'); |
||
41 | |||
42 | /** |
||
43 | * @var bool |
||
44 | */ |
||
45 | private $tested; |
||
46 | |||
47 | /** |
||
48 | * Constructor. |
||
49 | * |
||
50 | * @param array $files |
||
51 | * @param bool $tested |
||
52 | */ |
||
53 | public function __construct(array $files = array(), $tested = false) |
||
62 | |||
63 | /** |
||
64 | * Clear mime types and replace with sets of it. |
||
65 | * |
||
66 | * @param array $mimeTypes |
||
67 | * |
||
68 | * @return $this |
||
69 | */ |
||
70 | public function replaceAcceptedMimeTypes(array $mimeTypes) |
||
79 | |||
80 | /** |
||
81 | * Add sets of mime type. |
||
82 | * |
||
83 | * @param array $mimeTypes |
||
84 | * |
||
85 | * @return $this |
||
86 | */ |
||
87 | public function addAcceptedMimeTypes(array $mimeTypes) |
||
95 | |||
96 | /** |
||
97 | * Set mime type to all children. |
||
98 | * |
||
99 | * @param string $mimeType |
||
100 | * |
||
101 | * @return $this |
||
102 | */ |
||
103 | public function setAcceptedMimeType($mimeType) |
||
114 | |||
115 | /** |
||
116 | * Get all accepted mime types. |
||
117 | * |
||
118 | * @return array |
||
119 | */ |
||
120 | public function allAcceptedMimeTypes() |
||
124 | |||
125 | /** |
||
126 | * Check if specified mime type is accepted. |
||
127 | * |
||
128 | * @param string $mimeType |
||
129 | * |
||
130 | * @return bool |
||
131 | */ |
||
132 | public function isMimeTypeAccepted($mimeType) |
||
141 | |||
142 | /** |
||
143 | * {@inheritdoc} |
||
144 | */ |
||
145 | public function set($key, $value) |
||
157 | |||
158 | /** |
||
159 | * Check if file is valid to all composite children. |
||
160 | * |
||
161 | * @return bool |
||
162 | */ |
||
163 | public function isValid() |
||
177 | |||
178 | /** |
||
179 | * Execute closure function to all composite files children. |
||
180 | * |
||
181 | * Files will be deleted if exception. |
||
182 | * |
||
183 | * @param Closure $closure |
||
184 | * |
||
185 | * @return $this |
||
186 | */ |
||
187 | public function executeInSafeMode(Closure $closure) |
||
209 | |||
210 | /** |
||
211 | * Move all composite files children to new location. |
||
212 | * |
||
213 | * Return new UploadFileBag with files that uploaded. |
||
214 | * |
||
215 | * @param string $directory New folder location. |
||
216 | * |
||
217 | * @return $this |
||
218 | */ |
||
219 | public function move($directory) |
||
225 | |||
226 | /** |
||
227 | * Copy all composite files children to new location. |
||
228 | * |
||
229 | * Return new UploadedFileBag with files that copied. |
||
230 | * |
||
231 | * @param string $directory |
||
232 | * |
||
233 | * @return $this |
||
234 | */ |
||
235 | public function copy($directory) |
||
241 | |||
242 | /** |
||
243 | * Remove all composite files children. |
||
244 | * |
||
245 | * @return $this |
||
246 | */ |
||
247 | public function deleteAll() |
||
255 | |||
256 | /** |
||
257 | * Get all composite children error message. |
||
258 | * |
||
259 | * @return array |
||
260 | */ |
||
261 | public function getErrorMessage() |
||
275 | |||
276 | /** |
||
277 | * Walk into all file. |
||
278 | * |
||
279 | * @param Closure $callback |
||
280 | */ |
||
281 | public function walkIn(Closure $callback) |
||
285 | |||
286 | /** |
||
287 | * Convert file information to UploadedFile or sets of it. |
||
288 | * |
||
289 | * @param UploadedFile|array $fileInfo |
||
290 | * |
||
291 | * @return UploadedFile|UploadedFile[] |
||
292 | */ |
||
293 | protected function convertToFile($fileInfo) |
||
313 | |||
314 | /** |
||
315 | * Create UploadedFile from array file info. |
||
316 | * |
||
317 | * @param array $fileInfo |
||
318 | * |
||
319 | * @return UploadedFile|null |
||
320 | */ |
||
321 | protected function createFileFromArray(array $fileInfo) |
||
332 | |||
333 | /** |
||
334 | * Normalize nested $_FILES array. |
||
335 | * |
||
336 | * @param array $fileInfo |
||
337 | * |
||
338 | * @return array |
||
339 | */ |
||
340 | protected function normalizeFilesArray(array $fileInfo) |
||
360 | |||
361 | /** |
||
362 | * Collecting data. |
||
363 | * |
||
364 | * @param array $array |
||
365 | * @param mixed $data |
||
366 | * @param string|int $key |
||
367 | * @param string|int|null $parentKey |
||
368 | */ |
||
369 | private static function collectData(array &$array, $data, $key, $parentKey = null) |
||
385 | |||
386 | /** |
||
387 | * Check if specified file is file upload. |
||
388 | * |
||
389 | * @param array $fileInfo |
||
390 | * |
||
391 | * @return bool |
||
392 | */ |
||
393 | private static function isFileUpload(array $fileInfo) |
||
400 | |||
401 | /** |
||
402 | * @param string $directory |
||
403 | * @param Closure $caller |
||
404 | * |
||
405 | * @return $this |
||
406 | */ |
||
407 | private function createDirectoryAndCall($directory, Closure $caller) |
||
423 | |||
424 | /** |
||
425 | * Walk in recursively. |
||
426 | * |
||
427 | * @param Closure $callback |
||
428 | * @param array $data |
||
429 | * @param mixed|null $parentKey |
||
430 | */ |
||
431 | private function walkInRecursively(Closure $callback, array $data, $parentKey = null) |
||
443 | } |
||
444 |