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. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
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) |
||
| 74 | |||
| 75 | /** |
||
| 76 | * @inheritdoc |
||
| 77 | */ |
||
| 78 | public function count(array $topic_data) |
||
| 82 | |||
| 83 | /** |
||
| 84 | * @inheritdoc |
||
| 85 | */ |
||
| 86 | public function show_comments($content_type, array $topic_data, array &$update_count) |
||
| 119 | |||
| 120 | /** |
||
| 121 | * @param array $topic_data |
||
| 122 | * @param array $posts_data |
||
| 123 | * @param array $topic_tracking_info |
||
| 124 | * @param array $users_cache |
||
| 125 | * @param array $update_count |
||
| 126 | * @param string $type |
||
| 127 | * @param int $start |
||
| 128 | * @return void |
||
| 129 | */ |
||
| 130 | protected function show_posts(array $topic_data, array $posts_data, array $topic_tracking_info, array $users_cache, array &$update_count, $type, $start) |
||
| 157 | |||
| 158 | /** |
||
| 159 | * @param string $view |
||
| 160 | * @param array $topic_data |
||
| 161 | * @retrun void |
||
| 162 | */ |
||
| 163 | protected function find_unread($view, array $topic_data) |
||
| 191 | |||
| 192 | /** |
||
| 193 | * This is for determining where we are (page) |
||
| 194 | * @param int $start |
||
| 195 | * @param int $post_id |
||
| 196 | * @param array $topic_data |
||
| 197 | * @param string $sort_dir |
||
| 198 | * @param string $base_url |
||
| 199 | * @return void |
||
| 200 | */ |
||
| 201 | protected function build_pagination(&$start, $post_id, array $topic_data, $sort_dir, $base_url) |
||
| 220 | |||
| 221 | /** |
||
| 222 | * @param array $topic_data |
||
| 223 | * @param string $base_url |
||
| 224 | * @return void |
||
| 225 | */ |
||
| 226 | protected function check_requested_post_id(array $topic_data, $base_url) |
||
| 240 | |||
| 241 | /** |
||
| 242 | * @param array $topic_data |
||
| 243 | * @param string $sort_dir |
||
| 244 | * @param int $post_id |
||
| 245 | * @return int |
||
| 246 | */ |
||
| 247 | protected function get_next_posts_count(array $topic_data, $sort_dir, $post_id) |
||
| 265 | |||
| 266 | /** |
||
| 267 | * @param int $forum_id |
||
| 268 | * @param int $topic_id |
||
| 269 | * @param int $post_id |
||
| 270 | * @param string $sort_dir |
||
| 271 | * @return int |
||
| 272 | */ |
||
| 273 | protected function get_prev_posts_count($forum_id, $topic_id, $post_id, $sort_dir) |
||
| 303 | |||
| 304 | /** |
||
| 305 | * @param int $sort_days |
||
| 306 | * @param string $sort_key |
||
| 307 | * @param string $sort_dir |
||
| 308 | * @param string $u_sort_param |
||
| 309 | * @return void |
||
| 310 | */ |
||
| 311 | protected function set_sorting_options(&$sort_days, &$sort_key, &$sort_dir, &$u_sort_param) |
||
| 333 | |||
| 334 | /** |
||
| 335 | * @param int post_id |
||
| 336 | * @param array $attachments |
||
| 337 | * @return array |
||
| 338 | */ |
||
| 339 | protected function get_attachments_tpl_data($post_id, array $attachments) |
||
| 353 | |||
| 354 | /** |
||
| 355 | * @param array $poster_info |
||
| 356 | * @return string |
||
| 357 | */ |
||
| 358 | protected function get_poster_warnings(array $poster_info) |
||
| 362 | |||
| 363 | /** |
||
| 364 | * @param array $row |
||
| 365 | * @return bool |
||
| 366 | */ |
||
| 367 | protected function get_report_status(array $row) |
||
| 371 | |||
| 372 | /** |
||
| 373 | * @param array $row |
||
| 374 | * @param string $topic_url |
||
| 375 | * @return string |
||
| 376 | */ |
||
| 377 | protected function get_post_display_lang(array $row, $topic_url) |
||
| 381 | |||
| 382 | /** |
||
| 383 | * @param string $topic_url |
||
| 384 | * @param int $start |
||
| 385 | * @return void |
||
| 386 | */ |
||
| 387 | protected function set_form_action($topic_url, $start) |
||
| 391 | } |
||
| 392 |
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: