Conditions | 19 |
Paths | > 20000 |
Total Lines | 79 |
Code Lines | 53 |
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); |
||
100 | public function getForm($action = false) |
||
101 | { |
||
102 | $helper = \XoopsModules\Wgevents\Helper::getInstance(); |
||
103 | |||
104 | $fieldHandler = $helper->getHandler('Field'); |
||
105 | |||
106 | if (!$action) { |
||
107 | $action = $_SERVER['REQUEST_URI']; |
||
108 | } |
||
109 | $isAdmin = (\is_object($GLOBALS['xoopsUser']) && \is_object($GLOBALS['xoopsModule'])) ? $GLOBALS['xoopsUser']->isAdmin($GLOBALS['xoopsModule']->mid()) : false; |
||
110 | // Title |
||
111 | $title = $this->isNew() ? \_AM_WGEVENTS_FIELD_ADD : \_AM_WGEVENTS_FIELD_EDIT; |
||
112 | // Get Theme Form |
||
113 | \xoops_load('XoopsFormLoader'); |
||
114 | $form = new \XoopsThemeForm($title, 'form', $action, 'post', true); |
||
115 | $form->setExtra('enctype="multipart/form-data"'); |
||
116 | // Form Text fdCaption |
||
117 | $form->addElement(new \XoopsFormText(\_AM_WGEVENTS_FIELD_CAPTION, 'caption', 50, 255, $this->getVar('caption')), true); |
||
118 | // Form Editor TextArea fdDesc |
||
119 | $form->addElement(new \XoopsFormTextArea(\_AM_WGEVENTS_FIELD_DESC, 'desc', $this->getVar('desc', 'e'), 10, 47)); |
||
120 | // Form Select fdType |
||
121 | $formelementsHandler = new Forms\FormelementsHandler(); |
||
122 | $atTypeSelect = new \XoopsFormSelect(\_AM_WGEVENTS_FIELD_TYPE, 'type', $this->getVar('type'), 5); |
||
123 | $atTypeSelect->addOptionArray($formelementsHandler->getElementsCollection()); |
||
124 | $form->addElement($atTypeSelect); |
||
125 | // Form Editor TextArea fdValues |
||
126 | $atValuesText = ''; |
||
127 | if (!$this->isNew()) { |
||
128 | $fdValues = (string)$this->getVar('values'); |
||
129 | if ('' !== $fdValues) { |
||
130 | $atValuesText = \implode("\n", \unserialize($fdValues, ['allowed_classes' => false])); |
||
131 | } |
||
132 | } |
||
133 | $form->addElement(new \XoopsFormTextArea(\_AM_WGEVENTS_FIELD_VALUE, 'values', $atValuesText, 5, 47)); |
||
134 | // Form Text fdPlaceholder |
||
135 | $form->addElement(new \XoopsFormText(\_AM_WGEVENTS_FIELD_PLACEHOLDER, 'placeholder', 50, 255, $this->getVar('placeholder'))); |
||
136 | // Form Radio Yes/No fdRequired |
||
137 | $fdRequired = $this->isNew() ? 0 : $this->getVar('required'); |
||
138 | $form->addElement(new \XoopsFormRadioYN(\_AM_WGEVENTS_FIELD_REQUIRED, 'required', $fdRequired)); |
||
139 | // Form Radio Yes/No fdDefault |
||
140 | $fdDefault = $this->isNew() ? 0 : $this->getVar('default'); |
||
141 | $form->addElement(new \XoopsFormRadioYN(\_AM_WGEVENTS_FIELD_DEFAULT, 'default', $fdDefault)); |
||
142 | // Form Radio Yes/No fdPrint |
||
143 | $fdPrint = $this->isNew() ? 0 : $this->getVar('print'); |
||
144 | $form->addElement(new \XoopsFormRadioYN(\_MA_WGEVENTS_PRINT, 'print', $fdPrint)); |
||
145 | // Form Radio Yes/No fdDisplayDesc |
||
146 | $fdDisplayDesc = $this->isNew() ? 1 : $this->getVar('display_desc'); |
||
147 | $form->addElement(new \XoopsFormRadioYN(\_AM_WGEVENTS_FIELD_DISPLAY_DESC, 'display_desc', $fdDisplayDesc)); |
||
148 | // Form Radio Yes/No fdDisplayValues |
||
149 | $fdDisplayValues = $this->isNew() ? 1 : $this->getVar('display_values'); |
||
150 | $form->addElement(new \XoopsFormRadioYN(\_AM_WGEVENTS_FIELD_DISPLAY_VALUES, 'display_values', $fdDisplayValues)); |
||
151 | // Form Radio Yes/No fdDisplayPlaceholder |
||
152 | $fdDisplayPlaceholder = $this->isNew() ? 1 : $this->getVar('display_placeholder'); |
||
153 | $form->addElement(new \XoopsFormRadioYN(\_AM_WGEVENTS_FIELD_DISPLAY_PLACEHOLDER, 'display_placeholder', $fdDisplayPlaceholder)); |
||
154 | // Form Text fdWeight |
||
155 | $fdWeight = $this->isNew() ? $fieldHandler->getNextWeight() : $this->getVar('weight'); |
||
156 | $form->addElement(new \XoopsFormText(\_MA_WGEVENTS_WEIGHT, 'weight', 50, 255, $fdWeight)); |
||
157 | // Form Select Status fdStatus |
||
158 | $fdStatus = $this->isNew() ? Constants::STATUS_OFFLINE : $this->getVar('status'); |
||
159 | $atStatusSelect = new \XoopsFormRadio(\_MA_WGEVENTS_STATUS, 'status', $fdStatus); |
||
160 | $atStatusSelect->addOption(Constants::STATUS_OFFLINE, \_MA_WGEVENTS_STATUS_OFFLINE); |
||
161 | $atStatusSelect->addOption(Constants::STATUS_ONLINE, \_MA_WGEVENTS_STATUS_ONLINE); |
||
162 | $form->addElement($atStatusSelect); |
||
163 | // Form Text Date Select fdDatecreated |
||
164 | $fdDatecreated = $this->isNew() ? \time() : $this->getVar('datecreated'); |
||
165 | $form->addElement(new \XoopsFormTextDateSelect(\_MA_WGEVENTS_DATECREATED, 'datecreated', '', $fdDatecreated)); |
||
166 | // Form Select User fdSubmitter |
||
167 | $uidCurrent = \is_object($GLOBALS['xoopsUser']) ? $GLOBALS['xoopsUser']->uid() : 0; |
||
168 | $fdSubmitter = $this->isNew() ? $uidCurrent : $this->getVar('submitter'); |
||
169 | $form->addElement(new \XoopsFormSelectUser(\_MA_WGEVENTS_SUBMITTER, 'submitter', false, $fdSubmitter)); |
||
170 | // Form Text fdCustom |
||
171 | $fdCustom = $this->isNew() ? 1 : $this->getVar('custom'); |
||
172 | $form->addElement(new \XoopsFormHidden('custom', $fdCustom)); |
||
173 | // To Save |
||
174 | $form->addElement(new \XoopsFormHidden('op', 'save')); |
||
175 | $form->addElement(new \XoopsFormHidden('start', $this->start)); |
||
176 | $form->addElement(new \XoopsFormHidden('limit', $this->limit)); |
||
177 | $form->addElement(new \XoopsFormButtonTray('', \_SUBMIT, 'submit', '', false)); |
||
178 | return $form; |
||
179 | } |
||
233 |
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: