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 |
||
| 12 | class data extends query_builder |
||
| 13 | { |
||
| 14 | /** @var \phpbb\auth\auth */ |
||
| 15 | protected $auth; |
||
| 16 | |||
| 17 | /** @var \phpbb\config\config */ |
||
| 18 | protected $config; |
||
| 19 | |||
| 20 | /** @var \phpbb\content_visibility */ |
||
| 21 | protected $content_visibility; |
||
| 22 | |||
| 23 | /** @var \phpbb\db\driver\driver_interface */ |
||
| 24 | protected $db; |
||
| 25 | |||
| 26 | /** @var \phpbb\user */ |
||
| 27 | protected $user; |
||
| 28 | |||
| 29 | /** |
||
| 30 | * Constructor |
||
| 31 | * |
||
| 32 | * @param \phpbb\auth\auth $auth Auth object |
||
| 33 | * @param \phpbb\config\config $config Config object |
||
| 34 | * @param \phpbb\content_visibility $content_visibility Content visibility |
||
| 35 | * @param \phpbb\db\driver\driver_interface $db Database connection |
||
| 36 | * @param \phpbb\user $user User object |
||
| 37 | * @param integer $cache_time Cache results for 3 hours by default |
||
| 38 | */ |
||
| 39 | 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) |
|
| 49 | |||
| 50 | /** |
||
| 51 | * Get topics count |
||
| 52 | * |
||
| 53 | * @return integer |
||
| 54 | */ |
||
| 55 | 1 | public function get_topics_count() |
|
| 69 | |||
| 70 | /** |
||
| 71 | * Get topic data |
||
| 72 | * |
||
| 73 | * @param mixed|false $limit |
||
| 74 | * @param int $start |
||
| 75 | * @return array |
||
| 76 | */ |
||
| 77 | 12 | public function get_topic_data($limit = false, $start = 0) |
|
| 95 | |||
| 96 | /** |
||
| 97 | * Get post data |
||
| 98 | * |
||
| 99 | * @param mixed|false $topic_first_or_last_post (first|last) |
||
| 100 | * @param array $post_ids |
||
| 101 | * @param bool|false $limit |
||
| 102 | * @param int $start |
||
| 103 | * @param array $sql_array |
||
| 104 | * @return array |
||
| 105 | */ |
||
| 106 | 10 | public function get_post_data($topic_first_or_last_post = false, $post_ids = array(), $limit = false, $start = 0, $sql_array = array()) |
|
| 124 | |||
| 125 | /** |
||
| 126 | * Get attachments... |
||
| 127 | * |
||
| 128 | * @param int $forum_id |
||
| 129 | * @param array $allowed_extensions |
||
| 130 | * @param mixed|bool $limit |
||
| 131 | * @param bool $exclude_in_message |
||
| 132 | * @param string $order_by |
||
| 133 | * @return array |
||
| 134 | */ |
||
| 135 | 7 | public function get_attachments($forum_id = 0, $allowed_extensions = array(), $limit = false, $exclude_in_message = true, $order_by = 'filetime DESC') |
|
| 155 | |||
| 156 | /** |
||
| 157 | * Get topic tracking info |
||
| 158 | * |
||
| 159 | * @param int $forum_id |
||
| 160 | * @return array |
||
| 161 | */ |
||
| 162 | 5 | public function get_topic_tracking_info($forum_id = 0) |
|
| 173 | |||
| 174 | /** |
||
| 175 | * Returns an array of topic first post or last post ids |
||
| 176 | * |
||
| 177 | * @param string $first_or_last_post |
||
| 178 | * @return array |
||
| 179 | */ |
||
| 180 | 3 | public function get_topic_post_ids($first_or_last_post = 'first') |
|
| 184 | |||
| 185 | /** |
||
| 186 | * @param mixed $topic_first_or_last_post |
||
| 187 | * @param array $post_ids |
||
| 188 | * @param array $sql_array |
||
| 189 | * @return array |
||
| 190 | */ |
||
| 191 | 10 | private function _get_posts_sql_array($topic_first_or_last_post, array $post_ids, array $sql_array) |
|
| 208 | |||
| 209 | /** |
||
| 210 | * @param array $post_ids |
||
| 211 | * @param string $topic_first_or_last_post |
||
| 212 | * @return array |
||
| 213 | */ |
||
| 214 | 10 | private function _get_post_data_where(array $post_ids, $topic_first_or_last_post) |
|
| 231 | |||
| 232 | /** |
||
| 233 | * @param array $sql_where |
||
| 234 | * @param string $topic_first_or_last_post |
||
| 235 | */ |
||
| 236 | 3 | private function _limit_posts_by_topic(array &$sql_where, $topic_first_or_last_post) |
|
| 245 | |||
| 246 | /** |
||
| 247 | * @return array |
||
| 248 | */ |
||
| 249 | 5 | private function _get_tracking_info() |
|
| 263 | |||
| 264 | /** |
||
| 265 | * @param string $function |
||
| 266 | * @return array |
||
| 267 | */ |
||
| 268 | 5 | private function _build_tracking_info($function) |
|
| 278 | |||
| 279 | /** |
||
| 280 | * @return bool |
||
| 281 | */ |
||
| 282 | 5 | private function _can_track_by_lastread() |
|
| 286 | |||
| 287 | /** |
||
| 288 | * @return bool |
||
| 289 | */ |
||
| 290 | 1 | private function _can_track_anonymous() |
|
| 294 | |||
| 295 | /** |
||
| 296 | * @param int $forum_id |
||
| 297 | * @return bool |
||
| 298 | */ |
||
| 299 | 7 | private function _attachments_allowed($forum_id) |
|
| 303 | |||
| 304 | /** |
||
| 305 | * @param int $forum_id |
||
| 306 | * @return bool |
||
| 307 | */ |
||
| 308 | 6 | private function _user_can_download_attachments($forum_id) |
|
| 312 | |||
| 313 | /** |
||
| 314 | * @param array $allowed_extensions |
||
| 315 | * @param bool $exclude_in_message |
||
| 316 | * @param string $order_by |
||
| 317 | * @return string |
||
| 318 | */ |
||
| 319 | 6 | private function _get_attachment_sql($allowed_extensions, $exclude_in_message, $order_by) |
|
| 328 | } |
||
| 329 |