Total Complexity | 84 |
Total Lines | 511 |
Duplicated Lines | 0 % |
Changes | 0 |
Complex classes like Attachments often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use Attachments, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
24 | class Attachments |
||
25 | { |
||
26 | /** |
||
27 | * @var int $_msg The ID of the message this attachment is associated with |
||
28 | */ |
||
29 | protected $_msg = 0; |
||
30 | |||
31 | /** |
||
32 | * @var int|null $_board The ID of the board this attachment's post is in or null if it's not set |
||
33 | */ |
||
34 | protected $_board = null; |
||
35 | |||
36 | /** |
||
37 | * @var string|bool $_attachmentUploadDir An array of info about attachment upload directories or false |
||
38 | */ |
||
39 | protected $_attachmentUploadDir = false; |
||
40 | |||
41 | /** |
||
42 | * @var string $_attchDir The path to the current attachment directory |
||
43 | */ |
||
44 | protected $_attchDir = ''; |
||
45 | |||
46 | /** |
||
47 | * @var int $_currentAttachmentUploadDir ID of the current attachment directory |
||
48 | */ |
||
49 | protected $_currentAttachmentUploadDir; |
||
50 | |||
51 | /** |
||
52 | * @var bool $_canPostAttachment Whether or not an attachment can be posted |
||
53 | */ |
||
54 | protected $_canPostAttachment; |
||
55 | |||
56 | /** |
||
57 | * @var array $_generalErrors An array of information about any errors that occurred |
||
58 | */ |
||
59 | protected $_generalErrors = array(); |
||
60 | |||
61 | /** |
||
62 | * @var mixed $_initialError Not used? |
||
63 | */ |
||
64 | protected $_initialError; |
||
65 | |||
66 | /** |
||
67 | * @var array $_attachments Not used? |
||
68 | */ |
||
69 | protected $_attachments = array(); |
||
70 | |||
71 | /** |
||
72 | * @var array $_attachResults An array of information about the results of each file |
||
73 | */ |
||
74 | protected $_attachResults = array(); |
||
75 | |||
76 | /** |
||
77 | * @var array $_attachSuccess An array of information about successful attachments |
||
78 | */ |
||
79 | protected $_attachSuccess = array(); |
||
80 | |||
81 | /** |
||
82 | * @var array $_response An array of response information. @used-by \sendResponse() when adding attachments |
||
83 | */ |
||
84 | protected $_response = array( |
||
85 | 'error' => true, |
||
86 | 'data' => array(), |
||
87 | 'extra' => '', |
||
88 | ); |
||
89 | |||
90 | /** |
||
91 | * @var array $_subActions An array of all valid sub-actions |
||
92 | */ |
||
93 | protected $_subActions = array( |
||
94 | 'add', |
||
95 | 'delete', |
||
96 | ); |
||
97 | |||
98 | /** |
||
99 | * @var string|bool $_sa The current sub-action, or false if there isn't one |
||
100 | */ |
||
101 | protected $_sa = false; |
||
102 | |||
103 | /** |
||
104 | * Attachments constructor. |
||
105 | * |
||
106 | * Sets up some initial information - the message ID, board, current attachment upload dir, etc. |
||
107 | */ |
||
108 | public function __construct() |
||
122 | } |
||
123 | |||
124 | /** |
||
125 | * Handles calling the appropriate function based on the sub-action |
||
126 | */ |
||
127 | public function call() |
||
128 | { |
||
129 | global $smcFunc, $sourcedir; |
||
130 | |||
131 | require_once($sourcedir . '/Subs-Attachments.php'); |
||
132 | |||
133 | // Need this. For reasons... |
||
134 | loadLanguage('Post'); |
||
135 | |||
136 | $this->_sa = !empty($_REQUEST['sa']) ? $smcFunc['htmlspecialchars']($smcFunc['htmltrim']($_REQUEST['sa'])) : false; |
||
137 | |||
138 | if ($this->_canPostAttachment && $this->_sa && in_array($this->_sa, $this->_subActions)) |
||
139 | $this->{$this->_sa}(); |
||
140 | |||
141 | // Just send a generic message. |
||
142 | else |
||
143 | $this->setResponse(array( |
||
144 | 'text' => $this->_sa == 'add' ? 'attach_error_title' : 'attached_file_deleted_error', |
||
145 | 'type' => 'error', |
||
146 | 'data' => false, |
||
147 | )); |
||
148 | |||
149 | // Back to the future, oh, to the browser! |
||
150 | $this->sendResponse(); |
||
151 | } |
||
152 | |||
153 | /** |
||
154 | * Handles deleting the attachment |
||
155 | */ |
||
156 | public function delete() |
||
184 | )); |
||
185 | } |
||
186 | |||
187 | /** |
||
188 | * Handles adding an attachment |
||
189 | */ |
||
190 | public function add() |
||
191 | { |
||
192 | // You gotta be able to post attachments. |
||
193 | if (!$this->_canPostAttachment) |
||
194 | return $this->setResponse(array( |
||
195 | 'text' => 'attached_file_cannot', |
||
196 | 'type' => 'error', |
||
197 | 'data' => false, |
||
198 | )); |
||
199 | |||
200 | // Process them at once! |
||
201 | $this->processAttachments(); |
||
202 | |||
203 | // The attachments was created and moved the the right folder, time to update the DB. |
||
204 | if (!empty($_SESSION['temp_attachments'])) |
||
205 | $this->createAttach(); |
||
206 | |||
207 | // Set the response. |
||
208 | $this->setResponse(); |
||
209 | } |
||
210 | |||
211 | /** |
||
212 | * Moves an attachment to the proper directory and set the relevant data into $_SESSION['temp_attachments'] |
||
213 | */ |
||
214 | protected function processAttachments() |
||
385 | } |
||
386 | |||
387 | /** |
||
388 | * Actually attaches the file |
||
389 | */ |
||
390 | protected function createAttach() |
||
391 | { |
||
392 | global $txt, $user_info, $modSettings; |
||
393 | |||
394 | // Create an empty session var to keep track of all the files we attached. |
||
395 | if (!isset($_SESSION['already_attached'])) |
||
396 | $_SESSION['already_attached'] = array(); |
||
397 | |||
398 | foreach ($_SESSION['temp_attachments'] as $attachID => $attachment) |
||
399 | { |
||
400 | $attachmentOptions = array( |
||
401 | 'post' => $this->_msg, |
||
402 | 'poster' => $user_info['id'], |
||
403 | 'name' => $attachment['name'], |
||
404 | 'tmp_name' => $attachment['tmp_name'], |
||
405 | 'size' => isset($attachment['size']) ? $attachment['size'] : 0, |
||
406 | 'mime_type' => isset($attachment['type']) ? $attachment['type'] : '', |
||
407 | 'id_folder' => isset($attachment['id_folder']) ? $attachment['id_folder'] : $modSettings['currentAttachmentUploadDir'], |
||
408 | 'approved' => !$modSettings['postmod_active'] || allowedTo('post_attachment'), |
||
409 | 'errors' => array(), |
||
410 | ); |
||
411 | |||
412 | if (empty($attachment['errors'])) |
||
413 | { |
||
414 | if (createAttachment($attachmentOptions)) |
||
415 | { |
||
416 | // Avoid JS getting confused. |
||
417 | $attachmentOptions['attachID'] = $attachmentOptions['id']; |
||
418 | unset($attachmentOptions['id']); |
||
419 | |||
420 | $_SESSION['already_attached'][$attachmentOptions['attachID']] = $attachmentOptions['attachID']; |
||
421 | |||
422 | if (!empty($attachmentOptions['thumb'])) |
||
423 | $_SESSION['already_attached'][$attachmentOptions['thumb']] = $attachmentOptions['thumb']; |
||
424 | |||
425 | if ($this->_msg) |
||
426 | assignAttachments($_SESSION['already_attached'], $this->_msg); |
||
427 | } |
||
428 | } |
||
429 | else |
||
430 | { |
||
431 | // Sort out the errors for display and delete any associated files. |
||
432 | $log_these = array('attachments_no_create', 'attachments_no_write', 'attach_timeout', 'ran_out_of_space', 'cant_access_upload_path', 'attach_0_byte_file'); |
||
433 | |||
434 | foreach ($attachment['errors'] as $error) |
||
435 | { |
||
436 | $attachmentOptions['errors'][] = sprintf($txt['attach_warning'], $attachment['name']); |
||
437 | |||
438 | if (!is_array($error)) |
||
439 | { |
||
440 | $attachmentOptions['errors'][] = $txt[$error]; |
||
441 | if (in_array($error, $log_these)) |
||
442 | log_error($attachment['name'] . ': ' . $txt[$error], 'critical'); |
||
443 | } |
||
444 | else |
||
445 | $attachmentOptions['errors'][] = vsprintf($txt[$error[0]], (array) $error[1]); |
||
446 | } |
||
447 | if (file_exists($attachment['tmp_name'])) |
||
448 | unlink($attachment['tmp_name']); |
||
449 | } |
||
450 | |||
451 | // You don't need to know. |
||
452 | unset($attachmentOptions['tmp_name']); |
||
453 | unset($attachmentOptions['destination']); |
||
454 | |||
455 | // Regardless of errors, pass the results. |
||
456 | $this->_attachResults[] = $attachmentOptions; |
||
457 | } |
||
458 | |||
459 | // Temp save this on the db. |
||
460 | if (!empty($_SESSION['already_attached'])) |
||
461 | $this->_attachSuccess = $_SESSION['already_attached']; |
||
462 | |||
463 | unset($_SESSION['temp_attachments']); |
||
464 | |||
465 | // Allow user to see previews for all of this post's attachments, even if the post hasn't been submitted yet. |
||
466 | if (!isset($_SESSION['attachments_can_preview'])) |
||
467 | $_SESSION['attachments_can_preview'] = array(); |
||
468 | if (!empty($_SESSION['already_attached'])) |
||
469 | $_SESSION['attachments_can_preview'] += array_fill_keys(array_keys($_SESSION['already_attached']), true); |
||
470 | } |
||
471 | |||
472 | /** |
||
473 | * Sets up the response information |
||
474 | * |
||
475 | * @param array $data Data for the response if we're not adding an attachment |
||
476 | */ |
||
477 | protected function setResponse($data = array()) |
||
511 | } |
||
512 | |||
513 | /** |
||
514 | * Sends the response data |
||
515 | */ |
||
516 | protected function sendResponse() |
||
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.