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\user */ |
||
| 29 | protected $user; |
||
| 30 | |||
| 31 | /** |
||
| 32 | * Constructor |
||
| 33 | * |
||
| 34 | * @param \phpbb\auth\auth $auth Auth object |
||
| 35 | * @param \phpbb\config\config $config Config object |
||
| 36 | * @param \phpbb\content_visibility $content_visibility Content visibility |
||
| 37 | * @param \phpbb\db\driver\driver_interface $db Database connection |
||
| 38 | * @param \phpbb\user $user User object |
||
| 39 | * @param integer $cache_time Cache results for 3 hours by default |
||
| 40 | */ |
||
| 41 | 29 | public function __construct(\phpbb\auth\auth $auth, \phpbb\config\config $config, \phpbb\content_visibility $content_visibility, \phpbb\db\driver\driver_interface $db, \phpbb\user $user, $cache_time = 10800) |
|
| 51 | |||
| 52 | /** |
||
| 53 | * Get topics count |
||
| 54 | * |
||
| 55 | * @return integer |
||
| 56 | */ |
||
| 57 | 1 | public function get_topics_count() |
|
| 71 | |||
| 72 | /** |
||
| 73 | * Get topic data |
||
| 74 | * |
||
| 75 | * @param mixed|false $limit |
||
| 76 | * @param int $start |
||
| 77 | * @return array |
||
| 78 | */ |
||
| 79 | 12 | public function get_topic_data($limit = false, $start = 0) |
|
| 97 | |||
| 98 | /** |
||
| 99 | * Get post data |
||
| 100 | * |
||
| 101 | * @param mixed|false $topic_first_or_last_post (first|last) |
||
| 102 | * @param array $post_ids |
||
| 103 | * @param bool|false $limit |
||
| 104 | * @param int $start |
||
| 105 | * @param array $sql_array |
||
| 106 | * @return array |
||
| 107 | */ |
||
| 108 | 10 | public function get_post_data($topic_first_or_last_post = false, $post_ids = array(), $limit = false, $start = 0, $sql_array = array()) |
|
| 126 | |||
| 127 | /** |
||
| 128 | * Get attachments... |
||
| 129 | * |
||
| 130 | * @param int $forum_id |
||
| 131 | * @param array $allowed_extensions |
||
| 132 | * @param bool $exclude_in_message |
||
| 133 | * @param string $order_by |
||
| 134 | * @return array |
||
| 135 | */ |
||
| 136 | 7 | public function get_attachments($forum_id = 0, $allowed_extensions = array(), $exclude_in_message = true, $order_by = 'filetime DESC, post_msg_id ASC') |
|
| 156 | |||
| 157 | /** |
||
| 158 | * Get topic tracking info |
||
| 159 | * |
||
| 160 | * @param int $forum_id |
||
| 161 | * @return array |
||
| 162 | */ |
||
| 163 | 5 | public function get_topic_tracking_info($forum_id = 0) |
|
| 174 | |||
| 175 | /** |
||
| 176 | * Returns an array of topic first post or last post ids |
||
| 177 | * |
||
| 178 | * @param string $first_or_last_post |
||
| 179 | * @return array |
||
| 180 | */ |
||
| 181 | 3 | public function get_topic_post_ids($first_or_last_post = 'first') |
|
| 185 | |||
| 186 | /** |
||
| 187 | * @param mixed $topic_first_or_last_post |
||
| 188 | * @param array $post_ids |
||
| 189 | * @param array $sql_array |
||
| 190 | * @return array |
||
| 191 | */ |
||
| 192 | 10 | private function _get_posts_sql_array($topic_first_or_last_post, array $post_ids, array $sql_array) |
|
| 209 | |||
| 210 | /** |
||
| 211 | * @param array $post_ids |
||
| 212 | * @param string $topic_first_or_last_post |
||
| 213 | * @return array |
||
| 214 | */ |
||
| 215 | 10 | private function _get_post_data_where(array $post_ids, $topic_first_or_last_post) |
|
| 232 | |||
| 233 | /** |
||
| 234 | * @param array $sql_where |
||
| 235 | * @param string $topic_first_or_last_post |
||
| 236 | */ |
||
| 237 | 3 | private function _limit_posts_by_topic(array &$sql_where, $topic_first_or_last_post) |
|
| 246 | |||
| 247 | /** |
||
| 248 | * @return array |
||
| 249 | */ |
||
| 250 | 5 | private function _get_tracking_info() |
|
| 264 | |||
| 265 | /** |
||
| 266 | * @param string $function |
||
| 267 | * @return array |
||
| 268 | */ |
||
| 269 | 5 | private function _build_tracking_info($function) |
|
| 279 | |||
| 280 | /** |
||
| 281 | * @return bool |
||
| 282 | */ |
||
| 283 | 5 | private function _can_track_by_lastread() |
|
| 287 | |||
| 288 | /** |
||
| 289 | * @return bool |
||
| 290 | */ |
||
| 291 | 1 | private function _can_track_anonymous() |
|
| 295 | |||
| 296 | /** |
||
| 297 | * @param int $forum_id |
||
| 298 | * @return bool |
||
| 299 | */ |
||
| 300 | 7 | private function _attachments_allowed($forum_id) |
|
| 304 | |||
| 305 | /** |
||
| 306 | * @param int $forum_id |
||
| 307 | * @return bool |
||
| 308 | */ |
||
| 309 | 6 | private function _user_can_download_attachments($forum_id) |
|
| 313 | |||
| 314 | /** |
||
| 315 | * @param array $allowed_extensions |
||
| 316 | * @param bool $exclude_in_message |
||
| 317 | * @param string $order_by |
||
| 318 | * @return string |
||
| 319 | */ |
||
| 320 | 6 | private function _get_attachment_sql($allowed_extensions, $exclude_in_message, $order_by) |
|
| 329 | } |
||
| 330 |