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 |
||
17 | class listener implements EventSubscriberInterface |
||
18 | { |
||
19 | /** @var \tas2580\seourls\event\base */ |
||
20 | protected $base; |
||
21 | |||
22 | /** @var \phpbb\template\template */ |
||
23 | protected $template; |
||
24 | |||
25 | /** @var \phpbb\request\request */ |
||
26 | protected $request; |
||
27 | |||
28 | /** @var \phpbb\path_helper */ |
||
29 | protected $path_helper; |
||
30 | |||
31 | /** @var string phpbb_root_path */ |
||
32 | protected $phpbb_root_path; |
||
33 | |||
34 | /** @var string php_ext */ |
||
35 | protected $php_ext; |
||
36 | |||
37 | /** |
||
38 | * Constructor |
||
39 | * |
||
40 | * @param \tas2580\seourls\event\base $base |
||
41 | * @param \phpbb\template\template $template Template object |
||
42 | * @param \phpbb\request\request $request Request object |
||
43 | * @param \phpbb\path_helper $path_helper Controller helper object |
||
44 | * @param string $phpbb_root_path phpbb_root_path |
||
45 | * @param string $php_ext php_ext |
||
46 | * @access public |
||
47 | */ |
||
48 | public function __construct(\tas2580\seourls\event\base $base, \phpbb\template\template $template, \phpbb\request\request $request, \phpbb\path_helper $path_helper, $phpbb_root_path, $php_ext) |
||
49 | { |
||
50 | $this->base = $base; |
||
51 | $this->template = $template; |
||
52 | $this->request = $request; |
||
53 | $this->path_helper = $path_helper; |
||
54 | $this->phpbb_root_path = $phpbb_root_path; |
||
55 | $this->php_ext = $php_ext; |
||
56 | |||
57 | $this->in_viewtopic = false; |
||
58 | } |
||
59 | |||
60 | /** |
||
61 | * Assign functions defined in this class to event listeners in the core |
||
62 | * |
||
63 | * @return array |
||
64 | * @static |
||
65 | * @access public |
||
66 | */ |
||
67 | public static function getSubscribedEvents() |
||
68 | { |
||
69 | return array( |
||
70 | 'core.append_sid' => 'append_sid', |
||
71 | 'core.display_forums_modify_sql' => 'display_forums_modify_sql', |
||
72 | 'core.display_forums_modify_template_vars' => 'display_forums_modify_template_vars', |
||
73 | 'core.display_forums_modify_forum_rows' => 'display_forums_modify_forum_rows', |
||
74 | 'core.display_forums_modify_category_template_vars' => 'display_forums_modify_category_template_vars', |
||
75 | 'core.generate_forum_nav' => 'generate_forum_nav', |
||
76 | 'core.make_jumpbox_modify_tpl_ary' => 'make_jumpbox_modify_tpl_ary', // Not in phpBB |
||
77 | 'core.memberlist_view_profile' => 'memberlist_view_profile', |
||
78 | 'core.pagination_generate_page_link' => 'pagination_generate_page_link', |
||
79 | 'core.search_modify_tpl_ary' => 'search_modify_tpl_ary', |
||
80 | 'core.viewforum_modify_topicrow' => 'viewforum_modify_topicrow', |
||
81 | 'core.viewforum_get_topic_data' => 'viewforum_get_topic_data', |
||
82 | 'core.viewforum_get_shadowtopic_data' => 'viewforum_get_shadowtopic_data', |
||
83 | 'core.viewtopic_assign_template_vars_before' => 'viewtopic_assign_template_vars_before', |
||
84 | 'core.viewtopic_before_f_read_check' => 'viewtopic_before_f_read_check', |
||
85 | 'core.viewtopic_modify_page_title' => 'viewtopic_modify_page_title', |
||
86 | 'core.viewtopic_modify_post_row' => 'viewtopic_modify_post_row', |
||
87 | 'core.viewtopic_get_post_data' => 'viewtopic_get_post_data', |
||
88 | ); |
||
89 | } |
||
90 | |||
91 | /** |
||
92 | * Correct the path of $viewtopic_url |
||
93 | * |
||
94 | * @param object $event The event object |
||
95 | * @return null |
||
96 | * @access public |
||
97 | */ |
||
98 | public function append_sid($event) |
||
112 | |||
113 | /** |
||
114 | * Get informations for the last post from Database |
||
115 | * |
||
116 | * @param object $event The event object |
||
117 | * @return null |
||
118 | * @access public |
||
119 | */ |
||
120 | View Code Duplication | public function display_forums_modify_sql($event) |
|
|
|||
121 | { |
||
122 | $sql_array = $event['sql_ary']; |
||
123 | $sql_array['LEFT_JOIN'][] = array( |
||
124 | 'FROM' => array(TOPICS_TABLE => 't'), |
||
125 | 'ON' => "f.forum_last_post_id = t.topic_last_post_id" |
||
126 | ); |
||
127 | $sql_array['SELECT'] .= ', t.topic_title, t.topic_id, t.topic_posts_approved, t.topic_posts_unapproved, t.topic_posts_softdeleted'; |
||
128 | $event['sql_ary'] = $sql_array; |
||
129 | } |
||
130 | |||
131 | /** |
||
132 | * Store informations for the last post in forum_rows array |
||
133 | * |
||
134 | * @param object $event The event object |
||
135 | * @return null |
||
136 | * @access public |
||
137 | */ |
||
138 | public function display_forums_modify_forum_rows($event) |
||
139 | { |
||
140 | $forum_rows = $event['forum_rows']; |
||
141 | if ($event['row']['forum_last_post_time'] == $forum_rows[$event['parent_id']]['forum_last_post_time']) |
||
142 | { |
||
143 | $forum_rows[$event['parent_id']]['forum_name_last_post'] =$event['row']['forum_name']; |
||
144 | $forum_rows[$event['parent_id']]['topic_id_last_post'] =$event['row']['topic_id']; |
||
145 | $forum_rows[$event['parent_id']]['topic_title_last_post'] =$event['row']['topic_title']; |
||
146 | $event['forum_rows'] = $forum_rows; |
||
147 | } |
||
148 | } |
||
149 | |||
150 | /** |
||
151 | * Rewrite links to forums and subforums in forum index |
||
152 | * also correct the path of the forum images if we are in a forum |
||
153 | * |
||
154 | * @param object $event The event object |
||
155 | * @return null |
||
156 | * @access public |
||
157 | */ |
||
158 | public function display_forums_modify_template_vars($event) |
||
186 | |||
187 | /** |
||
188 | * Rewrite the categorie links |
||
189 | * |
||
190 | * @param object $event The event object |
||
191 | * @return null |
||
192 | * @access public |
||
193 | */ |
||
194 | public function display_forums_modify_category_template_vars($event) |
||
201 | |||
202 | /** |
||
203 | * Rewrite links in breadcrumbs |
||
204 | * |
||
205 | * @param object $event The event object |
||
206 | * @return null |
||
207 | * @access public |
||
208 | */ |
||
209 | public function generate_forum_nav($event) |
||
224 | |||
225 | // Not in phpBB |
||
226 | public function make_jumpbox_modify_tpl_ary($event) |
||
237 | |||
238 | /** |
||
239 | * Rewrite links to most active forum and topic on profile page |
||
240 | * |
||
241 | * @param object $event The event object |
||
242 | * @return null |
||
243 | * @access public |
||
244 | */ |
||
245 | public function memberlist_view_profile($event) |
||
253 | |||
254 | /** |
||
255 | * Rewrite pagination links |
||
256 | * |
||
257 | * @param object $event The event object |
||
258 | * @return null |
||
259 | * @access public |
||
260 | */ |
||
261 | public function pagination_generate_page_link($event) |
||
285 | |||
286 | /** |
||
287 | * Rewrite links in the search result |
||
288 | * |
||
289 | * @param object $event The event object |
||
290 | * @return null |
||
291 | * @access public |
||
292 | */ |
||
293 | public function search_modify_tpl_ary($event) |
||
306 | |||
307 | /** |
||
308 | * Rewrite links to topics in forum view |
||
309 | * |
||
310 | * @param object $event The event object |
||
311 | * @return null |
||
312 | * @access public |
||
313 | */ |
||
314 | public function viewforum_modify_topicrow($event) |
||
335 | |||
336 | /** |
||
337 | * Get forum_name for shaddow topics |
||
338 | * |
||
339 | * @param object $event The event object |
||
340 | * @return null |
||
341 | * @access public |
||
342 | */ |
||
343 | View Code Duplication | public function viewforum_get_shadowtopic_data($event) |
|
350 | |||
351 | /** |
||
352 | * Rewrite the canonical URL on viewforum.php |
||
353 | * |
||
354 | * @param object $event The event object |
||
355 | * @return null |
||
356 | * @access public |
||
357 | */ |
||
358 | public function viewforum_get_topic_data($event) |
||
372 | |||
373 | /** |
||
374 | * Rewrite the topic URL for the headline of the topic page and the link back to forum |
||
375 | * |
||
376 | * @param object $event The event object |
||
377 | * @return null |
||
378 | * @access public |
||
379 | */ |
||
380 | public function viewtopic_get_post_data($event) |
||
389 | |||
390 | /** |
||
391 | * Assign topic data to global variables for pagination |
||
392 | * |
||
393 | * @param object $event The event object |
||
394 | * @return null |
||
395 | * @access public |
||
396 | */ |
||
397 | public function viewtopic_assign_template_vars_before($event) |
||
407 | |||
408 | public function viewtopic_before_f_read_check() |
||
412 | |||
413 | /** |
||
414 | * Rewrite the canonical URL on viewtopic.php |
||
415 | * |
||
416 | * @param object $event The event object |
||
417 | * @return null |
||
418 | * @access public |
||
419 | */ |
||
420 | public function viewtopic_modify_page_title($event) |
||
428 | |||
429 | /** |
||
430 | * Rewrite mini post img link |
||
431 | * |
||
432 | * @param object $event The event object |
||
433 | * @return null |
||
434 | * @access public |
||
435 | */ |
||
436 | public function viewtopic_modify_post_row($event) |
||
444 | } |
||
445 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.