| Conditions | 5 |
| Paths | 12 |
| Total Lines | 52 |
| Code Lines | 36 |
| 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 |
||
| 158 | public function renderOptionFormTray($pid = 0) |
||
| 159 | { |
||
| 160 | \xoops_load('xoopsformloader'); |
||
| 161 | $pid = (int)$pid; |
||
| 162 | $barcolor_array = \XoopsLists::getImgListAsArray($GLOBALS['xoops']->path('modules/xoopspoll/assets/images/colorbars/')); |
||
| 163 | |||
| 164 | /** |
||
| 165 | * get all the options for this poll & add some blank options to allow adding more |
||
| 166 | */ |
||
| 167 | if (0 === $pid) { |
||
| 168 | $newOpts = (2 * Constants::NUM_ADDTL_OPTIONS); |
||
| 169 | } else { |
||
| 170 | $optionObjs = $this->getAllByPollId($pid); |
||
| 171 | $newOpts = Constants::NUM_ADDTL_OPTIONS; |
||
| 172 | } |
||
| 173 | $thisBarColorArray = $barcolor_array; |
||
| 174 | unset($thisBarColorArray['blank.gif']); |
||
| 175 | for ($i = 0; $i < $newOpts; ++$i) { |
||
| 176 | $thisObj = $this->create(); |
||
| 177 | $currentBar = \array_rand($thisBarColorArray); |
||
| 178 | unset($thisBarColorArray[$currentBar]); |
||
| 179 | $thisObj->setVar('option_color', $currentBar); |
||
| 180 | $optionObjs[] = $thisObj; |
||
| 181 | if (empty($thisBarColorArray)) { |
||
| 182 | $thisBarColorArray = $barcolor_array; |
||
| 183 | unset($thisBarColorArray['blank.gif']); |
||
| 184 | } |
||
| 185 | unset($thisObj); |
||
| 186 | } |
||
| 187 | /** |
||
| 188 | * add the options to the form |
||
| 189 | */ |
||
| 190 | $optionTray = new \XoopsFormElementTray(\_AM_XOOPSPOLL_POLLOPTIONS, ''); |
||
| 191 | $i = 0; |
||
| 192 | foreach ($optionObjs as $optObj) { |
||
| 193 | $colorSelect = new \XoopsFormSelect('', "option_color[{$i}]", $optObj->getVar('option_color')); |
||
| 194 | $colorSelect->addOptionArray($barcolor_array); |
||
| 195 | $colorSelect->setExtra("onchange='showImgSelected(\"option_color_image[{$i}]\", \"option_color[{$i}]\", \"modules/xoopspoll/assets/images/colorbars\", \"\", \"" . $GLOBALS['xoops']->url('') . "\")'"); |
||
| 196 | $colorLabel = new \XoopsFormLabel( |
||
| 197 | '', |
||
| 198 | "<img src='" . $GLOBALS['xoops']->url('modules/xoopspoll' . '/assets/images/colorbars/' . $optObj->getVar('option_color')) . "'" . " name='option_color_image[{$i}]'" . " id='option_color_image[{$i}]'" . " style='width: 30px; height: 15px;'" . " class='alignmiddle'" . " alt=''><br>" |
||
| 199 | ); |
||
| 200 | |||
| 201 | $optionTray->addElement(new \XoopsFormText('', "option_text[{$i}]", 50, 255, $optObj->getVar('option_text'))); |
||
| 202 | $optionTray->addElement(new \XoopsFormHidden("option_id[{$i}]", $optObj->getVar('option_id'))); |
||
| 203 | $optionTray->addElement($colorSelect); |
||
| 204 | $optionTray->addElement($colorLabel); |
||
| 205 | unset($colorSelect, $colorLabel); |
||
| 206 | ++$i; |
||
| 207 | } |
||
| 208 | |||
| 209 | return $optionTray; |
||
| 210 | } |
||
| 212 |
Let?s assume that you have a directory layout like this:
. |-- OtherDir | |-- Bar.php | `-- Foo.php `-- SomeDir `-- Foo.phpand let?s assume the following content of
Bar.php:If both files
OtherDir/Foo.phpandSomeDir/Foo.phpare 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.phpHowever, as
OtherDir/Foo.phpdoes 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: