Conditions | 6 |
Paths | 18 |
Total Lines | 56 |
Code Lines | 39 |
Lines | 19 |
Ratio | 33.93 % |
Changes | 1 | ||
Bugs | 1 | 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 |
||
33 | public function upload($name) |
||
34 | { |
||
35 | $instance = $this->_getInstance($name); |
||
36 | require_once Plugin::classPath('Field') . 'Lib/class.upload.php'; |
||
37 | $uploader = new \upload($this->request->data['Filedata']); |
||
38 | $maps = [ |
||
39 | 'min_width' => 'image_min_width', |
||
40 | 'min_height' => 'image_min_height', |
||
41 | 'max_width' => 'image_max_width', |
||
42 | 'max_height' => 'image_max_height', |
||
43 | 'min_ratio' => 'image_min_ratio', |
||
44 | 'max_ratio' => 'image_max_ratio', |
||
45 | 'min_pixels' => 'image_min_pixels', |
||
46 | 'max_pixels' => 'image_max_pixels', |
||
47 | ]; |
||
48 | |||
49 | foreach ($maps as $k => $v) { |
||
50 | if (!empty($instance->settings[$k])) { |
||
51 | $uploader->{$v} = $instance->settings[$k]; |
||
52 | } |
||
53 | } |
||
54 | |||
55 | $uploader->allowed = 'image/*'; |
||
56 | |||
57 | // start uploading |
||
58 | View Code Duplication | if (!empty($instance->settings['extensions'])) { |
|
59 | $exts = explode(',', $instance->settings['extensions']); |
||
60 | $exts = array_map('trim', $exts); |
||
61 | $exts = array_map('strtolower', $exts); |
||
62 | |||
63 | if (!in_array(strtolower($uploader->file_src_name_ext), $exts)) { |
||
64 | $this->_error(__d('field', 'Invalid file extension.'), 501); |
||
65 | } |
||
66 | } |
||
67 | |||
68 | $response = ''; |
||
69 | $uploader->file_overwrite = false; |
||
70 | $folder = normalizePath(WWW_ROOT . "/files/{$instance->settings['upload_folder']}/"); |
||
71 | $url = normalizePath("/files/{$instance->settings['upload_folder']}/", '/'); |
||
72 | |||
73 | $uploader->process($folder); |
||
74 | View Code Duplication | if ($uploader->processed) { |
|
75 | $response = json_encode([ |
||
76 | 'file_url' => Router::url($url . $uploader->file_dst_name, true), |
||
77 | 'file_size' => FileToolbox::bytesToSize($uploader->file_src_size), |
||
78 | 'file_name' => $uploader->file_dst_name, |
||
79 | 'mime_icon' => FileToolbox::fileIcon($uploader->file_src_mime), |
||
80 | ]); |
||
81 | } else { |
||
82 | $this->_error(__d('field', 'File upload error, details: {0}', $uploader->error), 502); |
||
83 | } |
||
84 | |||
85 | $this->viewBuilder()->layout('ajax'); |
||
86 | $this->title(__d('field', 'Upload Image')); |
||
87 | $this->set(compact('response')); |
||
88 | } |
||
89 | |||
196 |