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 |
||
20 | class Image implements \ArrayAccess, \Serializable |
||
|
|||
21 | { |
||
22 | /** |
||
23 | * @var string The new image name, to be provided or will be generated. |
||
24 | */ |
||
25 | protected $name; |
||
26 | |||
27 | /** |
||
28 | * @var int The image width in pixels |
||
29 | */ |
||
30 | protected $width; |
||
31 | |||
32 | /** |
||
33 | * @var int The image height in pixels |
||
34 | */ |
||
35 | protected $height; |
||
36 | |||
37 | /** |
||
38 | * @var string The image mime type (extension) |
||
39 | */ |
||
40 | protected $mime; |
||
41 | |||
42 | /** |
||
43 | * @var string The full image path (dir + image + mime) |
||
44 | */ |
||
45 | protected $fullPath; |
||
46 | |||
47 | /** |
||
48 | * @var string The folder or image storage location |
||
49 | */ |
||
50 | protected $location; |
||
51 | |||
52 | /** |
||
53 | * @var array A json format of all information about an image |
||
54 | */ |
||
55 | protected $serialize = array(); |
||
56 | |||
57 | /** |
||
58 | * @var array The min and max image size allowed for upload (in bytes) |
||
59 | */ |
||
60 | protected $size = array(100, 50000); |
||
61 | |||
62 | /** |
||
63 | * @var array The max height and width image allowed |
||
64 | */ |
||
65 | protected $dimensions = array(500, 5000); |
||
66 | |||
67 | /** |
||
68 | * @var array The mime types allowed for upload |
||
69 | */ |
||
70 | protected $mimeTypes = array("jpeg", "png", "gif"); |
||
71 | |||
72 | /** |
||
73 | * @var array list of known image types |
||
74 | */ |
||
75 | protected $imageMimes = array( |
||
76 | 1 => "gif", "jpeg", "png", "swf", "psd", |
||
77 | "bmp", "tiff", "tiff", "jpc", "jp2", "jpx", |
||
78 | "jb2", "swc", "iff", "wbmp", "xbm", "ico" |
||
79 | ); |
||
80 | |||
81 | /** |
||
82 | * @var array storage for the $_FILES global array |
||
83 | */ |
||
84 | private $_files = array(); |
||
85 | |||
86 | /** |
||
87 | * @var array error messages strings |
||
88 | */ |
||
89 | protected $commonErrors = array( |
||
90 | UPLOAD_ERR_OK => "", |
||
91 | UPLOAD_ERR_INI_SIZE => "Image is larger than the specified amount set by the server", |
||
92 | UPLOAD_ERR_FORM_SIZE => "Image is larger than the specified amount specified by browser", |
||
93 | UPLOAD_ERR_PARTIAL => "Image could not be fully uploaded. Please try again later", |
||
94 | UPLOAD_ERR_NO_FILE => "Image is not found", |
||
95 | UPLOAD_ERR_NO_TMP_DIR => "Can't write to disk, due to server configuration ( No tmp dir found )", |
||
96 | UPLOAD_ERR_CANT_WRITE => "Failed to write file to disk. Please check you file permissions", |
||
97 | UPLOAD_ERR_EXTENSION => "A PHP extension has halted this file upload process" |
||
98 | ); |
||
99 | |||
100 | /** |
||
101 | * Constructor |
||
102 | * |
||
103 | * @param array $_files represents the $_FILES array passed as dependency |
||
104 | */ |
||
105 | public function __construct(array $_files = []) |
||
109 | |||
110 | /** |
||
111 | * Gets the real image mime type |
||
112 | * |
||
113 | * @param $tmp_name string The upload tmp directory |
||
114 | * |
||
115 | * @return bool|string |
||
116 | */ |
||
117 | protected function getImageMime($tmp_name) |
||
125 | |||
126 | /** |
||
127 | * @param mixed $offset |
||
128 | * @param mixed $value |
||
129 | */ |
||
130 | public function offsetSet($offset, $value){} |
||
131 | |||
132 | /** |
||
133 | * @param mixed $offset |
||
134 | * @return null |
||
135 | */ |
||
136 | public function offsetExists($offset){} |
||
137 | |||
138 | /** |
||
139 | * @param mixed $offset |
||
140 | */ |
||
141 | public function offsetUnset($offset){} |
||
142 | |||
143 | /** |
||
144 | * Gets array value \ArrayAccess |
||
145 | * |
||
146 | * @param mixed $offset |
||
147 | * |
||
148 | * @return bool|mixed |
||
149 | */ |
||
150 | public function offsetGet($offset) |
||
159 | |||
160 | |||
161 | /** |
||
162 | * Provide image name if not provided |
||
163 | * |
||
164 | * @param null $name |
||
165 | * @return $this |
||
166 | */ |
||
167 | public function setName($name = null) |
||
175 | |||
176 | /** |
||
177 | * Define a mime type for uploading |
||
178 | * |
||
179 | * @param array $fileTypes |
||
180 | * |
||
181 | * @return $this |
||
182 | */ |
||
183 | public function setMime(array $fileTypes) |
||
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 | * Creates a location for upload storage |
||
205 | * |
||
206 | * @param $dir string the folder name to create |
||
207 | * @param int $permission chmod permission |
||
208 | * |
||
209 | * @return $this |
||
210 | */ |
||
211 | public function setLocation($dir = "Uploads", $permission = 0666) |
||
224 | |||
225 | /** |
||
226 | * Sets acceptable max image height and width |
||
227 | * |
||
228 | * @param $maxWidth int max width value |
||
229 | * @param $maxHeight int max height value |
||
230 | * |
||
231 | * @return $this |
||
232 | */ |
||
233 | public function setDimension($maxWidth, $maxHeight) |
||
238 | |||
239 | /** |
||
240 | * Returns the image name |
||
241 | * |
||
242 | * @return string |
||
243 | */ |
||
244 | public function getName() |
||
252 | |||
253 | /** |
||
254 | * Returns the full path of the image ex "location/image.mime" |
||
255 | * |
||
256 | * @return string |
||
257 | */ |
||
258 | public function getFullPath() |
||
263 | |||
264 | /** |
||
265 | * Returns the image size in bytes |
||
266 | * |
||
267 | * @return int |
||
268 | */ |
||
269 | public function getSize() |
||
273 | |||
274 | /** |
||
275 | * Returns the image height in pixels |
||
276 | * |
||
277 | * @return int |
||
278 | */ |
||
279 | public function getHeight() |
||
288 | |||
289 | /** |
||
290 | * Returns the image width |
||
291 | * |
||
292 | * @return int |
||
293 | */ |
||
294 | public function getWidth() |
||
303 | |||
304 | /** |
||
305 | * Returns the storage / folder name |
||
306 | * |
||
307 | * @return string |
||
308 | */ |
||
309 | public function getLocation() |
||
317 | |||
318 | /** |
||
319 | * Returns a JSON format of the image width, height, name, mime ... |
||
320 | * |
||
321 | * @return string |
||
322 | */ |
||
323 | public function getJson() |
||
327 | |||
328 | /** |
||
329 | * Returns the image mime type |
||
330 | * |
||
331 | * @return string |
||
332 | */ |
||
333 | public function getMime() |
||
337 | |||
338 | /** |
||
339 | * This methods validates and uploads the image |
||
340 | * |
||
341 | * @return bool|Image|null |
||
342 | * |
||
343 | * @throws BulletproofException |
||
344 | */ |
||
345 | public function upload() |
||
419 | |||
420 | /** |
||
421 | * Final upload method to be called, isolated for testing purposes |
||
422 | * |
||
423 | * @param $tmp_name int the temporary location of the image file |
||
424 | * @param $destination int upload destination |
||
425 | * |
||
426 | * @return bool |
||
427 | */ |
||
428 | public function moveUploadedFile($tmp_name, $destination) |
||
432 | } |
||
433 |