Conditions | 23 |
Paths | 209 |
Total Lines | 149 |
Code Lines | 100 |
Lines | 0 |
Ratio | 0 % |
Changes | 1 | ||
Bugs | 0 | Features | 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 declare(strict_types=1); |
||
48 | function xoopspollBlockSinglepollShow(mixed $options): array |
||
49 | { |
||
50 | $block = []; |
||
51 | |||
52 | /** @var \XoopsConfigHandler $configHandler */ |
||
53 | $configHandler = xoops_getHandler('config'); |
||
54 | $pollHandler = Helper::getInstance()->getHandler('Poll'); |
||
55 | /** @var \XoopsModuleHandler $moduleHandler */ |
||
56 | $moduleHandler = xoops_getHandler('module'); |
||
57 | $thisModule = $moduleHandler->getByDirname('xoopspoll'); |
||
58 | $thisModuleConfig = $configHandler->getConfigsByCat(0, $thisModule->getVar('mid')); |
||
59 | |||
60 | /* if admin hasn't initialized block then we'll pick a poll for them |
||
61 | * provided that one exists in the database |
||
62 | */ |
||
63 | if (0 === $options[1]) { |
||
64 | $criteria = null; |
||
65 | /** |
||
66 | * check to see if we want to include polls created with forum (newbb) |
||
67 | */ |
||
68 | if ($thisModuleConfig['hide_forum_polls'] |
||
69 | && ($thisModule instanceof \XoopsModule) |
||
70 | && $thisModule->isactive()) { |
||
71 | $newbbModule = $moduleHandler->getByDirname('newbb'); |
||
72 | if ($newbbModule instanceof \XoopsModule && $newbbModule->isactive()) { |
||
73 | /** @var Newbb\TopicHandler $topicHandler */ |
||
74 | $topicHandler = Newbb\Helper::getInstance()->getHandler('Topic'); |
||
75 | $tFields = ['topic_id', 'poll_id']; |
||
76 | $tArray = $topicHandler->getAll(new \Criteria('topic_haspoll', 0, '>'), $tFields, false); |
||
77 | if (!empty($tArray)) { |
||
78 | $tcriteria = []; |
||
79 | foreach ($tArray as $t) { |
||
80 | $tcriteria[] = $t['poll_id']; |
||
81 | } |
||
82 | if (!empty($tcriteria)) { |
||
83 | $tstring = '(' . implode(',', $tcriteria) . ')'; |
||
84 | $criteria = new \Criteria('poll_id', $tstring, 'NOT IN'); |
||
85 | } |
||
86 | } |
||
87 | unset($topicHandler, $tFields, $tArray); |
||
88 | } |
||
89 | unset($newbbModule); |
||
90 | } |
||
91 | |||
92 | if ($pollHandler->getCount($criteria) > 0) { |
||
93 | $pollIdArray = $pollHandler->getIds(); |
||
94 | $thisId = array_shift($pollIdArray); |
||
95 | $pollObj = $pollHandler->get($thisId); |
||
96 | } else { |
||
97 | return $block; |
||
98 | } |
||
99 | } else { |
||
100 | $pollObj = $pollHandler->get((int)$options[1]); |
||
101 | } |
||
102 | |||
103 | if ($pollObj instanceof Poll) { |
||
104 | if ((1 === $options[0]) || !$pollObj->hasExpired()) { |
||
105 | $block['langVote'] = _MD_XOOPSPOLL_VOTE; |
||
106 | $block['langResults'] = _MD_XOOPSPOLL_RESULTS; |
||
107 | $block['langExpires'] = _MB_XOOPSPOLL_WILLEXPIRE; |
||
108 | $block['langExpired'] = _MB_XOOPSPOLL_HASEXPIRED; |
||
109 | $block['langComments'] = _MB_XOOPSPOLL_COMMENTS; |
||
110 | $block['langComment'] = _MB_XOOPSPOLL_COMMENT; |
||
111 | $block['showResultsLink'] = $options[2]; |
||
112 | $block['asList'] = $options[3]; |
||
113 | $block['thisModuleDir'] = 'xoopspoll'; |
||
114 | $block['url'] = 'http' . (!empty($_SERVER['HTTPS']) ? 's' : '') . '://' . $_SERVER['SERVER_NAME'] . $_SERVER['REQUEST_URI']; |
||
115 | $block['dispVotes'] = $thisModuleConfig['disp_vote_nums']; |
||
116 | |||
117 | $optionHandler = Helper::getInstance()->getHandler('Option'); |
||
118 | |||
119 | $pollVars = $pollObj->getValues(); |
||
120 | $criteria = new \CriteriaCompo(); |
||
121 | $criteria->add(new \Criteria('poll_id', $pollVars['poll_id'], '=')); |
||
122 | $criteria->setSort('option_id'); |
||
123 | $optionsObjArray = $optionHandler->getAll($criteria); |
||
124 | // $optionsObjArray = $optionHandler->getAll($criteria, null, false); |
||
125 | |||
126 | if (Constants::MULTIPLE_SELECT_POLL === $pollVars['multiple']) { |
||
127 | $pollOptionType = 'checkbox'; |
||
128 | $pollOptionName = 'option_id[]'; |
||
129 | } else { |
||
130 | $pollOptionType = 'radio'; |
||
131 | $pollOptionName = 'option_id'; |
||
132 | } |
||
133 | |||
134 | $uid = 0; |
||
135 | if (isset($GLOBALS['xoopsUser']) && ($GLOBALS['xoopsUser'] instanceof \XoopsUser)) { |
||
136 | $uid = $GLOBALS['xoopsUser']->getVar('uid'); |
||
137 | } |
||
138 | |||
139 | $totalVotes = $pollVars['votes']; |
||
140 | $logHandler = Helper::getInstance()->getHandler('Log'); |
||
141 | $hasVoted = (bool)$logHandler->hasVoted($pollVars['poll_id'], xoops_getenv('REMOTE_ADDR'), $uid); |
||
142 | $canVote = (!$hasVoted) && $pollObj->isAllowedToVote(); |
||
143 | $pollOptionsArray = []; |
||
144 | foreach ($optionsObjArray as $optionObj) { |
||
145 | $percent = ($totalVotes > 0) ? (100 * $optionObj->getVar('option_count') / $totalVotes) : 0; |
||
146 | // $percent = ($totalVotes > 0) ? ceil(100 * $optionObj->getVar('option_count') / $totalVotes) . '%' : '0%'; |
||
147 | /*@TODO:: Change block templates to use Smarty html_options to support this... then comment |
||
148 | out old $pollOptionsArray assignment |
||
149 | $pollOptionsArray[] = array('options' => array($optionObj['option_id'] => $optionObj['option_text']), |
||
150 | 'count' => $optionObj['option_count'], |
||
151 | 'percent' => $percent, |
||
152 | 'color' => $optionObj['option_color'] |
||
153 | ); */ |
||
154 | $pollOptionsArray[] = [ |
||
155 | 'id' => $optionObj->getVar('option_id'), |
||
156 | 'text' => $optionObj->getVar('option_text'), |
||
157 | 'count' => $optionObj->getVar('option_count'), |
||
158 | 'percent' => sprintf(' %01.1f%%', $percent), |
||
159 | 'color' => $optionObj->getVar('option_color'), |
||
160 | ]; |
||
161 | } |
||
162 | |||
163 | $xuEndTimestamp = xoops_getUserTimestamp($pollObj->getVar('end_time')); |
||
164 | $xuEndFormattedTime = ucfirst(date(_MEDIUMDATESTRING, (int)$xuEndTimestamp)); |
||
165 | |||
166 | $isVisible = true === $pollObj->isResultVisible(); |
||
167 | |||
168 | $multiple = (bool)$pollVars['multiple']; |
||
169 | $multiLimit = (int)$pollVars['multilimit']; |
||
170 | $lang_multi = ''; |
||
171 | if ($multiple && ($multiLimit > 0)) { |
||
172 | $lang_multi = sprintf(_MB_XOOPSPOLL_MULTITEXT, $multiLimit); |
||
173 | } |
||
174 | |||
175 | $block['id'] = $pollVars['poll_id']; |
||
176 | $block['visible'] = $isVisible; |
||
177 | $block['question'] = $pollVars['question']; |
||
178 | $block['multiple'] = $multiple; |
||
179 | $block['lang_multi'] = $lang_multi; |
||
180 | $block['optionType'] = $pollOptionType; |
||
181 | $block['optionName'] = $pollOptionName; |
||
182 | $block['options'] = $pollOptionsArray; |
||
183 | $block['hasExpired'] = $pollObj->hasExpired(); |
||
184 | $block['votes'] = $pollVars['votes']; |
||
185 | $block['hasVoted'] = $hasVoted; |
||
186 | $block['canVote'] = $canVote; |
||
187 | $block['totalVotes'] = sprintf(_MD_XOOPSPOLL_TOTALVOTES, $totalVotes); |
||
188 | $block['endTime'] = $xuEndFormattedTime; // formatted output for current user |
||
189 | $block['comments'] = $pollObj->getComments($pollVars['poll_id']); |
||
|
|||
190 | $block['commentMode'] = Utility::commentMode(); |
||
191 | |||
192 | unset($optionsObjArray, $pollOptionsArray, $pollObj, $pollVars, $timeArray); |
||
193 | } |
||
194 | } |
||
195 | |||
196 | return $block; |
||
197 | } |
||
351 |
This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.
If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.