Complex classes like BlockedLog 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 BlockedLog, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 25 | class BlockedLog |
||
| 26 | { |
||
| 27 | /** |
||
| 28 | * Id of the log |
||
| 29 | * @var int |
||
| 30 | */ |
||
| 31 | public $id; |
||
| 32 | |||
| 33 | public $error = ''; |
||
| 34 | public $errors = array(); |
||
| 35 | |||
| 36 | /** |
||
| 37 | * Unique fingerprint of the log |
||
| 38 | * @var string |
||
| 39 | */ |
||
| 40 | public $signature = ''; |
||
| 41 | |||
| 42 | /** |
||
| 43 | * Unique fingerprint of the line log content |
||
| 44 | * @var string |
||
| 45 | */ |
||
| 46 | public $signature_line = ''; |
||
| 47 | |||
| 48 | public $amounts = null; |
||
| 49 | |||
| 50 | /** |
||
| 51 | * trigger action |
||
| 52 | * @var string |
||
| 53 | */ |
||
| 54 | public $action = ''; |
||
| 55 | |||
| 56 | /** |
||
| 57 | * Object element |
||
| 58 | * @var string |
||
| 59 | */ |
||
| 60 | public $element = ''; |
||
| 61 | |||
| 62 | /** |
||
| 63 | * Object id |
||
| 64 | * @var int |
||
| 65 | */ |
||
| 66 | public $fk_object = 0; |
||
| 67 | |||
| 68 | /** |
||
| 69 | * Log certified by remote authority or not |
||
| 70 | * @var boolean |
||
| 71 | */ |
||
| 72 | public $certified = false; |
||
| 73 | |||
| 74 | /** |
||
| 75 | * Author |
||
| 76 | * @var int |
||
| 77 | */ |
||
| 78 | public $fk_user = 0; |
||
| 79 | |||
| 80 | public $date_object = 0; |
||
| 81 | |||
| 82 | public $ref_object = ''; |
||
| 83 | |||
| 84 | public $object_data = null; |
||
| 85 | |||
| 86 | |||
| 87 | |||
| 88 | /** |
||
| 89 | * Constructor |
||
| 90 | * |
||
| 91 | * @param DoliDB $db Database handler |
||
| 92 | */ |
||
| 93 | public function __construct(DoliDB $db) |
||
| 98 | |||
| 99 | /** |
||
| 100 | * try to retrieve logged object link |
||
| 101 | */ |
||
| 102 | public function getObjectLink() |
||
| 154 | |||
| 155 | /** |
||
| 156 | * try to retrieve user author |
||
| 157 | */ |
||
| 158 | public function getUser() |
||
| 177 | |||
| 178 | /** |
||
| 179 | * Populate properties of log from object data |
||
| 180 | * |
||
| 181 | * @param Object $object object to store |
||
| 182 | * @param string $action action |
||
| 183 | * @param string $amounts amounts |
||
| 184 | */ |
||
| 185 | public function setObjectData(&$object, $action, $amounts) |
||
| 186 | { |
||
| 187 | global $langs, $user, $mysoc; |
||
| 188 | |||
| 189 | // Generic fields |
||
| 190 | |||
| 191 | // action |
||
| 192 | $this->action = $action; |
||
| 193 | // amount |
||
| 194 | $this->amounts= $amounts; |
||
| 195 | // date |
||
| 196 | if ($object->element == 'payment' || $object->element == 'payment_supplier') |
||
| 197 | { |
||
| 198 | $this->date_object = $object->datepaye; |
||
| 199 | } |
||
| 200 | elseif ($object->element=='payment_salary') |
||
| 201 | { |
||
| 202 | $this->date_object = $object->datev; |
||
| 203 | } |
||
| 204 | else { |
||
| 205 | $this->date_object = $object->date; |
||
| 206 | } |
||
| 207 | // ref |
||
| 208 | $this->ref_object = ((! empty($object->newref)) ? $object->newref : $object->ref); // newref is set when validating a draft, ref is set in other cases |
||
| 209 | // type of object |
||
| 210 | $this->element = $object->element; |
||
| 211 | // id of object |
||
| 212 | $this->fk_object = $object->id; |
||
| 213 | |||
| 214 | $this->object_data=new stdClass(); |
||
| 215 | |||
| 216 | // Add thirdparty info |
||
| 217 | |||
| 218 | if (empty($object->thirdparty) && method_exists($object, 'fetch_thirdparty')) $object->fetch_thirdparty(); |
||
| 219 | |||
| 220 | if (! empty($object->thirdparty)) |
||
| 221 | { |
||
| 222 | $this->object_data->thirdparty = new stdClass(); |
||
| 223 | |||
| 224 | foreach($object->thirdparty as $key=>$value) |
||
| 225 | { |
||
| 226 | if (in_array($key, array('fields'))) continue; // Discard some properties |
||
| 227 | if (! in_array($key, array( |
||
| 228 | 'name','name_alias','ref_ext','address','zip','town','state_code','country_code','idprof1','idprof2','idprof3','idprof4','idprof5','idprof6','phone','fax','email','barcode', |
||
| 229 | 'tva_intra', 'localtax1_assuj', 'localtax1_value', 'localtax2_assuj', 'localtax2_value', 'managers', 'capital', 'typent_code', 'forme_juridique_code', 'code_client', 'code_fournisseur' |
||
| 230 | ))) continue; // Discard if not into a dedicated list |
||
| 231 | if (!is_object($value)) $this->object_data->thirdparty->{$key} = $value; |
||
| 232 | } |
||
| 233 | } |
||
| 234 | |||
| 235 | // Add company info |
||
| 236 | if (! empty($mysoc)) |
||
| 237 | { |
||
| 238 | $this->object_data->mycompany = new stdClass(); |
||
| 239 | |||
| 240 | foreach($mysoc as $key=>$value) |
||
| 241 | { |
||
| 242 | if (in_array($key, array('fields'))) continue; // Discard some properties |
||
| 243 | if (! in_array($key, array( |
||
| 244 | 'name','name_alias','ref_ext','address','zip','town','state_code','country_code','idprof1','idprof2','idprof3','idprof4','idprof5','idprof6','phone','fax','email','barcode', |
||
| 245 | 'tva_intra', 'localtax1_assuj', 'localtax1_value', 'localtax2_assuj', 'localtax2_value', 'managers', 'capital', 'typent_code', 'forme_juridique_code', 'code_client', 'code_fournisseur' |
||
| 246 | ))) continue; // Discard if not into a dedicated list |
||
| 247 | if (!is_object($value)) $this->object_data->mycompany->{$key} = $value; |
||
| 248 | } |
||
| 249 | } |
||
| 250 | |||
| 251 | // Add user info |
||
| 252 | |||
| 253 | $this->fk_user = $user->id; |
||
| 254 | $this->user_fullname = $user->getFullName($langs); |
||
| 255 | |||
| 256 | // Field specific to object |
||
| 257 | |||
| 258 | if ($this->element == 'facture') |
||
| 259 | { |
||
| 260 | $this->object_data->total_ht = (double) $object->total_ht; |
||
| 261 | $this->object_data->total_tva = (double) $object->total_tva; |
||
| 262 | $this->object_data->total_ttc = (double) $object->total_ttc; |
||
| 263 | $this->object_data->total_localtax1 = (double) $object->total_localtax1; |
||
| 264 | $this->object_data->total_localtax2 = (double) $object->total_localtax2; |
||
| 265 | |||
| 266 | $this->object_data->revenue_stamp = (double) $object->revenue_stamp; |
||
| 267 | $this->object_data->date_pointoftax = (double) $object->date_pointoftax; |
||
| 268 | $this->object_data->note_public = (double) $object->note_public; |
||
| 269 | } |
||
| 270 | if($this->element == 'invoice_supplier') { |
||
| 271 | if(empty($object->thirdparty))$object->fetch_thirdparty(); |
||
| 272 | $this->object_data->thirdparty = new stdClass(); |
||
| 273 | |||
| 274 | foreach($object->thirdparty as $key=>$value) { |
||
| 275 | if(!is_object($value)) $this->object_data->thirdparty->{$key} = $value; |
||
| 276 | } |
||
| 277 | |||
| 278 | $this->object_data->total_ht = (double) $object->total_ht; |
||
| 279 | $this->object_data->total_tva = (double) $object->total_tva; |
||
| 280 | $this->object_data->total_ttc = (double) $object->total_ttc; |
||
| 281 | $this->object_data->total_localtax1 = (double) $object->total_localtax1; |
||
| 282 | $this->object_data->total_localtax2 = (double) $object->total_localtax2; |
||
| 283 | |||
| 284 | $this->object_data->revenue_stamp = (double) $object->revenue_stamp; |
||
| 285 | $this->object_data->date_pointoftax = (double) $object->date_pointoftax; |
||
| 286 | $this->object_data->note_public = (double) $object->note_public; |
||
| 287 | } |
||
| 288 | elseif ($this->element == 'payment'|| $object->element == 'payment_supplier') |
||
| 289 | { |
||
| 290 | $this->object_data->amounts = $object->amounts; |
||
| 291 | } |
||
| 292 | elseif($this->element == 'payment_salary') |
||
| 293 | { |
||
| 294 | $this->object_data->amounts = array($object->amount); |
||
| 295 | } |
||
| 296 | } |
||
| 297 | |||
| 298 | /** |
||
| 299 | * Get object from database |
||
| 300 | * |
||
| 301 | * @param int $id Id of object to load |
||
| 302 | * @return int >0 if OK, <0 if KO, 0 if not found |
||
| 303 | */ |
||
| 304 | public function fetch($id) { |
||
| 367 | |||
| 368 | /** |
||
| 369 | * Set block certified by authority |
||
| 370 | * |
||
| 371 | * @return boolean |
||
| 372 | */ |
||
| 373 | public function setCertified() { |
||
| 382 | |||
| 383 | /** |
||
| 384 | * Create blocked log in database. |
||
| 385 | * |
||
| 386 | * @param User $user Object user that create |
||
| 387 | * @return int <0 if KO, >0 if OK |
||
| 388 | */ |
||
| 389 | public function create($user) { |
||
| 494 | |||
| 495 | /** |
||
| 496 | * Check if current signature still correct compare to the chain |
||
| 497 | * |
||
| 498 | * @return boolean True if OK, False if KO |
||
| 499 | */ |
||
| 500 | public function checkSignature() |
||
| 521 | |||
| 522 | /** |
||
| 523 | * Return a string for signature |
||
| 524 | * |
||
| 525 | * @return string Key for signature |
||
| 526 | */ |
||
| 527 | private function buildKeyForSignature() |
||
| 532 | |||
| 533 | |||
| 534 | /** |
||
| 535 | * Get previous signature/hash in chain |
||
| 536 | * |
||
| 537 | * @param int $withlock 1=With a lock |
||
| 538 | * @param int $beforeid Before id |
||
| 539 | * @return string Hash of last record |
||
| 540 | */ |
||
| 541 | private function getPreviousHash($withlock=0, $beforeid=0) |
||
| 574 | |||
| 575 | /** |
||
| 576 | * Return array of log objects (with criterias) |
||
| 577 | * |
||
| 578 | * @param string $element element to search |
||
| 579 | * @param int $fk_object id of object to search |
||
| 580 | * @param int $limit max number of element, 0 for all |
||
| 581 | * @param string $sortfield sort field |
||
| 582 | * @param string $sortorder sort order |
||
| 583 | * @param int $search_start start time limit |
||
| 584 | * @param int $search_end end time limit |
||
| 585 | * @return array array of object log |
||
| 586 | */ |
||
| 587 | public function getLog($element, $fk_object, $limit = 0, $sortfield = '', $sortorder = '', $search_start = -1, $search_end = -1) |
||
| 646 | |||
| 647 | /** |
||
| 648 | * Return the signature (hash) of the "genesis-block" (Block 0) |
||
| 649 | * |
||
| 650 | * @return string Signature of genesis-block for current conf->entity |
||
| 651 | */ |
||
| 652 | public function getSignature() |
||
| 669 | |||
| 670 | } |
||
| 671 | |||
| 672 |