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 declare(strict_types=1); |
||
167 | public function assignResults(\XoopsTpl $tpl): void |
||
168 | { |
||
169 | $myts = \MyTextSanitizer::getInstance(); |
||
170 | $xuEndTimestamp = \xoops_getUserTimestamp($this->pollObj->getVar('end_time')); |
||
171 | $xuEndFormatted = \ucfirst(\date(_MEDIUMDATESTRING, (int)$xuEndTimestamp)); |
||
172 | $xuStartTimestamp = \xoops_getUserTimestamp($this->pollObj->getVar('start_time')); |
||
173 | $xuStartFormatted = \ucfirst(\date(_MEDIUMDATESTRING, (int)$xuStartTimestamp)); |
||
174 | $options = []; |
||
175 | |||
176 | // $logHandler = $this->helper->getHandler('Log'); |
||
177 | $criteria = new \CriteriaCompo(); |
||
178 | $criteria->add(new \Criteria('poll_id', $this->pollObj->getVar('poll_id'), '=')); |
||
179 | $criteria->setSort('option_id'); |
||
180 | $optObjsArray = $this->optionHandler->getAll($criteria); |
||
181 | $total = $this->pollObj->getVar('votes'); |
||
182 | $i = 0; |
||
183 | foreach ($optObjsArray as $optObj) { |
||
184 | $optionVars = $optObj->getValues(); |
||
185 | $percent = ($total > 0) ? (100 * $optionVars['option_count'] / $total) : 0; |
||
186 | if ($percent > 0) { |
||
187 | $width = (int)($percent * 2); |
||
188 | $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 . "%'>"; |
||
189 | } else { |
||
190 | $options[$i]['image'] = ''; |
||
191 | } |
||
192 | |||
193 | /* setup module config handler - required since this is called by newbb too */ |
||
194 | /** @var \XoopsModuleHandler $moduleHandler */ |
||
195 | $moduleHandler = \xoops_getHandler('module'); |
||
196 | /** @var \XoopsConfigHandler $configHandler */ |
||
197 | $configHandler = \xoops_getHandler('config'); |
||
198 | $xp_module = $moduleHandler->getByDirname('xoopspoll'); |
||
199 | $module_id = $xp_module->getVar('mid'); |
||
200 | $xp_config = $configHandler->getConfigsByCat(0, $module_id); |
||
201 | |||
202 | if ($xp_config['disp_vote_nums']) { |
||
203 | $options[$i]['percent'] = \sprintf(' %01.1f%% (%d)', $percent, $optionVars['option_count']); |
||
204 | } else { |
||
205 | $options[$i]['percent'] = \sprintf(' %01.1f%%', $percent); |
||
206 | } |
||
207 | $options[$i]['text'] = $optionVars['option_text']; |
||
208 | $options[$i]['total'] = $optionVars['option_count']; |
||
209 | $options[$i]['value'] = (int)$percent; |
||
210 | ++$i; |
||
211 | unset($optionVars); |
||
212 | } |
||
213 | $uid = (isset($GLOBALS['xoopsUser']) |
||
214 | && \is_object($GLOBALS['xoopsUser'])) ? $GLOBALS['xoopsUser']->getVar('uid') : 0; |
||
215 | $vote = null; |
||
216 | if (!$this->pollObj->hasExpired() && $this->pollObj->isAllowedToVote() |
||
217 | && !$this->logHandler->hasVoted($this->pollObj->getVar('poll_id'), \xoops_getenv('REMOTE_ADDR'), $uid)) { |
||
218 | $vote = "<a href='" . $GLOBALS['xoops']->url('modules/xoopspoll/index.php') . '?poll_id=' . $this->pollObj->getVar('poll_id') . "'>" . \_MD_XOOPSPOLL_VOTE_NOW . '</a>'; |
||
219 | } |
||
220 | if ($xp_config['disp_vote_nums']) { |
||
221 | $totalVotes = \sprintf(\_MD_XOOPSPOLL_TOTALVOTES, $total); |
||
222 | $totalVoters = \sprintf(\_MD_XOOPSPOLL_TOTALVOTERS, $this->pollObj->getVar('voters')); |
||
223 | } else { |
||
224 | $totalVotes = $totalVoters = ''; |
||
225 | } |
||
226 | |||
227 | $tpl->assign('poll', [ |
||
228 | 'question' => \htmlspecialchars($this->pollObj->getVar('question'), \ENT_QUOTES | \ENT_HTML5), |
||
229 | 'end_text' => $xuEndFormatted, |
||
230 | 'start_text' => $xuStartFormatted, |
||
231 | 'totalVotes' => $totalVotes, |
||
232 | 'totalVoters' => $totalVoters, |
||
233 | 'vote' => $vote, |
||
234 | 'options' => $options, |
||
235 | 'description' => $this->pollObj->getVar('description'), //allow html |
||
236 | ]); |
||
239 |
Let?s assume that you have a directory layout like this:
and let?s assume the following content of
Bar.php
:If both files
OtherDir/Foo.php
andSomeDir/Foo.php
are loaded in the same runtime, you will see a PHP error such as the following:PHP Fatal error: Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php
However, as
OtherDir/Foo.php
does not necessarily have to be loaded and the error is only triggered if it is loaded beforeOtherDir/Bar.php
, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias: