Conditions | 11 |
Paths | 9 |
Total Lines | 46 |
Code Lines | 14 |
Lines | 0 |
Ratio | 0 % |
Changes | 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 |
||
36 | public static function save(string $name, string $dir_name) { |
||
37 | |||
38 | if (false === ($file = Request::file($name))) return false; |
||
39 | |||
40 | # Check for upload errors |
||
41 | |||
42 | if ($file['error'] !== UPLOAD_ERR_OK) return self::translateError($file['error']); |
||
43 | |||
44 | # Check for secure upload |
||
45 | |||
46 | if (!is_uploaded_file($file['tmp_name'])) return 'UPLOADER_ERROR_SECURITY'; |
||
47 | |||
48 | # Check size |
||
49 | |||
50 | if ($file['size'] > CONFIG_UPLOADS_MAX_SIZE) return 'UPLOADER_ERROR_SIZE'; |
||
51 | |||
52 | # Check file extension |
||
53 | |||
54 | $extensions = ['php', 'phtml', 'php3', 'php4', 'php5', 'phps']; |
||
55 | |||
56 | $extension = strtolower(Explorer::getExtension($file['name'], false)); |
||
57 | |||
58 | if (in_array($extension, $extensions, true)) return 'UPLOADER_ERROR_TYPE'; |
||
59 | |||
60 | # Check target directory |
||
61 | |||
62 | if (!Explorer::isDir($dir_name) && !Explorer::createDir($dir_name)) return 'UPLOADER_ERROR_DIR'; |
||
63 | |||
64 | # Check target file |
||
65 | |||
66 | $base_name = basename($file['name']); $file_name = ($dir_name . '/' . $base_name); |
||
67 | |||
68 | if (Explorer::isDir($file_name) || Explorer::isFile($file_name)) return 'UPLOADER_ERROR_EXISTS'; |
||
69 | |||
70 | # Save uploaded file |
||
71 | |||
72 | if (!@move_uploaded_file($file['tmp_name'], $file_name)) return 'UPLOADER_ERROR_SAVE'; |
||
73 | |||
74 | # Set upload data |
||
75 | |||
76 | self::$base_name = $base_name; self::$file_name = $file_name; |
||
77 | |||
78 | # ------------------------ |
||
79 | |||
80 | return true; |
||
81 | } |
||
82 | |||
111 |