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 | 4 | 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) |
|
| 51 | { |
||
| 52 | 4 | $this->auth = $auth; |
|
| 53 | 4 | $this->config = $config; |
|
| 54 | 4 | $this->db = $db; |
|
| 55 | 4 | $this->request = $request; |
|
| 56 | 4 | $this->user = $user; |
|
| 57 | 4 | $this->sitemaker = $sitemaker; |
|
| 58 | 4 | $this->phpbb_root_path = $phpbb_root_path; |
|
| 59 | 4 | $this->php_ext = $php_ext; |
|
| 60 | 4 | } |
|
| 61 | |||
| 62 | 2 | public function build(array $topic_data, \phpbb\template\twig\twig &$template) |
|
| 63 | { |
||
| 64 | 2 | $this->user->add_lang('viewtopic'); |
|
| 65 | |||
| 66 | 2 | $forum_id = (int) $topic_data['forum_id']; |
|
| 67 | 2 | $topic_id = (int) $topic_data['topic_id']; |
|
| 68 | |||
| 69 | 2 | $cur_voted_id = $this->_get_users_votes($topic_id); |
|
| 70 | 2 | $s_can_vote = $this->_user_can_vote($forum_id, $topic_data, $cur_voted_id); |
|
| 71 | 2 | $viewtopic_url = append_sid("{$this->phpbb_root_path}viewtopic.{$this->php_ext}", "f=$forum_id&t=$topic_id"); |
|
| 72 | |||
| 73 | 2 | $poll_total = $poll_most = 0; |
|
| 74 | 2 | $poll_info = $this->_get_poll_info($topic_data, $poll_total, $poll_most); |
|
| 75 | 2 | $poll_end = $topic_data['poll_length'] + $topic_data['poll_start']; |
|
| 76 | |||
| 77 | 2 | $this->_build_poll_options($cur_voted_id, $poll_info, $poll_total, $poll_most, $template); |
|
| 78 | |||
| 79 | 2 | $template->assign_vars(array( |
|
| 80 | 2 | 'POLL_QUESTION' => $topic_data['poll_title'], |
|
| 81 | 2 | 'TOTAL_VOTES' => $poll_total, |
|
| 82 | 2 | 'POLL_LEFT_CAP_IMG' => $this->user->img('poll_left'), |
|
| 83 | 2 | 'POLL_RIGHT_CAP_IMG'=> $this->user->img('poll_right'), |
|
| 84 | |||
| 85 | 2 | 'L_MAX_VOTES' => $this->user->lang('MAX_OPTIONS_SELECT', (int) $topic_data['poll_max_options']), |
|
| 86 | 2 | 'L_POLL_LENGTH' => $this->_get_poll_length_lang($topic_data['poll_length'], $poll_end), |
|
| 87 | |||
| 88 | 2 | 'S_CAN_VOTE' => $s_can_vote, |
|
| 89 | 2 | 'S_DISPLAY_RESULTS' => $this->_show_results($s_can_vote, $cur_voted_id), |
|
| 90 | 2 | 'S_IS_MULTI_CHOICE' => $this->_poll_is_multiple_choice($topic_data['poll_max_options']), |
|
| 91 | 2 | 'S_POLL_ACTION' => $viewtopic_url, |
|
| 92 | 2 | 'S_FORM_TOKEN' => $this->sitemaker->get_form_key('posting'), |
|
| 93 | |||
| 94 | 2 | 'U_VIEW_RESULTS' => $viewtopic_url . '&view=viewpoll', |
|
| 95 | 2 | )); |
|
| 96 | 2 | } |
|
| 97 | |||
| 98 | /** |
||
| 99 | * @param int $forum_id |
||
| 100 | * @param array $topic_data |
||
| 101 | * @param array $cur_voted_id |
||
| 102 | * @return bool |
||
| 103 | */ |
||
| 104 | 2 | private function _user_can_vote($forum_id, array $topic_data, array $cur_voted_id) |
|
| 105 | { |
||
| 106 | 2 | return ($this->auth->acl_get('f_vote', $forum_id) && |
|
| 107 | 2 | (($topic_data['poll_length'] != 0 && $topic_data['poll_start'] + $topic_data['poll_length'] > time()) || $topic_data['poll_length'] == 0) && |
|
| 108 | 2 | $topic_data['topic_status'] != ITEM_LOCKED && |
|
| 109 | 2 | $topic_data['forum_status'] != ITEM_LOCKED && |
|
| 110 | 2 | (!sizeof($cur_voted_id) || |
|
| 111 | 2 | ($this->auth->acl_get('f_votechg', $forum_id) && $topic_data['poll_vote_change']))) ? true : false; |
|
| 112 | } |
||
| 113 | |||
| 114 | /** |
||
| 115 | * @param array $topic_data |
||
| 116 | * @param int $poll_total |
||
| 117 | * @param int $poll_most |
||
| 118 | * @return array |
||
| 119 | */ |
||
| 120 | 2 | private function _get_poll_info(array $topic_data, &$poll_total, &$poll_most) |
|
| 121 | { |
||
| 122 | 2 | $topic_id = (int) $topic_data['topic_id']; |
|
| 123 | 2 | $post_id = (int) $topic_data['topic_first_post_id']; |
|
| 124 | |||
| 125 | $sql = 'SELECT o.*, p.bbcode_bitfield, p.bbcode_uid |
||
| 126 | 2 | FROM ' . POLL_OPTIONS_TABLE . ' o, ' . POSTS_TABLE . " p |
|
| 127 | WHERE o.topic_id = $topic_id |
||
| 128 | 2 | AND p.post_id = $post_id |
|
| 129 | AND p.topic_id = o.topic_id |
||
| 130 | 2 | ORDER BY o.poll_option_id"; |
|
| 131 | 2 | $result = $this->db->sql_query($sql); |
|
| 132 | |||
| 133 | 2 | $poll_info = array(); |
|
| 134 | 2 | while ($row = $this->db->sql_fetchrow($result)) |
|
| 135 | { |
||
| 136 | 2 | $poll_info[] = $row; |
|
| 137 | 2 | $poll_total += $row['poll_option_total']; |
|
| 138 | 2 | $poll_most = ($row['poll_option_total'] >= $poll_most) ? $row['poll_option_total'] : $poll_most; |
|
| 139 | 2 | } |
|
| 140 | 2 | $this->db->sql_freeresult($result); |
|
| 141 | |||
| 142 | 2 | return $this->_parse_poll($topic_data, $poll_info); |
|
| 143 | } |
||
| 144 | |||
| 145 | /** |
||
| 146 | * @param array $topic_data |
||
| 147 | * @param array $poll_info |
||
| 148 | * @return array |
||
| 149 | */ |
||
| 150 | 2 | private function _parse_poll(array &$topic_data, array $poll_info) |
|
| 151 | { |
||
| 152 | 2 | $parse_flags = ($poll_info[0]['bbcode_bitfield'] ? OPTION_FLAG_BBCODE : 0) | OPTION_FLAG_SMILIES; |
|
| 153 | |||
| 154 | 2 | for ($i = 0, $size = sizeof($poll_info); $i < $size; $i++) |
|
| 155 | { |
||
| 156 | 2 | $poll_info[$i]['poll_option_text'] = generate_text_for_display($poll_info[$i]['poll_option_text'], $poll_info[$i]['bbcode_uid'], $poll_info[$i]['bbcode_bitfield'], $parse_flags, true); |
|
| 157 | 2 | } |
|
| 158 | |||
| 159 | 2 | $topic_data['poll_title'] = generate_text_for_display($topic_data['poll_title'], $poll_info[0]['bbcode_uid'], $poll_info[0]['bbcode_bitfield'], $parse_flags, true); |
|
| 160 | |||
| 161 | 2 | return $poll_info; |
|
| 162 | } |
||
| 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 | 2 | private function _build_poll_options(array $cur_voted_id, array $poll_info, $poll_total, $poll_most, \phpbb\template\twig\twig &$template) |
|
| 172 | { |
||
| 173 | 2 | foreach ($poll_info as $poll_option) |
|
| 174 | { |
||
| 175 | 2 | $option_pct = $this->_calculate_option_percent($poll_option['poll_option_total'], $poll_total); |
|
| 176 | 2 | $option_pct_rel = $this->_calculate_option_percent_rel($poll_option['poll_option_total'], $poll_most); |
|
| 177 | |||
| 178 | 2 | $template->assign_block_vars('poll_option', array( |
|
| 179 | 2 | 'POLL_OPTION_ID' => $poll_option['poll_option_id'], |
|
| 180 | 2 | 'POLL_OPTION_CAPTION' => $poll_option['poll_option_text'], |
|
| 181 | 2 | 'POLL_OPTION_RESULT' => $poll_option['poll_option_total'], |
|
| 182 | 2 | 'POLL_OPTION_PERCENT' => sprintf("%.1d%%", round($option_pct * 100)), |
|
| 183 | 2 | 'POLL_OPTION_PERCENT_REL' => sprintf("%.1d%%", round($option_pct_rel * 100)), |
|
| 184 | 2 | 'POLL_OPTION_PCT' => round($option_pct * 100), |
|
| 185 | 2 | 'POLL_OPTION_WIDTH' => round($option_pct * 250), |
|
| 186 | 2 | 'POLL_OPTION_VOTED' => $this->_user_has_voted_option($poll_option['poll_option_id'], $cur_voted_id), |
|
| 187 | 2 | 'POLL_OPTION_MOST_VOTES' => $this->_is_most_voted($poll_option['poll_option_total'], $poll_most), |
|
| 188 | 2 | )); |
|
| 189 | 2 | } |
|
| 190 | 2 | } |
|
| 191 | |||
| 192 | /** |
||
| 193 | * @param int $topic_id |
||
| 194 | * @return array |
||
| 195 | */ |
||
| 196 | 2 | private function _get_users_votes($topic_id) |
|
| 197 | { |
||
| 198 | 2 | $cur_voted_id = array(); |
|
| 199 | 2 | if ($this->user->data['is_registered']) |
|
| 200 | 2 | { |
|
| 201 | $sql = 'SELECT poll_option_id |
||
| 202 | 1 | FROM ' . POLL_VOTES_TABLE . ' |
|
| 203 | 1 | WHERE topic_id = ' . $topic_id . ' |
|
| 204 | 1 | AND vote_user_id = ' . $this->user->data['user_id']; |
|
| 205 | 1 | $result = $this->db->sql_query($sql); |
|
| 206 | |||
| 207 | 1 | while ($row = $this->db->sql_fetchrow($result)) |
|
| 208 | { |
||
| 209 | 1 | $cur_voted_id[] = $row['poll_option_id']; |
|
| 210 | 1 | } |
|
| 211 | 1 | $this->db->sql_freeresult($result); |
|
| 212 | 1 | } |
|
| 213 | else |
||
| 214 | { |
||
| 215 | // Cookie based guest tracking ... I don't like this but hum ho |
||
| 216 | // it's oft requested. This relies on "nice" users who don't feel |
||
| 217 | // the need to delete cookies to mess with results. |
||
| 218 | 1 | if ($this->request->is_set($this->config['cookie_name'] . '_poll_' . $topic_id, \phpbb\request\request_interface::COOKIE)) |
|
| 219 | 1 | { |
|
| 220 | 1 | $cur_voted_id = explode(',', $this->request->variable($this->config['cookie_name'] . '_poll_' . $topic_id, '', true, \phpbb\request\request_interface::COOKIE)); |
|
| 221 | 1 | $cur_voted_id = array_map('intval', $cur_voted_id); |
|
| 222 | 1 | } |
|
| 223 | } |
||
| 224 | |||
| 225 | 2 | return $cur_voted_id; |
|
| 226 | } |
||
| 227 | |||
| 228 | /** |
||
| 229 | * @param int $poll_option_id |
||
| 230 | * @param array $cur_voted_id |
||
| 231 | * @return bool |
||
| 232 | */ |
||
| 233 | 2 | 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 | 2 | 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 | 2 | 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 | 2 | private function _is_most_voted($poll_option_total, $poll_most) |
|
| 267 | |||
| 268 | /** |
||
| 269 | * @param int $poll_max_options |
||
| 270 | * @return bool |
||
| 271 | */ |
||
| 272 | 2 | 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 | 2 | 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 | 2 | private function _show_results($s_can_vote, array $cur_voted_id) |
|
| 296 | } |
||
| 297 |