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 A json format of all information about an image |
||
51 | */ |
||
52 | protected $serialize = array(); |
||
53 | |||
54 | /** |
||
55 | * @var array The min and max image size allowed for upload (in bytes) |
||
56 | */ |
||
57 | protected $size = array(100, 500000); |
||
58 | |||
59 | /** |
||
60 | * @var array The max height and width image allowed |
||
61 | */ |
||
62 | protected $dimensions = array(5000, 5000); |
||
63 | |||
64 | /** |
||
65 | * @var array The mime types allowed for upload |
||
66 | */ |
||
67 | protected $mimeTypes = array('jpeg', 'png', 'gif', 'jpg'); |
||
68 | |||
69 | /** |
||
70 | * @var array list of known image types |
||
71 | */ |
||
72 | protected $imageMimes = array( |
||
73 | 1 => 'gif', 'jpeg', 'png', 'swf', 'psd', |
||
74 | 'bmp', 'tiff', 'tiff', 'jpc', 'jp2', 'jpx', |
||
75 | 'jb2', 'swc', 'iff', 'wbmp', 'xbm', 'ico' |
||
76 | ); |
||
77 | /** |
||
78 | * @var array error messages strings |
||
79 | */ |
||
80 | protected $common_upload_errors = array( |
||
81 | UPLOAD_ERR_OK => '', |
||
82 | UPLOAD_ERR_INI_SIZE => 'Image is larger than the specified amount set by the server', |
||
83 | UPLOAD_ERR_FORM_SIZE => 'Image is larger than the specified amount specified by browser', |
||
84 | UPLOAD_ERR_PARTIAL => 'Image could not be fully uploaded. Please try again later', |
||
85 | UPLOAD_ERR_NO_FILE => 'Image is not found', |
||
86 | UPLOAD_ERR_NO_TMP_DIR => 'Can\'t write to disk, due to server configuration ( No tmp dir found )', |
||
87 | UPLOAD_ERR_CANT_WRITE => 'Failed to write file to disk. Please check you file permissions', |
||
88 | UPLOAD_ERR_EXTENSION => 'A PHP extension has halted this file upload process' |
||
89 | ); |
||
90 | /** |
||
91 | * @var array storage for the $_FILES global array |
||
92 | */ |
||
93 | private $_files = array(); |
||
94 | /** |
||
95 | * @var string storage for any errors |
||
96 | */ |
||
97 | private $error = ''; |
||
98 | |||
99 | /** |
||
100 | * @param array $_files represents the $_FILES array passed as dependency |
||
101 | */ |
||
102 | public function __construct(array $_files = array()) |
||
112 | |||
113 | /** |
||
114 | * @param mixed $offset |
||
115 | * @param mixed $value |
||
116 | */ |
||
117 | public function offsetSet($offset, $value) |
||
120 | |||
121 | /** |
||
122 | * @param mixed $offset |
||
123 | * @return null |
||
124 | */ |
||
125 | public function offsetExists($offset) |
||
128 | |||
129 | /** |
||
130 | * @param mixed $offset |
||
131 | */ |
||
132 | public function offsetUnset($offset) |
||
135 | |||
136 | /** |
||
137 | * Gets array value \ArrayAccess |
||
138 | * |
||
139 | * @param mixed $offset |
||
140 | * |
||
141 | * @return bool |
||
|
|||
142 | */ |
||
143 | public function offsetGet($offset) |
||
164 | |||
165 | /** |
||
166 | * Checks for the common upload errors |
||
167 | * |
||
168 | * @param $errors int error constant |
||
169 | * |
||
170 | * @return string |
||
171 | */ |
||
172 | protected function commonUploadErrors($errors) |
||
176 | |||
177 | /** |
||
178 | * Sets acceptable max image height and width |
||
179 | * |
||
180 | * @param $maxWidth int max width value |
||
181 | * @param $maxHeight int max height value |
||
182 | * |
||
183 | * @return $this |
||
184 | */ |
||
185 | 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() |
||
201 | |||
202 | /** |
||
203 | * Returns the image size in bytes |
||
204 | * |
||
205 | * @return int |
||
206 | */ |
||
207 | public function getSize() |
||
211 | |||
212 | /** |
||
213 | * Define a min and max image size for uploading |
||
214 | * |
||
215 | * @param $min int minimum value in bytes |
||
216 | * @param $max int maximum value in bytes |
||
217 | * |
||
218 | * @return $this |
||
219 | */ |
||
220 | public function setSize($min, $max) |
||
225 | |||
226 | /** |
||
227 | * Returns a JSON format of the image width, height, name, mime ... |
||
228 | * |
||
229 | * @return string |
||
230 | */ |
||
231 | public function getJson() |
||
235 | |||
236 | /** |
||
237 | * Returns the image mime type |
||
238 | * |
||
239 | * @return string |
||
240 | */ |
||
241 | public function getMime() |
||
248 | |||
249 | /** |
||
250 | * Define a mime type for uploading |
||
251 | * |
||
252 | * @param array $fileTypes |
||
253 | * |
||
254 | * @return $this |
||
255 | */ |
||
256 | public function setMime(array $fileTypes) |
||
261 | |||
262 | /** |
||
263 | * Gets the real image mime type |
||
264 | * |
||
265 | * @param $tmp_name string The upload tmp directory |
||
266 | * |
||
267 | * @return null|string |
||
268 | */ |
||
269 | protected function getImageMime($tmp_name) |
||
279 | |||
280 | /** |
||
281 | * Returns error string or false if no errors occurred |
||
282 | * |
||
283 | * @return string|bool |
||
284 | */ |
||
285 | public function getError() |
||
289 | |||
290 | /** |
||
291 | * This methods validates and uploads the image |
||
292 | * @return false|Image |
||
293 | */ |
||
294 | public function upload() |
||
371 | |||
372 | /** |
||
373 | * Returns the image name |
||
374 | * |
||
375 | * @return string |
||
376 | */ |
||
377 | public function getName() |
||
385 | |||
386 | /** |
||
387 | * Provide image name if not provided |
||
388 | * |
||
389 | * @param null $isNameProvided |
||
390 | * @return $this |
||
391 | */ |
||
392 | public function setName($isNameProvided = null) |
||
400 | |||
401 | /** |
||
402 | * Returns the image width |
||
403 | * |
||
404 | * @return int |
||
405 | */ |
||
406 | public function getWidth() |
||
415 | |||
416 | /** |
||
417 | * Returns the image height in pixels |
||
418 | * |
||
419 | * @return int |
||
420 | */ |
||
421 | public function getHeight() |
||
430 | |||
431 | /** |
||
432 | * Returns the storage / folder name |
||
433 | * |
||
434 | * @return string |
||
435 | */ |
||
436 | public function getLocation() |
||
444 | |||
445 | /** |
||
446 | * Creates a location for upload storage |
||
447 | * |
||
448 | * @param $dir string the folder name to create |
||
449 | * @param int $permission chmod permission |
||
450 | * |
||
451 | * @return $this |
||
452 | */ |
||
453 | public function setLocation($dir = 'bulletproof', $permission = 0666) |
||
473 | |||
474 | /** |
||
475 | * Final upload method to be called, isolated for testing purposes |
||
476 | * |
||
477 | * @param $tmp_name int the temporary location of the image file |
||
478 | * @param $destination int upload destination |
||
479 | * |
||
480 | * @return bool |
||
481 | */ |
||
482 | public function moveUploadedFile($tmp_name, $destination) |
||
486 | } |
||
487 |
This check compares the return type specified in the
@return
annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.