Conditions | 16 |
Paths | 57 |
Total Lines | 53 |
Code Lines | 37 |
Lines | 0 |
Ratio | 0 % |
Changes | 1 | ||
Bugs | 0 | Features | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
1 | <?php |
||
12 | public static function sys_avatar_upload($subject_id, &$avatar_field, $prefix = 'avatar') { |
||
13 | global $config, $lang, $user; |
||
14 | |||
15 | try { |
||
16 | $avatar_filename = $fullsize_filename = SN_ROOT_PHYSICAL . 'images/avatar/' . $prefix . '_' . $subject_id; |
||
17 | $avatar_filename .= '.png'; |
||
18 | $fullsize_filename .= '_full.png'; |
||
19 | if (sys_get_param_int('avatar_remove')) { |
||
20 | if (file_exists($avatar_filename) && !unlink($avatar_filename)) { |
||
21 | throw new Exception($lang['opt_msg_avatar_error_delete'], ERR_ERROR); |
||
22 | } |
||
23 | $avatar_field = 0; |
||
24 | throw new Exception($lang['opt_msg_avatar_removed'], ERR_NONE); |
||
25 | } elseif ($_FILES['avatar']['size']) { |
||
26 | if (!in_array($_FILES['avatar']['type'], array('image/gif', 'image/jpeg', 'image/jpg', 'image/pjpeg', 'image/png')) || $_FILES['avatar']['size'] > 204800) { |
||
27 | throw new Exception($lang['opt_msg_avatar_error_unsupported'], ERR_WARNING); |
||
28 | } |
||
29 | |||
30 | if ($_FILES['avatar']['error']) { |
||
31 | throw new Exception(sprintf($lang['opt_msg_avatar_error_upload'], $_FILES['avatar']['error']), ERR_ERROR); |
||
32 | } |
||
33 | |||
34 | if (!($avatar_image = imagecreatefromstring(file_get_contents($_FILES['avatar']['tmp_name'])))) { |
||
35 | throw new Exception($lang['opt_msg_avatar_error_unsupported'], ERR_WARNING); |
||
36 | } |
||
37 | |||
38 | $avatar_size = getimagesize($_FILES['avatar']['tmp_name']); |
||
39 | $avatar_max_width = $config->avatar_max_width; |
||
40 | $avatar_max_height = $config->avatar_max_height; |
||
41 | if ($avatar_size[0] > $avatar_max_width || $avatar_size[1] > $avatar_max_height) { |
||
42 | $aspect_ratio = min($avatar_max_width / $avatar_size[0], $avatar_max_height / $avatar_size[1]); |
||
43 | $avatar_image_new = imagecreatetruecolor($avatar_size[0] * $aspect_ratio, $avatar_size[0] * $aspect_ratio); |
||
44 | $result = imagecopyresized($avatar_image_new, $avatar_image, 0, 0, 0, 0, $avatar_size[0] * $aspect_ratio, $avatar_size[0] * $aspect_ratio, $avatar_size[0], $avatar_size[1]); |
||
45 | imagedestroy($avatar_image); |
||
46 | $avatar_image = $avatar_image_new; |
||
47 | } |
||
48 | |||
49 | if (file_exists($avatar_filename) && !unlink($avatar_filename)) { |
||
50 | throw new Exception($lang['opt_msg_avatar_error_delete'], ERR_ERROR); |
||
51 | } |
||
52 | |||
53 | if (!imagepng($avatar_image, $avatar_filename, 9)) { |
||
54 | throw new Exception($lang['opt_msg_avatar_error_writing'], ERR_ERROR); |
||
55 | } |
||
56 | |||
57 | $avatar_field = 1; |
||
58 | imagedestroy($avatar_image); |
||
59 | throw new Exception($lang['opt_msg_avatar_uploaded'], ERR_NONE); |
||
60 | } |
||
61 | } catch (Exception $e) { |
||
62 | return array( |
||
63 | 'STATUS' => in_array($e->getCode(), array(ERR_NONE, ERR_WARNING, ERR_ERROR)) ? $e->getCode() : ERR_ERROR, |
||
64 | 'MESSAGE' => $e->getMessage() |
||
65 | ); |
||
70 |