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