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 listener 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 listener, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
23 | class listener implements EventSubscriberInterface |
||
24 | { |
||
25 | /** @var auth */ |
||
26 | protected $auth; |
||
27 | |||
28 | /* @var config */ |
||
29 | protected $config; |
||
30 | |||
31 | /* @var helper */ |
||
32 | protected $helper; |
||
33 | |||
34 | /* @var ideas */ |
||
35 | protected $ideas; |
||
36 | |||
37 | /** @var language */ |
||
38 | protected $language; |
||
39 | |||
40 | /* @var linkhelper */ |
||
41 | protected $link_helper; |
||
42 | |||
43 | /* @var template */ |
||
44 | protected $template; |
||
45 | |||
46 | /* @var user */ |
||
47 | protected $user; |
||
48 | |||
49 | /** @var string */ |
||
50 | protected $php_ext; |
||
51 | |||
52 | /** |
||
53 | * @param \phpbb\auth\auth $auth |
||
54 | * @param \phpbb\config\config $config |
||
55 | * @param \phpbb\controller\helper $helper |
||
56 | * @param \phpbb\ideas\factory\ideas $ideas |
||
57 | * @param \phpbb\language\language $language |
||
58 | * @param \phpbb\ideas\factory\linkhelper $link_helper |
||
59 | * @param \phpbb\template\template $template |
||
60 | * @param \phpbb\user $user |
||
61 | * @param string $php_ext |
||
62 | */ |
||
63 | View Code Duplication | public function __construct(auth $auth, config $config, helper $helper, ideas $ideas, language $language, linkhelper $link_helper, template $template, user $user, $php_ext) |
|
77 | |||
78 | /** |
||
79 | * @inheritDoc |
||
80 | */ |
||
81 | public static function getSubscribedEvents() |
||
82 | { |
||
83 | return array( |
||
84 | 'core.viewforum_get_topic_data' => 'ideas_forum_redirect', |
||
85 | 'core.viewtopic_modify_post_row' => 'show_post_buttons', |
||
86 | 'core.viewtopic_modify_page_title' => 'show_idea', |
||
87 | 'core.viewtopic_add_quickmod_option_before' => 'adjust_quickmod_tools', |
||
88 | 'core.viewonline_overwrite_location' => 'viewonline_ideas', |
||
89 | 'core.posting_modify_template_vars' => 'submit_idea_template', |
||
90 | 'core.posting_modify_submit_post_before' => 'submit_idea_before', |
||
91 | 'core.posting_modify_submit_post_after' => ['submit_idea_after', 'edit_idea_title'], |
||
92 | ); |
||
93 | } |
||
94 | |||
95 | /** |
||
96 | * Redirect users from the forum to the Ideas centre |
||
97 | * |
||
98 | * @param \phpbb\event\data $event The event object |
||
99 | * @return void |
||
100 | * @access public |
||
101 | */ |
||
102 | public function ideas_forum_redirect($event) |
||
103 | { |
||
104 | if ($this->is_ideas_forum($event['forum_id'])) |
||
105 | { |
||
106 | redirect($this->helper->route('phpbb_ideas_index_controller')); |
||
107 | } |
||
108 | } |
||
109 | |||
110 | /** |
||
111 | * Show post buttons (hide delete, quote or warn user buttons) |
||
112 | * |
||
113 | * @param \phpbb\event\data $event The event object |
||
114 | * @return void |
||
115 | * @access public |
||
116 | */ |
||
117 | public function show_post_buttons($event) |
||
130 | |||
131 | /** |
||
132 | * Show the idea related to the current topic |
||
133 | * |
||
134 | * @param \phpbb\event\data $event The event object |
||
135 | * @return void |
||
136 | * @access public |
||
137 | */ |
||
138 | public function show_idea($event) |
||
236 | |||
237 | /** |
||
238 | * Adjust the QuickMod tools displayed |
||
239 | * (hide options to delete, restore, make global, sticky or announcement) |
||
240 | * |
||
241 | * @param \phpbb\event\data $event The event object |
||
242 | * @return void |
||
243 | * @access public |
||
244 | */ |
||
245 | public function adjust_quickmod_tools($event) |
||
270 | |||
271 | /** |
||
272 | * Show users as viewing Ideas on Who Is Online page |
||
273 | * |
||
274 | * @param \phpbb\event\data $event The event object |
||
275 | * @return void |
||
276 | * @access public |
||
277 | */ |
||
278 | public function viewonline_ideas($event) |
||
287 | |||
288 | /** |
||
289 | * Modify the Ideas forum's posting page |
||
290 | * |
||
291 | * @param \phpbb\event\data $event The event object |
||
292 | */ |
||
293 | public function submit_idea_template($event) |
||
314 | |||
315 | /** |
||
316 | * Prepare post data vars before posting a new idea/topic. |
||
317 | * |
||
318 | * @param \phpbb\event\data $event The event object |
||
319 | */ |
||
320 | public function submit_idea_before($event) |
||
330 | |||
331 | /** |
||
332 | * Submit the idea data after posting a new idea/topic. |
||
333 | * |
||
334 | * @param \phpbb\event\data $event The event object |
||
335 | */ |
||
336 | public function submit_idea_after($event) |
||
354 | |||
355 | /** |
||
356 | * Update the idea's title when post title is edited. |
||
357 | * |
||
358 | * @param \phpbb\event\data $event The event object |
||
359 | * @return void |
||
360 | * @access public |
||
361 | */ |
||
362 | public function edit_idea_title($event) |
||
375 | |||
376 | /** |
||
377 | * Test if we are on the posting page for a new idea |
||
378 | * |
||
379 | * @param string $mode Mode should be post |
||
380 | * @param int $forum_id The forum posting is being made in |
||
381 | * @param bool $topic_flag Flag for the state of the topic_id |
||
382 | * |
||
383 | * @return bool True if mode is post, forum is Ideas forum, and a topic id is |
||
384 | * expected to exist yet, false if any of these tests failed. |
||
385 | */ |
||
386 | protected function is_post_idea($mode, $forum_id, $topic_flag = true) |
||
400 | |||
401 | /** |
||
402 | * Check if forum id is for the ideas the forum |
||
403 | * |
||
404 | * @param int $forum_id |
||
405 | * @return bool |
||
406 | * @access public |
||
407 | */ |
||
408 | protected function is_ideas_forum($forum_id) |
||
412 | |||
413 | /** |
||
414 | * Check if a post is the first post in a topic |
||
415 | * |
||
416 | * @param int|string $topic_first_post_id |
||
417 | * @param int|string $post_id |
||
418 | * @return bool |
||
419 | * @access protected |
||
420 | */ |
||
421 | protected function is_first_post($topic_first_post_id, $post_id) |
||
425 | } |
||
426 |
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.