| Conditions | 14 |
| Paths | 46 |
| Total Lines | 76 |
| Lines | 45 |
| Ratio | 59.21 % |
| 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 |
||
| 23 | public function upload(Request $request, $project_key) |
||
| 24 | { |
||
| 25 | set_time_limit(0); |
||
| 26 | |||
| 27 | if (!is_writable(config('filesystems.disks.local.root', '/tmp'))) { |
||
| 28 | throw new \UnexpectedValueException('the user has not the writable permission to the directory.', -15103); |
||
| 29 | } |
||
| 30 | |||
| 31 | $thumbnail_size = 190; |
||
| 32 | |||
| 33 | $fields = array_keys($_FILES); |
||
| 34 | $field = array_pop($fields); |
||
| 35 | View Code Duplication | if (empty($_FILES) || $_FILES[$field]['error'] > 0) { |
|
|
|
|||
| 36 | throw new \UnexpectedValueException('upload file errors.', -15101); |
||
| 37 | } |
||
| 38 | |||
| 39 | $basename = md5(microtime() . $_FILES[$field]['name']); |
||
| 40 | $sub_save_path = config('filesystems.disks.local.root', '/tmp') . '/' . substr($basename, 0, 2) . '/'; |
||
| 41 | if (!is_dir($sub_save_path)) { |
||
| 42 | @mkdir($sub_save_path); |
||
| 43 | } |
||
| 44 | $filename = '/tmp/' . $basename; |
||
| 45 | move_uploaded_file($_FILES[$field]['tmp_name'], $filename); |
||
| 46 | $data = []; |
||
| 47 | $data['name'] = $_FILES[$field]['name']; |
||
| 48 | $data['size'] = $_FILES[$field]['size']; |
||
| 49 | $data['type'] = $_FILES[$field]['type']; |
||
| 50 | $data['index'] = $basename; |
||
| 51 | View Code Duplication | if (in_array($_FILES[$field]['type'], [ 'image/jpeg', 'image/jpg', 'image/png', 'image/gif' ])) { |
|
| 52 | $size = getimagesize($filename); |
||
| 53 | $width = $size[0]; $height = $size[1]; |
||
| 54 | $scale = $width < $height ? $height : $width; |
||
| 55 | $thumbnails_width = floor($thumbnail_size * $width / $scale); |
||
| 56 | $thumbnails_height = floor($thumbnail_size * $height / $scale); |
||
| 57 | $thumbnails_filename = $filename . '_thumbnails'; |
||
| 58 | if ($scale <= $thumbnail_size) { |
||
| 59 | @copy($filename, $thumbnails_filename); |
||
| 60 | } |
||
| 61 | else if ($_FILES[$field]['type'] == 'image/jpeg' || $_FILES[$field]['type'] == 'image/jpg') { |
||
| 62 | $src_image = imagecreatefromjpeg($filename); |
||
| 63 | $dst_image = imagecreatetruecolor($thumbnails_width, $thumbnails_height); |
||
| 64 | imagecopyresized($dst_image, $src_image, 0, 0, 0, 0, $thumbnails_width, $thumbnails_height, $width, $height); |
||
| 65 | imagejpeg($dst_image, $thumbnails_filename); |
||
| 66 | } |
||
| 67 | else if ($_FILES[$field]['type'] == 'image/png') { |
||
| 68 | $src_image = imagecreatefrompng($filename); |
||
| 69 | $dst_image = imagecreatetruecolor($thumbnails_width, $thumbnails_height); |
||
| 70 | imagecopyresized($dst_image, $src_image, 0, 0, 0, 0, $thumbnails_width, $thumbnails_height, $width, $height); |
||
| 71 | imagepng($dst_image, $thumbnails_filename); |
||
| 72 | } |
||
| 73 | else if ($_FILES[$field]['type'] == 'image/gif') { |
||
| 74 | $src_image = imagecreatefromgif($filename); |
||
| 75 | $dst_image = imagecreatetruecolor($thumbnails_width, $thumbnails_height); |
||
| 76 | imagecopyresized($dst_image, $src_image, 0, 0, 0, 0, $thumbnails_width, $thumbnails_height, $width, $height); |
||
| 77 | imagegif($dst_image, $thumbnails_filename); |
||
| 78 | } |
||
| 79 | else |
||
| 80 | { |
||
| 81 | @copy($filename, $thumbnails_filename); |
||
| 82 | } |
||
| 83 | $data['thumbnails_index'] = $basename . '_thumbnails'; |
||
| 84 | // move the thumbnails |
||
| 85 | @rename($thumbnails_filename, $sub_save_path . $data['thumbnails_index']); |
||
| 86 | } |
||
| 87 | // move original file |
||
| 88 | @rename($filename, $sub_save_path . $basename); |
||
| 89 | $data['uploader'] = [ 'id' => $this->user->id, 'name' => $this->user->first_name, 'email' => $this->user->email ]; |
||
| 90 | $file = File::create($data); |
||
| 91 | |||
| 92 | $issue_id = $request->input('issue_id'); |
||
| 93 | if (isset($issue_id) && $issue_id) { |
||
| 94 | Event::fire(new FileUploadEvent($project_key, $issue_id, $field, $file->id, $data['uploader'])); |
||
| 95 | } |
||
| 96 | |||
| 97 | return response()->json([ 'ecode' => 0, 'data' => [ 'field' => $field, 'file' => File::find($file->id), 'filename' => '/actionview/api/project/' . $project_key . '/file/' . $file->id ] ]); |
||
| 98 | } |
||
| 99 | |||
| 235 |
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.