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 |
||
18 | class Image implements \ArrayAccess |
||
19 | { |
||
20 | /** |
||
21 | * @var string The new image name, to be provided or will be generated. |
||
22 | */ |
||
23 | protected $name; |
||
24 | |||
25 | /** |
||
26 | * @var int The image width in pixels |
||
27 | */ |
||
28 | protected $width; |
||
29 | |||
30 | /** |
||
31 | * @var int The image height in pixels |
||
32 | */ |
||
33 | protected $height; |
||
34 | |||
35 | /** |
||
36 | * @var string The image mime type (extension) |
||
37 | */ |
||
38 | protected $mime; |
||
39 | |||
40 | /** |
||
41 | * @var string The full image path (dir + image + mime) |
||
42 | */ |
||
43 | protected $fullPath; |
||
44 | |||
45 | /** |
||
46 | * @var string The folder or image storage location |
||
47 | */ |
||
48 | protected $location; |
||
49 | |||
50 | /** |
||
51 | * @var array The min and max image size allowed for upload (in bytes) |
||
52 | */ |
||
53 | protected $size = array(100, 500000); |
||
54 | |||
55 | /** |
||
56 | * @var array The max height and width image allowed |
||
57 | */ |
||
58 | protected $dimensions = array(5000, 5000); |
||
59 | |||
60 | /** |
||
61 | * @var array The mime types allowed for upload |
||
62 | */ |
||
63 | protected $mimeTypes = array('jpeg', 'png', 'gif', 'jpg'); |
||
64 | |||
65 | /** |
||
66 | * @var array list of known image types |
||
67 | */ |
||
68 | protected $acceptedMimes = array( |
||
69 | 1 => 'gif', 'jpeg', 'png', 'swf', 'psd', |
||
70 | 'bmp', 'tiff', 'tiff', 'jpc', 'jp2', 'jpx', |
||
71 | 'jb2', 'swc', 'iff', 'wbmp', 'xbm', 'ico' |
||
72 | ); |
||
73 | |||
74 | /** |
||
75 | * @var array error messages strings |
||
76 | */ |
||
77 | protected $commonUploadErrors = array( |
||
78 | UPLOAD_ERR_OK => '', |
||
79 | UPLOAD_ERR_INI_SIZE => 'Image is larger than the specified amount set by the server', |
||
80 | UPLOAD_ERR_FORM_SIZE => 'Image is larger than the specified amount specified by browser', |
||
81 | UPLOAD_ERR_PARTIAL => 'Image could not be fully uploaded. Please try again later', |
||
82 | UPLOAD_ERR_NO_FILE => 'Image is not found', |
||
83 | UPLOAD_ERR_NO_TMP_DIR => 'Can\'t write to disk, due to server configuration ( No tmp dir found )', |
||
84 | UPLOAD_ERR_CANT_WRITE => 'Failed to write file to disk. Please check you file permissions', |
||
85 | UPLOAD_ERR_EXTENSION => 'A PHP extension has halted this file upload process' |
||
86 | ); |
||
87 | |||
88 | /** |
||
89 | * @var array storage for the $_FILES global array |
||
90 | */ |
||
91 | private $_files = array(); |
||
92 | |||
93 | /** |
||
94 | * @var string storage for any errors |
||
95 | */ |
||
96 | private $error = ''; |
||
97 | |||
98 | /** |
||
99 | * @param array $_files represents the $_FILES array passed as dependency |
||
100 | */ |
||
101 | public function __construct(array $_files = array()) |
||
110 | |||
111 | /** |
||
112 | * @param mixed $offset |
||
113 | * @param mixed $value |
||
114 | * |
||
115 | * @return null |
||
116 | */ |
||
117 | public function offsetSet($offset, $value){} |
||
118 | |||
119 | /** |
||
120 | * @param mixed $offset |
||
121 | * |
||
122 | * @return null |
||
123 | */ |
||
124 | public function offsetExists($offset){} |
||
125 | |||
126 | /** |
||
127 | * @param mixed $offset |
||
128 | * |
||
129 | * @return null |
||
130 | */ |
||
131 | public function offsetUnset($offset){} |
||
132 | |||
133 | /** |
||
134 | * Gets array value \ArrayAccess |
||
135 | * |
||
136 | * @param mixed $offset |
||
137 | * @return string|boolean |
||
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) |
||
176 | |||
177 | /** |
||
178 | * Returns the full path of the image ex 'location/image.mime' |
||
179 | * |
||
180 | * @return string |
||
181 | */ |
||
182 | public function getFullPath() |
||
186 | |||
187 | /** |
||
188 | * Returns the image size in bytes |
||
189 | * |
||
190 | * @return int |
||
191 | */ |
||
192 | public function getSize() |
||
196 | |||
197 | /** |
||
198 | * Define a min and max image size for uploading |
||
199 | * |
||
200 | * @param $min int minimum value in bytes |
||
201 | * @param $max int maximum value in bytes |
||
202 | * |
||
203 | * @return $this |
||
204 | */ |
||
205 | public function setSize($min, $max) |
||
210 | |||
211 | /** |
||
212 | * Returns a JSON format of the image width, height, name, mime ... |
||
213 | * |
||
214 | * @return string |
||
215 | */ |
||
216 | public function getJson() |
||
230 | |||
231 | /** |
||
232 | * Returns the image mime type |
||
233 | * |
||
234 | * @return null|string |
||
235 | */ |
||
236 | public function getMime() |
||
244 | |||
245 | /** |
||
246 | * Define a mime type for uploading |
||
247 | * |
||
248 | * @param array $fileTypes |
||
249 | * |
||
250 | * @return $this |
||
251 | */ |
||
252 | public function setMime(array $fileTypes) |
||
257 | |||
258 | /** |
||
259 | * Gets the real image mime type |
||
260 | * |
||
261 | * @param $tmp_name string The upload tmp directory |
||
262 | * |
||
263 | * @return null|string |
||
264 | */ |
||
265 | protected function getImageMime($tmp_name) |
||
274 | |||
275 | /** |
||
276 | * Returns error string or false if no errors occurred |
||
277 | * |
||
278 | * @return string|false |
||
279 | */ |
||
280 | public function getError() |
||
284 | |||
285 | /** |
||
286 | * Returns the image name |
||
287 | * |
||
288 | * @return string |
||
289 | */ |
||
290 | public function getName() |
||
298 | |||
299 | /** |
||
300 | * Provide image name if not provided |
||
301 | * |
||
302 | * @param null $isNameProvided |
||
303 | * @return $this |
||
304 | */ |
||
305 | public function setName($isNameProvided = null) |
||
313 | |||
314 | /** |
||
315 | * Returns the image width |
||
316 | * |
||
317 | * @return int |
||
318 | */ |
||
319 | public function getWidth() |
||
328 | |||
329 | /** |
||
330 | * Returns the image height in pixels |
||
331 | * |
||
332 | * @return int |
||
333 | */ |
||
334 | public function getHeight() |
||
343 | |||
344 | /** |
||
345 | * Returns the storage / folder name |
||
346 | * |
||
347 | * @return string |
||
348 | */ |
||
349 | public function getLocation() |
||
357 | |||
358 | /** |
||
359 | * Validate directory/permission before creating a folder |
||
360 | * |
||
361 | * @param $dir string the folder name to check |
||
362 | * @return bool |
||
363 | */ |
||
364 | private function isDirectoryValid($dir) |
||
368 | |||
369 | /** |
||
370 | * Creates a location for upload storage |
||
371 | * |
||
372 | * @param $dir string the folder name to create |
||
373 | * @param int $permission chmod permission |
||
374 | * @return $this |
||
375 | */ |
||
376 | public function setLocation($dir = 'bulletproof', $permission = 0666) |
||
395 | |||
396 | |||
397 | /** |
||
398 | * Validate image and upload |
||
399 | * |
||
400 | * @return false|Image |
||
401 | */ |
||
402 | public function upload() |
||
447 | |||
448 | |||
449 | /** |
||
450 | * Final upload method to be called, isolated for testing purposes |
||
451 | * |
||
452 | * @param $tmp_name int the temporary location of the image file |
||
453 | * @param $destination int upload destination |
||
454 | * |
||
455 | * @return bool |
||
456 | */ |
||
457 | protected function isSaved($tmp_name, $destination) |
||
461 | } |