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() |
||
| 73 | { |
||
| 74 | $sql_array = array( |
||
| 75 | 'SELECT' => 'COUNT(*) AS total_topics', |
||
| 76 | 'FROM' => $this->store['sql_array']['FROM'], |
||
| 77 | 'WHERE' => $this->store['sql_array']['WHERE'], |
||
| 78 | ); |
||
| 79 | $sql = $this->db->sql_build_query('SELECT', $sql_array); |
||
| 80 | $result = $this->db->sql_query($sql); |
||
| 81 | $total_topics = $this->db->sql_fetchfield('total_topics'); |
||
| 82 | $this->db->sql_freeresult($result); |
||
| 83 | |||
| 84 | return $total_topics; |
||
| 85 | } |
||
| 86 | |||
| 87 | /** |
||
| 88 | * Get topic data |
||
| 89 | * |
||
| 90 | * @param mixed|false $limit |
||
| 91 | * @param int $start |
||
| 92 | * @return array |
||
| 93 | */ |
||
| 94 | 12 | public function get_topic_data($limit = false, $start = 0) |
|
| 95 | { |
||
| 96 | 12 | $sql = $this->db->sql_build_query('SELECT', $this->store['sql_array']); |
|
| 97 | 12 | $result = $this->db->sql_query_limit($sql, $limit, $start, $this->cache_time); |
|
| 98 | |||
| 99 | 12 | while ($row = $this->db->sql_fetchrow($result)) |
|
| 100 | { |
||
| 101 | 10 | $this->store['topic'][$row['topic_id']] = $row; |
|
| 102 | |||
| 103 | 10 | $this->store['tracking'][$row['forum_id']]['topic_list'][] = $row['topic_id']; |
|
| 104 | 10 | $this->store['tracking'][$row['forum_id']]['mark_time'] =& $row['forum_mark_time']; |
|
| 105 | 10 | $this->store['post_ids']['first'][] = $row['topic_first_post_id']; |
|
| 106 | 10 | $this->store['post_ids']['last'][] = $row['topic_last_post_id']; |
|
| 107 | 10 | } |
|
| 108 | 12 | $this->db->sql_freeresult($result); |
|
| 109 | |||
| 110 | 12 | return $this->store['topic']; |
|
| 111 | } |
||
| 112 | |||
| 113 | /** |
||
| 114 | * Get post data |
||
| 115 | * |
||
| 116 | * @param mixed|false $topic_first_or_last_post (first|last) |
||
| 117 | * @param array $post_ids |
||
| 118 | * @param bool|false $limit |
||
| 119 | * @param int $start |
||
| 120 | * @param array $sql_array |
||
| 121 | * @return array |
||
| 122 | */ |
||
| 123 | 10 | public function get_post_data($topic_first_or_last_post = false, $post_ids = array(), $limit = false, $start = 0, $sql_array = array()) |
|
| 124 | { |
||
| 125 | 10 | $sql = $this->db->sql_build_query('SELECT_DISTINCT', $this->_get_posts_sql_array($topic_first_or_last_post, $post_ids, $sql_array)); |
|
| 126 | 10 | $result = $this->db->sql_query_limit($sql, $limit, $start, $this->cache_time); |
|
| 127 | |||
| 128 | 10 | $post_data = array(); |
|
| 129 | 10 | while ($row = $this->db->sql_fetchrow($result)) |
|
| 130 | { |
||
| 131 | 9 | $post_data[$row['topic_id']][$row['post_id']] = $row; |
|
| 132 | 9 | $this->store['poster_ids'][] = $row['poster_id']; |
|
| 133 | 9 | $this->store['poster_ids'][] = $row['post_edit_user']; |
|
| 134 | 9 | $this->store['poster_ids'][] = $row['post_delete_user']; |
|
| 135 | 9 | $this->store['attachments'][] = $row['post_id']; |
|
| 136 | 9 | } |
|
| 137 | 10 | $this->db->sql_freeresult($result); |
|
| 138 | |||
| 139 | 10 | return $post_data; |
|
| 140 | } |
||
| 141 | |||
| 142 | /** |
||
| 143 | * Get attachments... |
||
| 144 | * |
||
| 145 | * @param int $forum_id |
||
| 146 | * @param array $allowed_extensions |
||
| 147 | * @param bool $exclude_in_message |
||
| 148 | * @param string $order_by |
||
| 149 | * @return array |
||
| 150 | */ |
||
| 151 | 7 | public function get_attachments($forum_id = 0, $allowed_extensions = array(), $exclude_in_message = true, $order_by = 'filetime DESC, post_msg_id ASC') |
|
| 152 | { |
||
| 153 | 7 | $this->store['attachments'] = array_filter($this->store['attachments']); |
|
| 154 | |||
| 155 | 7 | $attachments = array(); |
|
| 156 | 7 | if ($this->_attachments_allowed($forum_id)) |
|
| 157 | 7 | { |
|
| 158 | 6 | $sql = $this->_get_attachment_sql($allowed_extensions, $exclude_in_message, $order_by); |
|
| 159 | 6 | $result = $this->db->sql_query($sql); |
|
| 160 | |||
| 161 | 6 | while ($row = $this->db->sql_fetchrow($result)) |
|
| 162 | { |
||
| 163 | 6 | $attachments[$row['post_msg_id']][] = $row; |
|
| 164 | 6 | } |
|
| 165 | 6 | $this->db->sql_freeresult($result); |
|
| 166 | 6 | } |
|
| 167 | 7 | $this->store['attachments'] = array(); |
|
| 168 | |||
| 169 | 7 | return $attachments; |
|
| 170 | } |
||
| 171 | |||
| 172 | /** |
||
| 173 | * Get topic tracking info |
||
| 174 | * |
||
| 175 | * @param int $forum_id |
||
| 176 | * @return array |
||
| 177 | */ |
||
| 178 | 5 | public function get_topic_tracking_info($forum_id = 0) |
|
| 179 | { |
||
| 180 | 5 | if (!sizeof($this->store['tracking'])) |
|
| 181 | 5 | { |
|
| 182 | return array(); |
||
| 183 | } |
||
| 184 | |||
| 185 | 5 | $tracking_info = $this->_get_tracking_info(); |
|
| 186 | |||
| 187 | 5 | return ($forum_id) ? (isset($tracking_info[$forum_id]) ? $tracking_info[$forum_id] : array()) : $tracking_info; |
|
| 188 | } |
||
| 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 | 3 | public function get_topic_post_ids($first_or_last_post = 'first') |
|
| 197 | { |
||
| 198 | 3 | return (isset($this->store['post_ids'][$first_or_last_post])) ? $this->store['post_ids'][$first_or_last_post] : array(); |
|
| 199 | } |
||
| 200 | |||
| 201 | /** |
||
| 202 | * Returns an array of topic first post or last post ids |
||
| 203 | */ |
||
| 204 | public function get_posters_info() |
||
| 205 | { |
||
| 206 | $this->store['poster_ids'] = array_filter(array_unique($this->store['poster_ids'])); |
||
| 207 | |||
| 208 | if (!sizeof($this->store['poster_ids'])) |
||
| 209 | { |
||
| 210 | return array(); |
||
| 211 | } |
||
| 212 | |||
| 213 | return $this->_get_user_data(); |
||
| 214 | } |
||
| 215 | |||
| 216 | /** |
||
| 217 | * @param mixed $topic_first_or_last_post |
||
| 218 | * @param array $post_ids |
||
| 219 | * @param array $sql_array |
||
| 220 | * @return array |
||
| 221 | */ |
||
| 222 | 10 | private function _get_posts_sql_array($topic_first_or_last_post, array $post_ids, array $sql_array) |
|
| 223 | { |
||
| 224 | 10 | $sql_array = array_merge_recursive( |
|
| 225 | array( |
||
| 226 | 10 | 'SELECT' => array('p.*'), |
|
| 227 | 10 | 'FROM' => array(POSTS_TABLE => 'p'), |
|
| 228 | 10 | 'WHERE' => $this->_get_post_data_where($post_ids, $topic_first_or_last_post), |
|
| 229 | 10 | 'ORDER_BY' => 'p.topic_id, p.post_time ASC', |
|
| 230 | 10 | ), |
|
| 231 | $sql_array |
||
| 232 | 10 | ); |
|
| 233 | |||
| 234 | 10 | $sql_array['SELECT'] = join(', ', array_filter($sql_array['SELECT'])); |
|
| 235 | 10 | $sql_array['WHERE'] = join(' AND ', array_filter($sql_array['WHERE'])); |
|
| 236 | |||
| 237 | 10 | return $sql_array; |
|
| 238 | } |
||
| 239 | |||
| 240 | /** |
||
| 241 | * @param array $post_ids |
||
| 242 | * @param string $topic_first_or_last_post |
||
| 243 | * @return array |
||
| 244 | */ |
||
| 245 | 10 | private function _get_post_data_where(array $post_ids, $topic_first_or_last_post) |
|
| 246 | { |
||
| 247 | 10 | $sql_where = array(); |
|
| 248 | |||
| 249 | 10 | if (sizeof($post_ids)) |
|
| 250 | 10 | { |
|
| 251 | 2 | $sql_where[] = $this->db->sql_in_set('p.post_id', $post_ids); |
|
| 252 | 2 | } |
|
| 253 | 8 | else if (sizeof($this->store['topic'])) |
|
| 254 | 8 | { |
|
| 255 | 3 | $this->_limit_posts_by_topic($sql_where, $topic_first_or_last_post); |
|
| 256 | 3 | } |
|
| 257 | |||
| 258 | 10 | $sql_where[] = $this->content_visibility->get_global_visibility_sql('post', $this->ex_fid_ary, 'p.'); |
|
| 259 | |||
| 260 | 10 | return $sql_where; |
|
| 261 | } |
||
| 262 | |||
| 263 | /** |
||
| 264 | * @param array $sql_where |
||
| 265 | * @param string $topic_first_or_last_post |
||
| 266 | */ |
||
| 267 | 3 | private function _limit_posts_by_topic(array &$sql_where, $topic_first_or_last_post) |
|
| 268 | { |
||
| 269 | 3 | $sql_where[] = $this->db->sql_in_set('p.topic_id', array_keys($this->store['topic'])); |
|
| 270 | |||
| 271 | if ($topic_first_or_last_post) |
||
| 272 | 3 | { |
|
| 273 | 3 | $sql_where[] = $this->db->sql_in_set('p.post_id', $this->get_topic_post_ids($topic_first_or_last_post)); |
|
| 274 | 3 | } |
|
| 275 | 3 | } |
|
| 276 | |||
| 277 | /** |
||
| 278 | * @return array |
||
| 279 | */ |
||
| 280 | 5 | private function _get_tracking_info() |
|
| 281 | { |
||
| 282 | 5 | $info = array(); |
|
| 283 | 5 | if ($this->_can_track_by_lastread()) |
|
| 284 | 5 | { |
|
| 285 | 4 | $info = $this->_build_tracking_info('get_topic_tracking'); |
|
| 286 | 4 | } |
|
| 287 | 1 | else if ($this->_can_track_anonymous()) |
|
| 288 | 1 | { |
|
| 289 | 1 | $info = $this->_build_tracking_info('get_complete_topic_tracking'); |
|
| 290 | 1 | } |
|
| 291 | |||
| 292 | 5 | return $info; |
|
| 293 | } |
||
| 294 | |||
| 295 | /** |
||
| 296 | * @param string $function |
||
| 297 | * @return array |
||
| 298 | */ |
||
| 299 | 5 | private function _build_tracking_info($function) |
|
| 300 | { |
||
| 301 | 5 | $tracking_info = array(); |
|
| 302 | 5 | foreach ($this->store['tracking'] as $fid => $forum) |
|
| 303 | { |
||
| 304 | 5 | $tracking_info[$fid] = call_user_func_array($function, array($fid, $forum['topic_list'], &$this->store['topic'], array($fid => $forum['mark_time']))); |
|
| 305 | 5 | } |
|
| 306 | |||
| 307 | 5 | return $tracking_info; |
|
| 308 | } |
||
| 309 | |||
| 310 | /** |
||
| 311 | * @return bool |
||
| 312 | */ |
||
| 313 | 5 | private function _can_track_by_lastread() |
|
| 317 | |||
| 318 | /** |
||
| 319 | * @return bool |
||
| 320 | */ |
||
| 321 | 1 | private function _can_track_anonymous() |
|
| 325 | |||
| 326 | /** |
||
| 327 | * @param int $forum_id |
||
| 328 | * @return bool |
||
| 329 | */ |
||
| 330 | 7 | private function _attachments_allowed($forum_id) |
|
| 331 | { |
||
| 332 | 7 | return ($this->store['attachments'] && $this->_user_can_download_attachments($forum_id)) ? true : false; |
|
| 333 | } |
||
| 334 | |||
| 335 | /** |
||
| 336 | * @param int $forum_id |
||
| 337 | * @return bool |
||
| 338 | */ |
||
| 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 | 6 | private function _get_attachment_sql($allowed_extensions, $exclude_in_message, $order_by) |
|
| 351 | { |
||
| 352 | return 'SELECT * |
||
| 353 | 6 | FROM ' . ATTACHMENTS_TABLE . ' |
|
| 354 | 6 | WHERE ' . $this->db->sql_in_set('post_msg_id', $this->store['attachments']) . |
|
| 355 | 6 | (($exclude_in_message) ? ' AND in_message = 0' : '') . |
|
| 356 | 6 | (sizeof($allowed_extensions) ? ' AND ' . $this->db->sql_in_set('extension', $allowed_extensions) : '') . ' |
|
| 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 |