Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
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 | * Get the path to the upload dir |
||
| 113 | * |
||
| 114 | * @param string $sFieldId The filename |
||
| 115 | * |
||
| 116 | * @return string |
||
| 117 | */ |
||
| 118 | protected function getUploadDir($sFieldId) |
||
| 136 | |||
| 137 | /** |
||
| 138 | * Get the path to the upload temp dir |
||
| 139 | * |
||
| 140 | * @return string |
||
| 141 | */ |
||
| 142 | protected function getUploadTempDir() |
||
| 159 | |||
| 160 | /** |
||
| 161 | * Get the path to the upload temp file |
||
| 162 | * |
||
| 163 | * @return string |
||
| 164 | */ |
||
| 165 | protected function getUploadTempFile() |
||
| 177 | |||
| 178 | /** |
||
| 179 | * Read uploaded files info from HTTP request data |
||
| 180 | * |
||
| 181 | * @return void |
||
| 182 | */ |
||
| 183 | protected function readFromHttpData() |
||
| 277 | |||
| 278 | /** |
||
| 279 | * Save uploaded files info to a temp file |
||
| 280 | * |
||
| 281 | * @return void |
||
| 282 | */ |
||
| 283 | protected function saveToTempFile() |
||
| 300 | |||
| 301 | /** |
||
| 302 | * Read uploaded files info from a temp file |
||
| 303 | * |
||
| 304 | * @return void |
||
| 305 | */ |
||
| 306 | protected function readFromTempFile() |
||
| 321 | |||
| 322 | /** |
||
| 323 | * Return the name of this plugin |
||
| 324 | * |
||
| 325 | * @return string |
||
| 326 | */ |
||
| 327 | public function getName() |
||
| 331 | |||
| 332 | /** |
||
| 333 | * Filter uploaded file name |
||
| 334 | * |
||
| 335 | * @param Closure $cFileFilter The closure which filters filenames |
||
| 336 | * |
||
| 337 | * @return void |
||
| 338 | */ |
||
| 339 | public function filter(Closure $cFileFilter) |
||
| 343 | |||
| 344 | /** |
||
| 345 | * Get the uploaded files |
||
| 346 | * |
||
| 347 | * @return array |
||
| 348 | */ |
||
| 349 | public function files() |
||
| 353 | |||
| 354 | /** |
||
| 355 | * Generate a hash for the registered browser events |
||
| 356 | * |
||
| 357 | * @return string |
||
| 358 | */ |
||
| 359 | public function generateHash() |
||
| 363 | |||
| 364 | /** |
||
| 365 | * Generate client side javascript code for the registered browser events |
||
| 366 | * |
||
| 367 | * @return string |
||
| 368 | */ |
||
| 369 | public function getScript() |
||
| 373 | |||
| 374 | /** |
||
| 375 | * Inform this plugin that other plugin can process the current request |
||
| 376 | * |
||
| 377 | * @return void |
||
| 378 | */ |
||
| 379 | public function noRequestPluginFound() |
||
| 386 | |||
| 387 | /** |
||
| 388 | * Check if this plugin can process the incoming Jaxon request |
||
| 389 | * |
||
| 390 | * @return boolean |
||
| 391 | */ |
||
| 392 | public function canProcessRequest() |
||
| 396 | |||
| 397 | /** |
||
| 398 | * Process the uploaded files into the HTTP request |
||
| 399 | * |
||
| 400 | * @return boolean |
||
| 401 | */ |
||
| 402 | public function processRequest() |
||
| 439 | } |
||
| 440 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.