| Conditions | 26 |
| Paths | 18240 |
| Total Lines | 167 |
| Code Lines | 78 |
| Lines | 82 |
| Ratio | 49.1 % |
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 |
||
| 143 | protected function processAttachments() |
||
| 144 | { |
||
| 145 | global $context, $modSettings, $smcFunc, $user_info, $txt; |
||
| 146 | |||
| 147 | if (!isset($_FILES['attachment']['name'])) |
||
| 148 | $_FILES['attachment']['tmp_name'] = array(); |
||
| 149 | |||
| 150 | // If there are attachments, calculate the total size and how many. |
||
| 151 | $context['attachments']['total_size'] = 0; |
||
| 152 | $context['attachments']['quantity'] = 0; |
||
| 153 | |||
| 154 | // If this isn't a new post, check the current attachments. |
||
| 155 | View Code Duplication | if (isset($_REQUEST['msg'])) |
|
| 156 | { |
||
| 157 | $context['attachments']['quantity'] = count($context['current_attachments']); |
||
| 158 | foreach ($context['current_attachments'] as $attachment) |
||
| 159 | $context['attachments']['total_size'] += $attachment['size']; |
||
| 160 | } |
||
| 161 | |||
| 162 | // A bit of house keeping first. |
||
| 163 | View Code Duplication | if (!empty($_SESSION['temp_attachments']) && count($_SESSION['temp_attachments']) == 1) |
|
| 164 | unset($_SESSION['temp_attachments']); |
||
| 165 | |||
| 166 | // Our infamous SESSION var, we are gonna have soo much fun with it! |
||
| 167 | if (!isset($_SESSION['temp_attachments'])) |
||
| 168 | $_SESSION['temp_attachments'] = array(); |
||
| 169 | |||
| 170 | // Make sure we're uploading to the right place. |
||
| 171 | if (!empty($modSettings['automanage_attachments'])) |
||
| 172 | automanage_attachments_check_directory(); |
||
| 173 | |||
| 174 | // Is the attachments folder actually there? |
||
| 175 | if (!empty($context['dir_creation_error'])) |
||
| 176 | $this->_generalErrors[] = $context['dir_creation_error']; |
||
| 177 | |||
| 178 | // The current attach folder ha some issues... |
||
| 179 | elseif (!is_dir($this->_attchDir)) |
||
| 180 | { |
||
| 181 | $this->_generalErrors[] = 'attach_folder_warning'; |
||
| 182 | log_error(sprintf($txt['attach_folder_admin_warning'], $this->_attchDir), 'critical'); |
||
| 183 | } |
||
| 184 | |||
| 185 | // If this isn't a new post, check the current attachments. |
||
| 186 | if (empty($this->_generalErrors) && $this->_msg) |
||
| 187 | { |
||
| 188 | $context['attachments'] = array(); |
||
| 189 | $request = $smcFunc['db_query']('', ' |
||
| 190 | SELECT COUNT(*), SUM(size) |
||
| 191 | FROM {db_prefix}attachments |
||
| 192 | WHERE id_msg = {int:id_msg} |
||
| 193 | AND attachment_type = {int:attachment_type}', |
||
| 194 | array( |
||
| 195 | 'id_msg' => (int) $this->_msg, |
||
| 196 | 'attachment_type' => 0, |
||
| 197 | ) |
||
| 198 | ); |
||
| 199 | list ($context['attachments']['quantity'], $context['attachments']['total_size']) = $smcFunc['db_fetch_row']($request); |
||
| 200 | $smcFunc['db_free_result']($request); |
||
| 201 | } |
||
| 202 | |||
| 203 | else |
||
| 204 | $context['attachments'] = array( |
||
| 205 | 'quantity' => 0, |
||
| 206 | 'total_size' => 0, |
||
| 207 | ); |
||
| 208 | |||
| 209 | // Check for other general errors here. |
||
| 210 | |||
| 211 | // If we have an initial error, delete the files. |
||
| 212 | if (!empty($this->_generalErrors)) |
||
| 213 | { |
||
| 214 | // And delete the files 'cos they ain't going nowhere. |
||
| 215 | View Code Duplication | foreach ($_FILES['attachment']['tmp_name'] as $n => $dummy) |
|
| 216 | if (file_exists($_FILES['attachment']['tmp_name'][$n])) |
||
| 217 | unlink($_FILES['attachment']['tmp_name'][$n]); |
||
| 218 | |||
| 219 | $_FILES['attachment']['tmp_name'] = array(); |
||
| 220 | |||
| 221 | // No point in going further with this. |
||
| 222 | return; |
||
| 223 | } |
||
| 224 | |||
| 225 | // Loop through $_FILES['attachment'] array and move each file to the current attachments folder. |
||
| 226 | View Code Duplication | foreach ($_FILES['attachment']['tmp_name'] as $n => $dummy) |
|
| 227 | { |
||
| 228 | if ($_FILES['attachment']['name'][$n] == '') |
||
| 229 | continue; |
||
| 230 | |||
| 231 | // First, let's first check for PHP upload errors. |
||
| 232 | $errors = array(); |
||
| 233 | if (!empty($_FILES['attachment']['error'][$n])) |
||
| 234 | { |
||
| 235 | if ($_FILES['attachment']['error'][$n] == 2) |
||
| 236 | $errors[] = array('file_too_big', array($modSettings['attachmentSizeLimit'])); |
||
| 237 | |||
| 238 | else |
||
| 239 | log_error($_FILES['attachment']['name'][$n] . ': ' . $txt['php_upload_error_' . $_FILES['attachment']['error'][$n]]); |
||
| 240 | |||
| 241 | // Log this one, because... |
||
| 242 | if ($_FILES['attachment']['error'][$n] == 6) |
||
| 243 | log_error($_FILES['attachment']['name'][$n] . ': ' . $txt['php_upload_error_6'], 'critical'); |
||
| 244 | |||
| 245 | // Weird, no errors were cached, still fill out a generic one. |
||
| 246 | if (empty($errors)) |
||
| 247 | $errors[] = 'attach_php_error'; |
||
| 248 | } |
||
| 249 | |||
| 250 | // Try to move and rename the file before doing any more checks on it. |
||
| 251 | $attachID = 'post_tmp_' . $user_info['id'] . '_' . md5(mt_rand()); |
||
| 252 | $destName = $this->_attchDir . '/' . $attachID; |
||
| 253 | |||
| 254 | // No errors, YAY! |
||
| 255 | if (empty($errors)) |
||
| 256 | { |
||
| 257 | $_SESSION['temp_attachments'][$attachID] = array( |
||
| 258 | 'name' => $smcFunc['htmlspecialchars'](basename($_FILES['attachment']['name'][$n])), |
||
| 259 | 'tmp_name' => $destName, |
||
| 260 | 'size' => $_FILES['attachment']['size'][$n], |
||
| 261 | 'type' => $_FILES['attachment']['type'][$n], |
||
| 262 | 'id_folder' => $modSettings['currentAttachmentUploadDir'], |
||
| 263 | 'errors' => array(), |
||
| 264 | ); |
||
| 265 | |||
| 266 | // Move the file to the attachments folder with a temp name for now. |
||
| 267 | if (@move_uploaded_file($_FILES['attachment']['tmp_name'][$n], $destName)) |
||
| 268 | @chmod($destName, 0644); |
||
| 269 | |||
| 270 | // This is madness!! |
||
| 271 | else |
||
| 272 | { |
||
| 273 | // File couldn't be moved. |
||
| 274 | $_SESSION['temp_attachments'][$attachID]['errors'][] = 'attach_timeout'; |
||
| 275 | if (file_exists($_FILES['attachment']['tmp_name'][$n])) |
||
| 276 | unlink($_FILES['attachment']['tmp_name'][$n]); |
||
| 277 | } |
||
| 278 | } |
||
| 279 | |||
| 280 | // Fill up a nice array with some data from the file and the errors encountered so far. |
||
| 281 | else |
||
| 282 | { |
||
| 283 | $_SESSION['temp_attachments'][$attachID] = array( |
||
| 284 | 'name' => $smcFunc['htmlspecialchars'](basename($_FILES['attachment']['name'][$n])), |
||
| 285 | 'tmp_name' => $destName, |
||
| 286 | 'errors' => $errors, |
||
| 287 | ); |
||
| 288 | |||
| 289 | if (file_exists($_FILES['attachment']['tmp_name'][$n])) |
||
| 290 | unlink($_FILES['attachment']['tmp_name'][$n]); |
||
| 291 | } |
||
| 292 | |||
| 293 | // If there's no errors to this point. We still do need to apply some additional checks before we are finished. |
||
| 294 | if (empty($_SESSION['temp_attachments'][$attachID]['errors'])) |
||
| 295 | attachmentChecks($attachID); |
||
| 296 | } |
||
| 297 | |||
| 298 | // Mod authors, finally a hook to hang an alternate attachment upload system upon |
||
| 299 | // Upload to the current attachment folder with the file name $attachID or 'post_tmp_' . $user_info['id'] . '_' . md5(mt_rand()) |
||
| 300 | // Populate $_SESSION['temp_attachments'][$attachID] with the following: |
||
| 301 | // name => The file name |
||
| 302 | // tmp_name => Path to the temp file ($this->_attchDir . '/' . $attachID). |
||
| 303 | // size => File size (required). |
||
| 304 | // type => MIME type (optional if not available on upload). |
||
| 305 | // id_folder => $modSettings['currentAttachmentUploadDir'] |
||
| 306 | // errors => An array of errors (use the index of the $txt variable for that error). |
||
| 307 | // Template changes can be done using "integrate_upload_template". |
||
| 308 | call_integration_hook('integrate_attachment_upload', array()); |
||
| 309 | } |
||
| 310 | |||
| 447 |
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVarassignment in line 1 and the$higherassignment in line 2 are dead. The first because$myVaris never used and the second because$higheris always overwritten for every possible time line.