Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
Complex classes like Bbcvols 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 Bbcvols, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
42 | class Bbcvols extends CommonObject |
||
43 | { |
||
44 | |||
45 | /** |
||
46 | * @var string Id to identify managed objects |
||
47 | */ |
||
48 | public $element = 'flightlog_bbcvols'; |
||
49 | |||
50 | /** |
||
51 | * @var string Name of table without prefix where object is stored |
||
52 | */ |
||
53 | public $table_element = 'bbc_vols'; |
||
54 | |||
55 | public $idBBC_vols; |
||
56 | public $date = ''; |
||
57 | public $lieuD; |
||
58 | public $lieuA; |
||
59 | public $heureD; |
||
60 | public $heureA; |
||
61 | public $BBC_ballons_idBBC_ballons; |
||
62 | public $nbrPax; |
||
63 | public $remarque; |
||
64 | public $incidents; |
||
65 | public $fk_type; |
||
66 | public $fk_pilot; |
||
67 | public $fk_organisateur; |
||
68 | public $is_facture; |
||
69 | public $kilometers; |
||
70 | public $cost; |
||
71 | public $fk_receiver; |
||
72 | public $justif_kilometers; |
||
73 | public $date_creation; |
||
74 | public $date_update; |
||
75 | |||
76 | /** |
||
77 | * @var Bbc_ballons |
||
78 | */ |
||
79 | private $balloon; |
||
80 | |||
81 | /** |
||
82 | * @var User |
||
83 | */ |
||
84 | private $pilot; |
||
85 | |||
86 | /** |
||
87 | * @var string |
||
88 | */ |
||
89 | private $passengerNames; |
||
90 | |||
91 | /** |
||
92 | * @var int |
||
93 | */ |
||
94 | private $orderId; |
||
95 | |||
96 | /** |
||
97 | * @var Commande |
||
98 | */ |
||
99 | private $order; |
||
100 | |||
101 | /** |
||
102 | * @return int |
||
103 | */ |
||
104 | public function getIdBBCVols() |
||
108 | |||
109 | /** |
||
110 | * @return int |
||
111 | */ |
||
112 | public function getId() |
||
116 | |||
117 | /** |
||
118 | * @param string|int $ref |
||
119 | * |
||
120 | * @return $this |
||
121 | */ |
||
122 | public function setRef($ref) |
||
127 | |||
128 | /** |
||
129 | * Constructor |
||
130 | * |
||
131 | * @param DoliDb $db Database handler |
||
132 | */ |
||
133 | public function __construct(DoliDB $db) |
||
140 | |||
141 | /** |
||
142 | * Create a flight |
||
143 | * |
||
144 | * @param User $user User that creates |
||
145 | * @param bool $notrigger false=launch triggers after, true=disable triggers |
||
146 | * |
||
147 | * @return int <0 if KO, Id of created object if OK |
||
148 | * @throws Exception |
||
149 | */ |
||
150 | public function create(User $user, $notrigger = false) |
||
297 | |||
298 | /** |
||
299 | * Load object in memory from the database |
||
300 | * |
||
301 | * @param int $id Id object |
||
302 | * @param string $ref Ref |
||
303 | * |
||
304 | * @return int <0 if KO, 0 if not found, >0 if OK |
||
305 | * @throws Exception |
||
306 | */ |
||
307 | public function fetch($id, $ref = null) |
||
391 | |||
392 | /** |
||
393 | * @param User $user User that modifies |
||
394 | * @param bool $notrigger false=launch triggers after, true=disable triggers |
||
395 | * |
||
396 | * @return int <0 if KO, >0 if OK |
||
397 | * @throws Exception |
||
398 | */ |
||
399 | public function update(User $user, $notrigger = false) |
||
400 | { |
||
401 | $error = 0; |
||
402 | |||
403 | dol_syslog(__METHOD__, LOG_DEBUG); |
||
404 | |||
405 | // Clean parameters |
||
406 | |||
407 | if (isset($this->idBBC_vols)) { |
||
408 | $this->idBBC_vols = trim($this->idBBC_vols); |
||
409 | } |
||
410 | if (isset($this->lieuD)) { |
||
411 | $this->lieuD = trim($this->lieuD); |
||
412 | } |
||
413 | if (isset($this->lieuA)) { |
||
414 | $this->lieuA = trim($this->lieuA); |
||
415 | } |
||
416 | if (isset($this->heureD)) { |
||
417 | $this->heureD = trim($this->heureD); |
||
418 | } |
||
419 | if (isset($this->heureA)) { |
||
420 | $this->heureA = trim($this->heureA); |
||
421 | } |
||
422 | if (isset($this->BBC_ballons_idBBC_ballons)) { |
||
423 | $this->BBC_ballons_idBBC_ballons = trim($this->BBC_ballons_idBBC_ballons); |
||
424 | } |
||
425 | if (isset($this->nbrPax)) { |
||
426 | $this->nbrPax = trim($this->nbrPax); |
||
427 | } |
||
428 | if (isset($this->remarque)) { |
||
429 | $this->remarque = trim($this->remarque); |
||
430 | } |
||
431 | if (isset($this->incidents)) { |
||
432 | $this->incidents = trim($this->incidents); |
||
433 | } |
||
434 | if (isset($this->fk_type)) { |
||
435 | $this->fk_type = trim($this->fk_type); |
||
436 | } |
||
437 | if (isset($this->fk_pilot)) { |
||
438 | $this->fk_pilot = trim($this->fk_pilot); |
||
439 | } |
||
440 | if (isset($this->fk_organisateur)) { |
||
441 | $this->fk_organisateur = trim($this->fk_organisateur); |
||
442 | } |
||
443 | if (isset($this->is_facture)) { |
||
444 | $this->is_facture = trim($this->is_facture); |
||
445 | } |
||
446 | if (isset($this->kilometers)) { |
||
447 | $this->kilometers = trim($this->kilometers); |
||
448 | } |
||
449 | if (isset($this->cost)) { |
||
450 | $this->cost = trim($this->cost); |
||
451 | } |
||
452 | if (isset($this->fk_receiver)) { |
||
453 | $this->fk_receiver = trim($this->fk_receiver); |
||
454 | } |
||
455 | if (isset($this->justif_kilometers)) { |
||
456 | $this->justif_kilometers = trim($this->justif_kilometers); |
||
457 | } |
||
458 | if (isset($this->passengerNames)) { |
||
459 | $this->passengerNames = trim($this->passengerNames); |
||
460 | } |
||
461 | if (isset($this->orderId)) { |
||
462 | $this->orderId = trim($this->orderId); |
||
463 | } |
||
464 | |||
465 | |||
466 | // Check parameters |
||
467 | // Put here code to add a control on parameters values |
||
468 | |||
469 | // Update request |
||
470 | $sql = 'UPDATE ' . MAIN_DB_PREFIX . $this->table_element . ' SET'; |
||
471 | |||
472 | $sql .= ' date = ' . (!isset($this->date) || dol_strlen($this->date) != 0 ? "'" . $this->db->idate($this->date) . "'" : 'null') . ','; |
||
473 | $sql .= ' lieuD = ' . (isset($this->lieuD) ? "'" . $this->db->escape($this->lieuD) . "'" : "null") . ','; |
||
474 | $sql .= ' lieuA = ' . (isset($this->lieuA) ? "'" . $this->db->escape($this->lieuA) . "'" : "null") . ','; |
||
475 | $sql .= ' heureD = ' . (isset($this->heureD) ? "'" . $this->heureD . "'" : "null") . ','; |
||
476 | $sql .= ' heureA = ' . (isset($this->heureA) ? "'" . $this->heureA . "'" : "null") . ','; |
||
477 | $sql .= ' BBC_ballons_idBBC_ballons = ' . (isset($this->BBC_ballons_idBBC_ballons) ? $this->BBC_ballons_idBBC_ballons : "null") . ','; |
||
478 | $sql .= ' nbrPax = ' . (isset($this->nbrPax) ? "'" . $this->db->escape($this->nbrPax) . "'" : "null") . ','; |
||
479 | $sql .= ' remarque = ' . (isset($this->remarque) ? "'" . $this->db->escape($this->remarque) . "'" : "null") . ','; |
||
480 | $sql .= ' incidents = ' . (isset($this->incidents) ? "'" . $this->db->escape($this->incidents) . "'" : "null") . ','; |
||
481 | $sql .= ' fk_type = ' . (isset($this->fk_type) && (int) $this->fk_type > 0 ? $this->fk_type : "null") . ','; |
||
482 | $sql .= ' fk_pilot = ' . (isset($this->fk_pilot) && (int) $this->fk_pilot > 0 ? $this->fk_pilot : "null") . ','; |
||
483 | $sql .= ' fk_organisateur = ' . (isset($this->fk_organisateur) && (int) $this->fk_organisateur > 0 ? $this->fk_organisateur : "null") . ','; |
||
484 | $sql .= ' is_facture = ' . (isset($this->is_facture) ? $this->is_facture : "0") . ','; |
||
485 | $sql .= ' kilometers = ' . (!empty($this->kilometers) ? $this->kilometers : "0") . ','; |
||
486 | $sql .= ' cost = ' . (isset($this->cost) ? "'" . $this->db->escape($this->cost) . "'" : "''") . ','; |
||
487 | $sql .= ' fk_receiver = ' . (isset($this->fk_receiver) && (int) $this->fk_receiver > 0 ? $this->fk_receiver : "null") . ','; |
||
488 | $sql .= ' justif_kilometers = ' . (isset($this->justif_kilometers) ? "'" . $this->db->escape($this->justif_kilometers) . "'," : "'',"); |
||
489 | $sql .= ' date_update = ' . "'" . date('Y-m-d H:i:s') . "',"; |
||
490 | $sql .= ' passenger_names = ' . "'" . trim($this->passengerNames) . "',"; |
||
491 | $sql .= ' order_id = ' . (!isset($this->orderId) || (int) $this->orderId === -1 || (int) $this->orderId === 0 ? 'null' : $this->orderId); |
||
492 | |||
493 | $sql .= ' WHERE idBBC_vols=' . $this->idBBC_vols; |
||
494 | |||
495 | $this->db->begin(); |
||
496 | |||
497 | $resql = $this->db->query($sql); |
||
498 | if (!$resql) { |
||
499 | $error++; |
||
500 | $this->errors[] = 'Error ' . $this->db->lasterror(); |
||
501 | dol_syslog(__METHOD__ . ' ' . join(',', $this->errors), LOG_ERR); |
||
502 | } |
||
503 | |||
504 | if (!$error && !$notrigger) { |
||
505 | $result = $this->call_trigger('BBC_FLIGHT_UPDATED', $user); |
||
506 | if ($result < 0) { |
||
507 | $error++; |
||
508 | } |
||
509 | } |
||
510 | |||
511 | // Commit or rollback |
||
512 | if ($error) { |
||
513 | $this->db->rollback(); |
||
514 | |||
515 | return -1 * $error; |
||
516 | } |
||
517 | |||
518 | $this->db->commit(); |
||
519 | return 1; |
||
520 | } |
||
521 | |||
522 | /** |
||
523 | * Delete object in database |
||
524 | * |
||
525 | * @param User $user User that deletes |
||
526 | * @param bool $notrigger false=launch triggers after, true=disable triggers |
||
527 | * |
||
528 | * @return int <0 if KO, >0 if OK |
||
529 | * @throws Exception |
||
530 | */ |
||
531 | public function delete(User $user, $notrigger = false) |
||
569 | |||
570 | /** |
||
571 | * Return a link to the user card (with optionaly the picto) |
||
572 | * Use this->id,this->lastname, this->firstname |
||
573 | * |
||
574 | * @param int $withpicto Include picto in link (0=No picto, 1=Include picto into link, 2=Only picto) |
||
575 | * @param string $option On what the link point to |
||
576 | * @param integer $notooltip 1=Disable tooltip |
||
577 | * @param int $maxlen Max length of visible user name |
||
578 | * @param string $morecss Add more css on link |
||
579 | * |
||
580 | * @return string String with URL |
||
581 | */ |
||
582 | public function getNomUrl($withpicto = 0, $option = '', $notooltip = 0, $maxlen = 24, $morecss = '') |
||
612 | |||
613 | /** |
||
614 | * Retourne le libelle du status d'un user (actif, inactif) |
||
615 | * |
||
616 | * @param int $mode 0=libelle long, 1=libelle court, 2=Picto + Libelle court, 3=Picto, 4=Picto + Libelle long, 5=Libelle court + Picto |
||
617 | * |
||
618 | * @return string Label of status |
||
619 | */ |
||
620 | public function getLibStatut($mode = 0) |
||
624 | |||
625 | /** |
||
626 | * Renvoi le libelle d'un status donne |
||
627 | * |
||
628 | * @param int $status Id status |
||
629 | * @param int $mode 0=libelle long, 1=libelle court, 2=Picto + Libelle court, 3=Picto, 4=Picto + Libelle long, 5=Libelle court + Picto |
||
630 | * |
||
631 | * @return string Label of status |
||
632 | */ |
||
633 | private function LibStatut($status, $mode = 0) |
||
691 | |||
692 | /** |
||
693 | * @return string |
||
694 | */ |
||
695 | public function __toString() |
||
699 | |||
700 | /** |
||
701 | * @return string |
||
702 | */ |
||
703 | public function toString() |
||
707 | |||
708 | /** |
||
709 | * @return string |
||
710 | */ |
||
711 | public function getStatus() |
||
715 | |||
716 | /** |
||
717 | * @return boolean |
||
718 | */ |
||
719 | public function hasFacture() |
||
725 | |||
726 | /** |
||
727 | * @param int $userId |
||
728 | * |
||
729 | * @return User |
||
730 | */ |
||
731 | private function fetchUser($userId) |
||
738 | |||
739 | /** |
||
740 | * @return Bbc_ballons |
||
741 | */ |
||
742 | private function fetchBalloon() |
||
749 | |||
750 | /** |
||
751 | * @return Bbc_ballons |
||
752 | */ |
||
753 | public function getBalloon() |
||
761 | |||
762 | /** |
||
763 | * @return User |
||
764 | */ |
||
765 | public function getPilot() |
||
773 | |||
774 | /** |
||
775 | * @return int |
||
776 | */ |
||
777 | public function getPilotId() |
||
781 | |||
782 | /** |
||
783 | * @return int |
||
784 | */ |
||
785 | public function getOrganisatorId() |
||
789 | |||
790 | /** |
||
791 | * @return Bbctypes |
||
792 | */ |
||
793 | public function getFlightType() |
||
800 | |||
801 | /** |
||
802 | * @return string |
||
803 | */ |
||
804 | public function getComment() |
||
808 | |||
809 | /** |
||
810 | * @return string |
||
811 | */ |
||
812 | public function getIncident() |
||
816 | |||
817 | /** |
||
818 | * Return true if the number of pax is greater than 0 |
||
819 | * |
||
820 | * @return boolean |
||
821 | */ |
||
822 | public function hasPax() |
||
826 | |||
827 | /** |
||
828 | * Regarding the type of the flight give an indication if the flight must have pax to be valid. |
||
829 | * |
||
830 | * @return boolean |
||
831 | */ |
||
832 | public function mustHavePax() |
||
836 | |||
837 | /** |
||
838 | * Returns true if the amount requested by the flight is 0. |
||
839 | * |
||
840 | * @return boolean |
||
841 | */ |
||
842 | public function isFree() |
||
846 | |||
847 | /** |
||
848 | * @return int |
||
849 | */ |
||
850 | public function getAmountReceived() |
||
854 | |||
855 | /** |
||
856 | * @return float |
||
857 | */ |
||
858 | public function getAmountPerPassenger() |
||
863 | |||
864 | /** |
||
865 | * @return boolean |
||
866 | */ |
||
867 | public function hasReceiver() |
||
871 | |||
872 | /** |
||
873 | * @return boolean |
||
874 | */ |
||
875 | public function hasKilometers() |
||
879 | |||
880 | /** |
||
881 | * @return boolean |
||
882 | */ |
||
883 | public function hasKilometersDescription() |
||
887 | |||
888 | /** |
||
889 | * @return int |
||
890 | */ |
||
891 | public function getKilometers() |
||
895 | |||
896 | /** |
||
897 | * @return string |
||
898 | */ |
||
899 | public function getPlaces() |
||
903 | |||
904 | /** |
||
905 | * @return string |
||
906 | */ |
||
907 | public function getDescription() |
||
911 | |||
912 | /** |
||
913 | * @return string |
||
914 | */ |
||
915 | public function getPassengerNames() |
||
919 | |||
920 | /** |
||
921 | * @param string $passengerNames |
||
922 | * |
||
923 | * @return Bbcvols |
||
924 | */ |
||
925 | public function setPassengerNames($passengerNames) |
||
930 | |||
931 | /** |
||
932 | * @return int |
||
933 | */ |
||
934 | public function getNumberOfPassengers() |
||
938 | |||
939 | /** |
||
940 | * @return int |
||
941 | */ |
||
942 | public function getOrderId() |
||
946 | |||
947 | /** |
||
948 | * @param int $orderId |
||
949 | * |
||
950 | * @return Bbcvols |
||
951 | */ |
||
952 | public function setOrderId($orderId) |
||
957 | |||
958 | /** |
||
959 | * Is an instruction flight (T6/T7) |
||
960 | */ |
||
961 | public function isInstruction() |
||
965 | |||
966 | /** |
||
967 | * @return bool |
||
968 | */ |
||
969 | public function isLinkedToOrder() |
||
973 | |||
974 | /** |
||
975 | * Fetch the order based on the order id. |
||
976 | */ |
||
977 | public function fetchOrder() |
||
988 | |||
989 | /** |
||
990 | * @return Commande |
||
991 | */ |
||
992 | public function getOrder() |
||
996 | |||
997 | /** |
||
998 | * Flag the flight as billed |
||
999 | * |
||
1000 | * @return $this |
||
1001 | */ |
||
1002 | public function bill() |
||
1007 | |||
1008 | /** |
||
1009 | * @return boolean |
||
1010 | */ |
||
1011 | public function isBilled() |
||
1015 | |||
1016 | /** |
||
1017 | * @return DateTime |
||
1018 | */ |
||
1019 | public function getDate() |
||
1023 | |||
1024 | /** |
||
1025 | * @param string $date |
||
1026 | * |
||
1027 | * @return Bbcvols |
||
1028 | */ |
||
1029 | public function setDate($date) |
||
1034 | |||
1035 | /** |
||
1036 | * @return mixed |
||
1037 | */ |
||
1038 | public function getLieuD() |
||
1042 | |||
1043 | /** |
||
1044 | * @param mixed $lieuD |
||
1045 | * |
||
1046 | * @return Bbcvols |
||
1047 | */ |
||
1048 | public function setLieuD($lieuD) |
||
1053 | |||
1054 | /** |
||
1055 | * @return mixed |
||
1056 | */ |
||
1057 | public function getLieuA() |
||
1061 | |||
1062 | /** |
||
1063 | * @param mixed $lieuA |
||
1064 | * |
||
1065 | * @return Bbcvols |
||
1066 | */ |
||
1067 | public function setLieuA($lieuA) |
||
1072 | |||
1073 | /** |
||
1074 | * @return mixed |
||
1075 | */ |
||
1076 | public function getHeureD() |
||
1080 | |||
1081 | /** |
||
1082 | * @param mixed $heureD |
||
1083 | * |
||
1084 | * @return Bbcvols |
||
1085 | */ |
||
1086 | public function setHeureD($heureD) |
||
1091 | |||
1092 | /** |
||
1093 | * @return mixed |
||
1094 | */ |
||
1095 | public function getHeureA() |
||
1099 | |||
1100 | /** |
||
1101 | * @param mixed $heureA |
||
1102 | * |
||
1103 | * @return Bbcvols |
||
1104 | */ |
||
1105 | public function setHeureA($heureA) |
||
1110 | |||
1111 | /** |
||
1112 | * @return mixed |
||
1113 | */ |
||
1114 | public function getBBCBallonsIdBBCBallons() |
||
1118 | |||
1119 | /** |
||
1120 | * @param mixed $BBC_ballons_idBBC_ballons |
||
1121 | * |
||
1122 | * @return Bbcvols |
||
1123 | */ |
||
1124 | public function setBBCBallonsIdBBCBallons($BBC_ballons_idBBC_ballons) |
||
1129 | |||
1130 | /** |
||
1131 | * @return mixed |
||
1132 | */ |
||
1133 | public function getNbrPax() |
||
1137 | |||
1138 | /** |
||
1139 | * @param mixed $nbrPax |
||
1140 | * |
||
1141 | * @return Bbcvols |
||
1142 | */ |
||
1143 | public function setNbrPax($nbrPax) |
||
1148 | |||
1149 | /** |
||
1150 | * @return mixed |
||
1151 | */ |
||
1152 | public function getRemarque() |
||
1156 | |||
1157 | /** |
||
1158 | * @param mixed $remarque |
||
1159 | * |
||
1160 | * @return Bbcvols |
||
1161 | */ |
||
1162 | public function setRemarque($remarque) |
||
1167 | |||
1168 | /** |
||
1169 | * @return mixed |
||
1170 | */ |
||
1171 | public function getIncidents() |
||
1175 | |||
1176 | /** |
||
1177 | * @param mixed $incidents |
||
1178 | * |
||
1179 | * @return Bbcvols |
||
1180 | */ |
||
1181 | public function setIncidents($incidents) |
||
1186 | |||
1187 | /** |
||
1188 | * @return mixed |
||
1189 | */ |
||
1190 | public function getFkType() |
||
1194 | |||
1195 | /** |
||
1196 | * @param mixed $fk_type |
||
1197 | * |
||
1198 | * @return Bbcvols |
||
1199 | */ |
||
1200 | public function setFkType($fk_type) |
||
1205 | |||
1206 | /** |
||
1207 | * @return mixed |
||
1208 | */ |
||
1209 | public function getFkPilot() |
||
1213 | |||
1214 | /** |
||
1215 | * @param mixed $fk_pilot |
||
1216 | * |
||
1217 | * @return Bbcvols |
||
1218 | */ |
||
1219 | public function setFkPilot($fk_pilot) |
||
1224 | |||
1225 | /** |
||
1226 | * @return mixed |
||
1227 | */ |
||
1228 | public function getFkOrganisateur() |
||
1232 | |||
1233 | /** |
||
1234 | * @param mixed $fk_organisateur |
||
1235 | * |
||
1236 | * @return Bbcvols |
||
1237 | */ |
||
1238 | public function setFkOrganisateur($fk_organisateur) |
||
1243 | |||
1244 | /** |
||
1245 | * @return mixed |
||
1246 | */ |
||
1247 | public function getisFacture() |
||
1251 | |||
1252 | /** |
||
1253 | * @param mixed $is_facture |
||
1254 | * |
||
1255 | * @return Bbcvols |
||
1256 | */ |
||
1257 | public function setIsFacture($is_facture) |
||
1262 | |||
1263 | /** |
||
1264 | * @return int |
||
1265 | */ |
||
1266 | public function getCost() |
||
1270 | |||
1271 | /** |
||
1272 | * @param int $cost |
||
1273 | * |
||
1274 | * @return Bbcvols |
||
1275 | */ |
||
1276 | public function setCost($cost) |
||
1281 | |||
1282 | /** |
||
1283 | * @return mixed |
||
1284 | */ |
||
1285 | public function getFkReceiver() |
||
1289 | |||
1290 | /** |
||
1291 | * @param mixed $fk_receiver |
||
1292 | * |
||
1293 | * @return Bbcvols |
||
1294 | */ |
||
1295 | public function setFkReceiver($fk_receiver) |
||
1300 | |||
1301 | /** |
||
1302 | * @return mixed |
||
1303 | */ |
||
1304 | public function getJustifKilometers() |
||
1308 | |||
1309 | /** |
||
1310 | * @param mixed $justif_kilometers |
||
1311 | * |
||
1312 | * @return Bbcvols |
||
1313 | */ |
||
1314 | public function setJustifKilometers($justif_kilometers) |
||
1319 | |||
1320 | |||
1321 | } |
||
1322 |
This check looks for assignments to scalar types that may be of the wrong type.
To ensure the code behaves as expected, it may be a good idea to add an explicit type cast.