Complex classes like data 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 data, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 14 | class data extends query_builder |
||
| 15 | { |
||
| 16 | /** @var \phpbb\auth\auth */ |
||
| 17 | protected $auth; |
||
| 18 | |||
| 19 | /** @var \phpbb\config\config */ |
||
| 20 | protected $config; |
||
| 21 | |||
| 22 | /** @var \phpbb\content_visibility */ |
||
| 23 | protected $content_visibility; |
||
| 24 | |||
| 25 | /** @var \phpbb\db\driver\driver_interface */ |
||
| 26 | protected $db; |
||
| 27 | |||
| 28 | /** @var \phpbb\language\language */ |
||
| 29 | protected $translator; |
||
| 30 | |||
| 31 | /** @var \phpbb\user */ |
||
| 32 | protected $user; |
||
| 33 | |||
| 34 | /** @var string */ |
||
| 35 | protected $phpbb_root_path; |
||
| 36 | |||
| 37 | /** @var string */ |
||
| 38 | protected $php_ext; |
||
| 39 | |||
| 40 | /** |
||
| 41 | * Constructor |
||
| 42 | * |
||
| 43 | * @param \phpbb\auth\auth $auth Auth object |
||
| 44 | * @param \phpbb\config\config $config Config object |
||
| 45 | * @param \phpbb\content_visibility $content_visibility Content visibility |
||
| 46 | * @param \phpbb\db\driver\driver_interface $db Database connection |
||
| 47 | * @param \phpbb\language\language $translator Language object |
||
| 48 | * @param \phpbb\user $user User object |
||
| 49 | * @param string $phpbb_root_path Path to the phpbb includes directory. |
||
| 50 | * @param string $php_ext php file extension |
||
| 51 | * @param integer $cache_time Cache results for 3 hours by default |
||
| 52 | */ |
||
| 53 | 26 | 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 $translator, \phpbb\user $user, $phpbb_root_path, $php_ext, $cache_time = 10800) |
|
| 66 | |||
| 67 | /** |
||
| 68 | * Get topics count |
||
| 69 | * |
||
| 70 | * @return integer |
||
| 71 | */ |
||
| 72 | public function get_topics_count() |
||
| 86 | 10 | ||
| 87 | 10 | /** |
|
| 88 | 12 | * Get topic data |
|
| 89 | * |
||
| 90 | 12 | * @param mixed|false $limit |
|
| 91 | * @param int $start |
||
| 92 | * @return array |
||
| 93 | */ |
||
| 94 | public function get_topic_data($limit = false, $start = 0) |
||
| 112 | 9 | ||
| 113 | /** |
||
| 114 | 9 | * Get post data |
|
| 115 | 9 | * |
|
| 116 | 9 | * @param mixed|false $topic_first_or_last_post (first|last) |
|
| 117 | 9 | * @param array $post_ids |
|
| 118 | 9 | * @param bool|false $limit |
|
| 119 | 9 | * @param int $start |
|
| 120 | 10 | * @param array $sql_array |
|
| 121 | * @return array |
||
| 122 | 10 | */ |
|
| 123 | public function get_post_data($topic_first_or_last_post = false, $post_ids = array(), $limit = false, $start = 0, $sql_array = array()) |
||
| 141 | 6 | ||
| 142 | 6 | /** |
|
| 143 | * Get attachments... |
||
| 144 | 6 | * |
|
| 145 | * @param int $forum_id |
||
| 146 | 6 | * @param array $allowed_extensions |
|
| 147 | 6 | * @param bool $exclude_in_message |
|
| 148 | 6 | * @param string $order_by |
|
| 149 | 6 | * @return array |
|
| 150 | 7 | */ |
|
| 151 | public function get_attachments($forum_id = 0, $allowed_extensions = array(), $exclude_in_message = true, $order_by = 'filetime DESC, post_msg_id ASC') |
||
| 171 | |||
| 172 | /** |
||
| 173 | * Get topic tracking info |
||
| 174 | * |
||
| 175 | * @param int $forum_id |
||
| 176 | * @return array |
||
| 177 | */ |
||
| 178 | public function get_topic_tracking_info($forum_id = 0) |
||
| 189 | |||
| 190 | /** |
||
| 191 | * Returns an array of topic first post or last post ids |
||
| 192 | * |
||
| 193 | * @param string $first_or_last_post |
||
| 194 | * @return array |
||
| 195 | */ |
||
| 196 | public function get_topic_post_ids($first_or_last_post = 'first') |
||
| 200 | |||
| 201 | /** |
||
| 202 | * Returns an array of topic first post or last post ids |
||
| 203 | */ |
||
| 204 | public function get_posters_info() |
||
| 215 | 10 | ||
| 216 | /** |
||
| 217 | 10 | * @param mixed $topic_first_or_last_post |
|
| 218 | 10 | * @param array $post_ids |
|
| 219 | * @param array $sql_array |
||
| 220 | 10 | * @return array |
|
| 221 | */ |
||
| 222 | private function _get_posts_sql_array($topic_first_or_last_post, array $post_ids, array $sql_array) |
||
| 239 | 3 | ||
| 240 | /** |
||
| 241 | 10 | * @param array $post_ids |
|
| 242 | * @param string $topic_first_or_last_post |
||
| 243 | 10 | * @return array |
|
| 244 | */ |
||
| 245 | private function _get_post_data_where(array $post_ids, $topic_first_or_last_post) |
||
| 262 | |||
| 263 | 5 | /** |
|
| 264 | * @param array $sql_where |
||
| 265 | 5 | * @param string $topic_first_or_last_post |
|
| 266 | 5 | */ |
|
| 267 | 5 | private function _limit_posts_by_topic(array &$sql_where, $topic_first_or_last_post) |
|
| 276 | |||
| 277 | /** |
||
| 278 | * @return array |
||
| 279 | */ |
||
| 280 | private function _get_tracking_info() |
||
| 294 | |||
| 295 | /** |
||
| 296 | 5 | * @param string $function |
|
| 297 | * @return array |
||
| 298 | 5 | */ |
|
| 299 | private function _build_tracking_info($function) |
||
| 309 | |||
| 310 | /** |
||
| 311 | * @return bool |
||
| 312 | */ |
||
| 313 | 7 | private function _can_track_by_lastread() |
|
| 317 | |||
| 318 | /** |
||
| 319 | * @return bool |
||
| 320 | */ |
||
| 321 | private function _can_track_anonymous() |
||
| 325 | |||
| 326 | /** |
||
| 327 | * @param int $forum_id |
||
| 328 | * @return bool |
||
| 329 | */ |
||
| 330 | private function _attachments_allowed($forum_id) |
||
| 334 | |||
| 335 | /** |
||
| 336 | 6 | * @param int $forum_id |
|
| 337 | 6 | * @return bool |
|
| 338 | 6 | */ |
|
| 339 | 6 | private function _user_can_download_attachments($forum_id) |
|
| 343 | |||
| 344 | /** |
||
| 345 | * @param array $allowed_extensions |
||
| 346 | * @param bool $exclude_in_message |
||
| 347 | * @param string $order_by |
||
| 348 | * @return string |
||
| 349 | */ |
||
| 350 | private function _get_attachment_sql($allowed_extensions, $exclude_in_message, $order_by) |
||
| 359 | |||
| 360 | /** |
||
| 361 | * @return array |
||
| 362 | */ |
||
| 363 | private function _get_user_data() |
||
| 401 | |||
| 402 | /** |
||
| 403 | * @param array $row |
||
| 404 | */ |
||
| 405 | private function _get_user_rank(array $row) |
||
| 431 | |||
| 432 | /** |
||
| 433 | * @param array $row |
||
| 434 | */ |
||
| 435 | private function _get_user_email(array $row) |
||
| 445 | } |
||
| 446 |