| Conditions | 14 |
| Paths | 12 |
| Total Lines | 53 |
| Code Lines | 43 |
| 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 |
||
| 20 | static function admin_upload_post () { |
||
| 21 | if (!isset($_FILES['file']) || !$_FILES['file']['tmp_name']) { |
||
| 22 | throw new ExitException(400); |
||
| 23 | } |
||
| 24 | $L = Language::instance(); |
||
| 25 | $file = $_FILES['file']; |
||
| 26 | switch ($file['error']) { |
||
| 27 | case UPLOAD_ERR_INI_SIZE: |
||
| 28 | case UPLOAD_ERR_FORM_SIZE: |
||
| 29 | throw new ExitException($L->file_too_large, 400); |
||
| 30 | case UPLOAD_ERR_NO_TMP_DIR: |
||
| 31 | throw new ExitException($L->temporary_folder_is_missing, 400); |
||
| 32 | case UPLOAD_ERR_CANT_WRITE: |
||
| 33 | throw new ExitException($L->cant_write_file_to_disk, 500); |
||
| 34 | case UPLOAD_ERR_PARTIAL: |
||
| 35 | case UPLOAD_ERR_NO_FILE: |
||
| 36 | throw new ExitException(400); |
||
| 37 | } |
||
| 38 | $target_directory = TEMP.'/System/admin'; |
||
| 39 | if (!is_dir($target_directory) && !mkdir($target_directory, 0770, true)) { |
||
| 40 | throw new ExitException(500); |
||
| 41 | } |
||
| 42 | $tmp_filename = Session::instance()->get_id().'.phar'; |
||
| 43 | $tmp_location = "$target_directory/$tmp_filename"; |
||
| 44 | // Cleanup |
||
| 45 | get_files_list( |
||
| 46 | $target_directory, |
||
| 47 | '/.*\.phar$/', |
||
| 48 | 'f', |
||
| 49 | true, |
||
| 50 | false, |
||
| 51 | false, |
||
| 52 | false, |
||
| 53 | false, |
||
| 54 | function ($file) { |
||
| 55 | unlink($file); |
||
| 56 | } |
||
| 57 | ); |
||
| 58 | if (!move_uploaded_file($file['tmp_name'], $tmp_location)) { |
||
| 59 | throw new ExitException(500); |
||
| 60 | } |
||
| 61 | $tmp_dir = "phar://$tmp_location"; |
||
| 62 | if (!file_exists("$tmp_dir/meta.json")) { |
||
| 63 | unlink($tmp_location); |
||
| 64 | throw new ExitException(400); |
||
| 65 | } |
||
| 66 | $meta = file_get_json("$tmp_dir/meta.json"); |
||
| 67 | if (!isset($meta['category'], $meta['package'], $meta['version'])) { |
||
| 68 | unlink($tmp_location); |
||
| 69 | throw new ExitException(400); |
||
| 70 | } |
||
| 71 | Page::instance()->json($meta); |
||
| 72 | } |
||
| 73 | } |
||
| 74 |