| Total Complexity | 43 |
| Total Lines | 387 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
Complex classes like comments 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 comments, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 12 | class comments extends form implements comments_interface |
||
| 13 | { |
||
| 14 | /** @var \phpbb\content_visibility */ |
||
| 15 | protected $content_visibility; |
||
| 16 | |||
| 17 | /** @var \phpbb\db\driver\driver_interface */ |
||
| 18 | protected $db; |
||
| 19 | |||
| 20 | /** @var \phpbb\pagination */ |
||
| 21 | protected $pagination; |
||
| 22 | |||
| 23 | /** @var \phpbb\request\request_interface */ |
||
| 24 | protected $request; |
||
| 25 | |||
| 26 | /** @var \phpbb\template\context */ |
||
| 27 | protected $template_context; |
||
| 28 | |||
| 29 | /** @var \blitze\sitemaker\services\forum\data */ |
||
|
|
|||
| 30 | protected $forum; |
||
| 31 | |||
| 32 | /** @var \blitze\content\services\topic */ |
||
| 33 | protected $topic; |
||
| 34 | |||
| 35 | /** @var array */ |
||
| 36 | private $sort_dir_sql = array('a' => 'ASC', 'd' => 'DESC'); |
||
| 37 | |||
| 38 | /** @var array */ |
||
| 39 | private $sort_by_sql = array( |
||
| 40 | 't' => 'p.post_time', |
||
| 41 | 's' => 'p.post_subject, p.post_id', |
||
| 42 | ); |
||
| 43 | |||
| 44 | /** |
||
| 45 | * Constructor |
||
| 46 | * |
||
| 47 | * @param \phpbb\auth\auth $auth Auth object |
||
| 48 | * @param \phpbb\config\config $config Config object |
||
| 49 | * @param \phpbb\content_visibility $content_visibility Phpbb Content visibility object |
||
| 50 | * @param \phpbb\db\driver\driver_interface $db Database object |
||
| 51 | * @param \phpbb\language\language $language Language Object |
||
| 52 | * @param \phpbb\pagination $pagination Pagination object |
||
| 53 | * @param \phpbb\request\request_interface $request Request object |
||
| 54 | * @param \phpbb\template\template $template Template object |
||
| 55 | * @param \phpbb\template\context $template_context Template context object |
||
| 56 | * @param \phpbb\user $user User object |
||
| 57 | * @param \blitze\sitemaker\services\forum\data $forum Forum Data object |
||
| 58 | * @param \blitze\content\services\topic $topic Topic object |
||
| 59 | * @param string $root_path Path to the phpbb directory. |
||
| 60 | * @param string $php_ext php file extension |
||
| 61 | */ |
||
| 62 | public function __construct(\phpbb\auth\auth $auth, \phpbb\config\config $config, \phpbb\content_visibility $content_visibility, \phpbb\db\driver\driver_interface $db, \phpbb\language\language $language, \phpbb\pagination $pagination, \phpbb\request\request_interface $request, \phpbb\template\template $template, \phpbb\template\context $template_context, \phpbb\user $user, \blitze\sitemaker\services\forum\data $forum, \blitze\content\services\topic $topic, $root_path, $php_ext) |
||
| 63 | { |
||
| 64 | parent::__construct($auth, $config, $language, $template, $user, $root_path, $php_ext); |
||
| 65 | |||
| 66 | $this->content_visibility = $content_visibility; |
||
| 67 | $this->db = $db; |
||
| 68 | $this->pagination = $pagination; |
||
| 69 | $this->request = $request; |
||
| 70 | $this->template_context = $template_context; |
||
| 71 | $this->forum = $forum; |
||
| 72 | $this->topic = $topic; |
||
| 73 | } |
||
| 74 | |||
| 75 | /** |
||
| 76 | * @inheritdoc |
||
| 77 | */ |
||
| 78 | public function count(array $topic_data) |
||
| 79 | { |
||
| 80 | return $this->content_visibility->get_count('topic_posts', $topic_data, $topic_data['forum_id']) - 1; |
||
| 81 | } |
||
| 82 | |||
| 83 | /** |
||
| 84 | * @inheritdoc |
||
| 85 | */ |
||
| 86 | public function show_comments($content_type, array $topic_data, array &$update_count) |
||
| 87 | { |
||
| 88 | if ($topic_data['total_comments']) |
||
| 89 | { |
||
| 90 | $view = $this->request->variable('view', ''); |
||
| 91 | $start = $this->request->variable('start', 0); |
||
| 92 | $post_id = $this->request->variable('p', 0); |
||
| 93 | |||
| 94 | $this->find_unread($view, $topic_data); |
||
| 95 | |||
| 96 | $sort_days = 0; |
||
| 97 | $sort_key = $sort_dir = $u_sort_param = ''; |
||
| 98 | $this->set_sorting_options($sort_days, $sort_key, $sort_dir, $u_sort_param); |
||
| 99 | |||
| 100 | $base_url = append_sid(trim(build_url(array('start', 'p')), '?'), (strlen($u_sort_param)) ? $u_sort_param : ''); |
||
| 101 | $this->build_pagination($start, $post_id, $topic_data, $sort_dir, $base_url); |
||
| 102 | |||
| 103 | $this->forum->query() |
||
| 104 | ->fetch_date_range(time(), $sort_days * 86400, 'post') |
||
| 105 | ->build(); |
||
| 106 | $posts_data = $this->forum->get_post_data(false, array(), (int) $this->config['posts_per_page'], $start, array( |
||
| 107 | 'WHERE' => array( |
||
| 108 | 'p.topic_id = ' . (int) $topic_data['topic_id'], |
||
| 109 | 'p.post_id <> ' . (int) $topic_data['topic_first_post_id'], |
||
| 110 | ), |
||
| 111 | 'ORDER_BY' => $this->sort_by_sql[$sort_key] . ' ' . $this->sort_dir_sql[$sort_dir], |
||
| 112 | )); |
||
| 113 | |||
| 114 | $topic_tracking_info = $this->forum->get_topic_tracking_info(); |
||
| 115 | $users_cache = $this->forum->get_posters_info(); |
||
| 116 | |||
| 117 | $this->show_posts($topic_data, array_values(array_shift($posts_data)), $topic_tracking_info, $users_cache, $update_count, $content_type, $start); |
||
| 118 | } |
||
| 119 | } |
||
| 120 | |||
| 121 | /** |
||
| 122 | * @param array $topic_data |
||
| 123 | * @param array $posts_data |
||
| 124 | * @param array $topic_tracking_info |
||
| 125 | * @param array $users_cache |
||
| 126 | * @param array $update_count |
||
| 127 | * @param string $type |
||
| 128 | * @param int $start |
||
| 129 | * @return void |
||
| 130 | */ |
||
| 131 | protected function show_posts(array $topic_data, array $posts_data, array $topic_tracking_info, array $users_cache, array &$update_count, $type, $start) |
||
| 132 | { |
||
| 133 | $attachments = $this->forum->get_attachments($topic_data['forum_id']); |
||
| 134 | $this->set_form_action($topic_data['topic_url'], $start); |
||
| 135 | |||
| 136 | for ($i = 0, $size = sizeof($posts_data); $i < $size; $i++) |
||
| 137 | { |
||
| 138 | $row = $posts_data[$i]; |
||
| 139 | $poster_id = $row['poster_id']; |
||
| 140 | |||
| 141 | $this->template->assign_block_vars('postrow', array_merge( |
||
| 142 | $this->topic->get_detail_template_data($type, $topic_data, $row, $users_cache, $attachments, $topic_tracking_info, $update_count), |
||
| 143 | $this->get_attachments_tpl_data($row['post_id'], $attachments), |
||
| 144 | array( |
||
| 145 | 'POST_SUBJECT' => $row['post_subject'], |
||
| 146 | 'POST_DATE' => $this->user->format_date($row['post_time'], false, false), |
||
| 147 | 'POSTER_WARNINGS' => $this->get_poster_warnings($users_cache[$poster_id]), |
||
| 148 | 'S_POST_REPORTED' => $this->get_report_status($row), |
||
| 149 | 'S_TOPIC_POSTER' => ($topic_data['topic_poster'] == $poster_id) ? true : false, |
||
| 150 | ) |
||
| 151 | )); |
||
| 152 | |||
| 153 | $this->topic->show_attachments($attachments, $row['post_id'], 'postrow.attachment'); |
||
| 154 | } |
||
| 155 | } |
||
| 156 | |||
| 157 | /** |
||
| 158 | * @param string $view |
||
| 159 | * @param array $topic_data |
||
| 160 | * @retrun void |
||
| 161 | */ |
||
| 162 | protected function find_unread($view, array $topic_data) |
||
| 163 | { |
||
| 164 | if ($view === 'unread') |
||
| 165 | { |
||
| 166 | $forum_id = (int) $topic_data['forum_id']; |
||
| 167 | $topic_id = (int) $topic_data['topic_id']; |
||
| 168 | |||
| 169 | // Get topic tracking info |
||
| 170 | $topic_tracking_info = get_complete_topic_tracking($forum_id, $topic_id); |
||
| 171 | $topic_last_read = (isset($topic_tracking_info[$topic_id])) ? $topic_tracking_info[$topic_id] : 0; |
||
| 172 | |||
| 173 | $sql = 'SELECT post_id, topic_id, forum_id |
||
| 174 | FROM ' . POSTS_TABLE . " |
||
| 175 | WHERE topic_id = $topic_id |
||
| 176 | AND " . $this->content_visibility->get_visibility_sql('post', $forum_id) . " |
||
| 177 | AND post_time > $topic_last_read |
||
| 178 | AND forum_id = $forum_id |
||
| 179 | ORDER BY post_time ASC, post_id ASC"; |
||
| 180 | $result = $this->db->sql_query_limit($sql, 1); |
||
| 181 | $row = $this->db->sql_fetchrow($result); |
||
| 182 | $this->db->sql_freeresult($result); |
||
| 183 | |||
| 184 | if ($row) |
||
| 185 | { |
||
| 186 | redirect(append_sid($topic_data['topic_url'], 'p=' . $row['post_id']) . '#p' . $row['post_id']); |
||
| 187 | } |
||
| 188 | } |
||
| 189 | } |
||
| 190 | |||
| 191 | /** |
||
| 192 | * This is for determining where we are (page) |
||
| 193 | * @param int $start |
||
| 194 | * @param int $post_id |
||
| 195 | * @param array $topic_data |
||
| 196 | * @param string $sort_dir |
||
| 197 | * @param string $base_url |
||
| 198 | * @return void |
||
| 199 | */ |
||
| 200 | protected function build_pagination(&$start, $post_id, array $topic_data, $sort_dir, $base_url) |
||
| 201 | { |
||
| 202 | if ($post_id) |
||
| 203 | { |
||
| 204 | $post_info = $this->get_post_info($post_id); |
||
| 205 | $this->check_requested_post_id($post_info, $topic_data, $base_url); |
||
| 206 | |||
| 207 | $prev_posts = $this->get_next_posts_count($post_info, $topic_data, $sort_dir, $post_id); |
||
| 208 | $start = (int) floor($prev_posts / $this->config['posts_per_page']) * $this->config['posts_per_page']; |
||
| 209 | } |
||
| 210 | |||
| 211 | $start = $this->pagination->validate_start($start, (int) $this->config['posts_per_page'], $topic_data['total_comments']); |
||
| 212 | $this->pagination->generate_template_pagination($base_url, 'pagination', 'start', $topic_data['total_comments'], (int) $this->config['posts_per_page'], $start); |
||
| 213 | $this->add_comment_anchor_to_pages(); |
||
| 214 | } |
||
| 215 | |||
| 216 | /** |
||
| 217 | * @return void |
||
| 218 | */ |
||
| 219 | protected function add_comment_anchor_to_pages() |
||
| 220 | { |
||
| 221 | $data =& $this->template_context->get_data_ref()['pagination']; |
||
| 222 | $data = (array) $data; |
||
| 223 | |||
| 224 | foreach ($data as &$row) |
||
| 225 | { |
||
| 226 | $row['PAGE_URL'] .= '#comments'; |
||
| 227 | } |
||
| 228 | } |
||
| 229 | |||
| 230 | /** |
||
| 231 | * @param array $post_info |
||
| 232 | * @param array $topic_data |
||
| 233 | * @param string $base_url |
||
| 234 | * @return void |
||
| 235 | */ |
||
| 236 | protected function check_requested_post_id(array $post_info, array $topic_data, $base_url) |
||
| 237 | { |
||
| 238 | // are we where we are supposed to be? |
||
| 239 | if (($post_info['post_visibility'] == ITEM_UNAPPROVED || $post_info['post_visibility'] == ITEM_REAPPROVE) && !$this->auth->acl_get('m_approve', $topic_data['forum_id'])) |
||
| 240 | { |
||
| 241 | // If post_id was submitted, we try at least to display the topic as a last resort... |
||
| 242 | if ($topic_data['topic_id']) |
||
| 243 | { |
||
| 244 | redirect($base_url); |
||
| 245 | } |
||
| 246 | |||
| 247 | trigger_error('NO_TOPIC'); |
||
| 248 | } |
||
| 249 | } |
||
| 250 | |||
| 251 | /** |
||
| 252 | * @param array $post_info |
||
| 253 | * @param array $topic_data |
||
| 254 | * @param string $sort_dir |
||
| 255 | * @param int $post_id |
||
| 256 | * @return int |
||
| 257 | */ |
||
| 258 | protected function get_next_posts_count(array $post_info, array $topic_data, $sort_dir, $post_id) |
||
| 259 | { |
||
| 260 | if ($post_id == $topic_data['topic_first_post_id'] || $post_id == $topic_data['topic_last_post_id']) |
||
| 261 | { |
||
| 262 | $check_sort = ($post_id == $topic_data['topic_first_post_id']) ? 'd' : 'a'; |
||
| 263 | |||
| 264 | $prev_posts_count = 0; |
||
| 265 | if ($sort_dir == $check_sort) |
||
| 266 | { |
||
| 267 | $prev_posts_count = $this->content_visibility->get_count('topic_posts', $topic_data, $topic_data['forum_id']) - 1; |
||
| 268 | } |
||
| 269 | return $prev_posts_count; |
||
| 270 | } |
||
| 271 | else |
||
| 272 | { |
||
| 273 | return $this->get_prev_posts_count($post_info, $topic_data['forum_id'], $topic_data['topic_id'], $sort_dir) - 1; |
||
| 274 | } |
||
| 275 | } |
||
| 276 | |||
| 277 | /** |
||
| 278 | * @param array $row |
||
| 279 | * @param int $forum_id |
||
| 280 | * @param int $topic_id |
||
| 281 | * @param string $sort_dir |
||
| 282 | * @return int |
||
| 283 | */ |
||
| 284 | protected function get_prev_posts_count(array $row, $forum_id, $topic_id, $sort_dir) |
||
| 285 | { |
||
| 286 | $sql = 'SELECT COUNT(p.post_id) AS prev_posts |
||
| 287 | FROM ' . POSTS_TABLE . " p |
||
| 288 | WHERE p.topic_id = $topic_id |
||
| 289 | AND " . $this->content_visibility->get_visibility_sql('post', $forum_id, 'p.'); |
||
| 290 | |||
| 291 | if ($sort_dir == 'd') |
||
| 292 | { |
||
| 293 | $sql .= " AND (p.post_time > {$row['post_time']} OR (p.post_time = {$row['post_time']} AND p.post_id >= {$row['post_id']}))"; |
||
| 294 | } |
||
| 295 | else |
||
| 296 | { |
||
| 297 | $sql .= " AND (p.post_time < {$row['post_time']} OR (p.post_time = {$row['post_time']} AND p.post_id <= {$row['post_id']}))"; |
||
| 298 | } |
||
| 299 | |||
| 300 | $result = $this->db->sql_query($sql); |
||
| 301 | $row = $this->db->sql_fetchrow($result); |
||
| 302 | $this->db->sql_freeresult($result); |
||
| 303 | |||
| 304 | return $row['prev_posts']; |
||
| 305 | } |
||
| 306 | |||
| 307 | /** |
||
| 308 | * @param int $post_id |
||
| 309 | * @return array |
||
| 310 | */ |
||
| 311 | protected function get_post_info($post_id) |
||
| 312 | { |
||
| 313 | $sql = 'SELECT post_id, post_time, post_visibility |
||
| 314 | FROM ' . POSTS_TABLE . ' p |
||
| 315 | WHERE post_id = ' . (int) $post_id; |
||
| 316 | $result = $this->db->sql_query($sql); |
||
| 317 | $row = $this->db->sql_fetchrow($result); |
||
| 318 | $this->db->sql_freeresult($result); |
||
| 319 | |||
| 320 | return $row; |
||
| 321 | } |
||
| 322 | |||
| 323 | /** |
||
| 324 | * @param int $sort_days |
||
| 325 | * @param string $sort_key |
||
| 326 | * @param string $sort_dir |
||
| 327 | * @param string $u_sort_param |
||
| 328 | * @return void |
||
| 329 | */ |
||
| 330 | protected function set_sorting_options(&$sort_days, &$sort_key, &$sort_dir, &$u_sort_param) |
||
| 331 | { |
||
| 332 | $default_sort_days = (!empty($this->user->data['user_post_show_days'])) ? $this->user->data['user_post_show_days'] : 0; |
||
| 333 | $default_sort_key = (!empty($this->user->data['user_post_sortby_type'])) ? $this->user->data['user_post_sortby_type'] : 't'; |
||
| 334 | $default_sort_dir = (!empty($this->user->data['user_post_sortby_dir'])) ? $this->user->data['user_post_sortby_dir'] : 'a'; |
||
| 335 | |||
| 336 | $sort_days = $this->request->variable('st', $default_sort_days); |
||
| 337 | $sort_key = $this->request->variable('sk', $default_sort_key); |
||
| 338 | $sort_dir = $this->request->variable('sd', $default_sort_dir); |
||
| 339 | |||
| 340 | $limit_days = array(0 => $this->language->lang('ALL_POSTS'), 1 => $this->language->lang('1_DAY'), 7 => $this->language->lang('7_DAYS'), 14 => $this->language->lang('2_WEEKS'), 30 => $this->language->lang('1_MONTH'), 90 => $this->language->lang('3_MONTHS'), 180 => $this->language->lang('6_MONTHS'), 365 => $this->language->lang('1_YEAR')); |
||
| 341 | $sort_by_text = array('t' => $this->language->lang('POST_TIME'), 's' => $this->language->lang('SUBJECT')); |
||
| 342 | |||
| 343 | $s_limit_days = $s_sort_key = $s_sort_dir = ''; |
||
| 344 | gen_sort_selects($limit_days, $sort_by_text, $sort_days, $sort_key, $sort_dir, $s_limit_days, $s_sort_key, $s_sort_dir, $u_sort_param, $default_sort_days, $default_sort_key, $default_sort_dir); |
||
| 345 | |||
| 346 | $this->template->assign_vars(array( |
||
| 347 | 'S_SELECT_SORT_DIR' => $s_sort_dir, |
||
| 348 | 'S_SELECT_SORT_KEY' => $s_sort_key, |
||
| 349 | 'S_SELECT_SORT_DAYS' => $s_limit_days, |
||
| 350 | )); |
||
| 351 | } |
||
| 352 | |||
| 353 | /** |
||
| 354 | * @param int $post_id |
||
| 355 | * @param array $attachments |
||
| 356 | * @return array |
||
| 357 | */ |
||
| 358 | protected function get_attachments_tpl_data($post_id, array $attachments) |
||
| 359 | { |
||
| 360 | $has_attachments = $multi_attachments = false; |
||
| 361 | if (!empty($attachments[$post_id])) |
||
| 362 | { |
||
| 363 | $has_attachments = true; |
||
| 364 | $multi_attachments = sizeof($attachments[$post_id]) > 1; |
||
| 365 | } |
||
| 366 | |||
| 367 | return array( |
||
| 368 | 'S_HAS_ATTACHMENTS' => $has_attachments, |
||
| 369 | 'S_MULTIPLE_ATTACHMENTS' => $multi_attachments, |
||
| 370 | ); |
||
| 371 | } |
||
| 372 | |||
| 373 | /** |
||
| 374 | * @param array $poster_info |
||
| 375 | * @return int |
||
| 376 | */ |
||
| 377 | protected function get_poster_warnings(array $poster_info) |
||
| 378 | { |
||
| 379 | return ($this->auth->acl_get('m_warn') && !empty($poster_info['warnings'])) ? $poster_info['warnings'] : 0; |
||
| 380 | } |
||
| 381 | |||
| 382 | /** |
||
| 383 | * @param array $row |
||
| 384 | * @return bool |
||
| 385 | */ |
||
| 386 | protected function get_report_status(array $row) |
||
| 389 | } |
||
| 390 | |||
| 391 | /** |
||
| 392 | * @param string $topic_url |
||
| 393 | * @param int $start |
||
| 394 | * @return void |
||
| 395 | */ |
||
| 396 | protected function set_form_action($topic_url, $start) |
||
| 399 | } |
||
| 400 | } |
||
| 401 |
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths