Complex classes like FileUpload 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 FileUpload, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
21 | class FileUpload extends RequestPlugin |
||
22 | { |
||
23 | use \Jaxon\Utils\Traits\Validator; |
||
24 | use \Jaxon\Utils\Traits\Translator; |
||
25 | |||
26 | /** |
||
27 | * The uploaded files copied in the user dir |
||
28 | * |
||
29 | * @var array |
||
30 | */ |
||
31 | protected $aUserFiles; |
||
32 | |||
33 | /** |
||
34 | * The name of file containing upload data |
||
35 | * |
||
36 | * @var string |
||
37 | */ |
||
38 | protected $sTempFile = ''; |
||
39 | |||
40 | /** |
||
41 | * The subdir where uploaded files are stored |
||
42 | * |
||
43 | * @var string |
||
44 | */ |
||
45 | protected $sUploadSubdir = ''; |
||
46 | |||
47 | /** |
||
48 | * A user defined function to transform uploaded file names |
||
49 | * |
||
50 | * @var Closure |
||
51 | */ |
||
52 | protected $fFileFilter = null; |
||
53 | |||
54 | /** |
||
55 | * Read uploaded files info from the $_FILES global var |
||
56 | */ |
||
57 | public function __construct() |
||
71 | |||
72 | /** |
||
73 | * Filter uploaded file name |
||
74 | * |
||
75 | * @param Closure $fFileFilter The closure which filters filenames |
||
76 | * |
||
77 | * @return void |
||
78 | */ |
||
79 | public function setFileFilter(Closure $fFileFilter) |
||
83 | |||
84 | /** |
||
85 | * Filter uploaded file name |
||
86 | * |
||
87 | * @param string $sFilename The filename |
||
88 | * @param string $sVarName The associated variable name |
||
89 | * |
||
90 | * @return string |
||
91 | */ |
||
92 | protected function filterFilename($sFilename, $sVarName) |
||
101 | |||
102 | /** |
||
103 | * Get the path to the upload dir |
||
104 | * |
||
105 | * @param string $sFieldId The filename |
||
106 | * |
||
107 | * @return string |
||
108 | */ |
||
109 | protected function getUploadDir($sFieldId) |
||
124 | |||
125 | /** |
||
126 | * Get the path to the upload temp dir |
||
127 | * |
||
128 | * @return string |
||
129 | */ |
||
130 | protected function getUploadTempDir() |
||
144 | |||
145 | /** |
||
146 | * Get the path to the upload temp file |
||
147 | * |
||
148 | * @return string |
||
149 | */ |
||
150 | protected function getUploadTempFile() |
||
162 | |||
163 | /** |
||
164 | * Read uploaded files info from HTTP request data |
||
165 | * |
||
166 | * @return void |
||
167 | */ |
||
168 | protected function readFromHttpData() |
||
261 | |||
262 | /** |
||
263 | * Save uploaded files info to a temp file |
||
264 | * |
||
265 | * @return void |
||
266 | */ |
||
267 | protected function saveToTempFile() |
||
284 | |||
285 | /** |
||
286 | * Read uploaded files info from a temp file |
||
287 | * |
||
288 | * @return void |
||
289 | */ |
||
290 | protected function readFromTempFile() |
||
305 | |||
306 | /** |
||
307 | * Return the name of this plugin |
||
308 | * |
||
309 | * @return string |
||
310 | */ |
||
311 | public function getName() |
||
315 | |||
316 | /** |
||
317 | * Get the uploaded files |
||
318 | * |
||
319 | * @return array |
||
320 | */ |
||
321 | public function getUploadedFiles() |
||
325 | |||
326 | /** |
||
327 | * Register a browser event |
||
328 | * |
||
329 | * @param array $aArgs An array containing the event specification |
||
330 | * |
||
331 | * @return \Jaxon\Request\Request |
||
332 | */ |
||
333 | public function register($aArgs) |
||
337 | |||
338 | /** |
||
339 | * Generate a hash for the registered browser events |
||
340 | * |
||
341 | * @return string |
||
342 | */ |
||
343 | public function generateHash() |
||
347 | |||
348 | /** |
||
349 | * Generate client side javascript code for the registered browser events |
||
350 | * |
||
351 | * @return string |
||
352 | */ |
||
353 | public function getScript() |
||
357 | |||
358 | /** |
||
359 | * Check if this plugin can process the incoming Jaxon request |
||
360 | * |
||
361 | * @return boolean |
||
362 | */ |
||
363 | public function canProcessRequest() |
||
367 | |||
368 | /** |
||
369 | * Process the uploaded files into the HTTP request |
||
370 | * |
||
371 | * @return boolean |
||
372 | */ |
||
373 | public function processRequest() |
||
389 | |||
390 | /** |
||
391 | * Check uploaded files validity and move them to the user dir |
||
392 | * |
||
393 | * @return boolean |
||
394 | */ |
||
395 | public function saveUploadedFiles() |
||
406 | } |
||
407 |
If you suppress an error, we recommend checking for the error condition explicitly: