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) |
|
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) |
|
345 | |||
346 | /** |
||
347 | * Get type constraints for database query |
||
348 | * |
||
349 | * @return null |
||
350 | * @throws \InvalidArgumentexception If unknown type is used |
||
351 | */ |
||
352 | 28 | protected function get_type_constraints() |
|
371 | |||
372 | /** |
||
373 | * Get type constraints for announcements |
||
374 | * |
||
375 | * @return null |
||
376 | */ |
||
377 | 12 | protected function get_announcements_constraints() |
|
385 | |||
386 | /** |
||
387 | * Get type constraints for news |
||
388 | * |
||
389 | * @return null |
||
390 | */ |
||
391 | 14 | protected function get_news_constraints() |
|
399 | |||
400 | /** |
||
401 | * Get additional type constraints for all news |
||
402 | * Overwrites topic type of get_news_constraints(). |
||
403 | * |
||
404 | * @return null |
||
405 | */ |
||
406 | 2 | protected function get_news_all_constraints() |
|
411 | |||
412 | /** |
||
413 | * Set module id |
||
414 | * |
||
415 | * @param int $module_id Module ID |
||
416 | * @return null |
||
417 | */ |
||
418 | 30 | public function set_module_id($module_id) |
|
422 | |||
423 | /** |
||
424 | * Set forums to exclude or include |
||
425 | * |
||
426 | * @param array $forum_from Forums that should be shown or |
||
427 | * excluded from being shown |
||
428 | * @param array $disallowed_forums Forums that user is not |
||
429 | * allowed to see |
||
430 | * @param bool $invert Whether forum IDs in forum_from |
||
431 | * should be used for excluding or |
||
432 | * including forums |
||
433 | * @return bool True if valid constraints were generated, false if not |
||
434 | */ |
||
435 | 30 | protected function set_forum_constraints($forum_from, $disallowed_forums, $invert = false) |
|
460 | |||
461 | /** |
||
462 | * Generate where string for database query |
||
463 | * |
||
464 | * @param array $access_list Array containing the forum IDs |
||
465 | * the user has access to |
||
466 | * @param string $sql_operator The sql operator to use |
||
467 | * @param string $sql_append The sql append type to use. |
||
468 | * Should be either AND or OR |
||
469 | * @return null |
||
470 | */ |
||
471 | 28 | protected function generate_where_string($access_list, $sql_operator, $sql_append) |
|
483 | |||
484 | /** |
||
485 | * Gets the a global forum ID for global announcements |
||
486 | * |
||
487 | * @return bool True if proper ID was selected, false if not |
||
488 | */ |
||
489 | 26 | protected function get_global_id() |
|
507 | |||
508 | /** |
||
509 | * Gets the first forum_id of FORUM_POST type forums |
||
510 | * |
||
511 | * @return array Database row of query |
||
512 | */ |
||
513 | 6 | protected function get_first_forum_id() |
|
532 | |||
533 | /** |
||
534 | * Resets constraints that might have been set before |
||
535 | * |
||
536 | * @return null |
||
537 | */ |
||
538 | 30 | protected function reset_constraints() |
|
542 | |||
543 | /** |
||
544 | * Get valid data based on setting |
||
545 | * |
||
546 | * @param mixed $setting Setting to check |
||
547 | * @param mixed $setting_true Data if setting is 'on' (not empty) |
||
548 | * @param mixed $setting_false Data if setting is 'off' (empty or 0) |
||
549 | * |
||
550 | * @return mixed Valid data based on setting |
||
551 | */ |
||
552 | 30 | protected function get_setting_based_data($setting, $setting_true, $setting_false) |
|
556 | |||
557 | /** |
||
558 | * Assert that all supplied arguments evaluate to true |
||
559 | * |
||
560 | * @return bool True if all evaluate to true, false if not |
||
561 | */ |
||
562 | 12 | protected function assert_all_true() |
|
574 | |||
575 | /** |
||
576 | * Get attachments of posts |
||
577 | * |
||
578 | * @param array $row Database row of post |
||
579 | * |
||
580 | * @return array Attachment data |
||
581 | */ |
||
582 | 12 | protected function get_post_attachments($row) |
|
605 | |||
606 | /** |
||
607 | * Check if user can download a file in this forum |
||
608 | * |
||
609 | * @param int $forum_id Forum ID to check |
||
610 | * |
||
611 | * @return bool True if user can download, false if not |
||
612 | */ |
||
613 | 12 | protected function user_can_download($forum_id) |
|
617 | |||
618 | /** |
||
619 | * Format message for display |
||
620 | * |
||
621 | * @param array $row Database row |
||
622 | * @param int $text_length Length of text |
||
623 | * @param bool $posts_striped Whether post is striped |
||
624 | * |
||
625 | * @return mixed|string |
||
626 | */ |
||
627 | 12 | protected function format_message($row, $text_length, &$posts_striped) |
|
642 | |||
643 | /** |
||
644 | * Shorten message to specified length |
||
645 | * |
||
646 | * @param string $message Post text |
||
647 | * @param string $bbcode_uid BBCode UID |
||
648 | * @param int $length Length the text should have after shortening |
||
649 | * |
||
650 | * @return string Shortened messsage |
||
651 | */ |
||
652 | 2 | public function shorten_message($message, $bbcode_uid, $length) |
|
663 | } |
||
664 |