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"); |
||
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 | /** |
||
79 | * @var array storage for the $_FILES global array |
||
80 | */ |
||
81 | private $_files = array(); |
||
82 | |||
83 | /** |
||
84 | * @var string storage for any errors |
||
85 | */ |
||
86 | private $error = ""; |
||
87 | |||
88 | /** |
||
89 | * @var array error messages strings |
||
90 | */ |
||
91 | private $error_messages = array( |
||
92 | 'upload' => array( |
||
93 | UPLOAD_ERR_OK => "", |
||
94 | UPLOAD_ERR_INI_SIZE => "Image is larger than the specified amount set by the server", |
||
95 | UPLOAD_ERR_FORM_SIZE => "Image is larger than the specified amount specified by browser", |
||
96 | UPLOAD_ERR_PARTIAL => "Image could not be fully uploaded. Please try again later", |
||
97 | UPLOAD_ERR_NO_FILE => "Image is not found", |
||
98 | UPLOAD_ERR_NO_TMP_DIR => "Can't write to disk, due to server configuration ( No tmp dir found )", |
||
99 | UPLOAD_ERR_CANT_WRITE => "Failed to write file to disk. Please check you file permissions", |
||
100 | UPLOAD_ERR_EXTENSION => "A PHP extension has halted this file upload process" |
||
101 | ), |
||
102 | 'location' => "Folder %s could not be created", |
||
103 | 'mime_type' => "Invalid File! Only (%s) image types are allowed", |
||
104 | 'file_size' => "Image size should be atleast more than min: %s and less than max: %s kb", |
||
105 | 'dimensions' => "Image height/width should be less than ' %s \ %s ' pixels", |
||
106 | 'too_small' => "Invalid! Image height/width is too small or maybe corrupted", |
||
107 | 'unknown' => "Upload failed, Unknown error occured" |
||
108 | ); |
||
109 | |||
110 | /** |
||
111 | * @param array $_files represents the $_FILES array passed as dependency |
||
112 | */ |
||
113 | public function __construct(array $_files = array()) |
||
117 | |||
118 | /** |
||
119 | * Gets the real image mime type |
||
120 | * |
||
121 | * @param $tmp_name string The upload tmp directory |
||
122 | * |
||
123 | * @return bool|string |
||
124 | */ |
||
125 | protected function getImageMime($tmp_name) |
||
132 | |||
133 | /** |
||
134 | * @param mixed $offset |
||
135 | * @param mixed $value |
||
136 | */ |
||
137 | public function offsetSet($offset, $value){} |
||
138 | |||
139 | /** |
||
140 | * @param mixed $offset |
||
141 | * @return null |
||
142 | */ |
||
143 | public function offsetExists($offset){} |
||
144 | |||
145 | /** |
||
146 | * @param mixed $offset |
||
147 | */ |
||
148 | public function offsetUnset($offset){} |
||
149 | |||
150 | /** |
||
151 | * Gets array value \ArrayAccess |
||
152 | * |
||
153 | * @param mixed $offset |
||
154 | * |
||
155 | * @return bool|mixed |
||
156 | */ |
||
157 | public function offsetGet($offset) |
||
170 | |||
171 | |||
172 | /** |
||
173 | * Provide image name if not provided |
||
174 | * |
||
175 | * @param null $isNameProvided |
||
176 | * @return $this |
||
177 | */ |
||
178 | public function setName($isNameProvided = null) |
||
186 | |||
187 | /** |
||
188 | * Define a mime type for uploading |
||
189 | * |
||
190 | * @param array $fileTypes |
||
191 | * |
||
192 | * @return $this |
||
193 | */ |
||
194 | public function setMime(array $fileTypes) |
||
199 | |||
200 | /** |
||
201 | * Define a min and max image size for uploading |
||
202 | * |
||
203 | * @param $min int minimum value in bytes |
||
204 | * @param $max int maximum value in bytes |
||
205 | * |
||
206 | * @return $this |
||
207 | */ |
||
208 | public function setSize($min, $max) |
||
213 | |||
214 | /** |
||
215 | * Creates a location for upload storage |
||
216 | * |
||
217 | * @param $dir string the folder name to create |
||
218 | * @param int $permission chmod permission |
||
219 | * |
||
220 | * @return $this |
||
221 | */ |
||
222 | public function setLocation($dir = "bulletproof", $permission = 0666) |
||
235 | |||
236 | /** |
||
237 | * Sets acceptable max image height and width |
||
238 | * |
||
239 | * @param $maxWidth int max width value |
||
240 | * @param $maxHeight int max height value |
||
241 | * |
||
242 | * @return $this |
||
243 | */ |
||
244 | public function setDimension($maxWidth, $maxHeight) |
||
249 | |||
250 | /** |
||
251 | * Replace error_messages array values with values of given array |
||
252 | * |
||
253 | * @param $new_error_messages array Array containing new error messages |
||
254 | * |
||
255 | * @return $this |
||
256 | */ |
||
257 | public function setErrorMessages($new_error_messages) |
||
264 | |||
265 | /** |
||
266 | * Returns the image name |
||
267 | * |
||
268 | * @return string |
||
269 | */ |
||
270 | public function getName() |
||
278 | |||
279 | /** |
||
280 | * Returns the full path of the image ex "location/image.mime" |
||
281 | * |
||
282 | * @return string |
||
283 | */ |
||
284 | public function getFullPath() |
||
289 | |||
290 | /** |
||
291 | * Returns the image size in bytes |
||
292 | * |
||
293 | * @return int |
||
294 | */ |
||
295 | public function getSize() |
||
299 | |||
300 | /** |
||
301 | * Returns the image height in pixels |
||
302 | * |
||
303 | * @return int |
||
304 | */ |
||
305 | public function getHeight() |
||
314 | |||
315 | /** |
||
316 | * Returns the image width |
||
317 | * |
||
318 | * @return int |
||
319 | */ |
||
320 | public function getWidth() |
||
329 | |||
330 | /** |
||
331 | * Returns the storage / folder name |
||
332 | * |
||
333 | * @return string |
||
334 | */ |
||
335 | public function getLocation() |
||
343 | |||
344 | /** |
||
345 | * Returns a JSON format of the image width, height, name, mime ... |
||
346 | * |
||
347 | * @return string |
||
348 | */ |
||
349 | public function getJson() |
||
353 | |||
354 | /** |
||
355 | * Returns the image mime type |
||
356 | * |
||
357 | * @return string |
||
358 | */ |
||
359 | public function getMime() |
||
366 | |||
367 | /** |
||
368 | * Returns error string or false if no errors occurred |
||
369 | * |
||
370 | * @return string|bool |
||
371 | */ |
||
372 | public function getError(){ |
||
375 | |||
376 | /** |
||
377 | * Checks for the common upload errors |
||
378 | * |
||
379 | * @param $e int error constant |
||
380 | */ |
||
381 | protected function uploadErrors($e) |
||
386 | |||
387 | /** |
||
388 | * Checks if the image size, of the uploaded file is available |
||
389 | * If it returns false, the file is mostly certainly, not an image. |
||
390 | * @return bool |
||
391 | */ |
||
392 | protected function validateImageSize(){ |
||
399 | |||
400 | /** |
||
401 | * This methods validates and uploads the image |
||
402 | * @return bool|Image|null |
||
403 | * @throws ImageUploaderException |
||
404 | */ |
||
405 | public function upload() |
||
493 | |||
494 | /** |
||
495 | * Final upload method to be called, isolated for testing purposes |
||
496 | * |
||
497 | * @param $tmp_name int the temporary location of the image file |
||
498 | * @param $destination int upload destination |
||
499 | * |
||
500 | * @return bool |
||
501 | */ |
||
502 | public function moveUploadedFile($tmp_name, $destination) |
||
506 | } |
||
507 |
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVar
assignment in line 1 and the$higher
assignment in line 2 are dead. The first because$myVar
is never used and the second because$higher
is always overwritten for every possible time line.