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 | } |
||
103 | |||
104 | $this->_files = $_files; |
||
105 | } |
||
106 | |||
107 | /** |
||
108 | * @param mixed $offset |
||
109 | * @param mixed $value |
||
110 | * @return null |
||
111 | */ |
||
112 | public function offsetSet($offset, $value){} |
||
113 | |||
114 | /** |
||
115 | * @param mixed $offset |
||
116 | * @return null |
||
117 | */ |
||
118 | public function offsetExists($offset){} |
||
119 | |||
120 | /** |
||
121 | * @param mixed $offset |
||
122 | * @return null |
||
123 | */ |
||
124 | public function offsetUnset($offset){} |
||
125 | |||
126 | /** |
||
127 | * Gets array value \ArrayAccess |
||
128 | * |
||
129 | * @param mixed $offset |
||
130 | * |
||
131 | * @return string|boolean |
||
132 | */ |
||
133 | public function offsetGet($offset) |
||
134 | { |
||
135 | // return error if requested |
||
136 | if ($offset == 'error') { |
||
137 | return $this->error; |
||
138 | } |
||
139 | |||
140 | // return false if $image['key'] isn't found |
||
141 | if (!isset($this->_files[$offset])) { |
||
142 | return false; |
||
143 | } |
||
144 | |||
145 | $this->_files = $this->_files[$offset]; |
||
146 | |||
147 | // check for common upload errors |
||
148 | if(isset($this->_files['error'])){ |
||
149 | $this->error = $this->commonUploadErrors[$this->_files['error']]; |
||
150 | } |
||
151 | |||
152 | return true; |
||
153 | } |
||
154 | |||
155 | /** |
||
156 | * Sets max image height and width limit |
||
157 | * |
||
158 | * @param $maxWidth int max width value |
||
159 | * @param $maxHeight int max height value |
||
160 | * |
||
161 | * @return $this |
||
162 | */ |
||
163 | public function setDimension($maxWidth, $maxHeight) |
||
164 | { |
||
165 | $this->dimensions = array($maxWidth, $maxHeight); |
||
166 | return $this; |
||
167 | } |
||
168 | |||
169 | /** |
||
170 | * Returns the full path of the image ex 'location/image.mime' |
||
171 | * |
||
172 | * @return string |
||
173 | */ |
||
174 | public function getFullPath() |
||
175 | { |
||
176 | return $this->fullPath = $this->getLocation() . '/' . $this->getName() . '.' . $this->getMime(); |
||
177 | } |
||
178 | |||
179 | /** |
||
180 | * Returns the image size in bytes |
||
181 | * |
||
182 | * @return int |
||
183 | */ |
||
184 | public function getSize() |
||
185 | { |
||
186 | return (int) $this->_files['size']; |
||
187 | } |
||
188 | |||
189 | /** |
||
190 | * Define a min and max image size for uploading |
||
191 | * |
||
192 | * @param $min int minimum value in bytes |
||
193 | * @param $max int maximum value in bytes |
||
194 | * |
||
195 | * @return $this |
||
196 | */ |
||
197 | public function setSize($min, $max) |
||
202 | |||
203 | /** |
||
204 | * Returns a JSON format of the image width, height, name, mime ... |
||
205 | * |
||
206 | * @return string |
||
207 | */ |
||
208 | public function getJson() |
||
209 | { |
||
210 | /* gather image info for json storage */ |
||
223 | |||
224 | /** |
||
225 | * Returns the image mime type |
||
226 | * |
||
227 | * @return null|string |
||
228 | */ |
||
229 | public function getMime() |
||
236 | |||
237 | /** |
||
238 | * Define a mime type for uploading |
||
239 | * |
||
240 | * @param array $fileTypes |
||
241 | * |
||
242 | * @return $this |
||
243 | */ |
||
244 | public function setMime(array $fileTypes) |
||
249 | |||
250 | /** |
||
251 | * Gets the real image mime type |
||
252 | * |
||
253 | * @param $tmp_name string The upload tmp directory |
||
254 | * |
||
255 | * @return null|string |
||
256 | */ |
||
257 | protected function getImageMime($tmp_name) |
||
267 | |||
268 | /** |
||
269 | * Returns error string or false if no errors occurred |
||
270 | * |
||
271 | * @return string|false |
||
272 | */ |
||
273 | public function getError() |
||
277 | |||
278 | /** |
||
279 | * Returns the image name |
||
280 | * |
||
281 | * @return string |
||
282 | */ |
||
283 | public function getName() |
||
291 | |||
292 | /** |
||
293 | * Provide image name if not provided |
||
294 | * |
||
295 | * @param null $isNameProvided |
||
296 | * @return $this |
||
297 | */ |
||
298 | public function setName($isNameProvided = null) |
||
306 | |||
307 | /** |
||
308 | * Returns the image width |
||
309 | * |
||
310 | * @return int |
||
311 | */ |
||
312 | public function getWidth() |
||
321 | |||
322 | /** |
||
323 | * Returns the image height in pixels |
||
324 | * |
||
325 | * @return int |
||
326 | */ |
||
327 | public function getHeight() |
||
336 | |||
337 | /** |
||
338 | * Returns the storage / folder name |
||
339 | * |
||
340 | * @return string |
||
341 | */ |
||
342 | public function getLocation() |
||
350 | |||
351 | /** |
||
352 | * Validate directory/permission before creating a folder |
||
353 | * |
||
354 | * @param $dir string the folder name to check |
||
355 | * |
||
356 | * @return bool |
||
357 | */ |
||
358 | private function isDirectoryValid($dir) |
||
362 | |||
363 | /** |
||
364 | * Creates a location for upload storage |
||
365 | * |
||
366 | * @param $dir string the folder name to create |
||
367 | * @param int $permission chmod permission |
||
368 | * |
||
369 | * @return $this |
||
370 | */ |
||
371 | public function setLocation($dir = 'bulletproof', $permission = 0666) |
||
390 | |||
391 | /** |
||
392 | * This methods validates and uploads the image |
||
393 | * @return false|Image |
||
394 | */ |
||
395 | public function upload() |
||
455 | |||
456 | |||
457 | /** |
||
458 | * Final upload method to be called, isolated for testing purposes |
||
459 | * |
||
460 | * @param $tmp_name int the temporary location of the image file |
||
461 | * @param $destination int upload destination |
||
462 | * |
||
463 | * @return bool |
||
464 | */ |
||
465 | public function moveUploadedFile($tmp_name, $destination) |
||
469 | } |