Conditions | 44 |
Paths | > 20000 |
Total Lines | 198 |
Code Lines | 131 |
Lines | 15 |
Ratio | 7.58 % |
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 |
||
298 | function recursiveTree($nodeId) |
||
299 | { |
||
300 | global $completTree, $ret_json, $listFoldersLimitedKeys, $listRestrictedFoldersForItemsKeys, $tree, $LANG, $last_visible_parent, $last_visible_parent_level, $SETTINGS; |
||
301 | |||
302 | // Load library |
||
303 | require_once $SETTINGS['cpassman_dir'].'/includes/libraries/protect/SuperGlobal/SuperGlobal.php'; |
||
304 | $superGlobal = new protect\SuperGlobal\SuperGlobal(); |
||
305 | |||
306 | // Prepare superGlobal variables |
||
307 | $session_forbiden_pfs = $superGlobal->get("forbiden_pfs", "SESSION"); |
||
308 | $session_groupes_visibles = $superGlobal->get("groupes_visibles", "SESSION"); |
||
309 | $session_list_restricted_folders_for_items = $superGlobal->get("list_restricted_folders_for_items", "SESSION"); |
||
310 | $session_user_id = $superGlobal->get("user_id", "SESSION"); |
||
311 | $session_login = $superGlobal->get("login", "SESSION"); |
||
312 | $session_user_read_only = $superGlobal->get("user_read_only", "SESSION"); |
||
313 | $session_no_access_folders = $superGlobal->get("no_access_folders", "SESSION"); |
||
314 | $session_list_folders_limited = $superGlobal->get("list_folders_limited", "SESSION"); |
||
315 | $session_read_only_folders = $superGlobal->get("read_only_folders", "SESSION"); |
||
316 | |||
317 | // Be sure that user can only see folders he/she is allowed to |
||
318 | if (in_array($completTree[$nodeId]->id, $session_forbiden_pfs) === false |
||
319 | || in_array($completTree[$nodeId]->id, $session_groupes_visibles) === true |
||
320 | || in_array($completTree[$nodeId]->id, $listFoldersLimitedKeys) === true |
||
321 | || in_array($completTree[$nodeId]->id, $listRestrictedFoldersForItemsKeys) === true |
||
322 | ) { |
||
323 | $displayThisNode = false; |
||
324 | $hide_node = false; |
||
325 | $nbChildrenItems = 0; |
||
326 | |||
327 | // Check if any allowed folder is part of the descendants of this node |
||
328 | $nodeDescendants = $tree->getDescendants($completTree[$nodeId]->id, true, false, true); |
||
329 | foreach ($nodeDescendants as $node) { |
||
330 | // manage tree counters |
||
331 | if (isset($SETTINGS['tree_counters']) === true && $SETTINGS['tree_counters'] === "1" |
||
332 | && in_array( |
||
333 | $node, |
||
334 | array_merge($session_groupes_visibles, $session_list_restricted_folders_for_items) |
||
335 | ) === true |
||
336 | ) { |
||
337 | DB::query( |
||
338 | "SELECT * FROM ".prefix_table("items")." |
||
339 | WHERE inactif=%i AND id_tree = %i", |
||
340 | 0, |
||
341 | $node |
||
342 | ); |
||
343 | $nbChildrenItems += DB::count(); |
||
344 | } |
||
345 | if (in_array( |
||
346 | $node, |
||
347 | array_merge( |
||
348 | $session_groupes_visibles, |
||
349 | $session_list_restricted_folders_for_items, |
||
350 | $session_no_access_folders |
||
351 | ) |
||
352 | ) === true |
||
353 | || @in_array($node, $listFoldersLimitedKeys) === true |
||
354 | || @in_array($node, $listRestrictedFoldersForItemsKeys) === true |
||
355 | ) { |
||
356 | $displayThisNode = true; |
||
357 | $hide_node = $show_but_block = false; |
||
358 | $text = $title = ""; |
||
359 | } |
||
360 | } |
||
361 | |||
362 | if ($displayThisNode === true) { |
||
363 | // get info about current folder |
||
364 | DB::query( |
||
365 | "SELECT * FROM ".prefix_table("items")." |
||
366 | WHERE inactif=%i AND id_tree = %i", |
||
367 | 0, |
||
368 | $completTree[$nodeId]->id |
||
369 | ); |
||
370 | $itemsNb = DB::count(); |
||
371 | |||
372 | // If personal Folder, convert id into user name |
||
373 | if ($completTree[$nodeId]->title == $session_user_id && $completTree[$nodeId]->nlevel == 1) { |
||
374 | $completTree[$nodeId]->title = $session_login; |
||
375 | } |
||
376 | |||
377 | // Decode if needed |
||
378 | $completTree[$nodeId]->title = htmlspecialchars_decode($completTree[$nodeId]->title, ENT_QUOTES); |
||
379 | |||
380 | // special case for READ-ONLY folder |
||
381 | if ($session_user_read_only === true |
||
382 | && in_array($completTree[$nodeId]->id, $session_user_read_only) === false |
||
383 | ) { |
||
384 | $title = $LANG['read_only_account']; |
||
385 | } |
||
386 | $text .= str_replace("&", "&", $completTree[$nodeId]->title); |
||
387 | $restricted = "0"; |
||
388 | $folderClass = "folder"; |
||
389 | |||
390 | if (in_array($completTree[$nodeId]->id, $session_groupes_visibles) === true) { |
||
391 | if (in_array($completTree[$nodeId]->id, $session_read_only_folders) === true) { |
||
392 | $text = "<i class='fa fa-eye'></i> ".$text; |
||
393 | $title = $LANG['read_only_account']; |
||
394 | $restricted = 1; |
||
395 | $folderClass = "folder_not_droppable"; |
||
396 | } elseif ($session_user_read_only === true |
||
397 | && in_array($completTree[$nodeId]->id, $_SESSION['personal_visible_groups']) === false |
||
398 | ) { |
||
399 | $text = "<i class='fa fa-eye'></i> ".$text; |
||
400 | } |
||
401 | $text .= ' (<span class=\'items_count\' id=\'itcount_'.$completTree[$nodeId]->id.'\'>'.$itemsNb.'</span>'; |
||
402 | // display tree counters |
||
403 | if (isset($SETTINGS['tree_counters']) === true |
||
404 | && $SETTINGS['tree_counters'] == 1 |
||
405 | ) { |
||
406 | $text .= '|'.$nbChildrenItems.'|'.(count($nodeDescendants) - 1); |
||
407 | } |
||
408 | $text .= ')'; |
||
409 | } elseif (in_array($completTree[$nodeId]->id, $listFoldersLimitedKeys) === true) { |
||
410 | $restricted = "1"; |
||
411 | if ($session_user_read_only === true) { |
||
412 | $text = "<i class='fa fa-eye'></i> ".$text; |
||
413 | } |
||
414 | $text .= ' (<span class=\'items_count\' id=\'itcount_'.$completTree[$nodeId]->id.'\'>'.count($session_list_folders_limited[$completTree[$nodeId]->id]).'</span>'; |
||
415 | } elseif (in_array($completTree[$nodeId]->id, $listRestrictedFoldersForItemsKeys) === true) { |
||
416 | $restricted = "1"; |
||
417 | if ($_SESSION['user_read_only'] === true) { |
||
418 | $text = "<i class='fa fa-eye'></i> ".$text; |
||
419 | } |
||
420 | $text .= ' (<span class=\'items_count\' id=\'itcount_'.$completTree[$nodeId]->id.'\'>'.count($_SESSION['list_restricted_folders_for_items'][$completTree[$nodeId]->id]).'</span>'; |
||
421 | } else { |
||
422 | $restricted = "1"; |
||
423 | $folderClass = "folder_not_droppable"; |
||
424 | if (isset($SETTINGS['show_only_accessible_folders']) === true |
||
425 | && $SETTINGS['show_only_accessible_folders'] === "1" |
||
426 | && $nbChildrenItems === 0 |
||
427 | ) { |
||
428 | // folder should not be visible |
||
429 | // only if it has no descendants |
||
430 | $nodeDirectDescendants = $tree->getDescendants($nodeId, false, false, true); |
||
431 | if (count( |
||
432 | array_diff( |
||
433 | $nodeDirectDescendants, |
||
434 | array_merge( |
||
435 | $session_groupes_visibles, |
||
436 | array_keys($session_list_restricted_folders_for_items) |
||
437 | ) |
||
438 | ) |
||
439 | ) !== count($nodeDirectDescendants)) { |
||
440 | // show it but block it |
||
441 | $show_but_block = true; |
||
442 | $hide_node = false; |
||
443 | } else { |
||
444 | // hide it |
||
445 | $hide_node = true; |
||
446 | } |
||
447 | } else { |
||
448 | // folder is visible but not accessible by user |
||
449 | $show_but_block = true; |
||
450 | } |
||
451 | } |
||
452 | |||
453 | // prepare json return for current node |
||
454 | if ($completTree[$nodeId]->parent_id === "0") { |
||
455 | $parent = "#"; |
||
456 | } else { |
||
457 | $parent = "li_".$completTree[$nodeId]->parent_id; |
||
458 | } |
||
459 | |||
460 | // handle displaying |
||
461 | if (isset($SETTINGS['show_only_accessible_folders']) === true |
||
462 | && $SETTINGS['show_only_accessible_folders'] === "1" |
||
463 | ) { |
||
464 | if ($hide_node === true) { |
||
465 | $last_visible_parent = $parent; |
||
466 | $last_visible_parent_level = $completTree[$nodeId]->nlevel--; |
||
467 | } elseif ($completTree[$nodeId]->nlevel < $last_visible_parent_level) { |
||
468 | $last_visible_parent = ""; |
||
469 | } |
||
470 | } |
||
471 | |||
472 | |||
473 | // json |
||
474 | if ($hide_node === false && $show_but_block === false) { |
||
475 | $ret_json .= (!empty($ret_json) ? ", " : "").'{'. |
||
476 | '"id":"li_'.$completTree[$nodeId]->id.'"'. |
||
477 | ', "parent":"'.(empty($last_visible_parent) ? $parent : $last_visible_parent).'"'. |
||
478 | ', "text":"'.str_replace('"', '"', $text).'"'. |
||
479 | ', "li_attr":{"class":"jstreeopen", "title":"ID ['.$completTree[$nodeId]->id.'] '.$title.'"}'. |
||
480 | ', "a_attr":{"id":"fld_'.$completTree[$nodeId]->id.'", "class":"'.$folderClass.'" , "onclick":"ListerItems(\''.$completTree[$nodeId]->id.'\', \''.$restricted.'\', 0, 1)", "ondblclick":"LoadTreeNode(\''.$completTree[$nodeId]->id.'\')"}'. |
||
481 | '}'; |
||
482 | } elseif ($show_but_block === true) { |
||
483 | $ret_json .= (!empty($ret_json) ? ", " : "").'{'. |
||
484 | '"id":"li_'.$completTree[$nodeId]->id.'"'. |
||
485 | ', "parent":"'.(empty($last_visible_parent) ? $parent : $last_visible_parent).'"'. |
||
486 | ', "text":"<i class=\'fa fa-close mi-red\'></i> '.$text.'"'. |
||
487 | ', "li_attr":{"class":"", "title":"ID ['.$completTree[$nodeId]->id.'] '.$LANG['no_access'].'"}'. |
||
488 | '}'; |
||
489 | } |
||
490 | foreach ($completTree[$nodeId]->children as $child) { |
||
491 | recursiveTree($child); |
||
492 | } |
||
493 | } |
||
494 | } |
||
495 | } |
||
496 |
The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.
The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.
To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.