| Conditions | 26 |
| Paths | 300 |
| Total Lines | 164 |
| Code Lines | 86 |
| 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 |
||
| 84 | public function upload() |
||
| 85 | { |
||
| 86 | $this->load->language('extension/installer'); |
||
| 87 | |||
| 88 | $json = array(); |
||
| 89 | |||
| 90 | // Check user has permission |
||
| 91 | if (!$this->user->hasPermission('modify', 'extension/installer')) { |
||
| 92 | $json['error'] = $this->language->get('error_permission'); |
||
| 93 | } |
||
| 94 | |||
| 95 | if (!$json) { |
||
| 96 | if (!empty($this->request->files['file']['name'])) { |
||
| 97 | if (substr($this->request->files['file']['name'], -10) != '.srmod.zip' && substr($this->request->files['file']['name'], -10) != '.srmod.xml') { |
||
| 98 | $json['error'] = $this->language->get('error_filetype'); |
||
| 99 | } |
||
| 100 | |||
| 101 | if ($this->request->files['file']['error'] != UPLOAD_ERR_OK) { |
||
| 102 | $json['error'] = $this->language->get('error_upload_' . $this->request->files['file']['error']); |
||
| 103 | } |
||
| 104 | } else { |
||
| 105 | $json['error'] = $this->language->get('error_upload'); |
||
| 106 | } |
||
| 107 | } |
||
| 108 | |||
| 109 | if (!$json) { |
||
| 110 | // If no temp directory exists create it |
||
| 111 | $path = 'temp-' . (new \Tokenly\TokenGenerator\TokenGenerator())->generateToken(32, 'SR'); |
||
| 112 | |||
| 113 | if (!is_dir($_SERVER['DOCUMENT_ROOT'] . '/storage/upload/' . $path)) { |
||
| 114 | mkdir($_SERVER['DOCUMENT_ROOT'] . '/storage/upload/' . $path, 0777); |
||
| 115 | } |
||
| 116 | |||
| 117 | // Set the steps required for installation |
||
| 118 | $json['step'] = array(); |
||
| 119 | $json['overwrite'] = array(); |
||
| 120 | |||
| 121 | if (strrchr($this->request->files['file']['name'], '.') == '.xml') { |
||
| 122 | $file = $_SERVER['DOCUMENT_ROOT'] . '/storage/upload/' . $path . '/install.xml'; |
||
| 123 | |||
| 124 | // If xml file copy it to the temporary directory |
||
| 125 | move_uploaded_file($this->request->files['file']['tmp_name'], $file); |
||
| 126 | |||
| 127 | if (file_exists($file)) { |
||
| 128 | $json['step'][] = array( |
||
| 129 | 'text' => $this->language->get('text_xml'), |
||
| 130 | 'url' => str_replace('&', '&', $this->url->link('extension/installer/xml', 'token=' . $this->session->data['token'], true)), |
||
| 131 | 'path' => $path |
||
| 132 | ); |
||
| 133 | |||
| 134 | // Clear temporary files |
||
| 135 | $json['step'][] = array( |
||
| 136 | 'text' => $this->language->get('text_remove'), |
||
| 137 | 'url' => str_replace('&', '&', $this->url->link('extension/installer/remove', 'token=' . $this->session->data['token'], true)), |
||
| 138 | 'path' => $path |
||
| 139 | ); |
||
| 140 | } else { |
||
| 141 | $json['error'] = $this->language->get('error_file'); |
||
| 142 | } |
||
| 143 | } |
||
| 144 | |||
| 145 | // If zip file copy it to the temp directory |
||
| 146 | if (strrchr($this->request->files['file']['name'], '.') == '.zip') { |
||
| 147 | $file = $_SERVER['DOCUMENT_ROOT'] . '/storage/upload/' . $path . '/upload.zip'; |
||
| 148 | |||
| 149 | move_uploaded_file($this->request->files['file']['tmp_name'], $file); |
||
| 150 | |||
| 151 | if (file_exists($file)) { |
||
| 152 | $zip = zip_open($file); |
||
| 153 | |||
| 154 | if ($zip) { |
||
| 155 | // Zip |
||
| 156 | $json['step'][] = array( |
||
| 157 | 'text' => $this->language->get('text_unzip'), |
||
| 158 | 'url' => str_replace('&', '&', $this->url->link('extension/installer/unzip', 'token=' . $this->session->data['token'], true)), |
||
| 159 | 'path' => $path |
||
| 160 | ); |
||
| 161 | |||
| 162 | // FTP |
||
| 163 | $json['step'][] = array( |
||
| 164 | 'text' => $this->language->get('text_ftp'), |
||
| 165 | 'url' => str_replace('&', '&', $this->url->link('extension/installer/ftp', 'token=' . $this->session->data['token'], true)), |
||
| 166 | 'path' => $path |
||
| 167 | ); |
||
| 168 | |||
| 169 | // Send make and array of actions to carry out |
||
| 170 | while ($entry = zip_read($zip)) { |
||
| 171 | $zip_name = zip_entry_name($entry); |
||
| 172 | |||
| 173 | // SQL |
||
| 174 | if (substr($zip_name, 0, 11) == 'install.sql') { |
||
| 175 | $json['step'][] = array( |
||
| 176 | 'text' => $this->language->get('text_sql'), |
||
| 177 | 'url' => str_replace('&', '&', $this->url->link('extension/installer/sql', 'token=' . $this->session->data['token'], true)), |
||
| 178 | 'path' => $path |
||
| 179 | ); |
||
| 180 | } |
||
| 181 | |||
| 182 | // XML |
||
| 183 | if (substr($zip_name, 0, 11) == 'install.xml') { |
||
| 184 | $json['step'][] = array( |
||
| 185 | 'text' => $this->language->get('text_xml'), |
||
| 186 | 'url' => str_replace('&', '&', $this->url->link('extension/installer/xml', 'token=' . $this->session->data['token'], true)), |
||
| 187 | 'path' => $path |
||
| 188 | ); |
||
| 189 | } |
||
| 190 | |||
| 191 | // PHP |
||
| 192 | if (substr($zip_name, 0, 11) == 'install.php') { |
||
| 193 | $json['step'][] = array( |
||
| 194 | 'text' => $this->language->get('text_php'), |
||
| 195 | 'url' => str_replace('&', '&', $this->url->link('extension/installer/php', 'token=' . $this->session->data['token'], true)), |
||
| 196 | 'path' => $path |
||
| 197 | ); |
||
| 198 | } |
||
| 199 | |||
| 200 | // Compare admin files |
||
| 201 | $file = SR_APPLICATION . substr($zip_name, 13); |
||
| 202 | |||
| 203 | if (is_file($file) && substr($zip_name, 0, 13) == 'upload/administration/') { |
||
| 204 | $json['overwrite'][] = substr($zip_name, 7); |
||
| 205 | } |
||
| 206 | |||
| 207 | // Compare catalog files |
||
| 208 | $file = $_SERVER['DOCUMENT_ROOT'] . '/application/' . substr($zip_name, 15); |
||
| 209 | |||
| 210 | if (is_file($file) && substr($zip_name, 0, 15) == 'upload/application/') { |
||
| 211 | $json['overwrite'][] = substr($zip_name, 7); |
||
| 212 | } |
||
| 213 | |||
| 214 | // Compare image files |
||
| 215 | $file = $_SERVER['DOCUMENT_ROOT'] . '/public_html/assets/images/' . substr($zip_name, 13); |
||
| 216 | |||
| 217 | if (is_file($file) && substr($zip_name, 0, 13) == 'upload/image/') { |
||
| 218 | $json['overwrite'][] = substr($zip_name, 7); |
||
| 219 | } |
||
| 220 | |||
| 221 | // Compare system files |
||
| 222 | $file = $_SERVER['DOCUMENT_ROOT'] . '/engine/' . substr($zip_name, 14); |
||
| 223 | |||
| 224 | if (is_file($file) && substr($zip_name, 0, 14) == 'upload/engine/') { |
||
| 225 | $json['overwrite'][] = substr($zip_name, 7); |
||
| 226 | } |
||
| 227 | } |
||
| 228 | |||
| 229 | // Clear temporary files |
||
| 230 | $json['step'][] = array( |
||
| 231 | 'text' => $this->language->get('text_remove'), |
||
| 232 | 'url' => str_replace('&', '&', $this->url->link('extension/installer/remove', 'token=' . $this->session->data['token'], true)), |
||
| 233 | 'path' => $path |
||
| 234 | ); |
||
| 235 | |||
| 236 | zip_close($zip); |
||
| 237 | } else { |
||
| 238 | $json['error'] = $this->language->get('error_unzip'); |
||
| 239 | } |
||
| 240 | } else { |
||
| 241 | $json['error'] = $this->language->get('error_file'); |
||
| 242 | } |
||
| 243 | } |
||
| 244 | } |
||
| 245 | |||
| 246 | $this->response->addHeader('Content-Type: application/json'); |
||
| 247 | $this->response->setOutput(json_encode($json)); |
||
| 248 | } |
||
| 555 |
You can fix this by adding a namespace to your class:
When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.