Conditions | 36 |
Paths | 4 |
Total Lines | 132 |
Code Lines | 60 |
Lines | 0 |
Ratio | 0 % |
Changes | 1 | ||
Bugs | 0 | Features | 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 |
||
134 | public function loadAttachmentContext($id_msg) |
||
135 | { |
||
136 | global $context, $modSettings, $scripturl, $topic; |
||
137 | |||
138 | // Set up the attachment info - based on code by Meriadoc. |
||
139 | $attachmentData = array(); |
||
140 | $have_unapproved = false; |
||
141 | if (isset($this->attachments[$id_msg]) && !empty($modSettings['attachmentEnable'])) |
||
142 | { |
||
143 | foreach ($this->attachments[$id_msg] as $i => $attachment) |
||
144 | { |
||
145 | if (!empty($context['ila_dont_show_attach_below']) && in_array($attachment['id_attach'], $context['ila_dont_show_attach_below'])) |
||
146 | { |
||
147 | continue; |
||
148 | } |
||
149 | $attachmentData[$i] = array( |
||
150 | 'id' => $attachment['id_attach'], |
||
151 | 'name' => preg_replace('~&#(\\d{1,7}|x[0-9a-fA-F]{1,6});~', '&#\\1;', htmlspecialchars($attachment['filename'], ENT_COMPAT, 'UTF-8')), |
||
152 | 'downloads' => $attachment['downloads'], |
||
153 | 'size' => byte_format($attachment['filesize']), |
||
154 | 'byte_size' => $attachment['filesize'], |
||
155 | 'href' => $scripturl . '?action=dlattach;topic=' . $topic . '.0;attach=' . $attachment['id_attach'], |
||
156 | 'link' => '<a href="' . $scripturl . '?action=dlattach;topic=' . $topic . '.0;attach=' . $attachment['id_attach'] . '">' . htmlspecialchars($attachment['filename'], ENT_COMPAT, 'UTF-8') . '</a>', |
||
157 | 'is_image' => !empty($attachment['width']) && !empty($attachment['height']) && !empty($modSettings['attachmentShowImages']), |
||
158 | 'is_approved' => $attachment['approved'], |
||
159 | 'file_hash' => $attachment['file_hash'], |
||
160 | ); |
||
161 | |||
162 | // If something is unapproved we'll note it so we can sort them. |
||
163 | if (!$attachment['approved']) |
||
164 | { |
||
165 | $have_unapproved = true; |
||
166 | } |
||
167 | |||
168 | if (!$attachmentData[$i]['is_image']) |
||
169 | { |
||
170 | continue; |
||
171 | } |
||
172 | |||
173 | $attachmentData[$i]['real_width'] = $attachment['width']; |
||
174 | $attachmentData[$i]['width'] = $attachment['width']; |
||
175 | $attachmentData[$i]['real_height'] = $attachment['height']; |
||
176 | $attachmentData[$i]['height'] = $attachment['height']; |
||
177 | |||
178 | // Let's see, do we want thumbs? |
||
179 | if (!empty($modSettings['attachmentThumbnails']) |
||
180 | && !empty($modSettings['attachmentThumbWidth']) |
||
181 | && !empty($modSettings['attachmentThumbHeight']) |
||
182 | && ($attachment['width'] > $modSettings['attachmentThumbWidth'] || $attachment['height'] > $modSettings['attachmentThumbHeight']) && strlen($attachment['filename']) < 249) |
||
183 | { |
||
184 | // A proper thumb doesn't exist yet? Create one! Or, it needs update. |
||
185 | if (empty($attachment['id_thumb']) |
||
186 | || $attachment['thumb_width'] > $modSettings['attachmentThumbWidth'] |
||
187 | || $attachment['thumb_height'] > $modSettings['attachmentThumbHeight']) |
||
188 | //|| ($attachment['thumb_width'] < $modSettings['attachmentThumbWidth'] && $attachment['thumb_height'] < $modSettings['attachmentThumbHeight'])) |
||
189 | { |
||
190 | $filename = getAttachmentFilename($attachment['filename'], $attachment['id_attach'], $attachment['id_folder'], false, $attachment['file_hash']); |
||
191 | $attachment = array_merge($attachment, updateAttachmentThumbnail($filename, $attachment['id_attach'], $id_msg, $attachment['id_thumb'], $attachment['filename'])); |
||
192 | } |
||
193 | |||
194 | // Only adjust dimensions on successful thumbnail creation. |
||
195 | if (!empty($attachment['thumb_width']) && !empty($attachment['thumb_height'])) |
||
196 | { |
||
197 | $attachmentData[$i]['width'] = $attachment['thumb_width']; |
||
198 | $attachmentData[$i]['height'] = $attachment['thumb_height']; |
||
199 | } |
||
200 | } |
||
201 | |||
202 | if (!empty($attachment['id_thumb'])) |
||
203 | { |
||
204 | $attachmentData[$i]['thumbnail'] = array( |
||
205 | 'id' => $attachment['id_thumb'], |
||
206 | 'href' => getUrl('action', ['action' => 'dlattach', 'topic' => $topic . '.0', 'attach' => $attachment['id_thumb'], 'image']), |
||
207 | ); |
||
208 | } |
||
209 | $attachmentData[$i]['thumbnail']['has_thumb'] = !empty($attachment['id_thumb']); |
||
210 | |||
211 | // If thumbnails are disabled, check the maximum size of the image. |
||
212 | if (!$attachmentData[$i]['thumbnail']['has_thumb'] && ((!empty($modSettings['max_image_width']) && $attachment['width'] > $modSettings['max_image_width']) || (!empty($modSettings['max_image_height']) && $attachment['height'] > $modSettings['max_image_height']))) |
||
213 | { |
||
214 | if (!empty($modSettings['max_image_width']) && (empty($modSettings['max_image_height']) || $attachment['height'] * $modSettings['max_image_width'] / $attachment['width'] <= $modSettings['max_image_height'])) |
||
215 | { |
||
216 | $attachmentData[$i]['width'] = $modSettings['max_image_width']; |
||
217 | $attachmentData[$i]['height'] = floor($attachment['height'] * $modSettings['max_image_width'] / $attachment['width']); |
||
218 | } |
||
219 | elseif (!empty($modSettings['max_image_width'])) |
||
220 | { |
||
221 | $attachmentData[$i]['width'] = floor($attachment['width'] * $modSettings['max_image_height'] / $attachment['height']); |
||
222 | $attachmentData[$i]['height'] = $modSettings['max_image_height']; |
||
223 | } |
||
224 | } |
||
225 | elseif ($attachmentData[$i]['thumbnail']['has_thumb']) |
||
226 | { |
||
227 | // Data attributes for use in expandThumb |
||
228 | $attachmentData[$i]['thumbnail']['lightbox'] = 'data-lightboxmessage="' . $id_msg . '" data-lightboximage="' . $attachment['id_attach'] . '"'; |
||
229 | |||
230 | /* |
||
231 | // If the image is too large to show inline, make it a popup. |
||
232 | // @todo this needs to be removed or depreciated |
||
233 | if (((!empty($modSettings['max_image_width']) && $attachmentData[$i]['real_width'] > $modSettings['max_image_width']) || (!empty($modSettings['max_image_height']) && $attachmentData[$i]['real_height'] > $modSettings['max_image_height']))) |
||
234 | { |
||
235 | $attachmentData[$i]['thumbnail']['javascript'] = 'return reqWin(\'' . $attachmentData[$i]['href'] . ';image\', ' . ($attachment['width'] + 20) . ', ' . ($attachment['height'] + 20) . ', true);'; |
||
236 | } |
||
237 | else |
||
238 | { |
||
239 | $attachmentData[$i]['thumbnail']['javascript'] = 'return expandThumb(' . $attachment['id_attach'] . ');'; |
||
240 | } |
||
241 | */ |
||
242 | } |
||
243 | |||
244 | if (!$attachmentData[$i]['thumbnail']['has_thumb']) |
||
245 | { |
||
246 | $attachmentData[$i]['downloads']++; |
||
247 | } |
||
248 | } |
||
249 | } |
||
250 | |||
251 | // Do we need to instigate a sort? |
||
252 | if ($have_unapproved) |
||
253 | { |
||
254 | // Unapproved attachments go first. |
||
255 | usort($attachmentData, function($a, $b) { |
||
256 | if ($a['is_approved'] === $b['is_approved']) |
||
257 | { |
||
258 | return 0; |
||
259 | } |
||
260 | |||
261 | return $a['is_approved'] > $b['is_approved'] ? -1 : 1; |
||
262 | }); |
||
263 | } |
||
264 | |||
265 | return $attachmentData; |
||
266 | } |
||
268 |
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..