Total Complexity | 52 |
Total Lines | 252 |
Duplicated Lines | 0 % |
Changes | 1 | ||
Bugs | 0 | Features | 0 |
Complex classes like AttachmentsDisplay 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 AttachmentsDisplay, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
19 | class AttachmentsDisplay |
||
20 | { |
||
21 | /** @var array The good old attachments array */ |
||
22 | protected $attachments = []; |
||
23 | |||
24 | /** @var array The message array */ |
||
25 | protected $messages = []; |
||
26 | |||
27 | /** @var bool If unapproved posts/attachments should be shown */ |
||
28 | protected $includeUnapproved = false; |
||
29 | |||
30 | /** |
||
31 | * @param int[] $messages |
||
32 | * @param int[] $posters |
||
33 | * @param bool $includeUnapproved |
||
34 | */ |
||
35 | public function __construct($messages, $posters, $includeUnapproved) |
||
57 | ); |
||
58 | } |
||
59 | } |
||
60 | |||
61 | /** |
||
62 | * Get all attachments associated with a set of posts. |
||
63 | * |
||
64 | * What it does: |
||
65 | * - This does not check permissions. |
||
66 | * |
||
67 | * @param int[] $messages array of messages ids |
||
68 | * @param bool $includeUnapproved = false |
||
69 | * @param string|null $filter name of a callback function |
||
70 | * @param array $all_posters |
||
71 | * |
||
72 | */ |
||
73 | protected function getAttachments($messages, $includeUnapproved = false, $filter = null, $all_posters = array()) |
||
74 | { |
||
75 | global $modSettings; |
||
76 | |||
77 | $db = database(); |
||
78 | |||
79 | $attachments = array(); |
||
80 | $temp = array(); |
||
81 | $db->fetchQuery(' |
||
82 | SELECT |
||
83 | a.id_attach, a.id_folder, a.id_msg, a.filename, a.file_hash, COALESCE(a.size, 0) AS filesize, a.downloads, a.approved, |
||
84 | a.width, a.height' . (empty($modSettings['attachmentShowImages']) || empty($modSettings['attachmentThumbnails']) ? '' : ', |
||
85 | COALESCE(thumb.id_attach, 0) AS id_thumb, thumb.width AS thumb_width, thumb.height AS thumb_height') . ' |
||
86 | FROM {db_prefix}attachments AS a' . (empty($modSettings['attachmentShowImages']) || empty($modSettings['attachmentThumbnails']) ? '' : ' |
||
87 | LEFT JOIN {db_prefix}attachments AS thumb ON (thumb.id_attach = a.id_thumb)') . ' |
||
88 | WHERE a.id_msg IN ({array_int:message_list}) |
||
89 | AND a.attachment_type = {int:attachment_type}', |
||
90 | array( |
||
91 | 'message_list' => $messages, |
||
92 | 'attachment_type' => 0, |
||
93 | ) |
||
94 | )->fetch_callback( |
||
95 | static function ($row) use ($includeUnapproved, $filter, $all_posters, &$attachments, &$temp) { |
||
96 | if (!$row['approved'] && !$includeUnapproved |
||
97 | && (empty($filter) || !call_user_func($filter, $row, $all_posters))) |
||
98 | { |
||
99 | return; |
||
100 | } |
||
101 | $temp[$row['id_attach']] = $row; |
||
102 | if (!isset($attachments[$row['id_msg']])) |
||
103 | { |
||
104 | $attachments[$row['id_msg']] = array(); |
||
105 | } |
||
106 | } |
||
107 | ); |
||
108 | |||
109 | // This is better than sorting it with the query... |
||
110 | ksort($temp); |
||
111 | |||
112 | foreach ($temp as $row) |
||
113 | { |
||
114 | $attachments[$row['id_msg']][] = $row; |
||
115 | } |
||
116 | |||
117 | $this->attachments = $attachments; |
||
118 | } |
||
119 | |||
120 | /** |
||
121 | * This loads an attachment's contextual data including, most importantly, its size if it is an image. |
||
122 | * |
||
123 | * What it does: |
||
124 | * |
||
125 | * - Pre-condition: $attachments array to have been filled with the proper attachment data, as Display() does. |
||
126 | * - It requires the view_attachments permission to calculate image size. |
||
127 | * - It attempts to keep the "aspect ratio" of the posted image in line, even if it has to be resized by |
||
128 | * the max_image_width and max_image_height settings. |
||
129 | * |
||
130 | * @param int $id_msg message number to load attachments for |
||
131 | * @return array of attachments |
||
132 | * @todo change this pre-condition, too fragile and error-prone. |
||
133 | * |
||
134 | */ |
||
135 | public function loadAttachmentContext($id_msg) |
||
191 | } |
||
192 | |||
193 | /** |
||
194 | * Function that prepares image attachments |
||
195 | * |
||
196 | * What it does: |
||
197 | * - Generates thumbnail if non exists, and they are enabled |
||
198 | * |
||
199 | * @param $attachmentData |
||
200 | * @param $attachment |
||
201 | * @param $id_msg |
||
202 | * @return void |
||
203 | */ |
||
204 | public function prepareAttachmentImage(&$attachmentData, $attachment, $id_msg) |
||
274 |