Complex classes like poll 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 poll, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 12 | class poll |
||
| 13 | { |
||
| 14 | /** @var \phpbb\auth\auth */ |
||
| 15 | protected $auth; |
||
| 16 | |||
| 17 | /** @var \phpbb\config\config */ |
||
| 18 | protected $config; |
||
| 19 | |||
| 20 | /** @var \phpbb\db\driver\driver_interface */ |
||
| 21 | protected $db; |
||
| 22 | |||
| 23 | /** @var \phpbb\request\request_interface */ |
||
| 24 | protected $request; |
||
| 25 | |||
| 26 | /** @var \phpbb\language\language */ |
||
| 27 | protected $translator; |
||
| 28 | |||
| 29 | /** @var \phpbb\user */ |
||
| 30 | protected $user; |
||
| 31 | |||
| 32 | /** @var \blitze\sitemaker\services\util */ |
||
| 33 | protected $sitemaker; |
||
| 34 | |||
| 35 | /** @var string */ |
||
| 36 | protected $phpbb_root_path; |
||
| 37 | |||
| 38 | /** @var string */ |
||
| 39 | protected $php_ext; |
||
| 40 | |||
| 41 | /** |
||
| 42 | * Constructor |
||
| 43 | * |
||
| 44 | * @param \phpbb\auth\auth $auth Permission object |
||
| 45 | * @param \phpbb\config\config $config Config object |
||
| 46 | * @param \phpbb\db\driver\driver_interface $db Database connection |
||
| 47 | * @param \phpbb\request\request_interface $request Request object |
||
| 48 | * @param \phpbb\language\language $translator Language object |
||
| 49 | * @param \phpbb\user $user User object |
||
| 50 | * @param \blitze\sitemaker\services\util $sitemaker Sitemaker Object |
||
| 51 | * @param string $phpbb_root_path Path to the phpbb includes directory. |
||
| 52 | * @param string $php_ext php file extension |
||
| 53 | */ |
||
| 54 | 4 | public function __construct(\phpbb\auth\auth $auth, \phpbb\config\config $config, \phpbb\db\driver\driver_interface $db, \phpbb\request\request_interface $request, \phpbb\language\language $translator, \phpbb\user $user, \blitze\sitemaker\services\util $sitemaker, $phpbb_root_path, $php_ext) |
|
| 66 | |||
| 67 | /** |
||
| 68 | * @param array $topic_data |
||
| 69 | * @param \phpbb\template\twig\twig $template |
||
| 70 | */ |
||
| 71 | 2 | public function build(array $topic_data, \phpbb\template\twig\twig &$template) |
|
| 106 | |||
| 107 | /** |
||
| 108 | * @param int $forum_id |
||
| 109 | * @param array $topic_data |
||
| 110 | * @param array $cur_voted_id |
||
| 111 | * @return bool |
||
| 112 | */ |
||
| 113 | 2 | private function user_can_vote($forum_id, array $topic_data, array $cur_voted_id) |
|
| 121 | |||
| 122 | /** |
||
| 123 | * @param int $forum_id |
||
| 124 | * @param array $topic_data |
||
| 125 | * @param array $cur_voted_id |
||
| 126 | * @return bool |
||
| 127 | */ |
||
| 128 | 2 | private function user_is_authorized($forum_id, array $topic_data, array $cur_voted_id) |
|
| 132 | |||
| 133 | /** |
||
| 134 | * @param int $forum_id |
||
| 135 | * @param array $topic_data |
||
| 136 | * @param array $cur_voted_id |
||
| 137 | * @return bool |
||
| 138 | */ |
||
| 139 | 2 | private function user_can_change_vote($forum_id, array $topic_data, array $cur_voted_id) |
|
| 143 | |||
| 144 | /** |
||
| 145 | * @param array $topic_data |
||
| 146 | * @return bool |
||
| 147 | */ |
||
| 148 | 1 | private function poll_is_still_open(array $topic_data) |
|
| 152 | |||
| 153 | /** |
||
| 154 | * @param array $topic_data |
||
| 155 | * @return bool |
||
| 156 | */ |
||
| 157 | 1 | private function is_topic_status_eligible(array $topic_data) |
|
| 161 | |||
| 162 | /** |
||
| 163 | * @param array $topic_data |
||
| 164 | * @param int $poll_total |
||
| 165 | * @param int $poll_most |
||
| 166 | * @return array |
||
| 167 | */ |
||
| 168 | 2 | private function get_poll_info(array $topic_data, &$poll_total, &$poll_most) |
|
| 192 | |||
| 193 | /** |
||
| 194 | * @param array $topic_data |
||
| 195 | * @param array $poll_info |
||
| 196 | * @return array |
||
| 197 | */ |
||
| 198 | 2 | private function parse_poll(array &$topic_data, array $poll_info) |
|
| 211 | |||
| 212 | /** |
||
| 213 | * @param array $cur_voted_id |
||
| 214 | * @param array $poll_info |
||
| 215 | * @param int $poll_total |
||
| 216 | * @param int $poll_most |
||
| 217 | * @param \phpbb\template\twig\twig $template |
||
| 218 | */ |
||
| 219 | 2 | private function build_poll_options(array $cur_voted_id, array $poll_info, $poll_total, $poll_most, \phpbb\template\twig\twig &$template) |
|
| 239 | |||
| 240 | /** |
||
| 241 | * @param int $topic_id |
||
| 242 | * @return array |
||
| 243 | */ |
||
| 244 | 2 | private function get_users_votes($topic_id) |
|
| 275 | |||
| 276 | /** |
||
| 277 | * @param int $poll_option_id |
||
| 278 | * @param array $cur_voted_id |
||
| 279 | * @return bool |
||
| 280 | */ |
||
| 281 | 2 | private function user_has_voted_option($poll_option_id, array $cur_voted_id) |
|
| 285 | |||
| 286 | /** |
||
| 287 | * @param int $poll_option_total |
||
| 288 | * @param int $poll_total |
||
| 289 | * @return float|int |
||
| 290 | */ |
||
| 291 | 2 | private function calculate_option_percent($poll_option_total, $poll_total) |
|
| 295 | |||
| 296 | /** |
||
| 297 | * @param int $poll_option_total |
||
| 298 | * @param int $poll_most |
||
| 299 | * @return float|int |
||
| 300 | */ |
||
| 301 | 2 | private function calculate_option_percent_rel($poll_option_total, $poll_most) |
|
| 305 | |||
| 306 | /** |
||
| 307 | * @param int $poll_option_total |
||
| 308 | * @param int $poll_most |
||
| 309 | * @return bool |
||
| 310 | */ |
||
| 311 | 2 | private function is_most_voted($poll_option_total, $poll_most) |
|
| 315 | |||
| 316 | /** |
||
| 317 | * @param int $poll_max_options |
||
| 318 | * @return bool |
||
| 319 | */ |
||
| 320 | 2 | private function poll_is_multiple_choice($poll_max_options) |
|
| 324 | |||
| 325 | /** |
||
| 326 | * @param int $poll_length |
||
| 327 | * @param int $poll_end |
||
| 328 | * @return string |
||
| 329 | */ |
||
| 330 | 2 | private function get_poll_length_lang($poll_length, $poll_end) |
|
| 334 | |||
| 335 | /** |
||
| 336 | * @param bool $s_can_vote |
||
| 337 | * @param array $cur_voted_id |
||
| 338 | * @return bool |
||
| 339 | */ |
||
| 340 | 2 | private function show_results($s_can_vote, array $cur_voted_id) |
|
| 344 | } |
||
| 345 |