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 |
||
| 17 | class Image implements \ArrayAccess |
||
| 18 | {
|
||
| 19 | /** |
||
| 20 | * @var string The new image name, to be provided or will be generated. |
||
| 21 | */ |
||
| 22 | protected $name; |
||
| 23 | |||
| 24 | /** |
||
| 25 | * @var int The image width in pixels |
||
| 26 | */ |
||
| 27 | protected $width; |
||
| 28 | |||
| 29 | /** |
||
| 30 | * @var int The image height in pixels |
||
| 31 | */ |
||
| 32 | protected $height; |
||
| 33 | |||
| 34 | /** |
||
| 35 | * @var string The image mime type (extension) |
||
| 36 | */ |
||
| 37 | protected $mime; |
||
| 38 | |||
| 39 | /** |
||
| 40 | * @var string The full image path (dir + image + mime) |
||
| 41 | */ |
||
| 42 | protected $fullPath; |
||
| 43 | |||
| 44 | /** |
||
| 45 | * @var string The folder or image storage location |
||
| 46 | */ |
||
| 47 | protected $location; |
||
| 48 | |||
| 49 | /** |
||
| 50 | * @var array The min and max image size allowed for upload (in bytes) |
||
| 51 | */ |
||
| 52 | protected $size = array(100, 500000); |
||
| 53 | |||
| 54 | /** |
||
| 55 | * @var array The max height and width image allowed |
||
| 56 | */ |
||
| 57 | protected $dimensions = array(5000, 5000); |
||
| 58 | |||
| 59 | /** |
||
| 60 | * @var array The mime types allowed for upload |
||
| 61 | */ |
||
| 62 | protected $mimeTypes = array('jpeg', 'png', 'gif', 'jpg');
|
||
| 63 | |||
| 64 | /** |
||
| 65 | * @var array list of known image types |
||
| 66 | */ |
||
| 67 | protected $acceptedMimes = array( |
||
| 68 | 1 => 'gif', 'jpeg', 'png', 'swf', 'psd', |
||
| 69 | 'bmp', 'tiff', 'tiff', 'jpc', 'jp2', 'jpx', |
||
| 70 | 'jb2', 'swc', 'iff', 'wbmp', 'xbm', 'ico' |
||
| 71 | ); |
||
| 72 | /** |
||
| 73 | * @var array error messages strings |
||
| 74 | */ |
||
| 75 | protected $commonUploadErrors = 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 | * @var array storage for the $_FILES global array |
||
| 87 | */ |
||
| 88 | private $_files = array(); |
||
| 89 | /** |
||
| 90 | * @var string storage for any errors |
||
| 91 | */ |
||
| 92 | private $error = ''; |
||
| 93 | |||
| 94 | /** |
||
| 95 | * @param array $_files represents the $_FILES array passed as dependency |
||
| 96 | */ |
||
| 97 | public function __construct(array $_files = array()) |
||
| 98 | {
|
||
| 99 | /* check if php_exif is enabled */ |
||
| 100 | if (!function_exists('exif_imagetype')) {
|
||
| 101 | $this->error = 'Function \'exif_imagetype\' Not found. Please enable \'php_exif\' in your PHP.ini'; |
||
| 102 | return null; |
||
| 103 | } |
||
| 104 | |||
| 105 | $this->_files = $_files; |
||
| 106 | } |
||
| 107 | |||
| 108 | /** |
||
| 109 | * @param mixed $offset |
||
| 110 | * @param mixed $value |
||
| 111 | * @return null |
||
| 112 | */ |
||
| 113 | public function offsetSet($offset, $value){}
|
||
| 114 | |||
| 115 | /** |
||
| 116 | * @param mixed $offset |
||
| 117 | * @return null |
||
| 118 | */ |
||
| 119 | public function offsetExists($offset){}
|
||
| 120 | |||
| 121 | /** |
||
| 122 | * @param mixed $offset |
||
| 123 | * @return null |
||
| 124 | */ |
||
| 125 | public function offsetUnset($offset){}
|
||
| 126 | |||
| 127 | /** |
||
| 128 | * Gets array value \ArrayAccess |
||
| 129 | * |
||
| 130 | * @param mixed $offset |
||
| 131 | * |
||
| 132 | * @return string|boolean |
||
| 133 | */ |
||
| 134 | public function offsetGet($offset) |
||
| 135 | {
|
||
| 136 | // return error if requested |
||
| 137 | if ($offset == 'error') {
|
||
| 138 | return $this->error; |
||
| 139 | } |
||
| 140 | |||
| 141 | // return false if $image['key'] isn't found |
||
| 142 | if (!isset($this->_files[$offset])) {
|
||
| 143 | return false; |
||
| 144 | } |
||
| 145 | |||
| 146 | $this->_files = $this->_files[$offset]; |
||
| 147 | |||
| 148 | // check for common upload errors |
||
| 149 | if(isset($this->_files['error'])){
|
||
| 150 | $this->error = $this->commonUploadErrors[$this->_files['error']]; |
||
| 151 | } |
||
| 152 | |||
| 153 | return true; |
||
| 154 | } |
||
| 155 | |||
| 156 | /** |
||
| 157 | * Sets max image height and width limit |
||
| 158 | * |
||
| 159 | * @param $maxWidth int max width value |
||
| 160 | * @param $maxHeight int max height value |
||
| 161 | * |
||
| 162 | * @return $this |
||
| 163 | */ |
||
| 164 | public function setDimension($maxWidth, $maxHeight) |
||
| 165 | {
|
||
| 166 | $this->dimensions = array($maxWidth, $maxHeight); |
||
| 167 | return $this; |
||
| 168 | } |
||
| 169 | |||
| 170 | /** |
||
| 171 | * Returns the full path of the image ex 'location/image.mime' |
||
| 172 | * |
||
| 173 | * @return string |
||
| 174 | */ |
||
| 175 | public function getFullPath() |
||
| 176 | {
|
||
| 177 | $this->fullPath = $this->location . '/' . $this->name . '.' . $this->mime; |
||
| 178 | return $this->fullPath; |
||
| 179 | } |
||
| 180 | |||
| 181 | /** |
||
| 182 | * Returns the image size in bytes |
||
| 183 | * |
||
| 184 | * @return int |
||
| 185 | */ |
||
| 186 | public function getSize() |
||
| 187 | {
|
||
| 188 | return (int) $this->_files['size']; |
||
| 189 | } |
||
| 190 | |||
| 191 | /** |
||
| 192 | * Define a min and max image size for uploading |
||
| 193 | * |
||
| 194 | * @param $min int minimum value in bytes |
||
| 195 | * @param $max int maximum value in bytes |
||
| 196 | * |
||
| 197 | * @return $this |
||
| 198 | */ |
||
| 199 | public function setSize($min, $max) |
||
| 204 | |||
| 205 | /** |
||
| 206 | * Returns a JSON format of the image width, height, name, mime ... |
||
| 207 | * |
||
| 208 | * @return string |
||
| 209 | */ |
||
| 210 | public function getJson() |
||
| 211 | {
|
||
| 212 | /* gather image info for json storage */ |
||
| 225 | |||
| 226 | /** |
||
| 227 | * Returns the image mime type |
||
| 228 | * |
||
| 229 | * @return null|string |
||
| 230 | */ |
||
| 231 | public function getMime() |
||
| 238 | |||
| 239 | /** |
||
| 240 | * Define a mime type for uploading |
||
| 241 | * |
||
| 242 | * @param array $fileTypes |
||
| 243 | * |
||
| 244 | * @return $this |
||
| 245 | */ |
||
| 246 | public function setMime(array $fileTypes) |
||
| 251 | |||
| 252 | /** |
||
| 253 | * Gets the real image mime type |
||
| 254 | * |
||
| 255 | * @param $tmp_name string The upload tmp directory |
||
| 256 | * |
||
| 257 | * @return null|string |
||
| 258 | */ |
||
| 259 | protected function getImageMime($tmp_name) |
||
| 269 | |||
| 270 | /** |
||
| 271 | * Returns error string or false if no errors occurred |
||
| 272 | * |
||
| 273 | * @return string|false |
||
| 274 | */ |
||
| 275 | public function getError() |
||
| 279 | |||
| 280 | /** |
||
| 281 | * Returns the image name |
||
| 282 | * |
||
| 283 | * @return string |
||
| 284 | */ |
||
| 285 | public function getName() |
||
| 293 | |||
| 294 | /** |
||
| 295 | * Provide image name if not provided |
||
| 296 | * |
||
| 297 | * @param null $isNameProvided |
||
| 298 | * @return $this |
||
| 299 | */ |
||
| 300 | public function setName($isNameProvided = null) |
||
| 308 | |||
| 309 | /** |
||
| 310 | * Returns the image width |
||
| 311 | * |
||
| 312 | * @return int |
||
| 313 | */ |
||
| 314 | public function getWidth() |
||
| 323 | |||
| 324 | /** |
||
| 325 | * Returns the image height in pixels |
||
| 326 | * |
||
| 327 | * @return int |
||
| 328 | */ |
||
| 329 | public function getHeight() |
||
| 338 | |||
| 339 | /** |
||
| 340 | * Returns the storage / folder name |
||
| 341 | * |
||
| 342 | * @return string |
||
| 343 | */ |
||
| 344 | public function getLocation() |
||
| 352 | |||
| 353 | /** |
||
| 354 | * Validate directory/permission before creating a folder |
||
| 355 | * |
||
| 356 | * @param $dir string the folder name to check |
||
| 357 | * |
||
| 358 | * @return bool |
||
| 359 | */ |
||
| 360 | private function isDirectoryValid($dir) |
||
| 371 | |||
| 372 | /** |
||
| 373 | * Creates a location for upload storage |
||
| 374 | * |
||
| 375 | * @param $dir string the folder name to create |
||
| 376 | * @param int $permission chmod permission |
||
| 377 | * |
||
| 378 | * @return $this |
||
| 379 | */ |
||
| 380 | public function setLocation($dir = 'bulletproof', $permission = 0666) |
||
| 398 | |||
| 399 | /** |
||
| 400 | * This methods validates and uploads the image |
||
| 401 | * @return false|Image |
||
| 402 | */ |
||
| 403 | public function upload() |
||
| 464 | |||
| 465 | |||
| 466 | /** |
||
| 467 | * Final upload method to be called, isolated for testing purposes |
||
| 468 | * |
||
| 469 | * @param $tmp_name int the temporary location of the image file |
||
| 470 | * @param $destination int upload destination |
||
| 471 | * |
||
| 472 | * @return bool |
||
| 473 | */ |
||
| 474 | public function moveUploadedFile($tmp_name, $destination) |
||
| 478 | } |