Conditions | 25 |
Paths | 13056 |
Total Lines | 124 |
Code Lines | 80 |
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 |
||
127 | function profile_getUserForm(XoopsUser $user, ProfileProfile $profile = null, $action = false) |
||
128 | { |
||
129 | $xoops = Xoops::getInstance(); |
||
130 | |||
131 | if ($action === false) { |
||
132 | $action = $_SERVER['REQUEST_URI']; |
||
133 | } |
||
134 | |||
135 | $title = $user->isNew() ? _PROFILE_AM_ADDUSER : XoopsLocale::EDIT_PROFILE; |
||
136 | |||
137 | $form = new Xoops\Form\ThemeForm($title, 'userinfo', $action, 'post', true); |
||
138 | |||
139 | /* @var $profile_handler ProfileProfileHandler */ |
||
140 | $profile_handler = \Xoops::getModuleHelper('profile')->getHandler('profile'); |
||
141 | // Dynamic fields |
||
142 | if (!$profile) { |
||
143 | $profile = $profile_handler->getProfile($user->getVar('uid')); |
||
144 | } |
||
145 | // Get fields |
||
146 | $fields = $profile_handler->loadFields(); |
||
147 | |||
148 | // Get ids of fields that can be edited |
||
149 | $gperm_handler = $xoops->getHandlerGroupPermission(); |
||
150 | $editable_fields = $gperm_handler->getItemIds('profile_edit', $xoops->user->getGroups(), $xoops->module->getVar('mid')); |
||
151 | |||
152 | if ($user->isNew() || $xoops->user->isAdmin()) { |
||
153 | $elements[0][] = array( |
||
154 | 'element' => new Xoops\Form\Text(XoopsLocale::USERNAME, 'uname', 40, $xoops->user->isAdmin() ? 60 |
||
155 | : $xoops->getConfig('maxuname'), $user->getVar('uname', 'e')), 'required' => 1 |
||
156 | ); |
||
157 | $email_text = new Xoops\Form\Text('', 'email', 40, 160, $user->getVar('email')); |
||
158 | } else { |
||
159 | $elements[0][] = array('element' => new Xoops\Form\Label(XoopsLocale::USERNAME, $user->getVar('uname')), 'required' => 0); |
||
160 | $email_text = new Xoops\Form\Label('', $user->getVar('email')); |
||
161 | } |
||
162 | $email_tray = new Xoops\Form\ElementTray(XoopsLocale::EMAIL, '<br />'); |
||
163 | $email_tray->addElement($email_text, ($user->isNew() || $xoops->user->isAdmin()) ? 1 : 0); |
||
164 | $weights[0][] = 0; |
||
165 | $elements[0][] = array('element' => $email_tray, 'required' => 0); |
||
166 | $weights[0][] = 0; |
||
167 | |||
168 | if ($xoops->user->isAdmin() && $user->getVar('uid') != $xoops->user->getVar('uid')) { |
||
169 | //If the user is an admin and is editing someone else |
||
170 | $pwd_text = new Xoops\Form\Password('', 'password'); |
||
171 | $pwd_text2 = new Xoops\Form\Password('', 'vpass'); |
||
172 | $pwd_tray = new Xoops\Form\ElementTray(XoopsLocale::PASSWORD . '<br />' . XoopsLocale::TYPE_NEW_PASSWORD_TWICE_TO_CHANGE_IT); |
||
173 | $pwd_tray->addElement($pwd_text); |
||
174 | $pwd_tray->addElement($pwd_text2); |
||
175 | $elements[0][] = array('element' => $pwd_tray, 'required' => 0); //cannot set an element tray required |
||
176 | $weights[0][] = 0; |
||
177 | |||
178 | $level_radio = new Xoops\Form\Radio(_PROFILE_MA_USERLEVEL, 'level', $user->getVar('level')); |
||
179 | $level_radio->addOption(1, _PROFILE_MA_ACTIVE); |
||
180 | $level_radio->addOption(0, _PROFILE_MA_INACTIVE); |
||
181 | //$level_radio->addOption(-1, _PROFILE_MA_DISABLED); |
||
182 | $elements[0][] = array('element' => $level_radio, 'required' => 0); |
||
183 | $weights[0][] = 0; |
||
184 | } |
||
185 | |||
186 | $elements[0][] = array('element' => new Xoops\Form\Hidden('uid', $user->getVar('uid')), 'required' => 0); |
||
187 | $weights[0][] = 0; |
||
188 | $elements[0][] = array('element' => new Xoops\Form\Hidden('op', 'save'), 'required' => 0); |
||
189 | $weights[0][] = 0; |
||
190 | |||
191 | $cat_handler = \Xoops::getModuleHelper('profile')->getHandler('category'); |
||
192 | $categories = array(); |
||
193 | $all_categories = $cat_handler->getObjects(null, true, false); |
||
194 | $count_fields = count($fields); |
||
195 | /* @var ProfileField $field */ |
||
196 | foreach ($fields as $field) { |
||
197 | if (in_array($field->getVar('field_id'), $editable_fields)) { |
||
198 | // Set default value for user fields if available |
||
199 | if ($user->isNew()) { |
||
200 | $default = $field->getVar('field_default'); |
||
201 | if ($default !== '' && $default !== null) { |
||
202 | $user->setVar($field->getVar('field_name'), $default); |
||
203 | } |
||
204 | } |
||
205 | |||
206 | if ($profile->getVar($field->getVar('field_name'), 'n') === null) { |
||
207 | $default = $field->getVar('field_default', 'n'); |
||
208 | $profile->setVar($field->getVar('field_name'), $default); |
||
209 | } |
||
210 | |||
211 | $fieldinfo['element'] = $field->getEditElement($user, $profile); |
||
212 | $fieldinfo['required'] = $field->getVar('field_required'); |
||
213 | |||
214 | $key = @$all_categories[$field->getVar('cat_id')]['cat_weight'] * $count_fields + $field->getVar('cat_id'); |
||
215 | $elements[$key][] = $fieldinfo; |
||
216 | $weights[$key][] = $field->getVar('field_weight'); |
||
217 | $categories[$key] = @$all_categories[$field->getVar('cat_id')]; |
||
218 | } |
||
219 | } |
||
220 | |||
221 | if ($xoops->isUser() && $xoops->user->isAdmin()) { |
||
222 | $xoops->loadLanguage('admin', 'profile'); |
||
223 | $gperm_handler = $xoops->getHandlerGroupPermission(); |
||
224 | //If user has admin rights on groups |
||
225 | include_once $xoops->path('modules/system/constants.php'); |
||
226 | if ($gperm_handler->checkRight('system_admin', XOOPS_SYSTEM_GROUP, $xoops->user->getGroups(), 1)) { |
||
227 | //add group selection |
||
228 | $group_select = new Xoops\Form\SelectGroup(XoopsLocale::USER_GROUPS, 'groups', false, $user->getGroups(), 5, true); |
||
229 | $elements[0][] = array('element' => $group_select, 'required' => 0); |
||
230 | //set as latest; |
||
231 | $weights[0][] = $count_fields + 1; |
||
232 | } |
||
233 | } |
||
234 | |||
235 | ksort($elements); |
||
236 | foreach (array_keys($elements) as $k) { |
||
237 | array_multisort($weights[$k], SORT_ASC, array_keys($elements[$k]), SORT_ASC, $elements[$k]); |
||
238 | $title = isset($categories[$k]) ? $categories[$k]['cat_title'] : _PROFILE_MA_DEFAULT; |
||
239 | $desc = isset($categories[$k]) ? $categories[$k]['cat_description'] : ""; |
||
240 | //$form->addElement(new Xoops\Form\Label("<div class='break'>{$title}</div>", $desc), false); |
||
241 | $desc = ($desc != '' ? ' - ' . $desc : ''); |
||
242 | $form->insertBreak($title . $desc); |
||
243 | foreach (array_keys($elements[$k]) as $i) { |
||
244 | $form->addElement($elements[$k][$i]['element'], $elements[$k][$i]['required']); |
||
245 | } |
||
246 | } |
||
247 | |||
248 | $form->addElement(new Xoops\Form\Hidden('uid', $user->getVar('uid'))); |
||
249 | $form->addElement(new Xoops\Form\Button('', 'submit', XoopsLocale::SAVE_CHANGES, 'submit')); |
||
250 | return $form; |
||
251 | } |
||
252 |
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: