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 |
||
| 18 | class data |
||
| 19 | { |
||
| 20 | /** @var auth */ |
||
| 21 | protected $auth; |
||
| 22 | |||
| 23 | /** @var config */ |
||
| 24 | protected $config; |
||
| 25 | |||
| 26 | /** @var content_visibility */ |
||
| 27 | protected $content_visibility; |
||
| 28 | |||
| 29 | /** @var driver_interface */ |
||
| 30 | protected $db; |
||
| 31 | |||
| 32 | /** @var user */ |
||
| 33 | protected $user; |
||
| 34 | |||
| 35 | /** @var string */ |
||
| 36 | protected $phpbb_root_path; |
||
| 37 | |||
| 38 | /** @var string */ |
||
| 39 | protected $php_ext; |
||
| 40 | |||
| 41 | /** @var array */ |
||
| 42 | protected $store; |
||
| 43 | |||
| 44 | /** @var array */ |
||
| 45 | protected $ex_fid_ary; |
||
| 46 | |||
| 47 | /** @var integer */ |
||
| 48 | protected $cache_time; |
||
| 49 | |||
| 50 | /** |
||
| 51 | * Constructor |
||
| 52 | * |
||
| 53 | * @param auth $auth Auth object |
||
| 54 | * @param config $config Config object |
||
| 55 | * @param content_visibility $content_visibility Content visibility |
||
| 56 | * @param driver_interface $db Database connection |
||
| 57 | * @param user $user User object |
||
| 58 | * @param string $phpbb_root_path Path to the phpbb includes directory. |
||
| 59 | * @param string $php_ext php file extension |
||
| 60 | * @param integer $cache_time Cache results for 3 hours by default |
||
| 61 | */ |
||
| 62 | 15 | public function __construct(auth $auth, config $config, content_visibility $content_visibility, driver_interface $db, user $user, $phpbb_root_path, $php_ext, $cache_time = 10800) |
|
| 75 | |||
| 76 | /** |
||
| 77 | * Begin query |
||
| 78 | * |
||
| 79 | * @return $this |
||
| 80 | */ |
||
| 81 | 9 | public function query() |
|
| 100 | |||
| 101 | /** |
||
| 102 | * Fetch Forum by id(s) |
||
| 103 | * |
||
| 104 | * @param $forum_id |
||
| 105 | * @return $this |
||
| 106 | */ |
||
| 107 | 6 | public function fetch_forum($forum_id) |
|
| 113 | |||
| 114 | /** |
||
| 115 | * Fetch Topic by id(s) |
||
| 116 | * |
||
| 117 | * @param mixed $topic_id Limit by topic id: single id or array of topic ids |
||
| 118 | * @return $this |
||
| 119 | */ |
||
| 120 | public function fetch_topic($topic_id) |
||
| 126 | |||
| 127 | /** |
||
| 128 | * Fetch Topic by Poster id(s) |
||
| 129 | * |
||
| 130 | * @param mixed $user_id User id of topic poster: single id or array of user ids |
||
| 131 | * @return $this |
||
| 132 | */ |
||
| 133 | public function fetch_topic_poster($user_id) |
||
| 139 | |||
| 140 | /** |
||
| 141 | * Fetch Post by id(s) |
||
| 142 | * |
||
| 143 | * @param mixed $post_id Limit by post id: single id or array of post ids |
||
| 144 | * @return $this |
||
| 145 | */ |
||
| 146 | public function fetch_post($post_id) |
||
| 154 | |||
| 155 | /** |
||
| 156 | * Fetch by Topic Type |
||
| 157 | * |
||
| 158 | * @param array $topic_type |
||
| 159 | * @return $this |
||
| 160 | */ |
||
| 161 | 6 | public function fetch_topic_type(array $topic_type) |
|
| 175 | |||
| 176 | /** |
||
| 177 | * Fetch Topic Watch info |
||
| 178 | * |
||
| 179 | * @return $this |
||
| 180 | */ |
||
| 181 | public function fetch_watch_status() |
||
| 200 | |||
| 201 | /** |
||
| 202 | * Fetch Topic Bookmark Info |
||
| 203 | * |
||
| 204 | * @return $this |
||
| 205 | */ |
||
| 206 | public function fetch_bookmark_status() |
||
| 219 | |||
| 220 | /** |
||
| 221 | * Fetch Topic Tracking Info |
||
| 222 | * |
||
| 223 | * @param bool $track |
||
| 224 | * @return $this |
||
| 225 | */ |
||
| 226 | 4 | public function fetch_tracking_info($track = true) |
|
| 246 | |||
| 247 | /** |
||
| 248 | * Fetch by Date Range |
||
| 249 | * |
||
| 250 | * @param int $start Unix start time |
||
| 251 | * @param int $stop Unix stop time |
||
| 252 | * @param string $mode |
||
| 253 | * @return $this |
||
| 254 | */ |
||
| 255 | 4 | public function fetch_date_range($start, $stop, $mode = 'topic') |
|
| 264 | |||
| 265 | /** |
||
| 266 | * Fetch by Custom Query |
||
| 267 | * |
||
| 268 | * @param array $sql_array Array of elements to merge into query |
||
| 269 | * array( |
||
| 270 | * 'SELECT' => array('p.*'), |
||
| 271 | * 'WHERE' => array('p.post_id = 2'), |
||
| 272 | * ) |
||
| 273 | * @return $this |
||
| 274 | */ |
||
| 275 | 5 | public function fetch_custom(array $sql_array) |
|
| 281 | |||
| 282 | /** |
||
| 283 | * Set Sorting Order |
||
| 284 | * |
||
| 285 | * @param string $sort_key The sorting key e.g. t.topic_time |
||
| 286 | * @param string $sort_dir Sort direction: ASC/DESC |
||
| 287 | * @return $this |
||
| 288 | */ |
||
| 289 | 9 | public function set_sorting($sort_key, $sort_dir = 'DESC') |
|
| 295 | |||
| 296 | /** |
||
| 297 | * Build the query |
||
| 298 | * |
||
| 299 | * @param bool|true $check_visibility Should we only return data from forums the user is allowed to see? |
||
| 300 | * @param bool|true $enable_caching Should the query be cached where possible? |
||
| 301 | * @return $this |
||
| 302 | */ |
||
| 303 | 9 | public function build($check_visibility = true, $enable_caching = true) |
|
| 316 | |||
| 317 | /** |
||
| 318 | * Get the query array |
||
| 319 | * |
||
| 320 | * @return array The sql array that can be used with sql_build_query |
||
| 321 | */ |
||
| 322 | public function get_sql_array() |
||
| 326 | |||
| 327 | /** |
||
| 328 | * Get topic data |
||
| 329 | * |
||
| 330 | * @param mixed|false $limit |
||
| 331 | * @param int $start |
||
| 332 | * @return array |
||
| 333 | */ |
||
| 334 | 9 | public function get_topic_data($limit = false, $start = 0) |
|
| 352 | |||
| 353 | /** |
||
| 354 | * Get post data |
||
| 355 | * |
||
| 356 | * @param string $topic_first_or_last_post |
||
| 357 | * @param array $post_ids |
||
| 358 | * @param bool|false $limit |
||
| 359 | * @param int $start |
||
| 360 | * @param array $sql_array |
||
| 361 | * @return array |
||
| 362 | */ |
||
| 363 | 3 | public function get_post_data($topic_first_or_last_post = '', $post_ids = array(), $limit = false, $start = 0, $sql_array = array()) |
|
| 397 | |||
| 398 | /** |
||
| 399 | * Get attachments... |
||
| 400 | * |
||
| 401 | * @param int $forum_id |
||
| 402 | * @return array |
||
| 403 | */ |
||
| 404 | public function get_attachments($forum_id) |
||
| 428 | |||
| 429 | /** |
||
| 430 | * Get topic tracking info |
||
| 431 | * |
||
| 432 | * @param int $forum_id |
||
| 433 | * @return array |
||
| 434 | */ |
||
| 435 | 4 | public function get_topic_tracking_info($forum_id = 0) |
|
| 446 | |||
| 447 | /** |
||
| 448 | * Returns an array of topic first post or last post ids |
||
| 449 | * |
||
| 450 | * @return array |
||
| 451 | */ |
||
| 452 | public function get_posters_info() |
||
| 470 | |||
| 471 | /** |
||
| 472 | * Returns an array of topic first post or last post ids |
||
| 473 | * |
||
| 474 | * @param string $first_or_last_post |
||
| 475 | * @return array |
||
| 476 | */ |
||
| 477 | 3 | public function get_topic_post_ids($first_or_last_post = 'first') |
|
| 481 | |||
| 482 | /** |
||
| 483 | * Reset data |
||
| 484 | */ |
||
| 485 | 9 | private function _reset() |
|
| 496 | |||
| 497 | /** |
||
| 498 | * @param int $column_id |
||
| 499 | * @param string $column |
||
| 500 | */ |
||
| 501 | 6 | private function _fetch($column_id, $column) |
|
| 508 | |||
| 509 | /** |
||
| 510 | * @param bool $enable_caching |
||
| 511 | */ |
||
| 512 | 9 | protected function _set_cache_time($enable_caching) |
|
| 519 | |||
| 520 | /** |
||
| 521 | * @param bool $check_visibility |
||
| 522 | */ |
||
| 523 | 9 | private function _set_topic_visibility($check_visibility) |
|
| 531 | |||
| 532 | /** |
||
| 533 | * @param array $post_ids |
||
| 534 | * @param string $topic_first_or_last_post |
||
| 535 | * @return array |
||
| 536 | */ |
||
| 537 | 3 | private function _get_post_data_where(array $post_ids, $topic_first_or_last_post) |
|
| 555 | |||
| 556 | /** |
||
| 557 | * @return array |
||
| 558 | */ |
||
| 559 | 4 | private function _get_tracking_info() |
|
| 573 | |||
| 574 | /** |
||
| 575 | * @param string $function |
||
| 576 | * @return array |
||
| 577 | */ |
||
| 578 | 4 | private function _build_tracking_info($function) |
|
| 588 | |||
| 589 | /** |
||
| 590 | * @return bool |
||
| 591 | */ |
||
| 592 | 4 | private function _can_track_by_lastread() |
|
| 596 | |||
| 597 | /** |
||
| 598 | * @return bool |
||
| 599 | */ |
||
| 600 | private function _can_track_anonymous() |
||
| 604 | |||
| 605 | /** |
||
| 606 | * @param int $forum_id |
||
| 607 | * @return bool |
||
| 608 | */ |
||
| 609 | private function _attachments_allowed($forum_id) |
||
| 613 | } |
||
| 614 |