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 | 'ERROR_09' => 'Error! the language could not found', |
||
100 | ), |
||
101 | ); |
||
102 | |||
103 | /** |
||
104 | * @var array storage for the global array |
||
105 | */ |
||
106 | private $_files = array(); |
||
107 | |||
108 | /** |
||
109 | * @var string storage for any errors |
||
110 | */ |
||
111 | private $error = ''; |
||
112 | |||
113 | /** |
||
114 | * @param array $_files represents the $_FILES array passed as dependency |
||
115 | */ |
||
116 | public function __construct(array $_files = array()) |
||
124 | |||
125 | /** |
||
126 | * \ArrayAccess unused method |
||
127 | * |
||
128 | * @param mixed $offset |
||
129 | * @param mixed $value |
||
130 | */ |
||
131 | public function offsetSet($offset, $value) {} |
||
132 | |||
133 | /** |
||
134 | * \ArrayAccess unused method |
||
135 | * |
||
136 | * @param mixed $offset |
||
137 | */ |
||
138 | public function offsetExists($offset){} |
||
139 | |||
140 | /** |
||
141 | * \ArrayAccess unused method |
||
142 | * |
||
143 | * @param mixed $offset |
||
144 | */ |
||
145 | public function offsetUnset($offset){} |
||
146 | |||
147 | /** |
||
148 | * \ArrayAccess - get array value from object |
||
149 | * |
||
150 | * @param mixed $offset |
||
151 | * |
||
152 | * @return string|bool |
||
153 | */ |
||
154 | public function offsetGet($offset) |
||
171 | |||
172 | /** |
||
173 | * Sets max image height and width limit. |
||
174 | * |
||
175 | * @param $maxWidth int max width value |
||
176 | * @param $maxHeight int max height value |
||
177 | * |
||
178 | * @return $this |
||
179 | */ |
||
180 | public function setDimension($maxWidth, $maxHeight) |
||
190 | |||
191 | /** |
||
192 | * Returns the full path of the image ex 'location/image.mime'. |
||
193 | * |
||
194 | * @return string |
||
195 | */ |
||
196 | public function getFullPath() |
||
200 | |||
201 | /** |
||
202 | * Define a language |
||
203 | * |
||
204 | * @param $lang string language code |
||
205 | * |
||
206 | * @return $this |
||
207 | */ |
||
208 | public function setLanguage($lang) |
||
218 | |||
219 | /** |
||
220 | * Returns the image size in bytes. |
||
221 | * |
||
222 | * @return int |
||
223 | */ |
||
224 | public function getSize() |
||
228 | |||
229 | /** |
||
230 | * Define a min and max image size for uploading. |
||
231 | * |
||
232 | * @param $min int minimum value in bytes |
||
233 | * @param $max int maximum value in bytes |
||
234 | * |
||
235 | * @return $this |
||
236 | */ |
||
237 | public function setSize($min, $max) |
||
242 | |||
243 | /** |
||
244 | * Returns a JSON format of the image width, height, name, mime ... |
||
245 | * |
||
246 | * @return string |
||
247 | */ |
||
248 | public function getJson() |
||
262 | |||
263 | /** |
||
264 | * Returns the image mime type. |
||
265 | * |
||
266 | * @return null|string |
||
267 | */ |
||
268 | public function getMime() |
||
276 | |||
277 | /** |
||
278 | * Define a mime type for uploading. |
||
279 | * |
||
280 | * @param array $fileTypes |
||
281 | * |
||
282 | * @return $this |
||
283 | */ |
||
284 | public function setMime(array $fileTypes) |
||
289 | |||
290 | /** |
||
291 | * Gets the real image mime type. |
||
292 | * |
||
293 | * @param $tmp_name string The upload tmp directory |
||
294 | * |
||
295 | * @return null|string |
||
296 | */ |
||
297 | protected function getImageMime($tmp_name) |
||
306 | |||
307 | /** |
||
308 | * Returns error string or false if no errors occurred. |
||
309 | * |
||
310 | * @return string|false |
||
311 | */ |
||
312 | public function getError() |
||
316 | |||
317 | /** |
||
318 | * Returns the image name. |
||
319 | * |
||
320 | * @return string |
||
321 | */ |
||
322 | public function getName() |
||
330 | |||
331 | /** |
||
332 | * Provide image name if not provided. |
||
333 | * |
||
334 | * @param null $isNameProvided |
||
335 | * |
||
336 | * @return $this |
||
337 | */ |
||
338 | public function setName($isNameProvided = null) |
||
346 | |||
347 | /** |
||
348 | * Returns the image width. |
||
349 | * |
||
350 | * @return int |
||
351 | */ |
||
352 | public function getWidth() |
||
362 | |||
363 | /** |
||
364 | * Returns the image height in pixels. |
||
365 | * |
||
366 | * @return int |
||
367 | */ |
||
368 | public function getHeight() |
||
378 | |||
379 | /** |
||
380 | * Returns the storage / folder name. |
||
381 | * |
||
382 | * @return string |
||
383 | */ |
||
384 | public function getLocation() |
||
392 | |||
393 | /** |
||
394 | * Validate directory/permission before creating a folder. |
||
395 | * |
||
396 | * @param $dir string the folder name to check |
||
397 | * |
||
398 | * @return bool |
||
399 | */ |
||
400 | private function isDirectoryValid($dir) |
||
404 | |||
405 | /** |
||
406 | * Creates a location for upload storage. |
||
407 | * |
||
408 | * @param $dir string the folder name to create |
||
409 | * @param int $permission chmod permission |
||
410 | * |
||
411 | * @return $this |
||
412 | */ |
||
413 | public function setLocation($dir = 'bulletproof', $permission = 0666) |
||
433 | |||
434 | /** |
||
435 | * Validate image size, dimension or mimetypes |
||
436 | * |
||
437 | * @return boolean |
||
438 | */ |
||
439 | protected function contraintsValidator() |
||
472 | |||
473 | /** |
||
474 | * Validate and save (upload) file |
||
475 | * |
||
476 | * @return false|Image |
||
477 | */ |
||
478 | public function upload() |
||
490 | |||
491 | /** |
||
492 | * Final upload method to be called, isolated for testing purposes. |
||
493 | * |
||
494 | * @param $tmp_name int the temporary location of the image file |
||
495 | * @param $destination int upload destination |
||
496 | * |
||
497 | * @return bool |
||
498 | */ |
||
499 | protected function isSaved($tmp_name, $destination) |
||
503 | } |