| Conditions | 11 |
| Paths | 144 |
| Total Lines | 69 |
| Code Lines | 54 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 0 | ||
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | <?php |
||
| 178 | public function assignResults(\XoopsTpl $tpl) |
||
| 179 | { |
||
| 180 | $myts = \MyTextSanitizer::getInstance(); |
||
| 181 | $xuEndTimestamp = \xoops_getUserTimestamp($this->pollObj->getVar('end_time')); |
||
| 182 | $xuEndFormatted = \ucfirst(\date(_MEDIUMDATESTRING, $xuEndTimestamp)); |
||
| 183 | $xuStartTimestamp = \xoops_getUserTimestamp($this->pollObj->getVar('start_time')); |
||
| 184 | $xuStartFormatted = \ucfirst(\date(_MEDIUMDATESTRING, $xuStartTimestamp)); |
||
| 185 | |||
| 186 | // $logHandler = Xoopspoll\Helper::getInstance()->getHandler('Log'); |
||
| 187 | $criteria = new \CriteriaCompo(); |
||
| 188 | $criteria->add(new \Criteria('poll_id', $this->pollObj->getVar('poll_id'), '=')); |
||
| 189 | $criteria->setSort('option_id'); |
||
| 190 | $optObjsArray = $this->optionHandler->getAll($criteria); |
||
| 191 | $total = $this->pollObj->getVar('votes'); |
||
| 192 | $i = 0; |
||
| 193 | foreach ($optObjsArray as $optObj) { |
||
| 194 | $optionVars = $optObj->getValues(); |
||
| 195 | $percent = ($total > 0) ? (100 * $optionVars['option_count'] / $total) : 0; |
||
| 196 | if ($percent > 0) { |
||
| 197 | $width = (int)($percent * 2); |
||
| 198 | $options[$i]['image'] = "<img src='" . $GLOBALS['xoops']->url("modules/xoopspoll/assets/images/colorbars/{$optionVars['option_color']}'") . " style='height: 14px; width: {$width}px; vertical-align: middle;' alt='" . (int)$percent . "%'>"; |
||
| 199 | } else { |
||
| 200 | $options[$i]['image'] = ''; |
||
| 201 | } |
||
| 202 | |||
| 203 | /* setup module config handler - required since this is called by newbb too */ |
||
| 204 | /** @var \XoopsModuleHandler $moduleHandler */ |
||
| 205 | $moduleHandler = \xoops_getHandler('module'); |
||
| 206 | $configHandler = \xoops_getHandler('config'); |
||
| 207 | $xp_module = $moduleHandler->getByDirname('xoopspoll'); |
||
| 208 | $module_id = $xp_module->getVar('mid'); |
||
| 209 | $xp_config = $configHandler->getConfigsByCat(0, $module_id); |
||
| 210 | |||
| 211 | if ($xp_config['disp_vote_nums']) { |
||
| 212 | $options[$i]['percent'] = \sprintf(' %01.1f%% (%d)', $percent, $optionVars['option_count']); |
||
| 213 | } else { |
||
| 214 | $options[$i]['percent'] = \sprintf(' %01.1f%%', $percent); |
||
| 215 | } |
||
| 216 | $options[$i]['text'] = $optionVars['option_text']; |
||
| 217 | $options[$i]['total'] = $optionVars['option_count']; |
||
| 218 | $options[$i]['value'] = (int) $percent; |
||
| 219 | ++$i; |
||
| 220 | unset($optionVars); |
||
| 221 | } |
||
| 222 | $uid = (isset($GLOBALS['xoopsUser']) |
||
| 223 | && \is_object($GLOBALS['xoopsUser'])) ? $GLOBALS['xoopsUser']->getVar('uid') : 0; |
||
| 224 | $vote = null; |
||
| 225 | if (!$this->pollObj->hasExpired() && $this->pollObj->isAllowedToVote() |
||
| 226 | && !$this->logHandler->hasVoted($this->pollObj->getVar('poll_id'), \xoops_getenv('REMOTE_ADDR'), $uid)) { |
||
| 227 | $vote = "<a href='" . $GLOBALS['xoops']->url('modules/xoopspoll/index.php') . '?poll_id=' . $this->pollObj->getVar('poll_id') . "'>" . \_MD_XOOPSPOLL_VOTE_NOW . '</a>'; |
||
| 228 | } |
||
| 229 | if ($xp_config['disp_vote_nums']) { |
||
| 230 | $totalVotes = \sprintf(\_MD_XOOPSPOLL_TOTALVOTES, $total); |
||
| 231 | $totalVoters = \sprintf(\_MD_XOOPSPOLL_TOTALVOTERS, $this->pollObj->getVar('voters')); |
||
| 232 | } else { |
||
| 233 | $totalVotes = $totalVoters = ''; |
||
| 234 | } |
||
| 235 | |||
| 236 | $tpl->assign( |
||
| 237 | 'poll', |
||
| 238 | [ |
||
| 239 | 'question' => $myts->htmlSpecialChars($this->pollObj->getVar('question')), |
||
| 240 | 'end_text' => $xuEndFormatted, |
||
| 241 | 'start_text' => $xuStartFormatted, |
||
| 242 | 'totalVotes' => $totalVotes, |
||
| 243 | 'totalVoters' => $totalVoters, |
||
| 244 | 'vote' => $vote, |
||
| 245 | 'options' => $options, |
||
| 246 | 'description' => $this->pollObj->getVar('description'), //allow html |
||
| 247 | ] |
||
| 251 |