| Conditions | 38 |
| Paths | 1 |
| Total Lines | 151 |
| Code Lines | 96 |
| 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 |
||
| 102 | function main() |
||
| 103 | {
|
||
| 104 | die('En main');
|
||
| 105 | Skin::$view = new CategoriesView($this); |
||
| 106 | if ($this->origin) {
|
||
| 107 | if ($this->type == AlCategorie::TYPE_PRODUCT) {
|
||
| 108 | $idProdOrigin = $this->origin; |
||
| 109 | } |
||
| 110 | if ($this->type == AlCategorie::TYPE_SUPPLIER) {
|
||
| 111 | $idSupplierOrigin = $this->origin; |
||
| 112 | } |
||
| 113 | if ($this->type == AlCategorie::TYPE_CUSTOMER) {
|
||
| 114 | $idCompanyOrigin = $this->origin; |
||
| 115 | } |
||
| 116 | if ($this->type == AlCategorie::TYPE_MEMBER) {
|
||
| 117 | $idMemberOrigin = $this->origin; |
||
| 118 | } |
||
| 119 | if ($this->type == AlCategorie::TYPE_CONTACT) {
|
||
| 120 | $idContactOrigin = $this->origin; |
||
| 121 | } |
||
| 122 | if ($this->type == AlCategorie::TYPE_PROJECT) {
|
||
| 123 | $idProjectOrigin = $this->origin; |
||
| 124 | } |
||
| 125 | } |
||
| 126 | |||
| 127 | if ($this->catorigin && $this->type == AlCategorie::TYPE_PRODUCT) {
|
||
| 128 | $idCatOrigin = $this->catorigin; |
||
| 129 | } |
||
| 130 | |||
| 131 | $this->object = new AlCategorie(); |
||
| 132 | |||
| 133 | $extrafields = new AlExtraFields(); |
||
| 134 | $extralabels = $extrafields->fetch_name_optionals_label($this->object->table_element); |
||
| 135 | |||
| 136 | // Initialize technical object to manage hooks. Note that conf->hooks_modules contains array array |
||
| 137 | Globals::$hookManager->initHooks(array('categorycard'));
|
||
| 138 | |||
| 139 | |||
| 140 | /* |
||
| 141 | * Actions |
||
| 142 | */ |
||
| 143 | |||
| 144 | // Add action |
||
| 145 | if ($this->action == 'add' && Globals::$user->rights->categorie->creer) {
|
||
| 146 | // Action ajout d'une categorie |
||
| 147 | if ($this->cancel) {
|
||
| 148 | if ($this->urlfrom) {
|
||
| 149 | header("Location: " . $urlfrom);
|
||
| 150 | exit; |
||
| 151 | } |
||
| 152 | if ($idProdOrigin) {
|
||
| 153 | header("Location: " . DOL_URL_ROOT . '/categories/viewcat.php?id=' . $idProdOrigin . '&type=' . $this->type);
|
||
| 154 | exit; |
||
| 155 | } |
||
| 156 | if ($idCompanyOrigin) {
|
||
| 157 | header("Location: " . DOL_URL_ROOT . '/categories/viewcat.php?id=' . $idCompanyOrigin . '&type=' . $this->type);
|
||
| 158 | exit; |
||
| 159 | } |
||
| 160 | if ($idSupplierOrigin) {
|
||
| 161 | header("Location: " . DOL_URL_ROOT . '/categories/viewcat.php?id=' . $idSupplierOrigin . '&type=' . $this->type);
|
||
| 162 | exit; |
||
| 163 | } |
||
| 164 | if ($idMemberOrigin) {
|
||
| 165 | header("Location: " . DOL_URL_ROOT . '/categories/viewcat.php?id=' . $idMemberOrigin . '&type=' . $this->type);
|
||
| 166 | exit; |
||
| 167 | } |
||
| 168 | if ($idContactOrigin) {
|
||
| 169 | header("Location: " . DOL_URL_ROOT . '/categories/viewcat.php?id=' . $idContactOrigin . '&type=' . $this->type);
|
||
| 170 | exit; |
||
| 171 | } |
||
| 172 | if ($idProjectOrigin) {
|
||
| 173 | header("Location: " . DOL_URL_ROOT . '/categories/viewcat.php?id=' . $idProjectOrigin . '&type=' . $this->type);
|
||
| 174 | exit; |
||
| 175 | } |
||
| 176 | header("Location: " . DOL_URL_ROOT . '/categories/index.php?leftmenu=cat&type=' . $this->type);
|
||
| 177 | exit; |
||
| 178 | } |
||
| 179 | |||
| 180 | $object->label = $this->label; |
||
| 181 | $object->color = $this->color; |
||
| 182 | $object->description = AlDolUtils::dol_htmlcleanlastbr($this->description); |
||
| 183 | $object->socid = ($this->socid ? $this->socid : 'null'); |
||
| 184 | $object->visible = $this->visible; |
||
| 185 | $object->type = $this->type; |
||
| 186 | |||
| 187 | if ($parent != "-1") {
|
||
| 188 | $object->fk_parent = $parent; |
||
| 189 | } |
||
| 190 | |||
| 191 | $ret = $extrafields->setOptionalsFromPost($extralabels, $object); |
||
| 192 | if ($ret < 0) {
|
||
| 193 | $error++; |
||
| 194 | } |
||
| 195 | |||
| 196 | if (!$object->label) {
|
||
| 197 | $error++; |
||
| 198 | setEventMessages(Globals::$langs->trans("ErrorFieldRequired", Globals::$langs->transnoentities("Ref")), null, 'errors');
|
||
| 199 | $this->action = 'create'; |
||
| 200 | } |
||
| 201 | |||
| 202 | // Create category in database |
||
| 203 | if (!$error) {
|
||
| 204 | $result = $object->create($user); |
||
| 205 | if ($result > 0) {
|
||
| 206 | $this->action = 'confirmed'; |
||
| 207 | $_POST["addcat"] = ''; |
||
| 208 | } else {
|
||
| 209 | setEventMessages($object->error, $object->errors, 'errors'); |
||
| 210 | } |
||
| 211 | } |
||
| 212 | } |
||
| 213 | |||
| 214 | // Confirm action |
||
| 215 | if (($this->action == 'add' || $this->action == 'confirmed') && Globals::$user->rights->categorie->creer) {
|
||
| 216 | // Action confirmation de creation categorie |
||
| 217 | if ($this->action == 'confirmed') {
|
||
| 218 | if ($urlfrom) {
|
||
| 219 | header("Location: " . $urlfrom);
|
||
| 220 | exit; |
||
| 221 | } |
||
| 222 | if ($backtopage) {
|
||
| 223 | header("Location: " . $backtopage);
|
||
| 224 | exit; |
||
| 225 | } |
||
| 226 | if ($idProdOrigin) {
|
||
| 227 | header("Location: " . DOL_URL_ROOT . '/categories/viewcat.php?id=' . $idProdOrigin . '&type=' . $this->type . '&mesg=' . urlencode(Globals::$langs->trans("CatCreated")));
|
||
| 228 | exit; |
||
| 229 | } |
||
| 230 | if ($idCompanyOrigin) {
|
||
| 231 | header("Location: " . DOL_URL_ROOT . '/categories/viewcat.php?id=' . $idCompanyOrigin . '&type=' . $this->type . '&mesg=' . urlencode(Globals::$langs->trans("CatCreated")));
|
||
| 232 | exit; |
||
| 233 | } |
||
| 234 | if ($idSupplierOrigin) {
|
||
| 235 | header("Location: " . DOL_URL_ROOT . '/categories/viewcat.php?id=' . $idSupplierOrigin . '&type=' . $this->type . '&mesg=' . urlencode(Globals::$langs->trans("CatCreated")));
|
||
| 236 | exit; |
||
| 237 | } |
||
| 238 | if ($idMemberOrigin) {
|
||
| 239 | header("Location: " . DOL_URL_ROOT . '/categories/viewcat.php?id=' . $idMemberOrigin . '&type=' . $this->type . '&mesg=' . urlencode(Globals::$langs->trans("CatCreated")));
|
||
| 240 | exit; |
||
| 241 | } |
||
| 242 | if ($idContactOrigin) {
|
||
| 243 | header("Location: " . DOL_URL_ROOT . '/categories/viewcat.php?id=' . $idContactOrigin . '&type=' . $this->type . '&mesg=' . urlencode(Globals::$langs->trans("CatCreated")));
|
||
| 244 | exit; |
||
| 245 | } |
||
| 246 | if ($idProjectOrigin) {
|
||
| 247 | header("Location: " . DOL_URL_ROOT . '/categories/viewcat.php?id=' . $idProjectOrigin . '&type=' . $this->type . '&mesg=' . urlencode(Globals::$langs->trans("CatCreated")));
|
||
| 248 | exit; |
||
| 249 | } |
||
| 250 | |||
| 251 | header("Location: " . DOL_URL_ROOT . '/categories/viewcat.php?id=' . $result . '&type=' . $this->type);
|
||
| 252 | exit; |
||
| 253 | } |
||
| 257 |
In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.