| Conditions | 16 |
| Paths | 256 |
| Total Lines | 99 |
| Code Lines | 55 |
| Lines | 0 |
| Ratio | 0 % |
| 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 |
||
| 49 | public function index(bool $executeActions = true): bool |
||
| 50 | { |
||
| 51 | global $conf; |
||
| 52 | global $db; |
||
| 53 | global $user; |
||
| 54 | global $hookmanager; |
||
| 55 | global $user; |
||
| 56 | global $menumanager; |
||
| 57 | global $langs; |
||
| 58 | |||
| 59 | |||
| 60 | // Load translation files required by the page |
||
| 61 | $langs->loadLangs(["agenda", "companies", "other", "mails"]); |
||
| 62 | |||
| 63 | $id = (GETPOST('id') ? GETPOSTINT('id') : GETPOSTINT('facid')); // For backward compatibility |
||
| 64 | $ref = GETPOST('ref', 'alpha'); |
||
| 65 | $lineid = GETPOSTINT('lineid'); |
||
| 66 | $socid = GETPOSTINT('socid'); |
||
| 67 | $action = GETPOST('action', 'aZ09'); |
||
| 68 | |||
| 69 | // Initialize technical objects |
||
| 70 | $object = new Availabilities($db); |
||
| 71 | $extrafields = new ExtraFields($db); |
||
| 72 | $diroutputmassaction = $conf->bookcal->dir_output . '/temp/massgeneration/' . $user->id; |
||
| 73 | $hookmanager->initHooks(['availabilitiescontact', 'globalcard']); // Note that conf->hooks_modules contains array |
||
| 74 | // Fetch optionals attributes and labels |
||
| 75 | $extrafields->fetch_name_optionals_label($object->table_element); |
||
| 76 | |||
| 77 | // Load object |
||
| 78 | include DOL_DOCUMENT_ROOT . '/core/actions_fetchobject.inc.php'; // Must be include, not include_once // Must be include, not include_once. Include fetch and fetch_thirdparty but not fetch_optionals |
||
| 79 | |||
| 80 | // There is several ways to check permission. |
||
| 81 | // Set $enablepermissioncheck to 1 to enable a minimum low level of checks |
||
| 82 | $enablepermissioncheck = 0; |
||
| 83 | if ($enablepermissioncheck) { |
||
| 84 | $permissiontoread = $user->hasRight('bookcal', 'availabilities', 'read'); |
||
| 85 | $permission = $user->hasRight('bookcal', 'availabilities', 'write'); |
||
| 86 | } else { |
||
| 87 | $permissiontoread = 1; |
||
| 88 | $permission = 1; |
||
| 89 | } |
||
| 90 | |||
| 91 | // Security check (enable the most restrictive one) |
||
| 92 | //if ($user->socid > 0) accessforbidden(); |
||
| 93 | //if ($user->socid > 0) $socid = $user->socid; |
||
| 94 | //$isdraft = (($object->status == $object::STATUS_DRAFT) ? 1 : 0); |
||
| 95 | //restrictedArea($user, $object->element, $object->id, $object->table_element, '', 'fk_soc', 'rowid', $isdraft); |
||
| 96 | if (!isModEnabled('bookcal')) { |
||
| 97 | accessforbidden(); |
||
| 98 | } |
||
| 99 | if (!$permissiontoread) { |
||
| 100 | accessforbidden(); |
||
| 101 | } |
||
| 102 | |||
| 103 | |||
| 104 | /* |
||
| 105 | * Add a new contact |
||
| 106 | */ |
||
| 107 | |||
| 108 | if ($action == 'addcontact' && $permission) { |
||
| 109 | $contactid = (GETPOST('userid') ? GETPOSTINT('userid') : GETPOSTINT('contactid')); |
||
| 110 | $typeid = (GETPOST('typecontact') ? GETPOST('typecontact') : GETPOST('type')); |
||
| 111 | $result = $object->add_contact($contactid, $typeid, GETPOST("source", 'aZ09')); |
||
| 112 | |||
| 113 | if ($result >= 0) { |
||
| 114 | header("Location: " . $_SERVER['PHP_SELF'] . "?id=" . $object->id); |
||
| 115 | exit; |
||
|
|
|||
| 116 | } else { |
||
| 117 | if ($object->error == 'DB_ERROR_RECORD_ALREADY_EXISTS') { |
||
| 118 | $langs->load("errors"); |
||
| 119 | setEventMessages($langs->trans("ErrorThisContactIsAlreadyDefinedAsThisType"), null, 'errors'); |
||
| 120 | } else { |
||
| 121 | setEventMessages($object->error, $object->errors, 'errors'); |
||
| 122 | } |
||
| 123 | } |
||
| 124 | } elseif ($action == 'swapstatut' && $permission) { |
||
| 125 | // Toggle the status of a contact |
||
| 126 | $result = $object->swapContactStatus(GETPOSTINT('ligne')); |
||
| 127 | } elseif ($action == 'deletecontact' && $permission) { |
||
| 128 | // Deletes a contact |
||
| 129 | $result = $object->delete_contact($lineid); |
||
| 130 | |||
| 131 | if ($result >= 0) { |
||
| 132 | header("Location: " . $_SERVER['PHP_SELF'] . "?id=" . $object->id); |
||
| 133 | exit; |
||
| 134 | } else { |
||
| 135 | dol_print_error($db); |
||
| 136 | } |
||
| 137 | } |
||
| 138 | |||
| 139 | |||
| 140 | /* |
||
| 141 | * View |
||
| 142 | */ |
||
| 143 | require_once realpath(BASE_PATH . '/../Dolibarr/Modules/BookCal/Views/availabilities_contact.php'); |
||
| 144 | |||
| 145 | $db->close(); |
||
| 146 | |||
| 147 | return true; |
||
| 148 | } |
||
| 150 |
For hinted functions/methods where all return statements with the correct type are only reachable via conditions, ?null? gets implicitly returned which may be incompatible with the hinted type. Let?s take a look at an example: