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 | public function __construct($options) |
||
49 | |||
50 | /** |
||
51 | * @param $file_name |
||
52 | * @return null|\stdClass |
||
53 | */ |
||
54 | private function get_file_object($file_name) |
||
69 | |||
70 | /** |
||
71 | * @param $file_name |
||
72 | * @return bool |
||
73 | */ |
||
74 | private function create_thumbnail($file_name) |
||
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) |
||
137 | { |
||
138 | $file = new \stdClass(); |
||
139 | $swDB = new SwDatabase(); |
||
140 | $userid = ($GLOBALS['xoopsUser'] && ($GLOBALS['xoopsUser'] instanceof \XoopsUser)) ? $GLOBALS['xoopsUser']->uid() : 0; |
||
141 | |||
142 | if (0 == $userid) { |
||
143 | return false; |
||
144 | } |
||
145 | // Generate new name for file |
||
146 | //$file->name = basename(stripslashes($name)); |
||
147 | $file->name = time() . mt_rand(0, 99999) . '.' . $this->getFileExtension($name); |
||
148 | $file->size = (int)$size; |
||
149 | $file->type = $type; |
||
150 | $img = XOOPS_URL . '/uploads/albums_smallworld/' . $userid . '/' . $file->name; |
||
151 | |||
152 | // Save to database for later use |
||
153 | $swDB->saveImage("null, '" . $userid . "', '" . $file->name . "', '" . addslashes($img) . "', '" . time() . "', ''"); |
||
154 | |||
155 | if (!$error && $file->name) { |
||
156 | if ('.' === $file->name[0]) { |
||
157 | $file->name = mb_substr($file->name, 1); |
||
158 | } |
||
159 | $file_path = $this->upload_dir . $file->name; |
||
160 | $append_file = is_file($file_path) && $file->size > filesize($file_path); |
||
161 | clearstatcache(); |
||
162 | if ($uploaded_file && is_uploaded_file($uploaded_file)) { |
||
163 | // multipart/formdata uploads (POST method uploads) |
||
164 | if ($append_file) { |
||
165 | file_put_contents($file_path, fopen($uploaded_file, 'rb'), FILE_APPEND); |
||
166 | } else { |
||
167 | move_uploaded_file($uploaded_file, $file_path); |
||
168 | } |
||
169 | } else { |
||
170 | // Non-multipart uploads (PUT method support) |
||
171 | file_put_contents($file_path, fopen('php://input', 'rb'), $append_file ? FILE_APPEND : 0); |
||
172 | } |
||
173 | $file_size = filesize($file_path); |
||
174 | if ($file_size === $file->size) { |
||
175 | $file->url = $this->upload_url . rawurlencode($file->name); |
||
176 | $file->thumbnail = $this->create_thumbnail($file->name) ? $this->thumbnails_url . rawurlencode($file->name) : null; |
||
177 | } |
||
178 | $file->size = $file_size; |
||
179 | } else { |
||
180 | $file->error = $error; |
||
181 | } |
||
182 | |||
183 | return $file; |
||
184 | } |
||
185 | |||
186 | public function get() |
||
187 | { |
||
188 | $file_name = isset($_REQUEST['file']) ? basename(stripslashes($_REQUEST['file'])) : null; |
||
189 | if (null !== $file_name) { |
||
190 | $info = $this->get_file_object($file_name); |
||
191 | } else { |
||
192 | $info = array_values(array_filter(array_map([$this, 'get_file_object'], scandir($this->upload_dir, SCANDIR_SORT_NONE)))); |
||
193 | } |
||
194 | header('Cache-Control: no-cache, must-revalidate'); |
||
195 | header('Content-type: application/json'); |
||
196 | echo json_encode($info); |
||
197 | } |
||
198 | |||
199 | public function post() |
||
200 | { |
||
201 | $upload = isset($_FILES[$this->field_name]) ? $_FILES[$this->field_name] : [ |
||
202 | 'tmp_name' => null, |
||
203 | 'name' => null, |
||
204 | 'size' => null, |
||
205 | 'type' => null, |
||
206 | 'error' => null, |
||
207 | ]; |
||
208 | if (is_array($upload['tmp_name']) && count($upload['tmp_name']) > 1) { |
||
209 | $info = []; |
||
210 | foreach ($upload['tmp_name'] as $index => $value) { |
||
211 | $info[] = $this->handle_file_upload($upload['tmp_name'][$index], $upload['name'][$index], $upload['size'][$index], $upload['type'][$index], $upload['error'][$index]); |
||
212 | } |
||
213 | } else { |
||
214 | if (is_array($upload['tmp_name'])) { |
||
215 | $upload = [ |
||
216 | 'tmp_name' => $upload['tmp_name'][0], |
||
217 | 'name' => $upload['name'][0], |
||
218 | 'size' => $upload['size'][0], |
||
219 | 'type' => $upload['type'][0], |
||
220 | 'error' => $upload['error'][0], |
||
221 | ]; |
||
222 | } |
||
223 | $info = $this->handle_file_upload( |
||
224 | $upload['tmp_name'], |
||
225 | isset($_SERVER['HTTP_X_FILE_NAME']) ? $_SERVER['HTTP_X_FILE_NAME'] : $upload['name'], |
||
226 | isset($_SERVER['HTTP_X_FILE_SIZE']) ? $_SERVER['HTTP_X_FILE_SIZE'] : $upload['size'], |
||
227 | isset($_SERVER['HTTP_X_FILE_TYPE']) ? $_SERVER['HTTP_X_FILE_TYPE'] : $upload['type'], |
||
228 | $upload['error'] |
||
229 | ); |
||
230 | } |
||
231 | if (isset($_SERVER['HTTP_X_REQUESTED_WITH']) && 'XMLHttpRequest' === $_SERVER['HTTP_X_REQUESTED_WITH']) { |
||
232 | header('Content-type: application/json'); |
||
233 | } else { |
||
234 | header('Content-type: text/plain'); |
||
235 | } |
||
236 | echo json_encode($info); |
||
237 | } |
||
238 | |||
239 | public function delete() |
||
262 | } |
||
263 |
If you suppress an error, we recommend checking for the error condition explicitly: