Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
Complex classes like fetch_posts 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 fetch_posts, and based on these observations, apply Extract Interface, too.
| 1 | <?php  | 
            ||
| 12 | class fetch_posts  | 
            ||
| 13 | { | 
            ||
| 14 | /**  | 
            ||
| 15 | * phpBB auth  | 
            ||
| 16 | * @var \phpbb\auth\auth  | 
            ||
| 17 | */  | 
            ||
| 18 | protected $auth;  | 
            ||
| 19 | |||
| 20 | /**  | 
            ||
| 21 | * phpBB cache  | 
            ||
| 22 | * @var \phpbb\cache\driver\driver_interface  | 
            ||
| 23 | */  | 
            ||
| 24 | protected $cache;  | 
            ||
| 25 | |||
| 26 | /**  | 
            ||
| 27 | * phpBB config  | 
            ||
| 28 | * @var \phpbb\config\config  | 
            ||
| 29 | */  | 
            ||
| 30 | protected $config;  | 
            ||
| 31 | |||
| 32 | /**  | 
            ||
| 33 | * phpBB db driver  | 
            ||
| 34 | * @var \phpbb\db\driver\driver_interface  | 
            ||
| 35 | */  | 
            ||
| 36 | protected $db;  | 
            ||
| 37 | |||
| 38 | /**  | 
            ||
| 39 | * Modules helper  | 
            ||
| 40 | * @var \board3\portal\includes\modules_helper  | 
            ||
| 41 | */  | 
            ||
| 42 | protected $modules_helper;  | 
            ||
| 43 | |||
| 44 | /**  | 
            ||
| 45 | * phpBB user  | 
            ||
| 46 | * @var \phpbb\user  | 
            ||
| 47 | */  | 
            ||
| 48 | protected $user;  | 
            ||
| 49 | |||
| 50 | /**  | 
            ||
| 51 | * SQL Query where constraint  | 
            ||
| 52 | * @var string  | 
            ||
| 53 | */  | 
            ||
| 54 | protected $where_string = '';  | 
            ||
| 55 | |||
| 56 | /**  | 
            ||
| 57 | * SQL topic type constraint  | 
            ||
| 58 | * @var string  | 
            ||
| 59 | */  | 
            ||
| 60 | protected $topic_type = '';  | 
            ||
| 61 | |||
| 62 | /**  | 
            ||
| 63 | * SQL user link constraint  | 
            ||
| 64 | * @var string  | 
            ||
| 65 | */  | 
            ||
| 66 | protected $user_link = '';  | 
            ||
| 67 | |||
| 68 | /**  | 
            ||
| 69 | * SQL post link constraint  | 
            ||
| 70 | * @var string  | 
            ||
| 71 | */  | 
            ||
| 72 | protected $post_link = '';  | 
            ||
| 73 | |||
| 74 | /**  | 
            ||
| 75 | * SQL topic order constraint  | 
            ||
| 76 | * @var string  | 
            ||
| 77 | */  | 
            ||
| 78 | protected $topic_order = '';  | 
            ||
| 79 | |||
| 80 | /**  | 
            ||
| 81 | * Module ID  | 
            ||
| 82 | * @var int  | 
            ||
| 83 | */  | 
            ||
| 84 | protected $module_id;  | 
            ||
| 85 | |||
| 86 | /**  | 
            ||
| 87 | * Forum ID to use for global topics  | 
            ||
| 88 | * @var int  | 
            ||
| 89 | */  | 
            ||
| 90 | protected $global_id;  | 
            ||
| 91 | |||
| 92 | /**  | 
            ||
| 93 | * Type of posts to fetch  | 
            ||
| 94 | * @var string  | 
            ||
| 95 | */  | 
            ||
| 96 | protected $type;  | 
            ||
| 97 | |||
| 98 | /**  | 
            ||
| 99 | * Constructor  | 
            ||
| 100 | * NOTE: The parameters of this method must match in order and type with  | 
            ||
| 101 | * the dependencies defined in the services.yml file for this service.  | 
            ||
| 102 | * @param \phpbb\auth\auth $auth phpBB auth object  | 
            ||
| 103 | * @param \phpbb\cache\driver $cache phpBB cache driver  | 
            ||
| 104 | * @param \phpbb\config\config $config phpBB config  | 
            ||
| 105 | * @param \phpbb\db\driver_interface $db phpBB database driver  | 
            ||
| 106 | * @param \board3\portal\includes\modules_helper $modules_helper Board3 modules helper  | 
            ||
| 107 | * @param \phpbb\user $user phpBB user object  | 
            ||
| 108 | */  | 
            ||
| 109 | 30 | View Code Duplication | public function __construct($auth, $cache, $config, $db, $modules_helper, $user)  | 
            
| 118 | |||
| 119 | /**  | 
            ||
| 120 | * Get posts defined by type and other settings  | 
            ||
| 121 | *  | 
            ||
| 122 | * @param string $forum_from Forums from which the posts should be retrieved  | 
            ||
| 123 | * @param bool $permissions Whether permissions should be taken into account  | 
            ||
| 124 | * during retrieval  | 
            ||
| 125 | * @param int $number_of_posts Number of posts to get  | 
            ||
| 126 | * @param int $text_length Length the text should be shortened to  | 
            ||
| 127 | * @param int $time The amount of days ago the posts could have been posted  | 
            ||
| 128 | * @param string $type Type of posts to get  | 
            ||
| 129 | * @param int $start At which position the query should start  | 
            ||
| 130 | * @param bool $invert Whether the permissions should be inverted  | 
            ||
| 131 | *  | 
            ||
| 132 | * @return array An array containing the posts that were retrieved from the database  | 
            ||
| 133 | */  | 
            ||
| 134 | 30 | public function get_posts($forum_from, $permissions, $number_of_posts, $text_length, $time, $type, $start = 0, $invert = false)  | 
            |
| 187 | |||
| 188 | /**  | 
            ||
| 189 | * Run SQL query for fetching posts from database  | 
            ||
| 190 | *  | 
            ||
| 191 | * @param int $post_time Post time  | 
            ||
| 192 | * @param int $number_of_posts Number of posts to fetch  | 
            ||
| 193 | * @param int $start Start of query  | 
            ||
| 194 | *  | 
            ||
| 195 | * @return int Result pointer  | 
            ||
| 196 | */  | 
            ||
| 197 | 22 | protected function run_sql_query($post_time, $number_of_posts, $start)  | 
            |
| 198 | 	{ | 
            ||
| 199 | $sql_array = array(  | 
            ||
| 200 | 'SELECT' => 't.forum_id,  | 
            ||
| 201 | t.topic_id,  | 
            ||
| 202 | t.topic_last_post_id,  | 
            ||
| 203 | t.topic_last_post_time,  | 
            ||
| 204 | t.topic_time,  | 
            ||
| 205 | t.topic_title,  | 
            ||
| 206 | t.topic_attachment,  | 
            ||
| 207 | t.topic_views,  | 
            ||
| 208 | t.poll_title,  | 
            ||
| 209 | t.topic_posts_approved,  | 
            ||
| 210 | t.topic_posts_unapproved,  | 
            ||
| 211 | t.topic_posts_softdeleted,  | 
            ||
| 212 | t.topic_poster,  | 
            ||
| 213 | t.topic_type,  | 
            ||
| 214 | t.topic_status,  | 
            ||
| 215 | t.topic_last_poster_name,  | 
            ||
| 216 | t.topic_last_poster_id,  | 
            ||
| 217 | t.topic_last_poster_colour,  | 
            ||
| 218 | t.icon_id,  | 
            ||
| 219 | u.username,  | 
            ||
| 220 | u.user_id,  | 
            ||
| 221 | u.user_type,  | 
            ||
| 222 | u.user_colour,  | 
            ||
| 223 | p.post_id,  | 
            ||
| 224 | p.poster_id,  | 
            ||
| 225 | p.post_time,  | 
            ||
| 226 | p.post_text,  | 
            ||
| 227 | p.post_attachment,  | 
            ||
| 228 | p.post_username,  | 
            ||
| 229 | p.enable_smilies,  | 
            ||
| 230 | p.enable_bbcode,  | 
            ||
| 231 | p.enable_magic_url,  | 
            ||
| 232 | p.bbcode_bitfield,  | 
            ||
| 233 | p.bbcode_uid,  | 
            ||
| 234 | f.forum_name,  | 
            ||
| 235 | 22 | f.enable_icons',  | 
            |
| 236 | 'FROM' => array(  | 
            ||
| 237 | 22 | TOPICS_TABLE => 't',  | 
            |
| 238 | 22 | ),  | 
            |
| 239 | 'LEFT_JOIN' => array(  | 
            ||
| 240 | array(  | 
            ||
| 241 | 22 | 'FROM' => array(USERS_TABLE => 'u'),  | 
            |
| 242 | 22 | 'ON' => $this->user_link,  | 
            |
| 243 | 22 | ),  | 
            |
| 244 | array(  | 
            ||
| 245 | 22 | 'FROM' => array(FORUMS_TABLE => 'f'),  | 
            |
| 246 | 22 | 'ON' => 't.forum_id=f.forum_id',  | 
            |
| 247 | 22 | ),  | 
            |
| 248 | array(  | 
            ||
| 249 | 22 | 'FROM' => array(POSTS_TABLE => 'p'),  | 
            |
| 250 | 22 | 'ON' => $this->post_link,  | 
            |
| 251 | 22 | ),  | 
            |
| 252 | 22 | ),  | 
            |
| 253 | 22 | 'WHERE' => $this->topic_type . '  | 
            |
| 254 | 22 | ' . $post_time . '  | 
            |
| 255 | 22 | AND t.topic_status <> ' . ITEM_MOVED . '  | 
            |
| 256 | AND t.topic_visibility = 1  | 
            ||
| 257 | AND t.topic_moved_id = 0  | 
            ||
| 258 | 22 | ' . $this->where_string,  | 
            |
| 259 | 22 | 'ORDER_BY' => $this->topic_order,  | 
            |
| 260 | 22 | );  | 
            |
| 261 | |||
| 262 | 22 | 		$sql_array['LEFT_JOIN'][] = array('FROM' => array(TOPICS_POSTED_TABLE => 'tp'), 'ON' => 'tp.topic_id = t.topic_id AND tp.user_id = ' . $this->user->data['user_id']); | 
            |
| 263 | 22 | $sql_array['SELECT'] .= ', tp.topic_posted';  | 
            |
| 264 | 22 | 		$sql = $this->db->sql_build_query('SELECT', $sql_array); | 
            |
| 265 | |||
| 266 | // Cache queries for 30 seconds  | 
            ||
| 267 | 22 | if ($number_of_posts != 0)  | 
            |
| 268 | 22 | 		{ | 
            |
| 269 | 20 | $result = $this->db->sql_query_limit($sql, $number_of_posts, $start, 30);  | 
            |
| 270 | 20 | }  | 
            |
| 271 | else  | 
            ||
| 272 | 		{ | 
            ||
| 273 | 2 | $result = $this->db->sql_query($sql, 30);  | 
            |
| 274 | }  | 
            ||
| 275 | |||
| 276 | 22 | return $result;  | 
            |
| 277 | }  | 
            ||
| 278 | |||
| 279 | /**  | 
            ||
| 280 | * Fill posts array with data  | 
            ||
| 281 | *  | 
            ||
| 282 | * @param array $row Database row  | 
            ||
| 283 | * @param int $text_length Text length  | 
            ||
| 284 | * @param int $i Array pointer  | 
            ||
| 285 | * @param int $have_icons Whether any post has icons  | 
            ||
| 286 | * @param array $posts The posts array  | 
            ||
| 287 | * @param array $topic_icons List of topic icons  | 
            ||
| 288 | */  | 
            ||
| 289 | 12 | public function fill_posts_array($row, $text_length, $i, &$have_icons, &$posts, &$topic_icons)  | 
            |
| 343 | |||
| 344 | /**  | 
            ||
| 345 | * Get type constraints for database query  | 
            ||
| 346 | *  | 
            ||
| 347 | * @return null  | 
            ||
| 348 | * @throws \InvalidArgumentexception If unknown type is used  | 
            ||
| 349 | */  | 
            ||
| 350 | 28 | protected function get_type_constraints()  | 
            |
| 369 | |||
| 370 | /**  | 
            ||
| 371 | * Get type constraints for announcements  | 
            ||
| 372 | *  | 
            ||
| 373 | * @return null  | 
            ||
| 374 | */  | 
            ||
| 375 | 12 | protected function get_announcements_constraints()  | 
            |
| 383 | |||
| 384 | /**  | 
            ||
| 385 | * Get type constraints for news  | 
            ||
| 386 | *  | 
            ||
| 387 | * @return null  | 
            ||
| 388 | */  | 
            ||
| 389 | 14 | protected function get_news_constraints()  | 
            |
| 397 | |||
| 398 | /**  | 
            ||
| 399 | * Get additional type constraints for all news  | 
            ||
| 400 | * Overwrites topic type of get_news_constraints().  | 
            ||
| 401 | *  | 
            ||
| 402 | * @return null  | 
            ||
| 403 | */  | 
            ||
| 404 | 2 | protected function get_news_all_constraints()  | 
            |
| 409 | |||
| 410 | /**  | 
            ||
| 411 | * Set module id  | 
            ||
| 412 | *  | 
            ||
| 413 | * @param int $module_id Module ID  | 
            ||
| 414 | * @return null  | 
            ||
| 415 | */  | 
            ||
| 416 | 30 | public function set_module_id($module_id)  | 
            |
| 420 | |||
| 421 | /**  | 
            ||
| 422 | * Set forums to exclude or include  | 
            ||
| 423 | *  | 
            ||
| 424 | * @param array $forum_from Forums that should be shown or  | 
            ||
| 425 | * excluded from being shown  | 
            ||
| 426 | * @param array $disallowed_forums Forums that user is not  | 
            ||
| 427 | * allowed to see  | 
            ||
| 428 | * @param bool $invert Whether forum IDs in forum_from  | 
            ||
| 429 | * should be used for excluding or  | 
            ||
| 430 | * including forums  | 
            ||
| 431 | * @return bool True if valid constraints were generated, false if not  | 
            ||
| 432 | */  | 
            ||
| 433 | 30 | protected function set_forum_constraints($forum_from, $disallowed_forums, $invert = false)  | 
            |
| 458 | |||
| 459 | /**  | 
            ||
| 460 | * Generate where string for database query  | 
            ||
| 461 | *  | 
            ||
| 462 | * @param array $access_list Array containing the forum IDs  | 
            ||
| 463 | * the user has access to  | 
            ||
| 464 | * @param string $sql_operator The sql operator to use  | 
            ||
| 465 | * @param string $sql_append The sql append type to use.  | 
            ||
| 466 | * Should be either AND or OR  | 
            ||
| 467 | * @return null  | 
            ||
| 468 | */  | 
            ||
| 469 | 28 | protected function generate_where_string($access_list, $sql_operator, $sql_append)  | 
            |
| 481 | |||
| 482 | /**  | 
            ||
| 483 | * Gets the a global forum ID for global announcements  | 
            ||
| 484 | *  | 
            ||
| 485 | * @return bool True if proper ID was selected, false if not  | 
            ||
| 486 | */  | 
            ||
| 487 | 26 | protected function get_global_id()  | 
            |
| 505 | |||
| 506 | /**  | 
            ||
| 507 | * Gets the first forum_id of FORUM_POST type forums  | 
            ||
| 508 | *  | 
            ||
| 509 | * @return array Database row of query  | 
            ||
| 510 | */  | 
            ||
| 511 | 6 | protected function get_first_forum_id()  | 
            |
| 530 | |||
| 531 | /**  | 
            ||
| 532 | * Resets constraints that might have been set before  | 
            ||
| 533 | *  | 
            ||
| 534 | * @return null  | 
            ||
| 535 | */  | 
            ||
| 536 | 30 | protected function reset_constraints()  | 
            |
| 540 | |||
| 541 | /**  | 
            ||
| 542 | * Get valid data based on setting  | 
            ||
| 543 | *  | 
            ||
| 544 | * @param mixed $setting Setting to check  | 
            ||
| 545 | * @param mixed $setting_true Data if setting is 'on' (not empty)  | 
            ||
| 546 | * @param mixed $setting_false Data if setting is 'off' (empty or 0)  | 
            ||
| 547 | *  | 
            ||
| 548 | * @return mixed Valid data based on setting  | 
            ||
| 549 | */  | 
            ||
| 550 | 30 | protected function get_setting_based_data($setting, $setting_true, $setting_false)  | 
            |
| 554 | |||
| 555 | /**  | 
            ||
| 556 | * Assert that all supplied arguments evaluate to true  | 
            ||
| 557 | *  | 
            ||
| 558 | * @return bool True if all evaluate to true, false if not  | 
            ||
| 559 | */  | 
            ||
| 560 | 12 | protected function assert_all_true()  | 
            |
| 572 | |||
| 573 | /**  | 
            ||
| 574 | * Get attachments of posts  | 
            ||
| 575 | *  | 
            ||
| 576 | * @param array $row Database row of post  | 
            ||
| 577 | *  | 
            ||
| 578 | * @return array Attachment data  | 
            ||
| 579 | */  | 
            ||
| 580 | 12 | protected function get_post_attachments($row)  | 
            |
| 603 | |||
| 604 | /**  | 
            ||
| 605 | * Check if user can download a file in this forum  | 
            ||
| 606 | *  | 
            ||
| 607 | * @param int $forum_id Forum ID to check  | 
            ||
| 608 | *  | 
            ||
| 609 | * @return bool True if user can download, false if not  | 
            ||
| 610 | */  | 
            ||
| 611 | 12 | protected function user_can_download($forum_id)  | 
            |
| 615 | |||
| 616 | /**  | 
            ||
| 617 | * Format message for display  | 
            ||
| 618 | *  | 
            ||
| 619 | * @param array $row Database row  | 
            ||
| 620 | * @param int $text_length Length of text  | 
            ||
| 621 | * @param bool $posts_striped Whether post is striped  | 
            ||
| 622 | *  | 
            ||
| 623 | * @return mixed|string  | 
            ||
| 624 | */  | 
            ||
| 625 | 12 | protected function format_message($row, $text_length, &$posts_striped)  | 
            |
| 640 | |||
| 641 | /**  | 
            ||
| 642 | * Shorten message to specified length  | 
            ||
| 643 | *  | 
            ||
| 644 | * @param string $message Post text  | 
            ||
| 645 | * @param string $bbcode_uid BBCode UID  | 
            ||
| 646 | * @param int $length Length the text should have after shortening  | 
            ||
| 647 | *  | 
            ||
| 648 | * @return string Shortened messsage  | 
            ||
| 649 | */  | 
            ||
| 650 | 2 | public function shorten_message($message, $bbcode_uid, $length)  | 
            |
| 661 | }  | 
            ||
| 662 |