| Conditions | 23 | 
| Paths | 1510 | 
| Total Lines | 119 | 
| Code Lines | 72 | 
| 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  | 
            ||
| 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(['bookmarks', 'other']);  | 
            ||
| 64 | |||
| 65 | // Get Parameters  | 
            ||
| 66 |         $id = GETPOSTINT("id"); | 
            ||
| 67 |         $action = GETPOST("action", "alpha"); | 
            ||
| 68 |         $title = (string) GETPOST("title", "alpha"); | 
            ||
| 69 |         $url = (string) GETPOST("url", "alpha"); | 
            ||
| 70 |         $urlsource = GETPOST("urlsource", "alpha"); | 
            ||
| 71 |         $target = GETPOSTINT("target"); | 
            ||
| 72 |         $userid = GETPOSTINT("userid"); | 
            ||
| 73 |         $position = GETPOSTINT("position"); | 
            ||
| 74 |         $backtopage = GETPOST('backtopage', 'alpha'); | 
            ||
| 75 | |||
| 76 | // Initialize Objects  | 
            ||
| 77 | $object = new Bookmark($db);  | 
            ||
| 78 |         if ($id > 0) { | 
            ||
| 79 | $object->fetch($id);  | 
            ||
| 80 | }  | 
            ||
| 81 | |||
| 82 | // Security check  | 
            ||
| 83 | restrictedArea($user, 'bookmark', $object);  | 
            ||
| 84 | |||
| 85 |         $permissiontoread = $user->hasRight('bookmark', 'lire'); | 
            ||
| 86 |         $permissiontoadd = $user->hasRight('bookmark', 'creer'); | 
            ||
| 87 |         $permissiontodelete = $user->hasRight('bookmark', 'supprimer'); | 
            ||
| 88 | |||
| 89 | |||
| 90 | /*  | 
            ||
| 91 | * Actions  | 
            ||
| 92 | */  | 
            ||
| 93 | |||
| 94 |         if ($action == 'add' || $action == 'addproduct' || $action == 'update') { | 
            ||
| 95 |             if ($action == 'update') { | 
            ||
| 96 | $invertedaction = 'edit';  | 
            ||
| 97 |             } else { | 
            ||
| 98 | $invertedaction = 'create';  | 
            ||
| 99 | }  | 
            ||
| 100 | |||
| 101 | $error = 0;  | 
            ||
| 102 | |||
| 103 |             if (GETPOST('cancel', 'alpha')) { | 
            ||
| 104 |                 if (empty($backtopage)) { | 
            ||
| 105 |                     $backtopage = ($urlsource ? $urlsource : ((!empty($url) && !preg_match('/^http/i', $url)) ? $url : DOL_URL_ROOT . '/bookmarks/list.php')); | 
            ||
| 106 | }  | 
            ||
| 107 |                 header("Location: " . $backtopage); | 
            ||
| 108 | exit;  | 
            ||
| 
                                                                                                    
                        
                         | 
                |||
| 109 | }  | 
            ||
| 110 | |||
| 111 |             if ($action == 'update') { | 
            ||
| 112 |                 $object->fetch(GETPOSTINT("id")); | 
            ||
| 113 | }  | 
            ||
| 114 | // Check if null because user not admin can't set an user and send empty value here.  | 
            ||
| 115 |             if (!empty($userid)) { | 
            ||
| 116 | $object->fk_user = $userid;  | 
            ||
| 117 | }  | 
            ||
| 118 | $object->title = $title;  | 
            ||
| 119 | $object->url = $url;  | 
            ||
| 120 | $object->target = $target;  | 
            ||
| 121 | $object->position = $position;  | 
            ||
| 122 | |||
| 123 |             if (!$title) { | 
            ||
| 124 | $error++;  | 
            ||
| 125 |                 setEventMessages($langs->transnoentities("ErrorFieldRequired", $langs->trans("BookmarkTitle")), null, 'errors'); | 
            ||
| 126 | }  | 
            ||
| 127 | |||
| 128 |             if (!$url) { | 
            ||
| 129 | $error++;  | 
            ||
| 130 |                 setEventMessages($langs->transnoentities("ErrorFieldRequired", $langs->trans("UrlOrLink")), null, 'errors'); | 
            ||
| 131 | }  | 
            ||
| 132 | |||
| 133 |             if (!$error) { | 
            ||
| 134 | $object->favicon = 'none';  | 
            ||
| 135 | |||
| 136 |                 if ($action == 'update') { | 
            ||
| 137 | $res = $object->update();  | 
            ||
| 138 |                 } else { | 
            ||
| 139 | $res = $object->create();  | 
            ||
| 140 | }  | 
            ||
| 141 | |||
| 142 |                 if ($res > 0) { | 
            ||
| 143 |                     if (empty($backtopage)) { | 
            ||
| 144 |                         $backtopage = ($urlsource ? $urlsource : ((!empty($url) && !preg_match('/^http/i', $url)) ? $url : DOL_URL_ROOT . '/bookmarks/list.php')); | 
            ||
| 145 | }  | 
            ||
| 146 |                     header("Location: " . $backtopage); | 
            ||
| 147 | exit;  | 
            ||
| 148 |                 } else { | 
            ||
| 149 |                     if ($object->errno == 'DB_ERROR_RECORD_ALREADY_EXISTS') { | 
            ||
| 150 |                         $langs->load("errors"); | 
            ||
| 151 |                         setEventMessages($langs->transnoentities("WarningBookmarkAlreadyExists"), null, 'warnings'); | 
            ||
| 152 |                     } else { | 
            ||
| 153 | setEventMessages($object->error, $object->errors, 'errors');  | 
            ||
| 154 | }  | 
            ||
| 155 | $action = $invertedaction;  | 
            ||
| 156 | }  | 
            ||
| 157 |             } else { | 
            ||
| 158 | $action = $invertedaction;  | 
            ||
| 159 | }  | 
            ||
| 160 | }  | 
            ||
| 161 | |||
| 162 | /*  | 
            ||
| 163 | * View  | 
            ||
| 164 | */  | 
            ||
| 165 | require_once realpath(BASE_PATH . '/../Dolibarr/Modules/BookMarks/Views/card.php');  | 
            ||
| 166 | |||
| 167 | $db->close();  | 
            ||
| 168 | |||
| 169 | return true;  | 
            ||
| 170 | }  | 
            ||
| 172 | 
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: