Conditions | 5 |
Paths | 12 |
Total Lines | 53 |
Code Lines | 37 |
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); |
||
153 | public function renderOptionFormTray(int $pid = 0): \XoopsFormElementTray |
||
154 | { |
||
155 | \xoops_load('xoopsformloader'); |
||
156 | $pid = (int)$pid; |
||
157 | $barcolor_array = \XoopsLists::getImgListAsArray($GLOBALS['xoops']->path('modules/xoopspoll/assets/images/colorbars/')); |
||
158 | $optionObjs = []; |
||
159 | |||
160 | /** |
||
161 | * get all the options for this poll & add some blank options to allow adding more |
||
162 | */ |
||
163 | if (0 === $pid) { |
||
164 | $newOpts = (2 * Constants::NUM_ADDTL_OPTIONS); |
||
165 | } else { |
||
166 | $optionObjs = $this->getAllByPollId($pid); |
||
167 | $newOpts = Constants::NUM_ADDTL_OPTIONS; |
||
168 | } |
||
169 | $thisBarColorArray = $barcolor_array; |
||
170 | unset($thisBarColorArray['blank.gif']); |
||
171 | for ($i = 0; $i < $newOpts; ++$i) { |
||
172 | $thisObj = $this->create(); |
||
173 | $currentBar = \array_rand($thisBarColorArray); |
||
174 | unset($thisBarColorArray[$currentBar]); |
||
175 | $thisObj->setVar('option_color', $currentBar); |
||
176 | $optionObjs[] = $thisObj; |
||
177 | if (empty($thisBarColorArray)) { |
||
178 | $thisBarColorArray = $barcolor_array; |
||
179 | unset($thisBarColorArray['blank.gif']); |
||
180 | } |
||
181 | unset($thisObj); |
||
182 | } |
||
183 | /** |
||
184 | * add the options to the form |
||
185 | */ |
||
186 | $optionTray = new \XoopsFormElementTray(\_AM_XOOPSPOLL_POLLOPTIONS, ''); |
||
187 | $i = 0; |
||
188 | foreach ($optionObjs as $optObj) { |
||
189 | $colorSelect = new \XoopsFormSelect('', "option_color[{$i}]", $optObj->getVar('option_color')); |
||
190 | $colorSelect->addOptionArray($barcolor_array); |
||
191 | $colorSelect->setExtra("onchange='showImgSelected(\"option_color_image[{$i}]\", \"option_color[{$i}]\", \"modules/xoopspoll/assets/images/colorbars\", \"\", \"" . $GLOBALS['xoops']->url('') . "\")'"); |
||
192 | $colorLabel = new \XoopsFormLabel( |
||
193 | '', |
||
194 | "<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>" |
||
195 | ); |
||
196 | |||
197 | $optionTray->addElement(new \XoopsFormText('', "option_text[{$i}]", 50, 255, $optObj->getVar('option_text'))); |
||
198 | $optionTray->addElement(new \XoopsFormHidden("option_id[{$i}]", $optObj->getVar('option_id'))); |
||
199 | $optionTray->addElement($colorSelect); |
||
200 | $optionTray->addElement($colorLabel); |
||
201 | unset($colorSelect, $colorLabel); |
||
202 | ++$i; |
||
203 | } |
||
204 | |||
205 | return $optionTray; |
||
206 | } |
||
208 |
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: