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()) |
||
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) |
||
156 | |||
157 | /** |
||
158 | * Sets max image height and width limit |
||
159 | * |
||
160 | * @param $maxWidth int max width value |
||
161 | * @param $maxHeight int max height value |
||
162 | * |
||
163 | * @return $this |
||
164 | */ |
||
165 | public function setDimension($maxWidth, $maxHeight) |
||
170 | |||
171 | /** |
||
172 | * Returns the full path of the image ex 'location/image.mime' |
||
173 | * |
||
174 | * @return string |
||
175 | */ |
||
176 | public function getFullPath() |
||
181 | |||
182 | /** |
||
183 | * Returns the image size in bytes |
||
184 | * |
||
185 | * @return int |
||
186 | */ |
||
187 | public function getSize() |
||
191 | |||
192 | /** |
||
193 | * Define a min and max image size for uploading |
||
194 | * |
||
195 | * @param $min int minimum value in bytes |
||
196 | * @param $max int maximum value in bytes |
||
197 | * |
||
198 | * @return $this |
||
199 | */ |
||
200 | public function setSize($min, $max) |
||
205 | |||
206 | /** |
||
207 | * Returns a JSON format of the image width, height, name, mime ... |
||
208 | * |
||
209 | * @return string |
||
210 | */ |
||
211 | public function getJson() |
||
226 | |||
227 | /** |
||
228 | * Returns the image mime type |
||
229 | * |
||
230 | * @return null|string |
||
231 | */ |
||
232 | public function getMime() |
||
239 | |||
240 | /** |
||
241 | * Define a mime type for uploading |
||
242 | * |
||
243 | * @param array $fileTypes |
||
244 | * |
||
245 | * @return $this |
||
246 | */ |
||
247 | public function setMime(array $fileTypes) |
||
252 | |||
253 | /** |
||
254 | * Gets the real image mime type |
||
255 | * |
||
256 | * @param $tmp_name string The upload tmp directory |
||
257 | * |
||
258 | * @return null|string |
||
259 | */ |
||
260 | protected function getImageMime($tmp_name) |
||
270 | |||
271 | /** |
||
272 | * Returns error string or false if no errors occurred |
||
273 | * |
||
274 | * @return string|false |
||
275 | */ |
||
276 | public function getError() |
||
280 | |||
281 | /** |
||
282 | * Returns the image name |
||
283 | * |
||
284 | * @return string |
||
285 | */ |
||
286 | public function getName() |
||
294 | |||
295 | /** |
||
296 | * Provide image name if not provided |
||
297 | * |
||
298 | * @param null $isNameProvided |
||
299 | * @return $this |
||
300 | */ |
||
301 | public function setName($isNameProvided = null) |
||
309 | |||
310 | /** |
||
311 | * Returns the image width |
||
312 | * |
||
313 | * @return int |
||
314 | */ |
||
315 | public function getWidth() |
||
324 | |||
325 | /** |
||
326 | * Returns the image height in pixels |
||
327 | * |
||
328 | * @return int |
||
329 | */ |
||
330 | public function getHeight() |
||
339 | |||
340 | /** |
||
341 | * Returns the storage / folder name |
||
342 | * |
||
343 | * @return string |
||
344 | */ |
||
345 | public function getLocation() |
||
353 | |||
354 | /** |
||
355 | * Creates a location for upload storage |
||
356 | * |
||
357 | * @param $dir string the folder name to create |
||
358 | * @param int $permission chmod permission |
||
359 | * |
||
360 | * @return $this |
||
361 | */ |
||
362 | public function setLocation($dir = 'bulletproof', $permission = 0666) |
||
381 | |||
382 | /** |
||
383 | * This methods validates and uploads the image |
||
384 | * @return false|null|Image |
||
|
|||
385 | */ |
||
386 | 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 | public function moveUploadedFile($tmp_name, $destination) |
||
461 | } |
This check looks for the generic type
array
as a return type and suggests a more specific type. This type is inferred from the actual code.