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 |
||
| 19 | class FileUpload extends RequestPlugin |
||
| 20 | { |
||
| 21 | use \Jaxon\Utils\Traits\Validator; |
||
| 22 | use \Jaxon\Utils\Traits\Translator; |
||
| 23 | |||
| 24 | /** |
||
| 25 | * The uploaded files copied in the user dir |
||
| 26 | * |
||
| 27 | * @var array |
||
| 28 | */ |
||
| 29 | protected $aUserFiles; |
||
| 30 | |||
| 31 | /** |
||
| 32 | * The name of file containing upload data |
||
| 33 | * |
||
| 34 | * @var string |
||
| 35 | */ |
||
| 36 | protected $sTempFile = ''; |
||
| 37 | |||
| 38 | /** |
||
| 39 | * A user defined function to transform uploaded file names |
||
| 40 | * |
||
| 41 | * @var Closure |
||
| 42 | */ |
||
| 43 | protected $fFileFilter = null; |
||
| 44 | |||
| 45 | /** |
||
| 46 | * Read uploaded files info from the $_FILES global var |
||
| 47 | */ |
||
| 48 | public function __construct() |
||
| 61 | |||
| 62 | /** |
||
| 63 | * Filter uploaded file name |
||
| 64 | * |
||
| 65 | * @param Closure $fFileFilter The closure which filters filenames |
||
| 66 | * |
||
| 67 | * @return void |
||
| 68 | */ |
||
| 69 | public function setFileFilter($fFileFilter) |
||
| 73 | |||
| 74 | /** |
||
| 75 | * Filter uploaded file name |
||
| 76 | * |
||
| 77 | * @param string $sFilename The filename |
||
| 78 | * @param string $sVarName The associated variable name |
||
| 79 | * |
||
| 80 | * @return string |
||
| 81 | */ |
||
| 82 | protected function filterFilename($sFilename, $sVarName) |
||
| 91 | |||
| 92 | /** |
||
| 93 | * Read uploaded files info from HTTP request data |
||
| 94 | * |
||
| 95 | * @return void |
||
| 96 | */ |
||
| 97 | protected function readFromHttpData() |
||
| 198 | |||
| 199 | /** |
||
| 200 | * Save uploaded files info to a temp file |
||
| 201 | * |
||
| 202 | * @return void |
||
| 203 | */ |
||
| 204 | protected function saveToTempFile() |
||
| 229 | |||
| 230 | /** |
||
| 231 | * Read uploaded files info from a temp file |
||
| 232 | * |
||
| 233 | * @return void |
||
| 234 | */ |
||
| 235 | protected function readFromTempFile() |
||
| 257 | |||
| 258 | /** |
||
| 259 | * Return the name of this plugin |
||
| 260 | * |
||
| 261 | * @return string |
||
| 262 | */ |
||
| 263 | public function getName() |
||
| 267 | |||
| 268 | /** |
||
| 269 | * Get the uploaded files |
||
| 270 | * |
||
| 271 | * @return array |
||
| 272 | */ |
||
| 273 | public function getUploadedFiles() |
||
| 277 | |||
| 278 | /** |
||
| 279 | * Register a browser event |
||
| 280 | * |
||
| 281 | * @param array $aArgs An array containing the event specification |
||
| 282 | * |
||
| 283 | * @return \Jaxon\Request\Request |
||
| 284 | */ |
||
| 285 | public function register($aArgs) |
||
| 289 | |||
| 290 | /** |
||
| 291 | * Generate a hash for the registered browser events |
||
| 292 | * |
||
| 293 | * @return string |
||
| 294 | */ |
||
| 295 | public function generateHash() |
||
| 299 | |||
| 300 | /** |
||
| 301 | * Generate client side javascript code for the registered browser events |
||
| 302 | * |
||
| 303 | * @return string |
||
| 304 | */ |
||
| 305 | public function getScript() |
||
| 309 | |||
| 310 | /** |
||
| 311 | * Check if this plugin can process the incoming Jaxon request |
||
| 312 | * |
||
| 313 | * @return boolean |
||
| 314 | */ |
||
| 315 | public function canProcessRequest() |
||
| 319 | |||
| 320 | /** |
||
| 321 | * Process the uploaded files into the HTTP request |
||
| 322 | * |
||
| 323 | * @return boolean |
||
| 324 | */ |
||
| 325 | public function processRequest() |
||
| 341 | |||
| 342 | /** |
||
| 343 | * Check uploaded files validity and move them to the user dir |
||
| 344 | * |
||
| 345 | * @return boolean |
||
| 346 | */ |
||
| 347 | public function saveUploadedFiles() |
||
| 358 | } |
||
| 359 |
Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable: