Complex classes like upload 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 upload, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 13 | class upload |
||
| 14 | { |
||
| 15 | |||
| 16 | /** |
||
| 17 | * Default directory persmissions (destination dir) |
||
| 18 | */ |
||
| 19 | protected $default_permissions = 750; |
||
| 20 | |||
| 21 | |||
| 22 | /** |
||
| 23 | * File post array |
||
| 24 | * |
||
| 25 | * @var array |
||
| 26 | */ |
||
| 27 | protected $files_post = array(); |
||
| 28 | |||
| 29 | |||
| 30 | /** |
||
| 31 | * Destination directory |
||
| 32 | * |
||
| 33 | * @var string |
||
| 34 | */ |
||
| 35 | protected $destination; |
||
| 36 | |||
| 37 | |||
| 38 | /** |
||
| 39 | * Fileinfo |
||
| 40 | * |
||
| 41 | * @var object |
||
| 42 | */ |
||
| 43 | protected $finfo; |
||
| 44 | |||
| 45 | |||
| 46 | /** |
||
| 47 | * Data about file |
||
| 48 | * |
||
| 49 | * @var array |
||
| 50 | */ |
||
| 51 | public $file = array(); |
||
| 52 | |||
| 53 | |||
| 54 | /** |
||
| 55 | * Max. file size |
||
| 56 | * |
||
| 57 | * @var int |
||
| 58 | */ |
||
| 59 | protected $max_file_size; |
||
| 60 | |||
| 61 | |||
| 62 | /** |
||
| 63 | * Allowed mime types |
||
| 64 | * |
||
| 65 | * @var array |
||
| 66 | */ |
||
| 67 | protected $mimes = array(); |
||
| 68 | |||
| 69 | |||
| 70 | /** |
||
| 71 | * External callback object |
||
| 72 | * |
||
| 73 | * @var obejct |
||
| 74 | */ |
||
| 75 | protected $external_callback_object; |
||
| 76 | |||
| 77 | |||
| 78 | /** |
||
| 79 | * External callback methods |
||
| 80 | * |
||
| 81 | * @var array |
||
| 82 | */ |
||
| 83 | protected $external_callback_methods = array(); |
||
| 84 | |||
| 85 | |||
| 86 | /** |
||
| 87 | * Temp path |
||
| 88 | * |
||
| 89 | * @var string |
||
| 90 | */ |
||
| 91 | protected $tmp_name; |
||
| 92 | |||
| 93 | |||
| 94 | /** |
||
| 95 | * Validation errors |
||
| 96 | * |
||
| 97 | * @var array |
||
| 98 | */ |
||
| 99 | protected $validation_errors = array(); |
||
| 100 | |||
| 101 | |||
| 102 | /** |
||
| 103 | * Filename (new) |
||
| 104 | * |
||
| 105 | * @var string |
||
| 106 | */ |
||
| 107 | protected $filename; |
||
| 108 | |||
| 109 | /** |
||
| 110 | * File extension |
||
| 111 | * @var string |
||
| 112 | */ |
||
| 113 | protected $extension; |
||
| 114 | |||
| 115 | /** |
||
| 116 | * Internal callbacks (filesize check, mime, etc) |
||
| 117 | * |
||
| 118 | * @var array |
||
| 119 | */ |
||
| 120 | private $callbacks = array(); |
||
| 121 | |||
| 122 | /** |
||
| 123 | * Root dir |
||
| 124 | * |
||
| 125 | * @var string |
||
| 126 | */ |
||
| 127 | protected $root; |
||
| 128 | |||
| 129 | /** |
||
| 130 | * Return upload object |
||
| 131 | * |
||
| 132 | * $destination = 'path/to/your/file/destination/folder'; |
||
| 133 | * |
||
| 134 | * @param string $phpbb_root_path |
||
| 135 | * @return Upload |
||
| 136 | */ |
||
| 137 | public static function factory($phpbb_root_path) |
||
| 141 | |||
| 142 | /** |
||
| 143 | * Define ROOT constant and set & create destination path |
||
| 144 | * |
||
| 145 | * @param string $phpbb_root_path |
||
| 146 | * @throws Exception |
||
| 147 | */ |
||
| 148 | public function __construct($phpbb_root_path) |
||
| 159 | |||
| 160 | /** |
||
| 161 | * Set target filename |
||
| 162 | * |
||
| 163 | * @param string $filename |
||
| 164 | */ |
||
| 165 | public function set_filename($filename) |
||
| 169 | |||
| 170 | /** |
||
| 171 | * Check & Save file |
||
| 172 | * |
||
| 173 | * Return data about current upload |
||
| 174 | * |
||
| 175 | * @return array |
||
| 176 | */ |
||
| 177 | public function upload($filename = '') |
||
| 185 | |||
| 186 | |||
| 187 | /** |
||
| 188 | * Save file on server |
||
| 189 | * |
||
| 190 | * Return state data |
||
| 191 | * |
||
| 192 | * @return array |
||
| 193 | */ |
||
| 194 | public function save() |
||
| 199 | |||
| 200 | |||
| 201 | /** |
||
| 202 | * Validate file (execute callbacks) |
||
| 203 | * |
||
| 204 | * Returns TRUE if validation successful |
||
| 205 | * |
||
| 206 | * @return bool |
||
| 207 | */ |
||
| 208 | public function check() |
||
| 221 | |||
| 222 | /** |
||
| 223 | * Get current state data |
||
| 224 | * |
||
| 225 | * @return array |
||
| 226 | */ |
||
| 227 | public function get_state() |
||
| 231 | |||
| 232 | |||
| 233 | /** |
||
| 234 | * Save file on server |
||
| 235 | */ |
||
| 236 | protected function save_file() |
||
| 260 | |||
| 261 | /** |
||
| 262 | * Set data about file |
||
| 263 | */ |
||
| 264 | protected function set_file_data() |
||
| 279 | |||
| 280 | /** |
||
| 281 | * Set validation error |
||
| 282 | * |
||
| 283 | * @param string $message |
||
| 284 | */ |
||
| 285 | public function set_error($message) |
||
| 289 | |||
| 290 | /** |
||
| 291 | * Return validation errors |
||
| 292 | * |
||
| 293 | * @return array |
||
| 294 | */ |
||
| 295 | public function get_errors() |
||
| 299 | |||
| 300 | /** |
||
| 301 | * Set external callback methods |
||
| 302 | * |
||
| 303 | * @param object $instance_of_callback_object |
||
| 304 | * @param array $callback_methods |
||
| 305 | * @throws Exception |
||
| 306 | */ |
||
| 307 | public function callbacks($instance_of_callback_object, $callback_methods) |
||
| 320 | |||
| 321 | /** |
||
| 322 | * Execute callbacks |
||
| 323 | */ |
||
| 324 | protected function validate() |
||
| 340 | |||
| 341 | /** |
||
| 342 | * Execute callbacks |
||
| 343 | */ |
||
| 344 | protected function execute_callbacks($callbacks, $object) |
||
| 350 | |||
| 351 | /** |
||
| 352 | * File mime type validation callback |
||
| 353 | * |
||
| 354 | * @param mixed $object |
||
| 355 | */ |
||
| 356 | protected function check_mime_type($object) |
||
| 364 | |||
| 365 | /** |
||
| 366 | * Set allowed mime types |
||
| 367 | * |
||
| 368 | * @param string[] $mimes |
||
| 369 | */ |
||
| 370 | public function set_allowed_mime_types($mimes) |
||
| 376 | |||
| 377 | /** |
||
| 378 | * File size validation callback |
||
| 379 | * |
||
| 380 | * @param object $object |
||
| 381 | */ |
||
| 382 | protected function check_file_size($object) |
||
| 391 | |||
| 392 | /** |
||
| 393 | * Set max. file size |
||
| 394 | * |
||
| 395 | * @param int $size |
||
| 396 | */ |
||
| 397 | public function set_max_file_size($size) |
||
| 403 | |||
| 404 | /** |
||
| 405 | * Set File array to object |
||
| 406 | * |
||
| 407 | * @param array $file |
||
| 408 | */ |
||
| 409 | public function file($file) |
||
| 413 | |||
| 414 | /** |
||
| 415 | * Set file array |
||
| 416 | * |
||
| 417 | * @param array $file |
||
| 418 | */ |
||
| 419 | protected function set_file_array($file) |
||
| 436 | |||
| 437 | /** |
||
| 438 | * Checks whether Files post array is valid |
||
| 439 | * |
||
| 440 | * @return bool |
||
| 441 | */ |
||
| 442 | protected function check_file_array($file) |
||
| 450 | |||
| 451 | /** |
||
| 452 | * Get file mime type |
||
| 453 | * |
||
| 454 | * @return string |
||
| 455 | */ |
||
| 456 | protected function get_file_mime() |
||
| 460 | |||
| 461 | /** |
||
| 462 | * Get file size |
||
| 463 | * |
||
| 464 | * @return int |
||
| 465 | */ |
||
| 466 | protected function get_file_size() |
||
| 470 | |||
| 471 | /** |
||
| 472 | * Set destination path (return TRUE on success) |
||
| 473 | * |
||
| 474 | * @param string $destination |
||
| 475 | * @return bool |
||
| 476 | */ |
||
| 477 | protected function set_destination($destination) |
||
| 482 | |||
| 483 | /** |
||
| 484 | * Checks whether destination folder exists |
||
| 485 | * |
||
| 486 | * @return bool |
||
| 487 | */ |
||
| 488 | protected function destination_exist() |
||
| 492 | |||
| 493 | /** |
||
| 494 | * Create path to destination |
||
| 495 | * |
||
| 496 | * @return bool |
||
| 497 | */ |
||
| 498 | protected function create_destination() |
||
| 502 | |||
| 503 | /** |
||
| 504 | * Set unique filename |
||
| 505 | * |
||
| 506 | * @return string |
||
| 507 | */ |
||
| 508 | protected function create_new_filename() |
||
| 513 | |||
| 514 | /** |
||
| 515 | * Convert bytes to mb. |
||
| 516 | * |
||
| 517 | * @param int $bytes |
||
| 518 | * @return double |
||
| 519 | */ |
||
| 520 | protected function bytes_to_mb($bytes) |
||
| 524 | |||
| 525 | |||
| 526 | } // end of Upload |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.