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\user */ |
||
| 27 | protected $user; |
||
| 28 | |||
| 29 | /** @var \blitze\sitemaker\services\util */ |
||
| 30 | protected $sitemaker; |
||
| 31 | |||
| 32 | /** @var string */ |
||
| 33 | protected $phpbb_root_path; |
||
| 34 | |||
| 35 | /** @var string */ |
||
| 36 | protected $php_ext; |
||
| 37 | |||
| 38 | /** |
||
| 39 | * Constructor |
||
| 40 | * |
||
| 41 | * @param \phpbb\auth\auth $auth Permission object |
||
| 42 | * @param \phpbb\config\config $config Config object |
||
| 43 | * @param \phpbb\db\driver\driver_interface $db Database connection |
||
| 44 | * @param \phpbb\request\request_interface $request Request object |
||
| 45 | * @param \phpbb\user $user User object |
||
| 46 | * @param \blitze\sitemaker\services\util $sitemaker Sitemaker Object |
||
| 47 | * @param string $phpbb_root_path Path to the phpbb includes directory. |
||
| 48 | * @param string $php_ext php file extension |
||
| 49 | */ |
||
| 50 | public function __construct(\phpbb\auth\auth $auth, \phpbb\config\config $config, \phpbb\db\driver\driver_interface $db, \phpbb\request\request_interface $request, \phpbb\user $user, \blitze\sitemaker\services\util $sitemaker, $phpbb_root_path, $php_ext) |
||
| 61 | |||
| 62 | public function build(array $topic_data, \phpbb\template\twig\twig &$template) |
||
| 97 | |||
| 98 | /** |
||
| 99 | * @param int $forum_id |
||
| 100 | * @param array $topic_data |
||
| 101 | * @param array $cur_voted_id |
||
| 102 | * @return bool |
||
| 103 | */ |
||
| 104 | private function _user_can_vote($forum_id, array $topic_data, array $cur_voted_id) |
||
| 113 | |||
| 114 | /** |
||
| 115 | * @param array $topic_data |
||
| 116 | * @param int $poll_total |
||
| 117 | * @param int $poll_most |
||
| 118 | * @return array |
||
| 119 | */ |
||
| 120 | private function _get_poll_info(array $topic_data, &$poll_total, &$poll_most) |
||
| 144 | |||
| 145 | /** |
||
| 146 | * @param array $topic_data |
||
| 147 | * @param array $poll_info |
||
| 148 | * @return array |
||
| 149 | */ |
||
| 150 | private function _parse_poll(array &$topic_data, array $poll_info) |
||
| 163 | |||
| 164 | /** |
||
| 165 | * @param array $cur_voted_id |
||
| 166 | * @param array $poll_info |
||
| 167 | * @param int $poll_total |
||
| 168 | * @param int $poll_most |
||
| 169 | * @param \phpbb\template\twig\twig $template |
||
| 170 | */ |
||
| 171 | private function _build_poll_options(array $cur_voted_id, array $poll_info, $poll_total, $poll_most, \phpbb\template\twig\twig &$template) |
||
| 191 | |||
| 192 | /** |
||
| 193 | * @param int $topic_id |
||
| 194 | * @return array |
||
| 195 | */ |
||
| 196 | private function _get_users_votes($topic_id) |
||
| 227 | |||
| 228 | /** |
||
| 229 | * @param int $poll_option_id |
||
| 230 | * @param array $cur_voted_id |
||
| 231 | * @return bool |
||
| 232 | */ |
||
| 233 | private function _user_has_voted_option($poll_option_id, array $cur_voted_id) |
||
| 237 | |||
| 238 | /** |
||
| 239 | * @param int $poll_option_total |
||
| 240 | * @param int $poll_total |
||
| 241 | * @return float|int |
||
| 242 | */ |
||
| 243 | private function _calculate_option_percent($poll_option_total, $poll_total) |
||
| 247 | |||
| 248 | /** |
||
| 249 | * @param int $poll_option_total |
||
| 250 | * @param int $poll_most |
||
| 251 | * @return float|int |
||
| 252 | */ |
||
| 253 | private function _calculate_option_percent_rel($poll_option_total, $poll_most) |
||
| 257 | |||
| 258 | /** |
||
| 259 | * @param int $poll_option_total |
||
| 260 | * @param int $poll_most |
||
| 261 | * @return bool |
||
| 262 | */ |
||
| 263 | private function _is_most_voted($poll_option_total, $poll_most) |
||
| 267 | |||
| 268 | /** |
||
| 269 | * @param int $poll_max_options |
||
| 270 | * @return bool |
||
| 271 | */ |
||
| 272 | private function _poll_is_multiple_choice($poll_max_options) |
||
| 276 | |||
| 277 | /** |
||
| 278 | * @param int $poll_length |
||
| 279 | * @param int $poll_end |
||
| 280 | * @return string |
||
| 281 | */ |
||
| 282 | private function _get_poll_length_lang($poll_length, $poll_end) |
||
| 286 | |||
| 287 | /** |
||
| 288 | * @param bool $s_can_vote |
||
| 289 | * @param array $cur_voted_id |
||
| 290 | * @return bool |
||
| 291 | */ |
||
| 292 | private function _show_results($s_can_vote, array $cur_voted_id) |
||
| 296 | } |
||
| 297 |