Complex classes like Image 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 Image, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 16 | class Image implements \ArrayAccess |
||
| 17 | { |
||
| 18 | /** |
||
| 19 | * @var string The new image name, to be provided or will be generated |
||
| 20 | */ |
||
| 21 | protected $name; |
||
| 22 | |||
| 23 | /** |
||
| 24 | * @var int The image width in pixels |
||
| 25 | */ |
||
| 26 | protected $width; |
||
| 27 | |||
| 28 | /** |
||
| 29 | * @var int The image height in pixels |
||
| 30 | */ |
||
| 31 | protected $height; |
||
| 32 | |||
| 33 | /** |
||
| 34 | * @var string The image mime type (extension) |
||
| 35 | */ |
||
| 36 | protected $mime; |
||
| 37 | |||
| 38 | /** |
||
| 39 | * @var string The full image path (dir + image + mime) |
||
| 40 | */ |
||
| 41 | protected $path; |
||
| 42 | |||
| 43 | /** |
||
| 44 | * @var string The folder or image storage storage |
||
| 45 | */ |
||
| 46 | protected $storage; |
||
| 47 | |||
| 48 | /** |
||
| 49 | * @var array The min and max image size allowed for upload (in bytes) |
||
| 50 | */ |
||
| 51 | protected $size = array(100, 500000); |
||
| 52 | |||
| 53 | /** |
||
| 54 | * @var array The max height and width image allowed |
||
| 55 | */ |
||
| 56 | protected $dimensions = array(5000, 5000); |
||
| 57 | |||
| 58 | /** |
||
| 59 | * @var array The mime types allowed for upload |
||
| 60 | */ |
||
| 61 | protected $mimeTypes = array('jpeg', 'png', 'gif', 'jpg'); |
||
| 62 | |||
| 63 | /** |
||
| 64 | * @var array list of known image types |
||
| 65 | */ |
||
| 66 | protected $acceptedMimes = array( |
||
| 67 | 1 => 'gif', 'jpeg', 'png', 'swf', 'psd', |
||
| 68 | 'bmp', 'tiff', 'tiff', 'jpc', 'jp2', 'jpx', |
||
| 69 | 'jb2', 'swc', 'iff', 'wbmp', 'xbm', 'ico' |
||
| 70 | ); |
||
| 71 | |||
| 72 | /** |
||
| 73 | * @var array error messages strings |
||
| 74 | */ |
||
| 75 | protected $commonErrors = array( |
||
| 76 | UPLOAD_ERR_OK => '', |
||
| 77 | UPLOAD_ERR_INI_SIZE => 'Image is larger than the specified amount set by the server', |
||
| 78 | UPLOAD_ERR_FORM_SIZE => 'Image is larger than the specified amount specified by browser', |
||
| 79 | UPLOAD_ERR_PARTIAL => 'Image could not be fully uploaded. Please try again later', |
||
| 80 | UPLOAD_ERR_NO_FILE => 'Image is not found', |
||
| 81 | UPLOAD_ERR_NO_TMP_DIR => 'Can\'t write to disk, due to server configuration ( No tmp dir found )', |
||
| 82 | UPLOAD_ERR_CANT_WRITE => 'Failed to write file to disk. Please check you file permissions', |
||
| 83 | UPLOAD_ERR_EXTENSION => 'A PHP extension has halted this file upload process' |
||
| 84 | ); |
||
| 85 | |||
| 86 | /** |
||
| 87 | * @var array storage for the global array |
||
| 88 | */ |
||
| 89 | private $_files = array(); |
||
| 90 | |||
| 91 | /** |
||
| 92 | * @var string storage for any errors |
||
| 93 | */ |
||
| 94 | private $error = ''; |
||
| 95 | |||
| 96 | /** |
||
| 97 | * @param array $_files represents the $_FILES array passed as dependency |
||
| 98 | */ |
||
| 99 | public function __construct(array $_files = array()) |
||
| 109 | |||
| 110 | /** |
||
| 111 | * \ArrayAccess unused method |
||
| 112 | * |
||
| 113 | * @param mixed $offset |
||
| 114 | * @param mixed $value |
||
| 115 | */ |
||
| 116 | public function offsetSet($offset, $value) {} |
||
| 117 | |||
| 118 | /** |
||
| 119 | * \ArrayAccess unused method |
||
| 120 | * |
||
| 121 | * @param mixed $offset |
||
| 122 | */ |
||
| 123 | public function offsetExists($offset){} |
||
| 124 | |||
| 125 | /** |
||
| 126 | * \ArrayAccess unused method |
||
| 127 | * |
||
| 128 | * @param mixed $offset |
||
| 129 | */ |
||
| 130 | public function offsetUnset($offset){} |
||
| 131 | |||
| 132 | /** |
||
| 133 | * \ArrayAccess - get array value from object |
||
| 134 | * |
||
| 135 | * @param mixed $offset |
||
| 136 | * |
||
| 137 | * @return string|bool |
||
| 138 | */ |
||
| 139 | public function offsetGet($offset) |
||
| 157 | |||
| 158 | /** |
||
| 159 | * Sets max image height and width limit. |
||
| 160 | * |
||
| 161 | * @param $maxWidth int max width value |
||
| 162 | * @param $maxHeight int max height value |
||
| 163 | * |
||
| 164 | * @return $this |
||
| 165 | */ |
||
| 166 | public function setDimension($maxWidth, $maxHeight) |
||
| 172 | |||
| 173 | /** |
||
| 174 | * Returns the full path of the image ex 'storage/image.mime'. |
||
| 175 | * |
||
| 176 | * @return string |
||
| 177 | */ |
||
| 178 | public function getPath() |
||
| 182 | |||
| 183 | /** |
||
| 184 | * Returns the image size in bytes. |
||
| 185 | * |
||
| 186 | * @return int |
||
| 187 | */ |
||
| 188 | public function getSize() |
||
| 192 | |||
| 193 | /** |
||
| 194 | * Define a min and max image size for uploading. |
||
| 195 | * |
||
| 196 | * @param $min int minimum value in bytes |
||
| 197 | * @param $max int maximum value in bytes |
||
| 198 | * |
||
| 199 | * @return $this |
||
| 200 | */ |
||
| 201 | public function setSize($min, $max) |
||
| 206 | |||
| 207 | /** |
||
| 208 | * Returns a JSON format of the image width, height, name, mime ... |
||
| 209 | * |
||
| 210 | * @return string |
||
| 211 | */ |
||
| 212 | public function getJson() |
||
| 226 | |||
| 227 | /** |
||
| 228 | * Returns the image mime type. |
||
| 229 | * |
||
| 230 | * @return null|string |
||
| 231 | */ |
||
| 232 | public function getMime() |
||
| 240 | |||
| 241 | /** |
||
| 242 | * Define a mime type for uploading. |
||
| 243 | * |
||
| 244 | * @param array $fileTypes |
||
| 245 | * |
||
| 246 | * @return $this |
||
| 247 | */ |
||
| 248 | public function setMime(array $fileTypes) |
||
| 253 | |||
| 254 | /** |
||
| 255 | * Gets the real image mime type. |
||
| 256 | * |
||
| 257 | * @param $tmp_name string The upload tmp directory |
||
| 258 | * |
||
| 259 | * @return null|string |
||
| 260 | */ |
||
| 261 | protected function getImageMime($tmp_name) |
||
| 270 | |||
| 271 | /** |
||
| 272 | * Returns error string |
||
| 273 | * |
||
| 274 | * @return string |
||
| 275 | */ |
||
| 276 | public function getError() |
||
| 280 | |||
| 281 | /** |
||
| 282 | * Returns the image name. |
||
| 283 | * |
||
| 284 | * @return string |
||
| 285 | */ |
||
| 286 | public function getName() |
||
| 290 | |||
| 291 | /** |
||
| 292 | * Provide image name if not provided. |
||
| 293 | * |
||
| 294 | * @param null $isNameProvided |
||
| 295 | * |
||
| 296 | * @return $this |
||
| 297 | */ |
||
| 298 | public function setName($isNameProvided = null) |
||
| 308 | |||
| 309 | /** |
||
| 310 | * Returns the image width. |
||
| 311 | * |
||
| 312 | * @return int |
||
| 313 | */ |
||
| 314 | public function getWidth() |
||
| 324 | |||
| 325 | /** |
||
| 326 | * Returns the image height in pixels. |
||
| 327 | * |
||
| 328 | * @return int |
||
| 329 | */ |
||
| 330 | public function getHeight() |
||
| 340 | |||
| 341 | /** |
||
| 342 | * Returns the storage / folder name. |
||
| 343 | * |
||
| 344 | * @return string |
||
| 345 | */ |
||
| 346 | public function getStorage() |
||
| 354 | |||
| 355 | /** |
||
| 356 | * Validate directory/permission before creating a folder. |
||
| 357 | * |
||
| 358 | * @param $dir string the folder name to check |
||
| 359 | * |
||
| 360 | * @return bool |
||
| 361 | */ |
||
| 362 | private function isDirectoryValid($dir) |
||
| 366 | |||
| 367 | /** |
||
| 368 | * Creates a storage for upload storage. |
||
| 369 | * |
||
| 370 | * @param $dir string the folder name to create |
||
| 371 | * @param int $permission chmod permission |
||
| 372 | * |
||
| 373 | * @return $this |
||
| 374 | */ |
||
| 375 | public function setStorage($dir = 'uploads', $permission = 0666) |
||
| 395 | |||
| 396 | |||
| 397 | public function validateMimes() { |
||
| 407 | |||
| 408 | public function validateSize() { |
||
| 419 | |||
| 420 | public function validateDimension() { |
||
| 433 | |||
| 434 | |||
| 435 | /** |
||
| 436 | * Validate and save (upload) file |
||
| 437 | * |
||
| 438 | * @return false|Image |
||
| 439 | */ |
||
| 440 | public function upload() |
||
| 453 | |||
| 454 | /** |
||
| 455 | * Final upload method to be called, isolated for testing purposes. |
||
| 456 | * |
||
| 457 | * @param $tmp_name int the temporary storage of the image file |
||
| 458 | * @param $destination int upload destination |
||
| 459 | * |
||
| 460 | * @return bool |
||
| 461 | */ |
||
| 462 | protected function isSaved($tmp_name, $destination) |
||
| 466 | } |