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 SwUploadHandler 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 SwUploadHandler, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
25 | class SwUploadHandler |
||
26 | { |
||
27 | private $upload_dir; |
||
28 | private $upload_url; |
||
29 | private $thumbnails_dir; |
||
30 | private $thumbnails_url; |
||
31 | private $thumbnail_max_width; |
||
32 | private $thumbnail_max_height; |
||
33 | private $field_name; |
||
34 | |||
35 | /** |
||
36 | * SwUploadHandler constructor. |
||
37 | * @param $options |
||
38 | */ |
||
39 | View Code Duplication | public function __construct($options) |
|
|
|||
40 | { |
||
41 | $this->upload_dir = $options['upload_dir']; |
||
42 | $this->upload_url = $options['upload_url']; |
||
43 | $this->thumbnails_dir = $options['thumbnails_dir']; |
||
44 | $this->thumbnails_url = $options['thumbnails_url']; |
||
45 | $this->thumbnail_max_width = $options['thumbnail_max_width']; |
||
46 | $this->thumbnail_max_height = $options['thumbnail_max_height']; |
||
47 | $this->field_name = $options['field_name']; |
||
48 | } |
||
49 | |||
50 | /** |
||
51 | * @param $file_name |
||
52 | * @return null|\stdClass |
||
53 | */ |
||
54 | View Code Duplication | private function get_file_object($file_name) |
|
55 | { |
||
56 | $file_path = $this->upload_dir . $file_name; |
||
57 | if (is_file($file_path) && '.' !== $file_name[0] && 'index.html' !== $file_name && 'Thumbs.db' !== $file_name) { |
||
58 | $file = new \stdClass(); |
||
59 | $file->name = $file_name; |
||
60 | $file->size = filesize($file_path); |
||
61 | $file->url = $this->upload_url . rawurlencode($file->name); |
||
62 | $file->thumbnail = is_file($this->thumbnails_dir . $file_name) ? $this->thumbnails_url . rawurlencode($file->name) : null; |
||
63 | |||
64 | return $file; |
||
65 | } |
||
66 | |||
67 | return null; |
||
68 | } |
||
69 | |||
70 | /** |
||
71 | * @param $file_name |
||
72 | * @return bool |
||
73 | */ |
||
74 | View Code Duplication | private function create_thumbnail($file_name) |
|
75 | { |
||
76 | $file_path = $this->upload_dir . $file_name; |
||
77 | $thumbnail_path = $this->thumbnails_dir . $file_name; |
||
78 | list($img_width, $img_height) = @getimagesize($file_path); |
||
79 | if (!$img_width || !$img_height) { |
||
80 | return false; |
||
81 | } |
||
82 | $scale = min($this->thumbnail_max_width / $img_width, $this->thumbnail_max_height / $img_height); |
||
83 | if ($scale > 1) { |
||
84 | $scale = 1; |
||
85 | } |
||
86 | $thumbnail_width = $img_width * $scale; |
||
87 | $thumbnail_height = $img_height * $scale; |
||
88 | $thumbnail_img = @imagecreatetruecolor($thumbnail_width, $thumbnail_height); |
||
89 | switch (mb_strtolower(mb_substr(mb_strrchr($file_name, '.'), 1))) { |
||
90 | case 'jpg': |
||
91 | case 'jpeg': |
||
92 | $src_img = @imagecreatefromjpeg($file_path); |
||
93 | $write_thumbnail = 'imagejpeg'; |
||
94 | break; |
||
95 | case 'gif': |
||
96 | $src_img = @imagecreatefromgif($file_path); |
||
97 | $write_thumbnail = 'imagegif'; |
||
98 | break; |
||
99 | case 'png': |
||
100 | $src_img = @imagecreatefrompng($file_path); |
||
101 | $write_thumbnail = 'imagepng'; |
||
102 | break; |
||
103 | default: |
||
104 | $src_img = $write_thumbnail = null; |
||
105 | } |
||
106 | $success = $src_img && @imagecopyresampled($thumbnail_img, $src_img, 0, 0, 0, 0, $thumbnail_width, $thumbnail_height, $img_width, $img_height) |
||
107 | && $write_thumbnail($thumbnail_img, $thumbnail_path); |
||
108 | // Free up memory (imagedestroy does not delete files): |
||
109 | @imagedestroy($src_img); |
||
110 | @imagedestroy($thumbnail_img); |
||
111 | |||
112 | return $success; |
||
113 | } |
||
114 | |||
115 | //function to return file extension from a path or file name |
||
116 | |||
117 | /** |
||
118 | * @param $path |
||
119 | * @return mixed |
||
120 | */ |
||
121 | public function getFileExtension($path) |
||
127 | |||
128 | /** |
||
129 | * @param $uploaded_file |
||
130 | * @param $name |
||
131 | * @param $size |
||
132 | * @param $type |
||
133 | * @param $error |
||
134 | * @return \stdClass|bool false if not XOOPS user |
||
135 | */ |
||
136 | private function handle_file_upload($uploaded_file, $name, $size, $type, $error) |
||
185 | |||
186 | View Code Duplication | public function get() |
|
198 | |||
199 | View Code Duplication | public function post() |
|
238 | |||
239 | public function delete() |
||
262 | } |
||
263 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.