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 | 26 | 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 topic data |
||
| 54 | * |
||
| 55 | * @param mixed|false $limit |
||
| 56 | * @param int $start |
||
| 57 | * @return array |
||
| 58 | */ |
||
| 59 | 12 | public function get_topic_data($limit = false, $start = 0) |
|
| 77 | |||
| 78 | /** |
||
| 79 | * Get post data |
||
| 80 | * |
||
| 81 | * @param mixed|false $topic_first_or_last_post (first|last) |
||
| 82 | * @param array $post_ids |
||
| 83 | * @param bool|false $limit |
||
| 84 | * @param int $start |
||
| 85 | * @param array $sql_array |
||
| 86 | * @return array |
||
| 87 | */ |
||
| 88 | 10 | public function get_post_data($topic_first_or_last_post = false, $post_ids = array(), $limit = false, $start = 0, $sql_array = array()) |
|
| 106 | |||
| 107 | /** |
||
| 108 | * Get attachments... |
||
| 109 | * |
||
| 110 | * @param int $forum_id |
||
| 111 | * @param array $allowed_extensions |
||
| 112 | * @param bool $exclude_in_message |
||
| 113 | * @param string $order_by |
||
| 114 | * @return array |
||
| 115 | */ |
||
| 116 | 7 | public function get_attachments($forum_id = 0, $allowed_extensions = array(), $exclude_in_message = true, $order_by = 'filetime DESC, post_msg_id ASC') |
|
| 136 | |||
| 137 | /** |
||
| 138 | * Get topic tracking info |
||
| 139 | * |
||
| 140 | * @param int $forum_id |
||
| 141 | * @return array |
||
| 142 | */ |
||
| 143 | 5 | public function get_topic_tracking_info($forum_id = 0) |
|
| 154 | |||
| 155 | /** |
||
| 156 | * Returns an array of topic first post or last post ids |
||
| 157 | * |
||
| 158 | * @param string $first_or_last_post |
||
| 159 | * @return array |
||
| 160 | */ |
||
| 161 | 3 | public function get_topic_post_ids($first_or_last_post = 'first') |
|
| 165 | |||
| 166 | /** |
||
| 167 | * @param mixed $topic_first_or_last_post |
||
| 168 | * @param array $post_ids |
||
| 169 | * @param array $sql_array |
||
| 170 | * @return array |
||
| 171 | */ |
||
| 172 | 10 | private function _get_posts_sql_array($topic_first_or_last_post, array $post_ids, array $sql_array) |
|
| 189 | |||
| 190 | /** |
||
| 191 | * @param array $post_ids |
||
| 192 | * @param string $topic_first_or_last_post |
||
| 193 | * @return array |
||
| 194 | */ |
||
| 195 | 10 | private function _get_post_data_where(array $post_ids, $topic_first_or_last_post) |
|
| 212 | |||
| 213 | /** |
||
| 214 | * @param array $sql_where |
||
| 215 | * @param string $topic_first_or_last_post |
||
| 216 | */ |
||
| 217 | 3 | private function _limit_posts_by_topic(array &$sql_where, $topic_first_or_last_post) |
|
| 226 | |||
| 227 | /** |
||
| 228 | * @return array |
||
| 229 | */ |
||
| 230 | 5 | private function _get_tracking_info() |
|
| 244 | |||
| 245 | /** |
||
| 246 | * @param string $function |
||
| 247 | * @return array |
||
| 248 | */ |
||
| 249 | 5 | private function _build_tracking_info($function) |
|
| 259 | |||
| 260 | /** |
||
| 261 | * @return bool |
||
| 262 | */ |
||
| 263 | 5 | private function _can_track_by_lastread() |
|
| 267 | |||
| 268 | /** |
||
| 269 | * @return bool |
||
| 270 | */ |
||
| 271 | 1 | private function _can_track_anonymous() |
|
| 275 | |||
| 276 | /** |
||
| 277 | * @param int $forum_id |
||
| 278 | * @return bool |
||
| 279 | */ |
||
| 280 | 7 | private function _attachments_allowed($forum_id) |
|
| 284 | |||
| 285 | /** |
||
| 286 | * @param int $forum_id |
||
| 287 | * @return bool |
||
| 288 | */ |
||
| 289 | 6 | private function _user_can_download_attachments($forum_id) |
|
| 293 | |||
| 294 | /** |
||
| 295 | * @param array $allowed_extensions |
||
| 296 | * @param bool $exclude_in_message |
||
| 297 | * @param string $order_by |
||
| 298 | * @return string |
||
| 299 | */ |
||
| 300 | 6 | private function _get_attachment_sql($allowed_extensions, $exclude_in_message, $order_by) |
|
| 309 | } |
||
| 310 |