Complex classes like Kohana_Upload_File 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 Kohana_Upload_File, and based on these observations, apply Extract Interface, too.
| 1 | <?php defined('SYSPATH') OR die('No direct script access.'); |
||
| 12 | class Kohana_Upload_File { |
||
| 13 | |||
| 14 | protected $_source; |
||
| 15 | |||
| 16 | protected $_server; |
||
| 17 | |||
| 18 | protected $_path; |
||
| 19 | |||
| 20 | protected $_temp; |
||
| 21 | |||
| 22 | protected $_filename; |
||
| 23 | |||
| 24 | protected $_transformations = array(); |
||
| 25 | |||
| 26 | protected $_thumbnails = array(); |
||
| 27 | |||
| 28 | protected $_extracted_from_source = FALSE; |
||
| 29 | |||
| 30 | |||
| 31 | 27 | public function __construct($server, $path, $filename = NULL) |
|
| 41 | |||
| 42 | /** |
||
| 43 | * Get / Set the path for the image on the server |
||
| 44 | * @param string $path |
||
| 45 | * @return string|Upload_File |
||
| 46 | */ |
||
| 47 | 20 | public function path($path = NULL) |
|
| 58 | |||
| 59 | 1 | public function move_to_server($new_server) |
|
| 91 | |||
| 92 | /** |
||
| 93 | * Get / Set the source. Automatically set the source_type |
||
| 94 | * |
||
| 95 | * @param mixed $source |
||
| 96 | * @return mixed |
||
| 97 | */ |
||
| 98 | 22 | public function source($source = NULL) |
|
| 115 | |||
| 116 | /** |
||
| 117 | * Get / Set transformations |
||
| 118 | * @param array $transformations |
||
| 119 | * @return array|Upload_File |
||
| 120 | */ |
||
| 121 | 1 | public function transformations(array $transformations = NULL) |
|
| 132 | |||
| 133 | |||
| 134 | /** |
||
| 135 | * Get / Set thumbnails |
||
| 136 | * @param array $thumbnails |
||
| 137 | * @return array|Upload_File |
||
| 138 | */ |
||
| 139 | 4 | public function thumbnails(array $thumbnails = NULL) |
|
| 150 | |||
| 151 | /** |
||
| 152 | * Get the Upload_Temp object. Create it if it's not already created |
||
| 153 | * @return Upload_Temp |
||
| 154 | */ |
||
| 155 | 22 | public function temp() |
|
| 164 | |||
| 165 | /** |
||
| 166 | * Get the upload server |
||
| 167 | * @return Upload_Server |
||
| 168 | */ |
||
| 169 | 27 | public function server($server = NULL) |
|
| 179 | |||
| 180 | /** |
||
| 181 | * Get / Set the current filename |
||
| 182 | * @param string $filename |
||
| 183 | * @return string|Upload_File |
||
| 184 | */ |
||
| 185 | 26 | public function filename($filename = NULL) |
|
| 206 | |||
| 207 | 1 | public function transform() |
|
| 219 | |||
| 220 | /** |
||
| 221 | * Save the current source to the temp folder |
||
| 222 | */ |
||
| 223 | 13 | public function save_to_temp() |
|
| 236 | |||
| 237 | /** |
||
| 238 | * Generate the thumbnails if they are not generated |
||
| 239 | * |
||
| 240 | * @return Upload_File $this |
||
| 241 | */ |
||
| 242 | 3 | public function generate_thumbnails() |
|
| 254 | |||
| 255 | /** |
||
| 256 | * Save the file by moving it from temporary to the upload server |
||
| 257 | * Generate the thumbnails if nesessary |
||
| 258 | * @return Upload_File $this |
||
| 259 | */ |
||
| 260 | 1 | public function save() |
|
| 280 | |||
| 281 | /** |
||
| 282 | * Clear temporary files |
||
| 283 | * @return Upload_File $this |
||
| 284 | */ |
||
| 285 | 16 | public function clear() |
|
| 291 | |||
| 292 | /** |
||
| 293 | * Delete the current file on the server and clear temporary files |
||
| 294 | * @return Upload_File $this |
||
| 295 | */ |
||
| 296 | 1 | public function delete() |
|
| 309 | |||
| 310 | /** |
||
| 311 | * Get the current filename (temp or server) |
||
| 312 | * @param string $thumbnail |
||
| 313 | * @return string |
||
| 314 | */ |
||
| 315 | 19 | public function file($thumbnail = NULL) |
|
| 319 | |||
| 320 | /** |
||
| 321 | * Get the current url (temp or server) |
||
| 322 | * @param string $thumbnail |
||
| 323 | * @param mixed $protocol |
||
| 324 | * @return string |
||
| 325 | */ |
||
| 326 | 1 | public function url($thumbnail = NULL) |
|
| 330 | |||
| 331 | /** |
||
| 332 | * Get the full path with the filename |
||
| 333 | * @param string $thumbnail |
||
| 334 | * @return string |
||
| 335 | */ |
||
| 336 | 3 | public function full_path($thumbnail = NULL) |
|
| 340 | |||
| 341 | 1 | public function temp_source() |
|
| 348 | |||
| 349 | 19 | protected function location($method, $thumbnail = NULL) |
|
| 370 | |||
| 371 | /** |
||
| 372 | * Check if its empty (no filename or source) |
||
| 373 | * @return boolean |
||
| 374 | */ |
||
| 375 | 14 | public function is_empty() |
|
| 379 | |||
| 380 | public function __toString() |
||
| 384 | } |
||
| 385 |
There are different options of fixing this problem.
If you want to be on the safe side, you can add an additional type-check:
If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:
Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.