| Total Complexity | 55 |
| Total Lines | 510 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
Complex classes like Cchargesociales often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use Cchargesociales, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 37 | * Class Cchargesociales |
||
| 38 | */ |
||
| 39 | class Cchargesociales |
||
| 40 | { |
||
| 41 | public $db; |
||
| 42 | |||
| 43 | public $id; |
||
| 44 | |||
| 45 | /** |
||
| 46 | * @var string Id to identify managed objects |
||
| 47 | */ |
||
| 48 | public $element = 'cchargesociales'; |
||
| 49 | |||
| 50 | /** |
||
| 51 | * @var string Name of table without prefix where object is stored |
||
| 52 | */ |
||
| 53 | public $table_element = 'c_chargesociales'; |
||
| 54 | |||
| 55 | /** |
||
| 56 | * @var string Label |
||
| 57 | * @deprecated |
||
| 58 | */ |
||
| 59 | public $libelle; |
||
| 60 | |||
| 61 | /** |
||
| 62 | * @var string Label |
||
| 63 | */ |
||
| 64 | public $label; |
||
| 65 | |||
| 66 | public $deductible; |
||
| 67 | public $active; |
||
| 68 | public $code; |
||
| 69 | |||
| 70 | /** |
||
| 71 | * @var int ID |
||
| 72 | */ |
||
| 73 | public $fk_pays; |
||
| 74 | |||
| 75 | /** |
||
| 76 | * @var string module |
||
| 77 | */ |
||
| 78 | public $module; |
||
| 79 | public $accountancy_code; |
||
| 80 | |||
| 81 | /** |
||
| 82 | * @var array array of errors |
||
| 83 | */ |
||
| 84 | public $errors = array(); |
||
| 85 | |||
| 86 | /** |
||
| 87 | * Constructor |
||
| 88 | * |
||
| 89 | * @param DoliDB $db Database handler |
||
| 90 | */ |
||
| 91 | public function __construct(DoliDB $db) |
||
|
|
|||
| 92 | { |
||
| 93 | $this->db = $db; |
||
| 94 | } |
||
| 95 | |||
| 96 | /** |
||
| 97 | * Create object into database |
||
| 98 | * |
||
| 99 | * @param User $user User that creates |
||
| 100 | * @param int $notrigger 0=launch triggers after, 1=disable triggers |
||
| 101 | * @return int Return integer <0 if KO, Id of created object if OK |
||
| 102 | */ |
||
| 103 | public function create(User $user, $notrigger = 0) |
||
| 176 | } |
||
| 177 | } |
||
| 178 | |||
| 179 | /** |
||
| 180 | * Load object in memory from the database |
||
| 181 | * |
||
| 182 | * @param int $id Id object |
||
| 183 | * @param string $ref Ref |
||
| 184 | * |
||
| 185 | * @return int Return integer <0 if KO, 0 if not found, >0 if OK |
||
| 186 | */ |
||
| 187 | public function fetch($id, $ref = null) |
||
| 188 | { |
||
| 189 | dol_syslog(__METHOD__, LOG_DEBUG); |
||
| 190 | |||
| 191 | $sql = 'SELECT'; |
||
| 192 | $sql .= " t.id,"; |
||
| 193 | $sql .= " t.libelle as label,"; |
||
| 194 | $sql .= " t.deductible,"; |
||
| 195 | $sql .= " t.active,"; |
||
| 196 | $sql .= " t.code,"; |
||
| 197 | $sql .= " t.fk_pays,"; |
||
| 198 | $sql .= " t.module,"; |
||
| 199 | $sql .= " t.accountancy_code"; |
||
| 200 | $sql .= ' FROM ' . MAIN_DB_PREFIX . $this->table_element . ' as t'; |
||
| 201 | if (null !== $ref) { |
||
| 202 | $sql .= " WHERE t.code = '" . $this->db->escape($ref) . "'"; |
||
| 203 | } else { |
||
| 204 | $sql .= ' WHERE t.id = ' . ((int) $id); |
||
| 205 | } |
||
| 206 | |||
| 207 | $resql = $this->db->query($sql); |
||
| 208 | if ($resql) { |
||
| 209 | $numrows = $this->db->num_rows($resql); |
||
| 210 | if ($numrows) { |
||
| 211 | $obj = $this->db->fetch_object($resql); |
||
| 212 | |||
| 213 | $this->id = $obj->id; |
||
| 214 | |||
| 215 | $this->libelle = $obj->label; |
||
| 216 | $this->label = $obj->label; |
||
| 217 | $this->deductible = $obj->deductible; |
||
| 218 | $this->active = $obj->active; |
||
| 219 | $this->code = $obj->code; |
||
| 220 | $this->fk_pays = $obj->fk_pays; |
||
| 221 | $this->module = $obj->module; |
||
| 222 | $this->accountancy_code = $obj->accountancy_code; |
||
| 223 | } |
||
| 224 | $this->db->free($resql); |
||
| 225 | |||
| 226 | if ($numrows) { |
||
| 227 | return 1; |
||
| 228 | } else { |
||
| 229 | return 0; |
||
| 230 | } |
||
| 231 | } else { |
||
| 232 | $this->errors[] = 'Error ' . $this->db->lasterror(); |
||
| 233 | dol_syslog(__METHOD__ . ' ' . implode(',', $this->errors), LOG_ERR); |
||
| 234 | |||
| 235 | return -1; |
||
| 236 | } |
||
| 237 | } |
||
| 238 | |||
| 239 | /** |
||
| 240 | * Update object into database |
||
| 241 | * |
||
| 242 | * @param User $user User that modifies |
||
| 243 | * @param int $notrigger 0=launch triggers after, 1=disable triggers |
||
| 244 | * @return int Return integer <0 if KO, >0 if OK |
||
| 245 | */ |
||
| 246 | public function update(User $user, $notrigger = 0) |
||
| 247 | { |
||
| 248 | $error = 0; |
||
| 249 | |||
| 250 | dol_syslog(__METHOD__, LOG_DEBUG); |
||
| 251 | |||
| 252 | // Clean parameters |
||
| 253 | |||
| 254 | $this->trimParameters( |
||
| 255 | array( |
||
| 256 | 'libelle', |
||
| 257 | 'deductible', |
||
| 258 | 'active', |
||
| 259 | 'code', |
||
| 260 | 'fk_pays', |
||
| 261 | 'module', |
||
| 262 | 'accountancy_code', |
||
| 263 | ) |
||
| 264 | ); |
||
| 265 | |||
| 266 | |||
| 267 | // Check parameters |
||
| 268 | // Put here code to add a control on parameters values |
||
| 269 | |||
| 270 | // Update request |
||
| 271 | $sql = 'UPDATE ' . MAIN_DB_PREFIX . $this->table_element . ' SET'; |
||
| 272 | $sql .= ' libelle = ' . (isset($this->libelle) ? "'" . $this->db->escape($this->libelle) . "'" : "null") . ','; |
||
| 273 | $sql .= ' deductible = ' . (isset($this->deductible) ? ((int) $this->deductible) : "null") . ','; |
||
| 274 | $sql .= ' active = ' . (isset($this->active) ? ((int) $this->active) : "null") . ','; |
||
| 275 | $sql .= ' code = ' . (isset($this->code) ? "'" . $this->db->escape($this->code) . "'" : "null") . ','; |
||
| 276 | $sql .= ' fk_pays = ' . ((isset($this->fk_pays) && $this->fk_pays > 0) ? ((int) $this->fk_pays) : "null") . ','; |
||
| 277 | $sql .= ' module = ' . (isset($this->module) ? "'" . $this->db->escape($this->module) . "'" : "null") . ','; |
||
| 278 | $sql .= ' accountancy_code = ' . (isset($this->accountancy_code) ? "'" . $this->db->escape($this->accountancy_code) . "'" : "null"); |
||
| 279 | $sql .= ' WHERE id=' . ((int) $this->id); |
||
| 280 | |||
| 281 | $this->db->begin(); |
||
| 282 | |||
| 283 | $resql = $this->db->query($sql); |
||
| 284 | if (!$resql) { |
||
| 285 | $error++; |
||
| 286 | $this->errors[] = 'Error ' . $this->db->lasterror(); |
||
| 287 | dol_syslog(__METHOD__ . ' ' . implode(',', $this->errors), LOG_ERR); |
||
| 288 | } |
||
| 289 | |||
| 290 | //if (!$error && !$notrigger) { |
||
| 291 | // Uncomment this and change MYOBJECT to your own tag if you |
||
| 292 | // want this action calls a trigger. |
||
| 293 | |||
| 294 | //// Call triggers |
||
| 295 | //$result=$this->call_trigger('MYOBJECT_MODIFY',$user); |
||
| 296 | //if ($result < 0) { $error++; //Do also what you must do to rollback action if trigger fail} |
||
| 297 | //// End call triggers |
||
| 298 | //} |
||
| 299 | |||
| 300 | // Commit or rollback |
||
| 301 | if ($error) { |
||
| 302 | $this->db->rollback(); |
||
| 303 | |||
| 304 | return -1 * $error; |
||
| 305 | } else { |
||
| 306 | $this->db->commit(); |
||
| 307 | |||
| 308 | return 1; |
||
| 309 | } |
||
| 310 | } |
||
| 311 | |||
| 312 | /** |
||
| 313 | * Delete object in database |
||
| 314 | * |
||
| 315 | * @param User $user User that deletes |
||
| 316 | * @param int $notrigger 0=launch triggers after, 1=disable triggers |
||
| 317 | * @return int Return integer <0 if KO, >0 if OK |
||
| 318 | */ |
||
| 319 | public function delete(User $user, $notrigger = 0) |
||
| 320 | { |
||
| 321 | dol_syslog(__METHOD__, LOG_DEBUG); |
||
| 322 | |||
| 323 | $error = 0; |
||
| 324 | |||
| 325 | $this->db->begin(); |
||
| 326 | |||
| 327 | //if (!$error) { |
||
| 328 | //if (!$notrigger) { |
||
| 329 | // Uncomment this and change MYOBJECT to your own tag if you |
||
| 330 | // want this action calls a trigger. |
||
| 331 | |||
| 332 | //// Call triggers |
||
| 333 | //$result=$this->call_trigger('MYOBJECT_DELETE',$user); |
||
| 334 | //if ($result < 0) { $error++; //Do also what you must do to rollback action if trigger fail} |
||
| 335 | //// End call triggers |
||
| 336 | //} |
||
| 337 | //} |
||
| 338 | |||
| 339 | if (!$error) { |
||
| 340 | $sql = 'DELETE FROM ' . MAIN_DB_PREFIX . $this->table_element; |
||
| 341 | $sql .= ' WHERE id = ' . ((int) $this->id); |
||
| 342 | |||
| 343 | $resql = $this->db->query($sql); |
||
| 344 | if (!$resql) { |
||
| 345 | $error++; |
||
| 346 | $this->errors[] = 'Error ' . $this->db->lasterror(); |
||
| 347 | dol_syslog(__METHOD__ . ' ' . implode(',', $this->errors), LOG_ERR); |
||
| 348 | } |
||
| 349 | } |
||
| 350 | |||
| 351 | // Commit or rollback |
||
| 352 | if ($error) { |
||
| 353 | $this->db->rollback(); |
||
| 354 | |||
| 355 | return -1 * $error; |
||
| 356 | } else { |
||
| 357 | $this->db->commit(); |
||
| 358 | |||
| 359 | return 1; |
||
| 360 | } |
||
| 361 | } |
||
| 362 | |||
| 363 | /** |
||
| 364 | * Load an object from its id and create a new one in database |
||
| 365 | * |
||
| 366 | * @param User $user User making the clone |
||
| 367 | * @param int $fromid Id of object to clone |
||
| 368 | * @return int New id of clone |
||
| 369 | */ |
||
| 370 | /*public function createFromClone(User $user, $fromid) |
||
| 371 | { |
||
| 372 | dol_syslog(__METHOD__, LOG_DEBUG); |
||
| 373 | |||
| 374 | $error = 0; |
||
| 375 | $object = new Cchargesociales($this->db); |
||
| 376 | |||
| 377 | $this->db->begin(); |
||
| 378 | |||
| 379 | // Load source object |
||
| 380 | $object->fetch($fromid); |
||
| 381 | // Reset object |
||
| 382 | $object->id = 0; |
||
| 383 | |||
| 384 | // Clear fields |
||
| 385 | // ... |
||
| 386 | |||
| 387 | // Create clone |
||
| 388 | $this->context['createfromclone'] = 'createfromclone'; |
||
| 389 | $result = $object->create($user); |
||
| 390 | |||
| 391 | // Other options |
||
| 392 | if ($result < 0) { |
||
| 393 | $error++; |
||
| 394 | $this->errors = $object->errors; |
||
| 395 | dol_syslog(__METHOD__.' '.implode(',', $this->errors), LOG_ERR); |
||
| 396 | } |
||
| 397 | |||
| 398 | unset($this->context['createfromclone']); |
||
| 399 | |||
| 400 | // End |
||
| 401 | if (!$error) { |
||
| 402 | $this->db->commit(); |
||
| 403 | |||
| 404 | return $object->id; |
||
| 405 | } else { |
||
| 406 | $this->db->rollback(); |
||
| 407 | |||
| 408 | return -1; |
||
| 409 | } |
||
| 410 | }*/ |
||
| 411 | |||
| 412 | /** |
||
| 413 | * Return a link to the user card (with optionally the picto) |
||
| 414 | * Use this->id,this->lastname, this->firstname |
||
| 415 | * |
||
| 416 | * @param int $withpicto Include picto in link (0=No picto, 1=Include picto into link, 2=Only picto) |
||
| 417 | * @param string $option On what the link point to |
||
| 418 | * @param integer $notooltip 1=Disable tooltip |
||
| 419 | * @param int $maxlen Max length of visible user name |
||
| 420 | * @param string $morecss Add more css on link |
||
| 421 | * @return string String with URL |
||
| 422 | */ |
||
| 423 | /*public function getNomUrl($withpicto = 0, $option = '', $notooltip = 0, $maxlen = 24, $morecss = '') |
||
| 424 | { |
||
| 425 | global $langs, $conf, $db; |
||
| 426 | global $dolibarr_main_authentication, $dolibarr_main_demo; |
||
| 427 | global $menumanager; |
||
| 428 | |||
| 429 | |||
| 430 | $result = ''; |
||
| 431 | $companylink = ''; |
||
| 432 | |||
| 433 | $label = '<u>'.$langs->trans("MyModule").'</u>'; |
||
| 434 | $label .= '<div width="100%">'; |
||
| 435 | $label .= '<b>'.$langs->trans('Ref').':</b> '.$this->ref; |
||
| 436 | |||
| 437 | $link = '<a href="'.DOL_URL_ROOT.'/tax/card.php?id='.$this->id.'"'; |
||
| 438 | $link .= ($notooltip ? '' : ' title="'.dol_escape_htmltag($label, 1).'" class="classfortooltip'.($morecss ? ' '.$morecss : '').'"'); |
||
| 439 | $link .= '>'; |
||
| 440 | $linkend = '</a>'; |
||
| 441 | |||
| 442 | if ($withpicto) { |
||
| 443 | $result .= ($link.img_object(($notooltip ? '' : $label), 'label', ($notooltip ? '' : 'class="classfortooltip"'), 0, 0, $notooltip ? 0 : 1).$linkend); |
||
| 444 | if ($withpicto != 2) { |
||
| 445 | $result .= ' '; |
||
| 446 | } |
||
| 447 | } |
||
| 448 | $result .= $link.$this->ref.$linkend; |
||
| 449 | return $result; |
||
| 450 | }*/ |
||
| 451 | |||
| 452 | /** |
||
| 453 | * Return the label of the status |
||
| 454 | * |
||
| 455 | * @param int $mode 0=long label, 1=short label, 2=Picto + short label, 3=Picto, 4=Picto + long label, 5=Short label + Picto, 6=Long label + Picto |
||
| 456 | * @return string Label of status |
||
| 457 | */ |
||
| 458 | /*public function getLibStatut($mode = 0) |
||
| 459 | { |
||
| 460 | return $this->LibStatut($this->status, $mode); |
||
| 461 | }*/ |
||
| 462 | |||
| 463 | // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps |
||
| 464 | /** |
||
| 465 | * Return the label of a given status |
||
| 466 | * |
||
| 467 | * @param int $status Id status |
||
| 468 | * @param int $mode 0=long label, 1=short label, 2=Picto + short label, 3=Picto, 4=Picto + long label, 5=Short label + Picto, 6=Long label + Picto |
||
| 469 | * @return string Label of status |
||
| 470 | */ |
||
| 471 | public function LibStatut($status, $mode = 0) |
||
| 472 | { |
||
| 473 | // phpcs:enable |
||
| 474 | global $langs; |
||
| 475 | |||
| 476 | if ($mode == 0) { |
||
| 477 | if ($status == 1) { |
||
| 478 | return $langs->trans('Enabled'); |
||
| 479 | } elseif ($status == 0) { |
||
| 480 | return $langs->trans('Disabled'); |
||
| 481 | } |
||
| 482 | } elseif ($mode == 1) { |
||
| 483 | if ($status == 1) { |
||
| 484 | return $langs->trans('Enabled'); |
||
| 485 | } elseif ($status == 0) { |
||
| 486 | return $langs->trans('Disabled'); |
||
| 487 | } |
||
| 488 | } elseif ($mode == 2) { |
||
| 489 | if ($status == 1) { |
||
| 490 | return img_picto($langs->trans('Enabled'), 'statut4') . ' ' . $langs->trans('Enabled'); |
||
| 491 | } elseif ($status == 0) { |
||
| 492 | return img_picto($langs->trans('Disabled'), 'statut5') . ' ' . $langs->trans('Disabled'); |
||
| 493 | } |
||
| 494 | } elseif ($mode == 3) { |
||
| 495 | if ($status == 1) { |
||
| 496 | return img_picto($langs->trans('Enabled'), 'statut4'); |
||
| 497 | } elseif ($status == 0) { |
||
| 498 | return img_picto($langs->trans('Disabled'), 'statut5'); |
||
| 499 | } |
||
| 500 | } elseif ($mode == 4) { |
||
| 501 | if ($status == 1) { |
||
| 502 | return img_picto($langs->trans('Enabled'), 'statut4') . ' ' . $langs->trans('Enabled'); |
||
| 503 | } elseif ($status == 0) { |
||
| 504 | return img_picto($langs->trans('Disabled'), 'statut5') . ' ' . $langs->trans('Disabled'); |
||
| 505 | } |
||
| 506 | } elseif ($mode == 5) { |
||
| 507 | if ($status == 1) { |
||
| 508 | return $langs->trans('Enabled') . ' ' . img_picto($langs->trans('Enabled'), 'statut4'); |
||
| 509 | } elseif ($status == 0) { |
||
| 510 | return $langs->trans('Disabled') . ' ' . img_picto($langs->trans('Disabled'), 'statut5'); |
||
| 511 | } |
||
| 512 | } |
||
| 513 | return ""; |
||
| 514 | } |
||
| 515 | |||
| 516 | |||
| 517 | /** |
||
| 518 | * Initialise object with example values |
||
| 519 | * Id must be 0 if object instance is a specimen |
||
| 520 | * |
||
| 521 | * @return int |
||
| 522 | */ |
||
| 523 | public function initAsSpecimen() |
||
| 536 | } |
||
| 537 | |||
| 538 | /** |
||
| 539 | * Trim object parameters |
||
| 540 | * |
||
| 541 | * @param string[] $parameters array of parameters to trim |
||
| 542 | * @return void |
||
| 543 | */ |
||
| 544 | private function trimParameters($parameters) |
||
| 545 | { |
||
| 546 | foreach ($parameters as $parameter) { |
||
| 547 | if (isset($this->$parameter)) { |
||
| 553 |