Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
Complex classes like Uploader 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 Uploader, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
12 | class Uploader{ |
||
|
|||
13 | |||
14 | /** |
||
15 | * The mime types allowed for upload |
||
16 | * |
||
17 | * @var array |
||
18 | */ |
||
19 | private static $allowedMIME = [ |
||
20 | "image" => array('image/jpeg', 'image/png', 'image/gif'), |
||
21 | "csv" => array('text/csv', 'application/vnd.ms-excel', 'text/plain'), |
||
22 | "file" => array('application/msword', |
||
23 | 'application/vnd.openxmlformats-officedocument.wordprocessingml.document', |
||
24 | 'application/pdf', |
||
25 | 'application/zip', |
||
26 | 'application/vnd.ms-powerpoint') |
||
27 | ]; |
||
28 | |||
29 | /** |
||
30 | * The min and max image size allowed for upload (in bytes) |
||
31 | * 1 KB = 1024 bytes, and 1 MB = 1048,576 bytes. |
||
32 | * |
||
33 | * @var array |
||
34 | */ |
||
35 | private static $fileSize = [100, 5242880]; |
||
36 | |||
37 | /** |
||
38 | * The max height and width image allowed for image |
||
39 | * |
||
40 | * @var array |
||
41 | */ |
||
42 | private static $dimensions = [2000, 2000]; |
||
43 | |||
44 | /** |
||
45 | * Array of validation errors |
||
46 | * |
||
47 | * @var array |
||
48 | */ |
||
49 | private static $errors = []; |
||
50 | |||
51 | /*** |
||
52 | * @access private |
||
53 | */ |
||
54 | private function __construct() {} |
||
55 | |||
56 | /** |
||
57 | * upload profile picture |
||
58 | * |
||
59 | * @param array $file |
||
60 | * @param mixed $id random id used in creating filename |
||
61 | * @return mixed false in case of failure, otherwise array of file created |
||
62 | * |
||
63 | */ |
||
64 | public static function uploadPicture($file, $id){ |
||
68 | |||
69 | /** |
||
70 | * upload a file - default |
||
71 | * |
||
72 | * @param array $file |
||
73 | * @param mixed $id random id used for creating filename |
||
74 | * @return mixed false in case of failure, array otherwise |
||
75 | * |
||
76 | */ |
||
77 | public static function uploadFile($file, $id = null){ |
||
80 | |||
81 | /** |
||
82 | * upload a CSV File |
||
83 | * |
||
84 | * @param array $file |
||
85 | * @return mixed false in case of failure, array otherwise |
||
86 | * |
||
87 | */ |
||
88 | public static function uploadCSV($file){ |
||
91 | |||
92 | /** |
||
93 | * upload & validate file |
||
94 | * |
||
95 | * @param array $file |
||
96 | * @param string $dir directory where we will upload the file |
||
97 | * @param mixed $id random id used for creating filename |
||
98 | * @param string $type it tells whether the file is image, csv, or normal file(default). |
||
99 | * @return mixed false in case of failure, array otherwise |
||
100 | * @throws Exception If file couldn't be uploaded |
||
101 | * |
||
102 | */ |
||
103 | private static function upload($file, $dir, $id, $type = "file"){ |
||
185 | |||
186 | /** |
||
187 | * get mime type allowed from $allowedMIME |
||
188 | * |
||
189 | * @param string $key |
||
190 | * @return array |
||
191 | */ |
||
192 | private static function getAllowedMime($key){ |
||
195 | |||
196 | /** |
||
197 | * If you can have only one file name based on each user, Then: |
||
198 | * Before uploading every new file, Delete all files with the same name and different extensions |
||
199 | * |
||
200 | * @param string $filePathWithoutExtension |
||
201 | * @param array $allowedMIME |
||
202 | * |
||
203 | */ |
||
204 | private static function deleteFiles($filePathWithoutExtension, $allowedMIME){ |
||
215 | |||
216 | /** |
||
217 | * Deletes a file |
||
218 | * |
||
219 | * @param string $path |
||
220 | * @throws Exception File couldn't be deleted |
||
221 | * |
||
222 | */ |
||
223 | public static function deleteFile($path){ |
||
232 | |||
233 | /** |
||
234 | * create a directory with random hashed name |
||
235 | * |
||
236 | * @param string $dir |
||
237 | * @return string |
||
238 | * @throws Exception If directory couldn't be created or If directory already exists! |
||
239 | */ |
||
240 | public static function createDirectory($dir){ |
||
256 | |||
257 | /** |
||
258 | * Deletes a directory. |
||
259 | * |
||
260 | * @param string $dir |
||
261 | * @throws Exception If directory couldn't be deleted |
||
262 | */ |
||
263 | public static function deleteDir($dir){ |
||
268 | |||
269 | /** |
||
270 | * checks if file exists in the File System or not |
||
271 | * |
||
272 | * @param string $path |
||
273 | * @return boolean |
||
274 | * |
||
275 | */ |
||
276 | public static function isFileExists($path){ |
||
279 | |||
280 | /** |
||
281 | * deletes a directory recursively |
||
282 | * |
||
283 | * @param string $dir |
||
284 | * @return boolean |
||
285 | * |
||
286 | */ |
||
287 | private static function delTree($dir) { |
||
297 | |||
298 | /** |
||
299 | * get mime type of file |
||
300 | * |
||
301 | * Don't use either $_FILES["file"]["type"], or pathinfo($_FILES['file']['name'], PATHINFO_EXTENSION, |
||
302 | * Because their values aren't secure and can be easily be spoofed. |
||
303 | * |
||
304 | * @param array $file |
||
305 | * @return mixed false if failed, string otherwise |
||
306 | * @throws Exception if finfo_open() method doesn't exists |
||
307 | * |
||
308 | */ |
||
309 | View Code Duplication | private static function mime($file){ |
|
325 | |||
326 | /** |
||
327 | * get hashed file name, and Optionally provided by an id |
||
328 | * |
||
329 | * @access private |
||
330 | * @param string $id random id |
||
331 | * @return string hashed file name |
||
332 | * |
||
333 | */ |
||
334 | private static function getHashedName($id = null){ |
||
339 | |||
340 | /** |
||
341 | * Convert/Map the MIME of a file to extension |
||
342 | * |
||
343 | * @param string $mime |
||
344 | * @return string extension |
||
345 | * |
||
346 | */ |
||
347 | private static function MimeToExtension($mime){ |
||
360 | |||
361 | /** |
||
362 | * get file name |
||
363 | * This ensures file name will be safe |
||
364 | * |
||
365 | * @param array $file |
||
366 | * @return string |
||
367 | * |
||
368 | */ |
||
369 | private static function getFileName($file){ |
||
376 | |||
377 | /** |
||
378 | * get errors |
||
379 | * |
||
380 | * @return array errors |
||
381 | */ |
||
382 | public static function errors(){ |
||
385 | |||
386 | } |
||
387 |
You can fix this by adding a namespace to your class:
When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.