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 $fullPath; |
||
| 42 | |||
| 43 | /** |
||
| 44 | * @var string The folder or image storage location |
||
| 45 | */ |
||
| 46 | protected $location; |
||
| 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 string The language |
||
| 74 | */ |
||
| 75 | protected $language = 'en'; |
||
| 76 | |||
| 77 | /** |
||
| 78 | * @var array error messages strings |
||
| 79 | */ |
||
| 80 | protected $commonUploadErrors = array( |
||
| 81 | 'en' => array( |
||
| 82 | UPLOAD_ERR_OK => '', |
||
| 83 | UPLOAD_ERR_INI_SIZE => 'Image is larger than the specified amount set by the server', |
||
| 84 | UPLOAD_ERR_FORM_SIZE => 'Image is larger than the specified amount specified by browser', |
||
| 85 | UPLOAD_ERR_PARTIAL => 'Image could not be fully uploaded. Please try again later', |
||
| 86 | UPLOAD_ERR_NO_FILE => 'Image is not found', |
||
| 87 | UPLOAD_ERR_NO_TMP_DIR => 'Can\'t write to disk, due to server configuration ( No tmp dir found )', |
||
| 88 | UPLOAD_ERR_CANT_WRITE => 'Failed to write file to disk. Please check you file permissions', |
||
| 89 | UPLOAD_ERR_EXTENSION => 'A PHP extension has halted this file upload process', |
||
| 90 | |||
| 91 | 'ERROR_01' => 'Function \'exif_imagetype\' Not found. Please enable \'php_exif\' in your php.ini', |
||
| 92 | 'ERROR_02' => 'No file input found with name: (%1$s)', |
||
| 93 | 'ERROR_03' => 'Invalid dimension! Values must be integers', |
||
| 94 | 'ERROR_04' => 'Can not create a directory (%1$s), please check write permission', |
||
| 95 | 'ERROR_05' => 'Error! directory (%1$s) could not be created', |
||
| 96 | 'ERROR_06' => 'Invalid File! Only (%1$s) image types are allowed', |
||
| 97 | 'ERROR_07' => 'Image size should be minumum (%1$s), upto maximum (%2$s)', |
||
| 98 | 'ERROR_08' => 'Image height/width should be less than (%1$s)/(%2$s) pixels' |
||
| 99 | ), |
||
| 100 | ); |
||
| 101 | |||
| 102 | /** |
||
| 103 | * @var array storage for the global array |
||
| 104 | */ |
||
| 105 | private $_files = array(); |
||
| 106 | |||
| 107 | /** |
||
| 108 | * @var string storage for any errors |
||
| 109 | */ |
||
| 110 | private $error = ''; |
||
| 111 | |||
| 112 | /** |
||
| 113 | * @param array $_files represents the $_FILES array passed as dependency |
||
| 114 | */ |
||
| 115 | public function __construct(array $_files = array()) |
||
| 123 | |||
| 124 | /** |
||
| 125 | * \ArrayAccess unused method |
||
| 126 | * |
||
| 127 | * @param mixed $offset |
||
| 128 | * @param mixed $value |
||
| 129 | */ |
||
| 130 | public function offsetSet($offset, $value) {} |
||
| 131 | |||
| 132 | /** |
||
| 133 | * \ArrayAccess unused method |
||
| 134 | * |
||
| 135 | * @param mixed $offset |
||
| 136 | */ |
||
| 137 | public function offsetExists($offset){} |
||
| 138 | |||
| 139 | /** |
||
| 140 | * \ArrayAccess unused method |
||
| 141 | * |
||
| 142 | * @param mixed $offset |
||
| 143 | */ |
||
| 144 | public function offsetUnset($offset){} |
||
| 145 | |||
| 146 | /** |
||
| 147 | * \ArrayAccess - get array value from object |
||
| 148 | * |
||
| 149 | * @param mixed $offset |
||
| 150 | * |
||
| 151 | * @return string|bool |
||
| 152 | */ |
||
| 153 | public function offsetGet($offset) |
||
| 170 | |||
| 171 | /** |
||
| 172 | * Sets max image height and width limit. |
||
| 173 | * |
||
| 174 | * @param $maxWidth int max width value |
||
| 175 | * @param $maxHeight int max height value |
||
| 176 | * |
||
| 177 | * @return $this |
||
| 178 | */ |
||
| 179 | public function setDimension($maxWidth, $maxHeight) |
||
| 189 | |||
| 190 | /** |
||
| 191 | * Returns the full path of the image ex 'location/image.mime'. |
||
| 192 | * |
||
| 193 | * @return string |
||
| 194 | */ |
||
| 195 | public function getFullPath() |
||
| 199 | |||
| 200 | /** |
||
| 201 | * Define a language |
||
| 202 | * |
||
| 203 | * @param $lang string language code |
||
| 204 | * |
||
| 205 | * @return $this |
||
| 206 | */ |
||
| 207 | public function setLanguage($lang) |
||
| 217 | |||
| 218 | /** |
||
| 219 | * Returns the image size in bytes. |
||
| 220 | * |
||
| 221 | * @return int |
||
| 222 | */ |
||
| 223 | public function getSize() |
||
| 227 | |||
| 228 | /** |
||
| 229 | * Define a min and max image size for uploading. |
||
| 230 | * |
||
| 231 | * @param $min int minimum value in bytes |
||
| 232 | * @param $max int maximum value in bytes |
||
| 233 | * |
||
| 234 | * @return $this |
||
| 235 | */ |
||
| 236 | public function setSize($min, $max) |
||
| 241 | |||
| 242 | /** |
||
| 243 | * Returns a JSON format of the image width, height, name, mime ... |
||
| 244 | * |
||
| 245 | * @return string |
||
| 246 | */ |
||
| 247 | public function getJson() |
||
| 261 | |||
| 262 | /** |
||
| 263 | * Returns the image mime type. |
||
| 264 | * |
||
| 265 | * @return null|string |
||
| 266 | */ |
||
| 267 | public function getMime() |
||
| 275 | |||
| 276 | /** |
||
| 277 | * Define a mime type for uploading. |
||
| 278 | * |
||
| 279 | * @param array $fileTypes |
||
| 280 | * |
||
| 281 | * @return $this |
||
| 282 | */ |
||
| 283 | public function setMime(array $fileTypes) |
||
| 288 | |||
| 289 | /** |
||
| 290 | * Gets the real image mime type. |
||
| 291 | * |
||
| 292 | * @param $tmp_name string The upload tmp directory |
||
| 293 | * |
||
| 294 | * @return null|string |
||
| 295 | */ |
||
| 296 | protected function getImageMime($tmp_name) |
||
| 305 | |||
| 306 | /** |
||
| 307 | * Returns error string or false if no errors occurred. |
||
| 308 | * |
||
| 309 | * @return string|false |
||
| 310 | */ |
||
| 311 | public function getError() |
||
| 315 | |||
| 316 | /** |
||
| 317 | * Returns the image name. |
||
| 318 | * |
||
| 319 | * @return string |
||
| 320 | */ |
||
| 321 | public function getName() |
||
| 329 | |||
| 330 | /** |
||
| 331 | * Provide image name if not provided. |
||
| 332 | * |
||
| 333 | * @param null $isNameProvided |
||
| 334 | * |
||
| 335 | * @return $this |
||
| 336 | */ |
||
| 337 | public function setName($isNameProvided = null) |
||
| 345 | |||
| 346 | /** |
||
| 347 | * Returns the image width. |
||
| 348 | * |
||
| 349 | * @return int |
||
| 350 | */ |
||
| 351 | public function getWidth() |
||
| 361 | |||
| 362 | /** |
||
| 363 | * Returns the image height in pixels. |
||
| 364 | * |
||
| 365 | * @return int |
||
| 366 | */ |
||
| 367 | public function getHeight() |
||
| 377 | |||
| 378 | /** |
||
| 379 | * Returns the storage / folder name. |
||
| 380 | * |
||
| 381 | * @return string |
||
| 382 | */ |
||
| 383 | public function getLocation() |
||
| 391 | |||
| 392 | /** |
||
| 393 | * Validate directory/permission before creating a folder. |
||
| 394 | * |
||
| 395 | * @param $dir string the folder name to check |
||
| 396 | * |
||
| 397 | * @return bool |
||
| 398 | */ |
||
| 399 | private function isDirectoryValid($dir) |
||
| 403 | |||
| 404 | /** |
||
| 405 | * Creates a location for upload storage. |
||
| 406 | * |
||
| 407 | * @param $dir string the folder name to create |
||
| 408 | * @param int $permission chmod permission |
||
| 409 | * |
||
| 410 | * @return $this |
||
| 411 | */ |
||
| 412 | public function setLocation($dir = 'bulletproof', $permission = 0666) |
||
| 432 | |||
| 433 | /** |
||
| 434 | * Validate image size, dimension or mimetypes |
||
| 435 | * |
||
| 436 | * @return boolean |
||
| 437 | */ |
||
| 438 | protected function contraintsValidator() |
||
| 471 | |||
| 472 | /** |
||
| 473 | * Validate and save (upload) file |
||
| 474 | * |
||
| 475 | * @return false|Image |
||
| 476 | */ |
||
| 477 | public function upload() |
||
| 489 | |||
| 490 | /** |
||
| 491 | * Final upload method to be called, isolated for testing purposes. |
||
| 492 | * |
||
| 493 | * @param $tmp_name int the temporary location of the image file |
||
| 494 | * @param $destination int upload destination |
||
| 495 | * |
||
| 496 | * @return bool |
||
| 497 | */ |
||
| 498 | protected function isSaved($tmp_name, $destination) |
||
| 502 | } |