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 |
||
| 23 | class FileUpload extends RequestPlugin |
||
| 24 | { |
||
| 25 | use \Jaxon\Features\Config; |
||
| 26 | use \Jaxon\Features\Validator; |
||
| 27 | use \Jaxon\Features\Translator; |
||
| 28 | |||
| 29 | /** |
||
| 30 | * The uploaded files copied in the user dir |
||
| 31 | * |
||
| 32 | * @var array |
||
| 33 | */ |
||
| 34 | protected $aUserFiles = []; |
||
| 35 | |||
| 36 | /** |
||
| 37 | * The name of file containing upload data |
||
| 38 | * |
||
| 39 | * @var string |
||
| 40 | */ |
||
| 41 | protected $sTempFile = ''; |
||
| 42 | |||
| 43 | /** |
||
| 44 | * The subdir where uploaded files are stored |
||
| 45 | * |
||
| 46 | * @var string |
||
| 47 | */ |
||
| 48 | protected $sUploadSubdir = ''; |
||
| 49 | |||
| 50 | /** |
||
| 51 | * A user defined function to transform uploaded file names |
||
| 52 | * |
||
| 53 | * @var Closure |
||
| 54 | */ |
||
| 55 | protected $cFileFilter = null; |
||
| 56 | |||
| 57 | /** |
||
| 58 | * Is the current request an HTTP upload |
||
| 59 | * |
||
| 60 | * @var boolean |
||
| 61 | */ |
||
| 62 | protected $bRequestIsHttpUpload = false; |
||
| 63 | |||
| 64 | /** |
||
| 65 | * Read uploaded files info from the $_FILES global var |
||
| 66 | */ |
||
| 67 | public function __construct() |
||
| 80 | |||
| 81 | /** |
||
| 82 | * Filter uploaded file name |
||
| 83 | * |
||
| 84 | * @param Closure $cFileFilter The closure which filters filenames |
||
| 85 | * |
||
| 86 | * @return void |
||
| 87 | */ |
||
| 88 | public function setFileFilter(Closure $cFileFilter) |
||
| 92 | |||
| 93 | /** |
||
| 94 | * Filter uploaded file name |
||
| 95 | * |
||
| 96 | * @param string $sFilename The filename |
||
| 97 | * @param string $sVarName The associated variable name |
||
| 98 | * |
||
| 99 | * @return string |
||
| 100 | */ |
||
| 101 | protected function filterFilename($sFilename, $sVarName) |
||
| 110 | |||
| 111 | /** |
||
| 112 | * Verify that the upload dir exists and is writable |
||
| 113 | * |
||
| 114 | * @param string $sUploadDir The filename |
||
| 115 | * @param string $sUploadSubDir The filename |
||
| 116 | * |
||
| 117 | * @return string |
||
| 118 | */ |
||
| 119 | private function _makeUploadDir($sUploadDir, $sUploadSubDir) |
||
| 134 | |||
| 135 | /** |
||
| 136 | * Get the path to the upload dir |
||
| 137 | * |
||
| 138 | * @param string $sFieldId The filename |
||
| 139 | * |
||
| 140 | * @return string |
||
| 141 | */ |
||
| 142 | protected function getUploadDir($sFieldId) |
||
| 150 | |||
| 151 | /** |
||
| 152 | * Get the path to the upload temp dir |
||
| 153 | * |
||
| 154 | * @return string |
||
| 155 | */ |
||
| 156 | protected function getUploadTempDir() |
||
| 163 | |||
| 164 | /** |
||
| 165 | * Get the path to the upload temp file |
||
| 166 | * |
||
| 167 | * @return string |
||
| 168 | */ |
||
| 169 | protected function getUploadTempFile() |
||
| 181 | |||
| 182 | /** |
||
| 183 | * Read uploaded files info from HTTP request data |
||
| 184 | * |
||
| 185 | * @return void |
||
| 186 | */ |
||
| 187 | protected function readFromHttpData() |
||
| 281 | |||
| 282 | /** |
||
| 283 | * Save uploaded files info to a temp file |
||
| 284 | * |
||
| 285 | * @return void |
||
| 286 | */ |
||
| 287 | protected function saveToTempFile() |
||
| 304 | |||
| 305 | /** |
||
| 306 | * Read uploaded files info from a temp file |
||
| 307 | * |
||
| 308 | * @return void |
||
| 309 | */ |
||
| 310 | protected function readFromTempFile() |
||
| 325 | |||
| 326 | /** |
||
| 327 | * Return the name of this plugin |
||
| 328 | * |
||
| 329 | * @return string |
||
| 330 | */ |
||
| 331 | public function getName() |
||
| 335 | |||
| 336 | /** |
||
| 337 | * Filter uploaded file name |
||
| 338 | * |
||
| 339 | * @param Closure $cFileFilter The closure which filters filenames |
||
| 340 | * |
||
| 341 | * @return void |
||
| 342 | */ |
||
| 343 | public function filter(Closure $cFileFilter) |
||
| 347 | |||
| 348 | /** |
||
| 349 | * Get the uploaded files |
||
| 350 | * |
||
| 351 | * @return array |
||
| 352 | */ |
||
| 353 | public function files() |
||
| 357 | |||
| 358 | /** |
||
| 359 | * Generate a hash for the registered browser events |
||
| 360 | * |
||
| 361 | * @return string |
||
| 362 | */ |
||
| 363 | public function generateHash() |
||
| 367 | |||
| 368 | /** |
||
| 369 | * Generate client side javascript code for the registered browser events |
||
| 370 | * |
||
| 371 | * @return string |
||
| 372 | */ |
||
| 373 | public function getScript() |
||
| 377 | |||
| 378 | /** |
||
| 379 | * Inform this plugin that other plugin can process the current request |
||
| 380 | * |
||
| 381 | * @return void |
||
| 382 | */ |
||
| 383 | public function noRequestPluginFound() |
||
| 390 | |||
| 391 | /** |
||
| 392 | * Check if this plugin can process the incoming Jaxon request |
||
| 393 | * |
||
| 394 | * @return boolean |
||
| 395 | */ |
||
| 396 | public function canProcessRequest() |
||
| 400 | |||
| 401 | /** |
||
| 402 | * Process the uploaded files into the HTTP request |
||
| 403 | * |
||
| 404 | * @return boolean |
||
| 405 | */ |
||
| 406 | public function processRequest() |
||
| 443 | } |
||
| 444 |