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 | public function __construct(auth $auth, config $config, helper $helper, ideas $ideas, language $language, linkhelper $link_helper, template $template, user $user, $php_ext) |
||
| 64 | { |
||
| 65 | $this->auth = $auth; |
||
| 66 | $this->config = $config; |
||
| 67 | $this->helper = $helper; |
||
| 68 | $this->ideas = $ideas; |
||
| 69 | $this->language = $language; |
||
| 70 | $this->link_helper = $link_helper; |
||
| 71 | $this->template = $template; |
||
| 72 | $this->user = $user; |
||
| 73 | $this->php_ext = $php_ext; |
||
| 74 | |||
| 75 | $this->language->add_lang('common', 'phpbb/ideas'); |
||
| 76 | } |
||
| 77 | |||
| 78 | /** |
||
| 79 | * @inheritDoc |
||
| 80 | */ |
||
| 81 | public static function getSubscribedEvents() |
||
| 92 | |||
| 93 | /** |
||
| 94 | * Redirect users from the forum to the Ideas centre |
||
| 95 | * |
||
| 96 | * @param \phpbb\event\data $event The event object |
||
| 97 | * @return void |
||
| 98 | * @access public |
||
| 99 | */ |
||
| 100 | public function ideas_forum_redirect($event) |
||
| 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) |
||
| 237 | |||
| 238 | /** |
||
| 239 | * Adjust the QuickMod tools displayed |
||
| 240 | * (hide options to delete, restore, make global, sticky or announcement) |
||
| 241 | * |
||
| 242 | * @param \phpbb\event\data $event The event object |
||
| 243 | * @return void |
||
| 244 | * @access public |
||
| 245 | */ |
||
| 246 | public function adjust_quickmod_tools($event) |
||
| 271 | |||
| 272 | /** |
||
| 273 | * Show users as viewing Ideas on Who Is Online page |
||
| 274 | * |
||
| 275 | * @param \phpbb\event\data $event The event object |
||
| 276 | * @return void |
||
| 277 | * @access public |
||
| 278 | */ |
||
| 279 | public function viewonline_ideas($event) |
||
| 300 | |||
| 301 | /** |
||
| 302 | * Update the idea's title when post title is edited. |
||
| 303 | * |
||
| 304 | * @param \phpbb\event\data $event The event object |
||
| 305 | * @return void |
||
| 306 | * @access public |
||
| 307 | */ |
||
| 308 | public function edit_idea_title($event) |
||
| 321 | |||
| 322 | /** |
||
| 323 | * Check if forum id is for the ideas the forum |
||
| 324 | * |
||
| 325 | * @param int $forum_id |
||
| 326 | * @return bool |
||
| 327 | * @access public |
||
| 328 | */ |
||
| 329 | protected function is_ideas_forum($forum_id) |
||
| 333 | |||
| 334 | /** |
||
| 335 | * Check if a post is the first post in a topic |
||
| 336 | * |
||
| 337 | * @param int|string $topic_first_post_id |
||
| 338 | * @param int|string $post_id |
||
| 339 | * @return bool |
||
| 340 | * @access protected |
||
| 341 | */ |
||
| 342 | protected function is_first_post($topic_first_post_id, $post_id) |
||
| 346 | } |
||
| 347 |