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