| Conditions | 18 | 
| Paths | 2032 | 
| Total Lines | 139 | 
| Code Lines | 77 | 
| 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  | 
            ||
| 23 | public static function loadMenu()  | 
            ||
| 24 |     { | 
            ||
| 25 | |||
| 26 | global $langs, $user, $conf; // To export to dol_eval function  | 
            ||
| 27 | global $mainmenu, $leftmenu; // To export to dol_eval function  | 
            ||
| 28 | global $db;  | 
            ||
| 29 | |||
| 30 | /*  | 
            ||
| 31 | dd([  | 
            ||
| 32 | 'user' => $user,  | 
            ||
| 33 | \DoliCore\Model\Menu::loadTopMenu(1, empty($user->socid) ? 0 : 1),  | 
            ||
| 34 | \DoliCore\Model\Menu::loadSideMenu(1, empty($user->socid) ? 0 : 1),  | 
            ||
| 35 | ]);  | 
            ||
| 36 | */  | 
            ||
| 37 | |||
| 38 | $type_user = empty($user->socid) ? 0 : 1;  | 
            ||
| 39 | |||
| 40 | $sql = "SELECT m.rowid, m.type, m.module, m.fk_menu, m.fk_mainmenu, m.fk_leftmenu, m.url, m.titre,";  | 
            ||
| 41 | $sql .= " m.prefix, m.langs, m.perms, m.enabled, m.target, m.mainmenu, m.leftmenu, m.position";  | 
            ||
| 42 | $sql .= " FROM " . $db->prefix() . "menu as m";  | 
            ||
| 43 | $sql .= " WHERE m.entity IN (0," . $conf->entity . ")";  | 
            ||
| 44 |         if ($type_user == 0) { | 
            ||
| 45 | $sql .= " AND m.usertype IN (0,2)";  | 
            ||
| 46 | }  | 
            ||
| 47 |         if ($type_user == 1) { | 
            ||
| 48 | $sql .= " AND m.usertype IN (1,2)";  | 
            ||
| 49 | }  | 
            ||
| 50 | $sql .= " ORDER BY m.type DESC, m.position, m.rowid";  | 
            ||
| 51 | |||
| 52 | //dol_syslog(get_class($this)."::menuLoad mymainmenu=".$mymainmenu." myleftmenu=".$myleftmenu." type_user=".$type_user." menu_handler=".$menu_handler." tabMenu size=".count($tabMenu), LOG_DEBUG);  | 
            ||
| 53 | $resql = $db->query($sql);  | 
            ||
| 54 |         if ($resql) { | 
            ||
| 55 | $numa = $db->num_rows($resql);  | 
            ||
| 56 | |||
| 57 | $a = 0;  | 
            ||
| 58 | $b = 0;  | 
            ||
| 59 |             while ($a < $numa) { | 
            ||
| 60 | //$objm = $db->fetch_object($resql);  | 
            ||
| 61 | $menu = $db->fetch_array($resql);  | 
            ||
| 62 | |||
| 63 | // Define $right  | 
            ||
| 64 | $perms = true;  | 
            ||
| 65 |                 if (isset($menu['perms'])) { | 
            ||
| 66 | $tmpcond = $menu['perms'];  | 
            ||
| 67 |                     if ($leftmenu == 'all') { | 
            ||
| 68 |                         $tmpcond = preg_replace('/\$leftmenu\s*==\s*["\'a-zA-Z_]+/', '1==1', $tmpcond); // Force the part of condition on leftmenu to true | 
            ||
| 69 | }  | 
            ||
| 70 | $perms = verifCond($tmpcond);  | 
            ||
| 71 | //print "verifCond rowid=".$menu['rowid']." ".$tmpcond.":".$perms."<br>\n";  | 
            ||
| 72 | }  | 
            ||
| 73 | |||
| 74 | // Define $enabled  | 
            ||
| 75 | $enabled = true;  | 
            ||
| 76 |                 if (isset($menu['enabled'])) { | 
            ||
| 77 | $tmpcond = $menu['enabled'];  | 
            ||
| 78 |                     if ($leftmenu == 'all') { | 
            ||
| 79 |                         $tmpcond = preg_replace('/\$leftmenu\s*==\s*["\'a-zA-Z_]+/', '1==1', $tmpcond); // Force the part of condition on leftmenu to true | 
            ||
| 80 | }  | 
            ||
| 81 | $enabled = verifCond($tmpcond);  | 
            ||
| 82 | //var_dump($menu['type'].' - '.$menu['titre'].' - '.$menu['enabled'].' => '.$enabled);  | 
            ||
| 83 | }  | 
            ||
| 84 | |||
| 85 | // Define $title  | 
            ||
| 86 |                 if ($enabled) { | 
            ||
| 87 | $title = $langs->trans($menu['titre']); // If $menu['titre'] start with $, a dol_eval is done.  | 
            ||
| 88 | //var_dump($title.'-'.$menu['titre']);  | 
            ||
| 89 |                     if ($title == $menu['titre']) {   // Translation not found | 
            ||
| 90 |                         if (!empty($menu['langs'])) {    // If there is a dedicated translation file | 
            ||
| 91 | //print 'Load file '.$menu['langs'].'<br>';  | 
            ||
| 92 | $langs->load($menu['langs']);  | 
            ||
| 93 | }  | 
            ||
| 94 | |||
| 95 | $substitarray = ['__LOGIN__' => $user->login, '__USER_ID__' => $user->id, '__USER_SUPERVISOR_ID__' => $user->fk_user];  | 
            ||
| 96 | $menu['titre'] = make_substitutions($menu['titre'], $substitarray);  | 
            ||
| 97 | |||
| 98 |                         if (preg_match("/\//", $menu['titre'])) { // To manage translation when title is string1/string2 | 
            ||
| 99 |                             $tab_titre = explode("/", $menu['titre']); | 
            ||
| 100 | $title = $langs->trans($tab_titre[0]) . "/" . $langs->trans($tab_titre[1]);  | 
            ||
| 101 |                         } elseif (preg_match('/\|\|/', $menu['titre'])) { | 
            ||
| 102 | // To manage different translation (Title||AltTitle@ConditionForAltTitle)  | 
            ||
| 103 |                             $tab_title = explode("||", $menu['titre']); | 
            ||
| 104 |                             $alt_title = explode("@", $tab_title[1]); | 
            ||
| 105 | $title_enabled = verifCond($alt_title[1]);  | 
            ||
| 106 | $title = ($title_enabled ? $langs->trans($alt_title[0]) : $langs->trans($tab_title[0]));  | 
            ||
| 107 |                         } else { | 
            ||
| 108 | $title = $langs->trans($menu['titre']);  | 
            ||
| 109 | }  | 
            ||
| 110 | }  | 
            ||
| 111 | //$tmp4=microtime(true);  | 
            ||
| 112 | //print '>>> 3 '.($tmp4 - $tmp3).'<br>';  | 
            ||
| 113 | |||
| 114 | // We complete tabMenu  | 
            ||
| 115 | $tabMenu[$b]['rowid'] = $menu['rowid'];  | 
            ||
| 116 | $tabMenu[$b]['module'] = $menu['module'];  | 
            ||
| 117 | $tabMenu[$b]['fk_menu'] = $menu['fk_menu'];  | 
            ||
| 118 | $tabMenu[$b]['url'] = $menu['url'];  | 
            ||
| 119 |                     if (!preg_match("/^(http:\/\/|https:\/\/)/i", $tabMenu[$b]['url'])) { | 
            ||
| 120 |                         if (preg_match('/\?/', $tabMenu[$b]['url'])) { | 
            ||
| 121 | $tabMenu[$b]['url'] .= '&idmenu=' . $menu['rowid'];  | 
            ||
| 122 |                         } else { | 
            ||
| 123 | $tabMenu[$b]['url'] .= '?idmenu=' . $menu['rowid'];  | 
            ||
| 124 | }  | 
            ||
| 125 | }  | 
            ||
| 126 | $tabMenu[$b]['titre'] = $title;  | 
            ||
| 127 | $tabMenu[$b]['prefix'] = $menu['prefix'];  | 
            ||
| 128 | $tabMenu[$b]['target'] = $menu['target'];  | 
            ||
| 129 | $tabMenu[$b]['mainmenu'] = $menu['mainmenu'];  | 
            ||
| 130 | $tabMenu[$b]['leftmenu'] = $menu['leftmenu'];  | 
            ||
| 131 | $tabMenu[$b]['perms'] = $perms;  | 
            ||
| 132 | $tabMenu[$b]['langs'] = $menu['langs']; // Note that this should not be used, lang file should be already loaded.  | 
            ||
| 133 | $tabMenu[$b]['enabled'] = $enabled;  | 
            ||
| 134 | $tabMenu[$b]['type'] = $menu['type'];  | 
            ||
| 135 | $tabMenu[$b]['fk_mainmenu'] = $menu['fk_mainmenu'];  | 
            ||
| 136 | $tabMenu[$b]['fk_leftmenu'] = $menu['fk_leftmenu'];  | 
            ||
| 137 | $tabMenu[$b]['position'] = (int) $menu['position'];  | 
            ||
| 138 | |||
| 139 | $b++;  | 
            ||
| 140 | }  | 
            ||
| 141 | |||
| 142 | $a++;  | 
            ||
| 143 | }  | 
            ||
| 144 | $db->free($resql);  | 
            ||
| 145 | |||
| 146 | // Currently $tabMenu is sorted on position.  | 
            ||
| 147 | // If a child have a position lower that its parent, we can make a loop to fix this here, but we prefer to show a warning  | 
            ||
| 148 | // into the leftMenuCharger later to avoid useless operations.  | 
            ||
| 149 | |||
| 150 | /*  | 
            ||
| 151 | dump([  | 
            ||
| 152 | 'user' => $user,  | 
            ||
| 153 | 'sql' => $sql,  | 
            ||
| 154 | 'result' => $tabMenu,  | 
            ||
| 155 | ]);  | 
            ||
| 156 | */  | 
            ||
| 157 | |||
| 158 | return 1;  | 
            ||
| 159 |         } else { | 
            ||
| 160 | dol_print_error($db);  | 
            ||
| 161 | return -1;  | 
            ||
| 162 | }  | 
            ||
| 165 |