Complex classes like Cpaiement 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. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
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 Cpaiement, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 31 | class Cpaiement |
||
| 32 | { |
||
| 33 | /** |
||
| 34 | * @var string Id to identify managed objects |
||
| 35 | */ |
||
| 36 | public $element = 'cpaiement'; |
||
| 37 | |||
| 38 | /** |
||
| 39 | * @var string Name of table without prefix where object is stored |
||
| 40 | */ |
||
| 41 | public $table_element = 'c_paiement'; |
||
| 42 | |||
| 43 | public $code; |
||
| 44 | public $libelle; |
||
| 45 | public $type; |
||
| 46 | public $active; |
||
| 47 | public $accountancy_code; |
||
| 48 | public $module; |
||
| 49 | |||
| 50 | |||
| 51 | /** |
||
| 52 | * Constructor |
||
| 53 | * |
||
| 54 | * @param DoliDb $db Database handler |
||
| 55 | */ |
||
| 56 | public function __construct(DoliDB $db) |
||
| 60 | |||
| 61 | /** |
||
| 62 | * Create object into database |
||
| 63 | * |
||
| 64 | * @param User $user User that creates |
||
| 65 | * @param bool $notrigger false=launch triggers after, true=disable triggers |
||
| 66 | * |
||
| 67 | * @return int <0 if KO, Id of created object if OK |
||
| 68 | */ |
||
| 69 | public function create(User $user, $notrigger = false) |
||
| 70 | { |
||
| 71 | dol_syslog(__METHOD__, LOG_DEBUG); |
||
| 72 | |||
| 73 | $error = 0; |
||
| 74 | |||
| 75 | // Clean parameters |
||
| 76 | |||
| 77 | if (isset($this->code)) { |
||
| 78 | $this->code = trim($this->code); |
||
| 79 | } |
||
| 80 | if (isset($this->libelle)) { |
||
| 81 | $this->libelle = trim($this->libelle); |
||
| 82 | } |
||
| 83 | if (isset($this->type)) { |
||
| 84 | $this->type = trim($this->type); |
||
| 85 | } |
||
| 86 | if (isset($this->active)) { |
||
| 87 | $this->active = trim($this->active); |
||
| 88 | } |
||
| 89 | if (isset($this->accountancy_code)) { |
||
| 90 | $this->accountancy_code = trim($this->accountancy_code); |
||
| 91 | } |
||
| 92 | if (isset($this->module)) { |
||
| 93 | $this->module = trim($this->module); |
||
| 94 | } |
||
| 95 | |||
| 96 | |||
| 97 | |||
| 98 | // Check parameters |
||
| 99 | // Put here code to add control on parameters values |
||
| 100 | |||
| 101 | // Insert request |
||
| 102 | $sql = 'INSERT INTO ' . MAIN_DB_PREFIX . $this->table_element . '('; |
||
| 103 | |||
| 104 | $sql.= 'entity,'; |
||
| 105 | $sql.= 'code,'; |
||
| 106 | $sql.= 'libelle,'; |
||
| 107 | $sql.= 'type,'; |
||
| 108 | $sql.= 'active,'; |
||
| 109 | $sql.= 'accountancy_code,'; |
||
| 110 | $sql.= 'module'; |
||
| 111 | |||
| 112 | |||
| 113 | $sql .= ') VALUES ('; |
||
| 114 | |||
| 115 | $sql .= ' '.(! isset($this->entity)?getEntity('c_paiement'):$this->entity).','; |
||
| 116 | $sql .= ' '.(! isset($this->code)?'NULL':"'".$this->db->escape($this->code)."'").','; |
||
| 117 | $sql .= ' '.(! isset($this->libelle)?'NULL':"'".$this->db->escape($this->libelle)."'").','; |
||
| 118 | $sql .= ' '.(! isset($this->type)?'NULL':$this->type).','; |
||
| 119 | $sql .= ' '.(! isset($this->active)?'NULL':$this->active).','; |
||
| 120 | $sql .= ' '.(! isset($this->accountancy_code)?'NULL':"'".$this->db->escape($this->accountancy_code)."'").','; |
||
| 121 | $sql .= ' '.(! isset($this->module)?'NULL':"'".$this->db->escape($this->module)."'"); |
||
| 122 | |||
| 123 | |||
| 124 | $sql .= ')'; |
||
| 125 | |||
| 126 | $this->db->begin(); |
||
| 127 | |||
| 128 | $resql = $this->db->query($sql); |
||
| 129 | if (!$resql) { |
||
| 130 | $error ++; |
||
| 131 | $this->errors[] = 'Error ' . $this->db->lasterror(); |
||
| 132 | dol_syslog(__METHOD__ . ' ' . join(',', $this->errors), LOG_ERR); |
||
| 133 | } |
||
| 134 | |||
| 135 | if (!$error) { |
||
| 136 | $this->id = $this->db->last_insert_id(MAIN_DB_PREFIX . $this->table_element); |
||
| 137 | |||
| 138 | // Uncomment this and change MYOBJECT to your own tag if you |
||
| 139 | // want this action to call a trigger. |
||
| 140 | //if (!$notrigger) { |
||
| 141 | |||
| 142 | // // Call triggers |
||
| 143 | // $result=$this->call_trigger('MYOBJECT_CREATE',$user); |
||
| 144 | // if ($result < 0) $error++; |
||
| 145 | // // End call triggers |
||
| 146 | //} |
||
| 147 | } |
||
| 148 | |||
| 149 | // Commit or rollback |
||
| 150 | if ($error) { |
||
| 151 | $this->db->rollback(); |
||
| 152 | |||
| 153 | return - 1 * $error; |
||
| 154 | } else { |
||
| 155 | $this->db->commit(); |
||
| 156 | |||
| 157 | return $this->id; |
||
| 158 | } |
||
| 159 | } |
||
| 160 | |||
| 161 | /** |
||
| 162 | * Load object in memory from the database |
||
| 163 | * |
||
| 164 | * @param int $id Id object |
||
| 165 | * @param string $ref Ref |
||
| 166 | * |
||
| 167 | * @return int <0 if KO, 0 if not found, >0 if OK |
||
| 168 | */ |
||
| 169 | public function fetch($id, $ref = null) |
||
| 220 | |||
| 221 | /** |
||
| 222 | * Update object into database |
||
| 223 | * |
||
| 224 | * @param User $user User that modifies |
||
| 225 | * @param bool $notrigger false=launch triggers after, true=disable triggers |
||
| 226 | * |
||
| 227 | * @return int <0 if KO, >0 if OK |
||
| 228 | */ |
||
| 229 | public function update(User $user, $notrigger = false) |
||
| 302 | |||
| 303 | /** |
||
| 304 | * Delete object in database |
||
| 305 | * |
||
| 306 | * @param User $user User that deletes |
||
| 307 | * @param bool $notrigger false=launch triggers after, true=disable triggers |
||
| 308 | * |
||
| 309 | * @return int <0 if KO, >0 if OK |
||
| 310 | */ |
||
| 311 | public function delete(User $user, $notrigger = false) |
||
| 352 | |||
| 353 | |||
| 354 | /** |
||
| 355 | * Initialise object with example values |
||
| 356 | * Id must be 0 if object instance is a specimen |
||
| 357 | * |
||
| 358 | * @return void |
||
| 359 | */ |
||
| 360 | public function initAsSpecimen() |
||
| 371 | } |
||
| 372 |