Conditions | 27 |
Paths | > 20000 |
Total Lines | 171 |
Code Lines | 80 |
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 |
||
214 | protected function processAttachments() |
||
215 | { |
||
216 | global $context, $modSettings, $smcFunc, $user_info, $txt; |
||
217 | |||
218 | if (!isset($_FILES['attachment']['name'])) |
||
219 | $_FILES['attachment']['tmp_name'] = array(); |
||
220 | |||
221 | // If there are attachments, calculate the total size and how many. |
||
222 | $context['attachments']['total_size'] = 0; |
||
223 | $context['attachments']['quantity'] = 0; |
||
224 | |||
225 | // If this isn't a new post, check the current attachments. |
||
226 | if (isset($_REQUEST['msg'])) |
||
227 | { |
||
228 | $context['attachments']['quantity'] = count($context['current_attachments']); |
||
229 | foreach ($context['current_attachments'] as $attachment) |
||
230 | $context['attachments']['total_size'] += $attachment['size']; |
||
231 | } |
||
232 | |||
233 | // A bit of house keeping first. |
||
234 | if (!empty($_SESSION['temp_attachments']) && count($_SESSION['temp_attachments']) == 1) |
||
235 | unset($_SESSION['temp_attachments']); |
||
236 | |||
237 | // Our infamous SESSION var, we are gonna have soo much fun with it! |
||
238 | if (!isset($_SESSION['temp_attachments'])) |
||
239 | $_SESSION['temp_attachments'] = array(); |
||
240 | |||
241 | // Make sure we're uploading to the right place. |
||
242 | if (!empty($modSettings['automanage_attachments'])) |
||
243 | automanage_attachments_check_directory(); |
||
244 | |||
245 | // Is the attachments folder actually there? |
||
246 | if (!empty($context['dir_creation_error'])) |
||
247 | $this->_generalErrors[] = $context['dir_creation_error']; |
||
248 | |||
249 | // The current attach folder ha some issues... |
||
250 | elseif (!is_dir($this->_attchDir)) |
||
251 | { |
||
252 | $this->_generalErrors[] = 'attach_folder_warning'; |
||
253 | log_error(sprintf($txt['attach_folder_admin_warning'], $this->_attchDir), 'critical'); |
||
254 | } |
||
255 | |||
256 | // If this isn't a new post, check the current attachments. |
||
257 | if (empty($this->_generalErrors) && $this->_msg) |
||
258 | { |
||
259 | $context['attachments'] = array(); |
||
260 | $request = $smcFunc['db_query']('', ' |
||
261 | SELECT COUNT(*), SUM(size) |
||
262 | FROM {db_prefix}attachments |
||
263 | WHERE id_msg = {int:id_msg} |
||
264 | AND attachment_type = {int:attachment_type}', |
||
265 | array( |
||
266 | 'id_msg' => (int) $this->_msg, |
||
267 | 'attachment_type' => 0, |
||
268 | ) |
||
269 | ); |
||
270 | list ($context['attachments']['quantity'], $context['attachments']['total_size']) = $smcFunc['db_fetch_row']($request); |
||
271 | $smcFunc['db_free_result']($request); |
||
272 | } |
||
273 | |||
274 | else |
||
275 | $context['attachments'] = array( |
||
276 | 'quantity' => 0, |
||
277 | 'total_size' => 0, |
||
278 | ); |
||
279 | |||
280 | // Check for other general errors here. |
||
281 | |||
282 | // If we have an initial error, delete the files. |
||
283 | if (!empty($this->_generalErrors)) |
||
284 | { |
||
285 | // And delete the files 'cos they ain't going nowhere. |
||
286 | foreach ($_FILES['attachment']['tmp_name'] as $n => $dummy) |
||
287 | if (file_exists($_FILES['attachment']['tmp_name'][$n])) |
||
288 | unlink($_FILES['attachment']['tmp_name'][$n]); |
||
289 | |||
290 | $_FILES['attachment']['tmp_name'] = array(); |
||
291 | |||
292 | // No point in going further with this. |
||
293 | return; |
||
294 | } |
||
295 | |||
296 | // Loop through $_FILES['attachment'] array and move each file to the current attachments folder. |
||
297 | foreach ($_FILES['attachment']['tmp_name'] as $n => $dummy) |
||
298 | { |
||
299 | if ($_FILES['attachment']['name'][$n] == '') |
||
300 | continue; |
||
301 | |||
302 | // First, let's first check for PHP upload errors. |
||
303 | $errors = array(); |
||
304 | if (!empty($_FILES['attachment']['error'][$n])) |
||
305 | { |
||
306 | if ($_FILES['attachment']['error'][$n] == 2) |
||
307 | $errors[] = array('file_too_big', array($modSettings['attachmentSizeLimit'])); |
||
308 | |||
309 | else |
||
310 | log_error($_FILES['attachment']['name'][$n] . ': ' . $txt['php_upload_error_' . $_FILES['attachment']['error'][$n]]); |
||
311 | |||
312 | // Log this one, because... |
||
313 | if ($_FILES['attachment']['error'][$n] == 6) |
||
314 | log_error($_FILES['attachment']['name'][$n] . ': ' . $txt['php_upload_error_6'], 'critical'); |
||
315 | |||
316 | // Weird, no errors were cached, still fill out a generic one. |
||
317 | if (empty($errors)) |
||
318 | $errors[] = 'attach_php_error'; |
||
319 | } |
||
320 | |||
321 | // Try to move and rename the file before doing any more checks on it. |
||
322 | $attachID = 'post_tmp_' . $user_info['id'] . '_' . md5(mt_rand()); |
||
323 | $destName = $this->_attchDir . '/' . $attachID; |
||
324 | |||
325 | // No errors, YAY! |
||
326 | if (empty($errors)) |
||
327 | { |
||
328 | // The reported MIME type of the attachment might not be reliable. |
||
329 | $detected_mime_type = get_mime_type($_FILES['attachment']['tmp_name'][$n], true); |
||
330 | if ($detected_mime_type !== false) |
||
331 | $_FILES['attachment']['type'][$n] = $detected_mime_type; |
||
332 | |||
333 | $_SESSION['temp_attachments'][$attachID] = array( |
||
334 | 'name' => $smcFunc['htmlspecialchars'](basename($_FILES['attachment']['name'][$n])), |
||
335 | 'tmp_name' => $destName, |
||
336 | 'size' => $_FILES['attachment']['size'][$n], |
||
337 | 'type' => $_FILES['attachment']['type'][$n], |
||
338 | 'id_folder' => $modSettings['currentAttachmentUploadDir'], |
||
339 | 'errors' => array(), |
||
340 | ); |
||
341 | |||
342 | // Move the file to the attachments folder with a temp name for now. |
||
343 | if (@move_uploaded_file($_FILES['attachment']['tmp_name'][$n], $destName)) |
||
344 | smf_chmod($destName, 0644); |
||
345 | |||
346 | // This is madness!! |
||
347 | else |
||
348 | { |
||
349 | // File couldn't be moved. |
||
350 | $_SESSION['temp_attachments'][$attachID]['errors'][] = 'attach_timeout'; |
||
351 | if (file_exists($_FILES['attachment']['tmp_name'][$n])) |
||
352 | unlink($_FILES['attachment']['tmp_name'][$n]); |
||
353 | } |
||
354 | } |
||
355 | |||
356 | // Fill up a nice array with some data from the file and the errors encountered so far. |
||
357 | else |
||
358 | { |
||
359 | $_SESSION['temp_attachments'][$attachID] = array( |
||
360 | 'name' => $smcFunc['htmlspecialchars'](basename($_FILES['attachment']['name'][$n])), |
||
361 | 'tmp_name' => $destName, |
||
362 | 'errors' => $errors, |
||
363 | ); |
||
364 | |||
365 | if (file_exists($_FILES['attachment']['tmp_name'][$n])) |
||
366 | unlink($_FILES['attachment']['tmp_name'][$n]); |
||
367 | } |
||
368 | |||
369 | // If there's no errors to this point. We still do need to apply some additional checks before we are finished. |
||
370 | if (empty($_SESSION['temp_attachments'][$attachID]['errors'])) |
||
371 | attachmentChecks($attachID); |
||
372 | } |
||
373 | |||
374 | // Mod authors, finally a hook to hang an alternate attachment upload system upon |
||
375 | // Upload to the current attachment folder with the file name $attachID or 'post_tmp_' . $user_info['id'] . '_' . md5(mt_rand()) |
||
376 | // Populate $_SESSION['temp_attachments'][$attachID] with the following: |
||
377 | // name => The file name |
||
378 | // tmp_name => Path to the temp file ($this->_attchDir . '/' . $attachID). |
||
379 | // size => File size (required). |
||
380 | // type => MIME type (optional if not available on upload). |
||
381 | // id_folder => $modSettings['currentAttachmentUploadDir'] |
||
382 | // errors => An array of errors (use the index of the $txt variable for that error). |
||
383 | // Template changes can be done using "integrate_upload_template". |
||
384 | call_integration_hook('integrate_attachment_upload', array()); |
||
385 | } |
||
538 | ?> |
This check looks for function or method calls that always return null and whose return value is used.
The method
getObject()
can return nothing but null, so it makes no sense to use the return value.The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.