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:
1 | <?php |
||
18 | class topicsolved |
||
19 | { |
||
20 | /** No-one can mark topics as solved. */ |
||
21 | const TOPIC_SOLVED_NO = 0; |
||
22 | |||
23 | /** Topic starter and moderators can mark topics as solved. */ |
||
24 | const TOPIC_SOLVED_YES = 1; |
||
25 | |||
26 | /** Only moderators can mark topics as solved. */ |
||
27 | const TOPIC_SOLVED_MOD = 2; |
||
28 | |||
29 | /** @var \phpbb\db\driver\driver_interface */ |
||
30 | protected $db; |
||
31 | |||
32 | /** @var \phpbb\user */ |
||
33 | protected $user; |
||
34 | |||
35 | /** @var \phpbb\auth\auth */ |
||
36 | protected $auth; |
||
37 | |||
38 | /** @var \phpbb\event\dispatcher_interface */ |
||
39 | protected $dispatcher; |
||
40 | |||
41 | /** @var string core.root_path */ |
||
42 | protected $root_path; |
||
43 | |||
44 | /** @var string core.php_ext */ |
||
45 | protected $php_ext; |
||
46 | |||
47 | /** |
||
48 | * Constructor |
||
49 | * |
||
50 | * @param \phpbb\db\driver\driver_interface $db Database object |
||
51 | * @param \phpbb\user $user |
||
52 | * @param \phpbb\auth\auth $auth |
||
53 | * @param \phpbb\event\dispatcher_interface $dispatcher |
||
54 | * @param string $root_path core.root_path |
||
55 | * @param string $php_ext core.php_ext |
||
56 | */ |
||
57 | 12 | public function __construct( |
|
73 | |||
74 | /** |
||
75 | * Determine if user is allowed to mark a post as solved or unsolved. |
||
76 | * |
||
77 | * @param string $solved Either "solved" or "unsolved". |
||
78 | * @param array $topic_data Topic to be solved or unsolved. |
||
79 | * |
||
80 | * @throws \phpbb\exception\runtime_exception |
||
81 | * if an invalid solved parameter is specified. |
||
82 | * |
||
83 | * @return bool User is authorized to (un)solve topic. |
||
84 | */ |
||
85 | 1 | public function user_can_solve_post($solved, $topic_data) |
|
119 | |||
120 | /** |
||
121 | * Fetches all topic solved data related to the given post. |
||
122 | * |
||
123 | * @param int $post_id ID of post to fetch topic/forum data for. |
||
124 | * |
||
125 | * @return mixed topic data, or false if not found |
||
126 | */ |
||
127 | 1 | public function get_topic_data($post_id) |
|
150 | |||
151 | /** |
||
152 | * Update topic with the given data. |
||
153 | * |
||
154 | * @param int $topic_id Topic to update. |
||
155 | * @param array $data Topic data to update. |
||
156 | * |
||
157 | * @return mixed true if successful |
||
158 | */ |
||
159 | 3 | public function update_topic($topic_id, $data) |
|
170 | |||
171 | /** |
||
172 | * Marks a topic as solved. |
||
173 | * |
||
174 | * @param array $topic_data Topic to be marked as solved. |
||
175 | * @param int $post_id Post to mark as the solution. |
||
176 | */ |
||
177 | 2 | View Code Duplication | public function mark_solved($topic_data, $post_id) |
204 | |||
205 | /** |
||
206 | * Marks a topic as unsolved. |
||
207 | * |
||
208 | * @param array $topic_data Topic to be marked as unsolved. |
||
209 | */ |
||
210 | 1 | View Code Duplication | public function mark_unsolved($topic_data) |
237 | |||
238 | /** |
||
239 | * Checks if the currently logged in user has permission to lock a post. |
||
240 | * |
||
241 | * Regular users won't have permission to solve any topics other than their |
||
242 | * own, and moderator permissions are forum based, so we only need to know |
||
243 | * the forum, not the post. |
||
244 | * |
||
245 | * @param int $forum_id Forum to check permissions on. |
||
246 | * |
||
247 | * @return bool true if user has permission to lock a post. |
||
248 | */ |
||
249 | 4 | public function user_can_lock_post($forum_id) |
|
265 | |||
266 | /** |
||
267 | * Generate markup for the given solved indicator image. |
||
268 | * |
||
269 | * @param string $type One of "head", "list", or "post". |
||
270 | * @param string $alt Language code for title and alternative text. |
||
271 | * @param string $url Optional link to solved post. |
||
272 | * |
||
273 | * @return string HTML markup for image. |
||
274 | */ |
||
275 | 6 | public function image($type, $alt = '', $url = '') |
|
295 | |||
296 | /** |
||
297 | * Generate link to specific post (usually solution post). |
||
298 | * |
||
299 | * @param int $forum_id |
||
300 | * @param int $topic_id |
||
301 | * @param int $post_id |
||
302 | * |
||
303 | * @return string Relative URL to post |
||
304 | */ |
||
305 | 1 | public function get_link_to_post($forum_id, $topic_id, $post_id) |
|
310 | } |
||
311 |