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