Conditions | 31 |
Paths | 9312 |
Total Lines | 137 |
Code Lines | 101 |
Lines | 15 |
Ratio | 10.95 % |
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 |
||
265 | public function doAlter($confirm, $mode, $msg = '') |
||
266 | { |
||
267 | $conf = $this->conf; |
||
268 | $misc = $this->misc; |
||
269 | $lang = $this->lang; |
||
270 | $action = $this->action; |
||
271 | $data = $misc->getDatabaseAccessor(); |
||
272 | |||
273 | if (!isset($_REQUEST['username'])) { |
||
274 | $_REQUEST['username'] = []; |
||
275 | } |
||
276 | |||
277 | if (!isset($_REQUEST['groupname'])) { |
||
278 | $_REQUEST['groupname'] = []; |
||
279 | } |
||
280 | |||
281 | if (!isset($_REQUEST['privilege'])) { |
||
282 | $_REQUEST['privilege'] = []; |
||
283 | } |
||
284 | |||
285 | if ($confirm) { |
||
286 | // Get users from the database |
||
287 | $users = $data->getUsers(); |
||
288 | // Get groups from the database |
||
289 | $groups = $data->getGroups(); |
||
290 | |||
291 | $this->printTrail($_REQUEST['subject']); |
||
292 | |||
293 | switch ($mode) { |
||
294 | case 'grant': |
||
295 | $this->printTitle($lang['strgrant'], 'pg.privilege.grant'); |
||
296 | break; |
||
297 | case 'revoke': |
||
298 | $this->printTitle($lang['strrevoke'], 'pg.privilege.revoke'); |
||
299 | break; |
||
300 | } |
||
301 | $this->printMsg($msg); |
||
302 | |||
303 | echo '<form action="' . SUBFOLDER . "/src/views/privileges.php\" method=\"post\">\n"; |
||
304 | echo "<table>\n"; |
||
305 | echo "<tr><th class=\"data left\">{$lang['strusers']}</th>\n"; |
||
306 | echo '<td class="data1"><select name="username[]" multiple="multiple" size="', min(6, $users->recordCount()), "\">\n"; |
||
307 | while (!$users->EOF) { |
||
308 | $uname = htmlspecialchars($users->fields['usename']); |
||
309 | echo "<option value=\"{$uname}\"", |
||
310 | in_array($users->fields['usename'], $_REQUEST['username']) ? ' selected="selected"' : '', ">{$uname}</option>\n"; |
||
311 | $users->moveNext(); |
||
312 | } |
||
313 | echo "</select></td></tr>\n"; |
||
314 | echo "<tr><th class=\"data left\">{$lang['strgroups']}</th>\n"; |
||
315 | echo "<td class=\"data1\">\n"; |
||
316 | echo '<input type="checkbox" id="public" name="public"', (isset($_REQUEST['public']) ? ' checked="checked"' : ''), " /><label for=\"public\">PUBLIC</label>\n"; |
||
317 | // Only show groups if there are groups! |
||
318 | View Code Duplication | if ($groups->recordCount() > 0) { |
|
319 | echo '<br /><select name="groupname[]" multiple="multiple" size="', min(6, $groups->recordCount()), "\">\n"; |
||
320 | while (!$groups->EOF) { |
||
321 | $gname = htmlspecialchars($groups->fields['groname']); |
||
322 | echo "<option value=\"{$gname}\"", |
||
323 | in_array($groups->fields['groname'], $_REQUEST['groupname']) ? ' selected="selected"' : '', ">{$gname}</option>\n"; |
||
324 | $groups->moveNext(); |
||
325 | } |
||
326 | echo "</select>\n"; |
||
327 | } |
||
328 | echo "</td></tr>\n"; |
||
329 | echo "<tr><th class=\"data left required\">{$lang['strprivileges']}</th>\n"; |
||
330 | echo "<td class=\"data1\">\n"; |
||
331 | foreach ($data->privlist[$_REQUEST['subject']] as $v) { |
||
332 | $v = htmlspecialchars($v); |
||
333 | echo "<input type=\"checkbox\" id=\"privilege[$v]\" name=\"privilege[$v]\"", |
||
334 | isset($_REQUEST['privilege'][$v]) ? ' checked="checked"' : '', " /><label for=\"privilege[$v]\">{$v}</label><br />\n"; |
||
335 | } |
||
336 | echo "</td></tr>\n"; |
||
337 | // Grant option |
||
338 | if ($data->hasGrantOption()) { |
||
339 | echo "<tr><th class=\"data left\">{$lang['stroptions']}</th>\n"; |
||
340 | echo "<td class=\"data1\">\n"; |
||
341 | if ($mode == 'grant') { |
||
342 | echo '<input type="checkbox" id="grantoption" name="grantoption"', |
||
343 | isset($_REQUEST['grantoption']) ? ' checked="checked"' : '', " /><label for=\"grantoption\">GRANT OPTION</label>\n"; |
||
344 | } elseif ($mode == 'revoke') { |
||
345 | echo '<input type="checkbox" id="grantoption" name="grantoption"', |
||
346 | isset($_REQUEST['grantoption']) ? ' checked="checked"' : '', " /><label for=\"grantoption\">GRANT OPTION FOR</label><br />\n"; |
||
347 | echo '<input type="checkbox" id="cascade" name="cascade"', |
||
348 | isset($_REQUEST['cascade']) ? ' checked="checked"' : '', " /><label for=\"cascade\">CASCADE</label><br />\n"; |
||
349 | } |
||
350 | echo "</td></tr>\n"; |
||
351 | } |
||
352 | echo "</table>\n"; |
||
353 | |||
354 | echo "<p><input type=\"hidden\" name=\"action\" value=\"save\" />\n"; |
||
355 | echo '<input type="hidden" name="mode" value="', htmlspecialchars($mode), "\" />\n"; |
||
356 | echo '<input type="hidden" name="subject" value="', htmlspecialchars($_REQUEST['subject']), "\" />\n"; |
||
357 | if (isset($_REQUEST[$_REQUEST['subject'] . '_oid'])) { |
||
358 | echo '<input type="hidden" name="', htmlspecialchars($_REQUEST['subject'] . '_oid'), |
||
359 | '" value="', htmlspecialchars($_REQUEST[$_REQUEST['subject'] . '_oid']), "\" />\n"; |
||
360 | } |
||
361 | |||
362 | echo '<input type="hidden" name="', htmlspecialchars($_REQUEST['subject']), |
||
363 | '" value="', htmlspecialchars($_REQUEST[$_REQUEST['subject']]), "\" />\n"; |
||
364 | if ($_REQUEST['subject'] == 'column') { |
||
365 | echo '<input type="hidden" name="table" value="', |
||
366 | htmlspecialchars($_REQUEST['table']), "\" />\n"; |
||
367 | } |
||
368 | |||
369 | echo $misc->form; |
||
370 | if ($mode == 'grant') { |
||
371 | echo "<input type=\"submit\" name=\"grant\" value=\"{$lang['strgrant']}\" />\n"; |
||
372 | } elseif ($mode == 'revoke') { |
||
373 | echo "<input type=\"submit\" name=\"revoke\" value=\"{$lang['strrevoke']}\" />\n"; |
||
374 | } |
||
375 | |||
376 | echo "<input type=\"submit\" name=\"cancel\" value=\"{$lang['strcancel']}\" /></p>"; |
||
377 | echo "</form>\n"; |
||
378 | } else { |
||
379 | // Determine whether object should be ref'd by name or oid. |
||
380 | View Code Duplication | if (isset($_REQUEST[$_REQUEST['subject'] . '_oid'])) { |
|
381 | $object = $_REQUEST[$_REQUEST['subject'] . '_oid']; |
||
382 | } else { |
||
383 | $object = $_REQUEST[$_REQUEST['subject']]; |
||
384 | } |
||
385 | |||
386 | if (isset($_REQUEST['table'])) { |
||
387 | $table = $_REQUEST['table']; |
||
388 | } else { |
||
389 | $table = null; |
||
390 | } |
||
391 | |||
392 | $status = $data->setPrivileges(($mode == 'grant') ? 'GRANT' : 'REVOKE', $_REQUEST['subject'], $object, |
||
393 | isset($_REQUEST['public']), $_REQUEST['username'], $_REQUEST['groupname'], array_keys($_REQUEST['privilege']), |
||
394 | isset($_REQUEST['grantoption']), isset($_REQUEST['cascade']), $table); |
||
395 | |||
396 | if ($status == 0) { |
||
397 | $this->doDefault($lang['strgranted']); |
||
398 | } elseif ($status == -3 || $status == -4) { |
||
399 | $this->doAlter(true, $_REQUEST['mode'], $lang['strgrantbad']); |
||
400 | } else { |
||
401 | $this->doAlter(true, $_REQUEST['mode'], $lang['strgrantfailed']); |
||
402 | } |
||
406 |
Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable: