| Conditions | 12 |
| Paths | 36 |
| Total Lines | 107 |
| Code Lines | 58 |
| 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 |
||
| 46 | public function index($executeActions = true): bool |
||
| 47 | { |
||
| 48 | global $conf; |
||
| 49 | global $db; |
||
| 50 | global $user; |
||
| 51 | global $hookmanager; |
||
| 52 | global $user; |
||
| 53 | global $menumanager; |
||
| 54 | global $langs; |
||
| 55 | global $mysoc; |
||
| 56 | |||
| 57 | $action = GETPOST('action', 'aZ09'); |
||
| 58 | $cancel = GETPOST('cancel', 'alpha'); |
||
| 59 | |||
| 60 | // Load translation files required by the page |
||
| 61 | $langs->loadLangs(["companies", "products", "admin", "users", "other"]); |
||
| 62 | |||
| 63 | // Security check |
||
| 64 | if (!$user->admin) { |
||
| 65 | accessforbidden(); |
||
| 66 | } |
||
| 67 | |||
| 68 | $dirstandard = []; |
||
| 69 | $dirsmartphone = []; |
||
| 70 | $dirmenus = array_merge(["/core/menus/"], (array) $conf->modules_parts['menus']); |
||
| 71 | foreach ($dirmenus as $dirmenu) { |
||
| 72 | $dirstandard[] = $dirmenu . 'standard'; |
||
| 73 | $dirsmartphone[] = $dirmenu . 'smartphone'; |
||
| 74 | } |
||
| 75 | |||
| 76 | $error = 0; |
||
| 77 | |||
| 78 | // This can be a big page. The execution time limit is increased. |
||
| 79 | // This setting can only be changed when the 'safe_mode' is inactive. |
||
| 80 | $err = error_reporting(); |
||
| 81 | error_reporting(0); // Disable all errors |
||
| 82 | //error_reporting(E_ALL); |
||
| 83 | @set_time_limit(300); // Need more than 240 on Windows 7/64 |
||
|
|
|||
| 84 | error_reporting($err); |
||
| 85 | |||
| 86 | |||
| 87 | /* |
||
| 88 | * Actions |
||
| 89 | */ |
||
| 90 | |||
| 91 | if ($action == 'update' && !$cancel) { |
||
| 92 | $_SESSION["mainmenu"] = "home"; // The menu manager may have changed |
||
| 93 | |||
| 94 | dolibarr_set_const($db, "MAIN_MENU_STANDARD", GETPOST('MAIN_MENU_STANDARD', 'alpha'), 'chaine', 0, '', $conf->entity); |
||
| 95 | dolibarr_set_const($db, "MAIN_MENU_SMARTPHONE", GETPOST('MAIN_MENU_SMARTPHONE', 'alpha'), 'chaine', 0, '', $conf->entity); |
||
| 96 | |||
| 97 | dolibarr_set_const($db, "MAIN_MENUFRONT_STANDARD", GETPOST('MAIN_MENUFRONT_STANDARD', 'alpha'), 'chaine', 0, '', $conf->entity); |
||
| 98 | dolibarr_set_const($db, "MAIN_MENUFRONT_SMARTPHONE", GETPOST('MAIN_MENUFRONT_SMARTPHONE', 'alpha'), 'chaine', 0, '', $conf->entity); |
||
| 99 | |||
| 100 | // Define list of menu handlers to initialize |
||
| 101 | $listofmenuhandler = []; |
||
| 102 | $listofmenuhandler[preg_replace('/(_backoffice|_frontoffice|_menu)?\.php/i', '', GETPOST('MAIN_MENU_STANDARD', 'alpha'))] = 1; |
||
| 103 | $listofmenuhandler[preg_replace('/(_backoffice|_frontoffice|_menu)?\.php/i', '', GETPOST('MAIN_MENUFRONT_STANDARD', 'alpha'))] = 1; |
||
| 104 | if (GETPOST('MAIN_MENU_SMARTPHONE', 'alpha')) { |
||
| 105 | $listofmenuhandler[preg_replace('/(_backoffice|_frontoffice|_menu)?\.php/i', '', GETPOST('MAIN_MENU_SMARTPHONE', 'alpha'))] = 1; |
||
| 106 | } |
||
| 107 | if (GETPOST('MAIN_MENUFRONT_SMARTPHONE', 'alpha')) { |
||
| 108 | $listofmenuhandler[preg_replace('/(_backoffice|_frontoffice|_menu)?\.php/i', '', GETPOST('MAIN_MENUFRONT_SMARTPHONE', 'alpha'))] = 1; |
||
| 109 | } |
||
| 110 | |||
| 111 | // Initialize menu handlers |
||
| 112 | foreach ($listofmenuhandler as $key => $val) { |
||
| 113 | // Load sql init_menu_handler.sql file |
||
| 114 | $dirmenus = array_merge(["/core/menus/"], (array) $conf->modules_parts['menus']); |
||
| 115 | foreach ($dirmenus as $dirmenu) { |
||
| 116 | $file = 'init_menu_' . $key . '.sql'; |
||
| 117 | $fullpath = dol_buildpath($dirmenu . $file); |
||
| 118 | //print 'action='.$action.' Search menu into fullpath='.$fullpath.'<br>';exit; |
||
| 119 | |||
| 120 | if (file_exists($fullpath)) { |
||
| 121 | $db->begin(); |
||
| 122 | |||
| 123 | $result = run_sql($fullpath, 1, '', 1, $key, 'none'); |
||
| 124 | if ($result > 0) { |
||
| 125 | $db->commit(); |
||
| 126 | } else { |
||
| 127 | $error++; |
||
| 128 | setEventMessages($langs->trans("FailedToInitializeMenu") . ' ' . $key, null, 'errors'); |
||
| 129 | $db->rollback(); |
||
| 130 | } |
||
| 131 | } |
||
| 132 | } |
||
| 133 | } |
||
| 134 | |||
| 135 | if (!$error) { |
||
| 136 | $db->close(); |
||
| 137 | |||
| 138 | // We make a header redirect because we need to change menu NOW. |
||
| 139 | header("Location: " . $_SERVER['PHP_SELF']); |
||
| 140 | exit; |
||
| 141 | } |
||
| 142 | } |
||
| 143 | |||
| 144 | |||
| 145 | /* |
||
| 146 | * View |
||
| 147 | */ |
||
| 148 | require_once realpath(BASE_PATH . '/../Dolibarr/Modules/Admin/Views/admin_menu.php'); |
||
| 149 | |||
| 150 | $db->close(); |
||
| 151 | |||
| 152 | return true; |
||
| 153 | } |
||
| 155 |
If you suppress an error, we recommend checking for the error condition explicitly: