| Total Complexity | 45 |
| Total Lines | 331 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
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.
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 |
||
| 13 | class poll |
||
| 14 | { |
||
| 15 | /** @var \phpbb\auth\auth */ |
||
| 16 | protected $auth; |
||
| 17 | |||
| 18 | /** @var \phpbb\config\config */ |
||
| 19 | protected $config; |
||
| 20 | |||
| 21 | /** @var \phpbb\db\driver\driver_interface */ |
||
| 22 | protected $db; |
||
| 23 | |||
| 24 | /** @var \phpbb\request\request_interface */ |
||
| 25 | protected $request; |
||
| 26 | |||
| 27 | /** @var \phpbb\language\language */ |
||
| 28 | protected $translator; |
||
| 29 | |||
| 30 | /** @var \phpbb\user */ |
||
| 31 | protected $user; |
||
| 32 | |||
| 33 | /** @var \blitze\sitemaker\services\util */ |
||
| 34 | protected $sitemaker; |
||
| 35 | |||
| 36 | /** @var string */ |
||
| 37 | protected $phpbb_root_path; |
||
| 38 | |||
| 39 | /** @var string */ |
||
| 40 | protected $php_ext; |
||
| 41 | |||
| 42 | /** |
||
| 43 | * Constructor |
||
| 44 | * |
||
| 45 | * @param \phpbb\auth\auth $auth Permission object |
||
| 46 | * @param \phpbb\config\config $config Config object |
||
| 47 | * @param \phpbb\db\driver\driver_interface $db Database connection |
||
| 48 | * @param \phpbb\request\request_interface $request Request object |
||
| 49 | * @param \phpbb\language\language $translator Language object |
||
| 50 | * @param \phpbb\user $user User object |
||
| 51 | * @param \blitze\sitemaker\services\util $sitemaker Sitemaker Object |
||
| 52 | * @param string $phpbb_root_path Path to the phpbb includes directory. |
||
| 53 | * @param string $php_ext php file extension |
||
| 54 | */ |
||
| 55 | 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) |
||
| 56 | { |
||
| 57 | $this->auth = $auth; |
||
| 58 | $this->config = $config; |
||
| 59 | $this->db = $db; |
||
| 60 | $this->request = $request; |
||
| 61 | $this->translator = $translator; |
||
| 62 | $this->user = $user; |
||
| 63 | $this->sitemaker = $sitemaker; |
||
| 64 | $this->phpbb_root_path = $phpbb_root_path; |
||
| 65 | $this->php_ext = $php_ext; |
||
| 66 | } |
||
| 67 | |||
| 68 | /** |
||
| 69 | * @param array $topic_data |
||
| 70 | * @return array |
||
| 71 | */ |
||
| 72 | public function build(array $topic_data) |
||
| 73 | { |
||
| 74 | $this->translator->add_lang('viewtopic'); |
||
| 75 | |||
| 76 | $forum_id = (int) $topic_data['forum_id']; |
||
| 77 | $topic_id = (int) $topic_data['topic_id']; |
||
| 78 | |||
| 79 | $cur_voted_id = $this->get_users_votes($topic_id); |
||
| 80 | $s_can_vote = $this->user_can_vote($forum_id, $topic_data, $cur_voted_id); |
||
| 81 | $viewtopic_url = append_sid("{$this->phpbb_root_path}viewtopic.{$this->php_ext}", "f=$forum_id&t=$topic_id"); |
||
| 82 | |||
| 83 | $poll_total = $poll_most = 0; |
||
| 84 | $poll_info = $this->get_poll_info($topic_data, $poll_total, $poll_most); |
||
| 85 | $poll_end = $topic_data['poll_length'] + $topic_data['poll_start']; |
||
| 86 | |||
| 87 | return array( |
||
| 88 | 'POLL_QUESTION' => $topic_data['poll_title'], |
||
| 89 | 'TOTAL_VOTES' => $poll_total, |
||
| 90 | 'POLL_LEFT_CAP_IMG' => $this->user->img('poll_left'), |
||
| 91 | 'POLL_RIGHT_CAP_IMG' => $this->user->img('poll_right'), |
||
| 92 | |||
| 93 | 'MAX_VOTES' => $this->translator->lang('MAX_OPTIONS_SELECT', (int) $topic_data['poll_max_options']), |
||
| 94 | 'POLL_LENGTH' => $this->get_poll_length_lang($topic_data['poll_length'], $poll_end), |
||
| 95 | 'POLL_OPTIONS' => $this->get_poll_options($cur_voted_id, $poll_info, $poll_total, $poll_most), |
||
| 96 | |||
| 97 | 'S_CAN_VOTE' => $s_can_vote, |
||
| 98 | 'S_DISPLAY_RESULTS' => $this->show_results($s_can_vote, $cur_voted_id), |
||
| 99 | 'S_IS_MULTI_CHOICE' => $this->poll_is_multiple_choice($topic_data['poll_max_options']), |
||
| 100 | 'S_POLL_ACTION' => $viewtopic_url, |
||
| 101 | 'S_FORM_TOKEN' => $this->sitemaker->get_form_key('posting'), |
||
| 102 | |||
| 103 | 'U_VIEW_RESULTS' => $viewtopic_url . '&view=viewpoll', |
||
| 104 | ); |
||
| 105 | } |
||
| 106 | |||
| 107 | /** |
||
| 108 | * @param int $forum_id |
||
| 109 | * @param array $topic_data |
||
| 110 | * @param array $cur_voted_id |
||
| 111 | * @return bool |
||
| 112 | */ |
||
| 113 | private function user_can_vote($forum_id, array $topic_data, array $cur_voted_id) |
||
| 114 | { |
||
| 115 | return ($this->user_is_authorized($forum_id, $topic_data, $cur_voted_id) && |
||
| 116 | $this->poll_is_still_open($topic_data) && |
||
| 117 | $this->is_topic_status_eligible($topic_data)); |
||
| 118 | } |
||
| 119 | |||
| 120 | /** |
||
| 121 | * @param int $forum_id |
||
| 122 | * @param array $topic_data |
||
| 123 | * @param array $cur_voted_id |
||
| 124 | * @return bool |
||
| 125 | */ |
||
| 126 | private function user_is_authorized($forum_id, array $topic_data, array $cur_voted_id) |
||
| 127 | { |
||
| 128 | return ($this->auth->acl_get('f_vote', $forum_id) && $this->user_can_change_vote($forum_id, $topic_data, $cur_voted_id)); |
||
| 129 | } |
||
| 130 | |||
| 131 | /** |
||
| 132 | * @param int $forum_id |
||
| 133 | * @param array $topic_data |
||
| 134 | * @param array $cur_voted_id |
||
| 135 | * @return bool |
||
| 136 | */ |
||
| 137 | private function user_can_change_vote($forum_id, array $topic_data, array $cur_voted_id) |
||
| 140 | } |
||
| 141 | |||
| 142 | /** |
||
| 143 | * @param array $topic_data |
||
| 144 | * @return bool |
||
| 145 | */ |
||
| 146 | private function poll_is_still_open(array $topic_data) |
||
| 147 | { |
||
| 148 | return (($topic_data['poll_length'] != 0 && $topic_data['poll_start'] + $topic_data['poll_length'] > time()) || $topic_data['poll_length'] == 0); |
||
| 149 | } |
||
| 150 | |||
| 151 | /** |
||
| 152 | * @param array $topic_data |
||
| 153 | * @return bool |
||
| 154 | */ |
||
| 155 | private function is_topic_status_eligible(array $topic_data) |
||
| 156 | { |
||
| 157 | return ($topic_data['topic_status'] != ITEM_LOCKED && $topic_data['forum_status'] != ITEM_LOCKED); |
||
| 158 | } |
||
| 159 | |||
| 160 | /** |
||
| 161 | * @param array $topic_data |
||
| 162 | * @param int $poll_total |
||
| 163 | * @param int $poll_most |
||
| 164 | * @return array |
||
| 165 | */ |
||
| 166 | private function get_poll_info(array $topic_data, &$poll_total, &$poll_most) |
||
| 167 | { |
||
| 168 | $topic_id = (int) $topic_data['topic_id']; |
||
| 169 | $post_id = (int) $topic_data['topic_first_post_id']; |
||
| 170 | |||
| 171 | $sql = 'SELECT o.*, p.bbcode_bitfield, p.bbcode_uid |
||
| 172 | FROM ' . POLL_OPTIONS_TABLE . ' o, ' . POSTS_TABLE . " p |
||
| 173 | WHERE o.topic_id = $topic_id |
||
| 174 | AND p.post_id = $post_id |
||
| 175 | AND p.topic_id = o.topic_id |
||
| 176 | ORDER BY o.poll_option_id"; |
||
| 177 | $result = $this->db->sql_query($sql); |
||
| 178 | |||
| 179 | $poll_info = array(); |
||
| 180 | while ($row = $this->db->sql_fetchrow($result)) |
||
| 181 | { |
||
| 182 | $poll_info[] = $row; |
||
| 183 | $poll_total += $row['poll_option_total']; |
||
| 184 | $poll_most = ($row['poll_option_total'] >= $poll_most) ? $row['poll_option_total'] : $poll_most; |
||
| 185 | } |
||
| 186 | $this->db->sql_freeresult($result); |
||
| 187 | |||
| 188 | return $this->parse_poll($topic_data, $poll_info); |
||
| 189 | } |
||
| 190 | |||
| 191 | /** |
||
| 192 | * @param array $topic_data |
||
| 193 | * @param array $poll_info |
||
| 194 | * @return array |
||
| 195 | */ |
||
| 196 | private function parse_poll(array &$topic_data, array $poll_info) |
||
| 197 | { |
||
| 198 | $parse_flags = ($poll_info[0]['bbcode_bitfield'] ? OPTION_FLAG_BBCODE : 0) | OPTION_FLAG_SMILIES; |
||
| 199 | |||
| 200 | for ($i = 0, $size = sizeof($poll_info); $i < $size; $i++) |
||
| 201 | { |
||
| 202 | $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); |
||
| 203 | } |
||
| 204 | |||
| 205 | $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); |
||
| 206 | |||
| 207 | return $poll_info; |
||
| 208 | } |
||
| 209 | |||
| 210 | /** |
||
| 211 | * @param array $cur_voted_id |
||
| 212 | * @param array $poll_info |
||
| 213 | * @param int $poll_total |
||
| 214 | * @param int $poll_most |
||
| 215 | * @return array |
||
| 216 | */ |
||
| 217 | private function get_poll_options(array $cur_voted_id, array $poll_info, $poll_total, $poll_most) |
||
| 218 | { |
||
| 219 | $options = []; |
||
| 220 | foreach ($poll_info as $poll_option) |
||
| 221 | { |
||
| 222 | $option_pct = $this->calculate_option_percent($poll_option['poll_option_total'], $poll_total); |
||
| 223 | $option_pct_rel = $this->calculate_option_percent_rel($poll_option['poll_option_total'], $poll_most); |
||
| 224 | |||
| 225 | $options[] = array( |
||
| 226 | 'POLL_OPTION_ID' => $poll_option['poll_option_id'], |
||
| 227 | 'POLL_OPTION_CAPTION' => $poll_option['poll_option_text'], |
||
| 228 | 'POLL_OPTION_RESULT' => $poll_option['poll_option_total'], |
||
| 229 | 'POLL_OPTION_PERCENT' => sprintf("%.1d%%", round($option_pct * 100)), |
||
| 230 | 'POLL_OPTION_PERCENT_REL' => sprintf("%.1d%%", round($option_pct_rel * 100)), |
||
| 231 | 'POLL_OPTION_PCT' => round($option_pct * 100), |
||
| 232 | 'POLL_OPTION_WIDTH' => round($option_pct * 250), |
||
| 233 | 'POLL_OPTION_VOTED' => $this->user_has_voted_option($poll_option['poll_option_id'], $cur_voted_id), |
||
| 234 | 'POLL_OPTION_MOST_VOTES' => $this->is_most_voted($poll_option['poll_option_total'], $poll_most), |
||
| 235 | ); |
||
| 236 | } |
||
| 237 | |||
| 238 | return $options; |
||
| 239 | } |
||
| 240 | |||
| 241 | /** |
||
| 242 | * @param int $topic_id |
||
| 243 | * @return array |
||
| 244 | */ |
||
| 245 | private function get_users_votes($topic_id) |
||
| 275 | } |
||
| 276 | |||
| 277 | /** |
||
| 278 | * @param int $poll_option_id |
||
| 279 | * @param array $cur_voted_id |
||
| 280 | * @return bool |
||
| 281 | */ |
||
| 282 | private function user_has_voted_option($poll_option_id, array $cur_voted_id) |
||
| 285 | } |
||
| 286 | |||
| 287 | /** |
||
| 288 | * @param int $poll_option_total |
||
| 289 | * @param int $poll_total |
||
| 290 | * @return float|int |
||
| 291 | */ |
||
| 292 | private function calculate_option_percent($poll_option_total, $poll_total) |
||
| 293 | { |
||
| 294 | return ($poll_total > 0) ? $poll_option_total / $poll_total : 0; |
||
| 295 | } |
||
| 296 | |||
| 297 | /** |
||
| 298 | * @param int $poll_option_total |
||
| 299 | * @param int $poll_most |
||
| 300 | * @return float|int |
||
| 301 | */ |
||
| 302 | private function calculate_option_percent_rel($poll_option_total, $poll_most) |
||
| 303 | { |
||
| 304 | return ($poll_most > 0) ? $poll_option_total / $poll_most : 0; |
||
| 305 | } |
||
| 306 | |||
| 307 | /** |
||
| 308 | * @param int $poll_option_total |
||
| 309 | * @param int $poll_most |
||
| 310 | * @return bool |
||
| 311 | */ |
||
| 312 | private function is_most_voted($poll_option_total, $poll_most) |
||
| 313 | { |
||
| 314 | return ($poll_option_total > 0 && $poll_option_total == $poll_most) ? true : false; |
||
| 315 | } |
||
| 316 | |||
| 317 | /** |
||
| 318 | * @param int $poll_max_options |
||
| 319 | * @return bool |
||
| 320 | */ |
||
| 321 | private function poll_is_multiple_choice($poll_max_options) |
||
| 324 | } |
||
| 325 | |||
| 326 | /** |
||
| 327 | * @param int $poll_length |
||
| 328 | * @param int $poll_end |
||
| 329 | * @return string |
||
| 330 | */ |
||
| 331 | private function get_poll_length_lang($poll_length, $poll_end) |
||
| 334 | } |
||
| 335 | |||
| 336 | /** |
||
| 337 | * @param bool $s_can_vote |
||
| 338 | * @param array $cur_voted_id |
||
| 339 | * @return bool |
||
| 340 | */ |
||
| 341 | private function show_results($s_can_vote, array $cur_voted_id) |
||
| 344 | } |
||
| 345 | } |
||
| 346 |