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[]|array |
||
93 | */ |
||
94 | private $orderIds; |
||
95 | |||
96 | /** |
||
97 | * @var Commande[]|array |
||
98 | */ |
||
99 | private $orders; |
||
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) |
||
141 | |||
142 | /** |
||
143 | * Create a flight |
||
144 | * |
||
145 | * @param User $user User that creates |
||
146 | * @param bool $notrigger false=launch triggers after, true=disable triggers |
||
147 | * |
||
148 | * @return int <0 if KO, Id of created object if OK |
||
149 | * @throws Exception |
||
150 | */ |
||
151 | public function create(User $user, $notrigger = false) |
||
294 | |||
295 | /** |
||
296 | * Inserts the order linked to the flight |
||
297 | */ |
||
298 | private function insertOrders(){ |
||
334 | |||
335 | /** |
||
336 | * Inserts the order linked to the flight |
||
337 | */ |
||
338 | private function updateOrders(){ |
||
347 | |||
348 | /** |
||
349 | * Inserts the order linked to the flight |
||
350 | */ |
||
351 | private function deleteOrders(){ |
||
368 | |||
369 | /** |
||
370 | * Load object in memory from the database |
||
371 | * |
||
372 | * @param int $id Id object |
||
373 | * @param string $ref Ref |
||
374 | * |
||
375 | * @return int <0 if KO, 0 if not found, >0 if OK |
||
376 | * @throws Exception |
||
377 | */ |
||
378 | public function fetch($id, $ref = null) |
||
460 | |||
461 | /** |
||
462 | * @param User $user User that modifies |
||
463 | * @param bool $notrigger false=launch triggers after, true=disable triggers |
||
464 | * |
||
465 | * @return int <0 if KO, >0 if OK |
||
466 | * @throws Exception |
||
467 | */ |
||
468 | public function update(User $user, $notrigger = false) |
||
588 | |||
589 | /** |
||
590 | * Delete object in database |
||
591 | * |
||
592 | * @param User $user User that deletes |
||
593 | * @param bool $notrigger false=launch triggers after, true=disable triggers |
||
594 | * |
||
595 | * @return int <0 if KO, >0 if OK |
||
596 | * @throws Exception |
||
597 | */ |
||
598 | public function delete(User $user, $notrigger = false) |
||
636 | |||
637 | /** |
||
638 | * Return a link to the user card (with optionaly the picto) |
||
639 | * Use this->id,this->lastname, this->firstname |
||
640 | * |
||
641 | * @param int $withpicto Include picto in link (0=No picto, 1=Include picto into link, 2=Only picto) |
||
642 | * @param string $option On what the link point to |
||
643 | * @param integer $notooltip 1=Disable tooltip |
||
644 | * @param int $maxlen Max length of visible user name |
||
645 | * @param string $morecss Add more css on link |
||
646 | * |
||
647 | * @return string String with URL |
||
648 | */ |
||
649 | public function getNomUrl($withpicto = 0, $option = '', $notooltip = 0, $maxlen = 24, $morecss = '') |
||
679 | |||
680 | /** |
||
681 | * Retourne le libelle du status d'un user (actif, inactif) |
||
682 | * |
||
683 | * @param int $mode 0=libelle long, 1=libelle court, 2=Picto + Libelle court, 3=Picto, 4=Picto + Libelle long, 5=Libelle court + Picto |
||
684 | * |
||
685 | * @return string Label of status |
||
686 | */ |
||
687 | public function getLibStatut($mode = 0) |
||
691 | |||
692 | /** |
||
693 | * Renvoi le libelle d'un status donne |
||
694 | * |
||
695 | * @param int $status Id status |
||
696 | * @param int $mode 0=libelle long, 1=libelle court, 2=Picto + Libelle court, 3=Picto, 4=Picto + Libelle long, 5=Libelle court + Picto |
||
697 | * |
||
698 | * @return string Label of status |
||
699 | */ |
||
700 | private function LibStatut($status, $mode = 0) |
||
758 | |||
759 | /** |
||
760 | * @return string |
||
761 | */ |
||
762 | public function __toString() |
||
766 | |||
767 | /** |
||
768 | * @return string |
||
769 | */ |
||
770 | public function toString() |
||
774 | |||
775 | /** |
||
776 | * @return string |
||
777 | */ |
||
778 | public function getStatus() |
||
782 | |||
783 | /** |
||
784 | * @return boolean |
||
785 | */ |
||
786 | public function hasFacture() |
||
792 | |||
793 | /** |
||
794 | * @param int $userId |
||
795 | * |
||
796 | * @return User |
||
797 | */ |
||
798 | private function fetchUser($userId) |
||
805 | |||
806 | /** |
||
807 | * @return Bbc_ballons |
||
808 | */ |
||
809 | private function fetchBalloon() |
||
816 | |||
817 | /** |
||
818 | * @return Bbc_ballons |
||
819 | */ |
||
820 | public function getBalloon() |
||
828 | |||
829 | /** |
||
830 | * @return User |
||
831 | */ |
||
832 | public function getPilot() |
||
840 | |||
841 | /** |
||
842 | * @return int |
||
843 | */ |
||
844 | public function getPilotId() |
||
848 | |||
849 | /** |
||
850 | * @return int |
||
851 | */ |
||
852 | public function getOrganisatorId() |
||
856 | |||
857 | /** |
||
858 | * @return Bbctypes |
||
859 | */ |
||
860 | public function getFlightType() |
||
867 | |||
868 | /** |
||
869 | * @return string |
||
870 | */ |
||
871 | public function getComment() |
||
875 | |||
876 | /** |
||
877 | * @return string |
||
878 | */ |
||
879 | public function getIncident() |
||
883 | |||
884 | /** |
||
885 | * Return true if the number of pax is greater than 0 |
||
886 | * |
||
887 | * @return boolean |
||
888 | */ |
||
889 | public function hasPax() |
||
893 | |||
894 | /** |
||
895 | * Regarding the type of the flight give an indication if the flight must have pax to be valid. |
||
896 | * |
||
897 | * @return boolean |
||
898 | */ |
||
899 | public function mustHavePax() |
||
903 | |||
904 | /** |
||
905 | * Returns true if the amount requested by the flight is 0. |
||
906 | * |
||
907 | * @return boolean |
||
908 | */ |
||
909 | public function isFree() |
||
913 | |||
914 | /** |
||
915 | * @return int |
||
916 | */ |
||
917 | public function getAmountReceived() |
||
921 | |||
922 | /** |
||
923 | * @return float |
||
924 | */ |
||
925 | public function getAmountPerPassenger() |
||
930 | |||
931 | /** |
||
932 | * @return boolean |
||
933 | */ |
||
934 | public function hasReceiver() |
||
938 | |||
939 | /** |
||
940 | * @return boolean |
||
941 | */ |
||
942 | public function hasKilometers() |
||
946 | |||
947 | /** |
||
948 | * @return boolean |
||
949 | */ |
||
950 | public function hasKilometersDescription() |
||
954 | |||
955 | /** |
||
956 | * @return int |
||
957 | */ |
||
958 | public function getKilometers() |
||
962 | |||
963 | /** |
||
964 | * @return string |
||
965 | */ |
||
966 | public function getPlaces() |
||
970 | |||
971 | /** |
||
972 | * @return string |
||
973 | */ |
||
974 | public function getDescription() |
||
978 | |||
979 | /** |
||
980 | * @return string |
||
981 | */ |
||
982 | public function getPassengerNames() |
||
986 | |||
987 | /** |
||
988 | * @param string $passengerNames |
||
989 | * |
||
990 | * @return Bbcvols |
||
991 | */ |
||
992 | public function setPassengerNames($passengerNames) |
||
997 | |||
998 | /** |
||
999 | * @return int |
||
1000 | */ |
||
1001 | public function getNumberOfPassengers() |
||
1005 | |||
1006 | /** |
||
1007 | * @return int[]|array |
||
1008 | */ |
||
1009 | public function getOrderIds() |
||
1013 | |||
1014 | /** |
||
1015 | * @param int $orderId |
||
1016 | * @param int $nbrPassengers |
||
1017 | */ |
||
1018 | public function addOrderId($orderId, $nbrPassengers){ |
||
1029 | |||
1030 | /** |
||
1031 | * Is an instruction flight (T6/T7) |
||
1032 | */ |
||
1033 | public function isInstruction() |
||
1037 | |||
1038 | /** |
||
1039 | * @return bool |
||
1040 | */ |
||
1041 | public function isLinkedToOrder() |
||
1045 | |||
1046 | /** |
||
1047 | * Fetch the orders based on the order ids. |
||
1048 | */ |
||
1049 | public function fetchOrder() |
||
1078 | |||
1079 | /** |
||
1080 | * @return Commande[]|array |
||
1081 | */ |
||
1082 | public function getOrders() |
||
1089 | |||
1090 | /** |
||
1091 | * Flag the flight as billed |
||
1092 | * |
||
1093 | * @return $this |
||
1094 | */ |
||
1095 | public function bill() |
||
1100 | |||
1101 | /** |
||
1102 | * @return boolean |
||
1103 | */ |
||
1104 | public function isBilled() |
||
1108 | |||
1109 | /** |
||
1110 | * @return DateTime |
||
1111 | */ |
||
1112 | public function getDate() |
||
1116 | |||
1117 | /** |
||
1118 | * @param string $date |
||
1119 | * |
||
1120 | * @return Bbcvols |
||
1121 | */ |
||
1122 | public function setDate($date) |
||
1127 | |||
1128 | /** |
||
1129 | * @return mixed |
||
1130 | */ |
||
1131 | public function getLieuD() |
||
1135 | |||
1136 | /** |
||
1137 | * @param mixed $lieuD |
||
1138 | * |
||
1139 | * @return Bbcvols |
||
1140 | */ |
||
1141 | public function setLieuD($lieuD) |
||
1146 | |||
1147 | /** |
||
1148 | * @return mixed |
||
1149 | */ |
||
1150 | public function getLieuA() |
||
1154 | |||
1155 | /** |
||
1156 | * @param mixed $lieuA |
||
1157 | * |
||
1158 | * @return Bbcvols |
||
1159 | */ |
||
1160 | public function setLieuA($lieuA) |
||
1165 | |||
1166 | /** |
||
1167 | * @return mixed |
||
1168 | */ |
||
1169 | public function getHeureD() |
||
1173 | |||
1174 | /** |
||
1175 | * @param mixed $heureD |
||
1176 | * |
||
1177 | * @return Bbcvols |
||
1178 | */ |
||
1179 | public function setHeureD($heureD) |
||
1184 | |||
1185 | /** |
||
1186 | * @return mixed |
||
1187 | */ |
||
1188 | public function getHeureA() |
||
1192 | |||
1193 | /** |
||
1194 | * @param mixed $heureA |
||
1195 | * |
||
1196 | * @return Bbcvols |
||
1197 | */ |
||
1198 | public function setHeureA($heureA) |
||
1203 | |||
1204 | /** |
||
1205 | * @return mixed |
||
1206 | */ |
||
1207 | public function getBBCBallonsIdBBCBallons() |
||
1211 | |||
1212 | /** |
||
1213 | * @param mixed $BBC_ballons_idBBC_ballons |
||
1214 | * |
||
1215 | * @return Bbcvols |
||
1216 | */ |
||
1217 | public function setBBCBallonsIdBBCBallons($BBC_ballons_idBBC_ballons) |
||
1222 | |||
1223 | /** |
||
1224 | * @return mixed |
||
1225 | */ |
||
1226 | public function getNbrPax() |
||
1230 | |||
1231 | /** |
||
1232 | * @param mixed $nbrPax |
||
1233 | * |
||
1234 | * @return Bbcvols |
||
1235 | */ |
||
1236 | public function setNbrPax($nbrPax) |
||
1241 | |||
1242 | /** |
||
1243 | * @return mixed |
||
1244 | */ |
||
1245 | public function getRemarque() |
||
1249 | |||
1250 | /** |
||
1251 | * @param mixed $remarque |
||
1252 | * |
||
1253 | * @return Bbcvols |
||
1254 | */ |
||
1255 | public function setRemarque($remarque) |
||
1260 | |||
1261 | /** |
||
1262 | * @return mixed |
||
1263 | */ |
||
1264 | public function getIncidents() |
||
1268 | |||
1269 | /** |
||
1270 | * @param mixed $incidents |
||
1271 | * |
||
1272 | * @return Bbcvols |
||
1273 | */ |
||
1274 | public function setIncidents($incidents) |
||
1279 | |||
1280 | /** |
||
1281 | * @return mixed |
||
1282 | */ |
||
1283 | public function getFkType() |
||
1287 | |||
1288 | /** |
||
1289 | * @param mixed $fk_type |
||
1290 | * |
||
1291 | * @return Bbcvols |
||
1292 | */ |
||
1293 | public function setFkType($fk_type) |
||
1298 | |||
1299 | /** |
||
1300 | * @return mixed |
||
1301 | */ |
||
1302 | public function getFkPilot() |
||
1306 | |||
1307 | /** |
||
1308 | * @param mixed $fk_pilot |
||
1309 | * |
||
1310 | * @return Bbcvols |
||
1311 | */ |
||
1312 | public function setFkPilot($fk_pilot) |
||
1317 | |||
1318 | /** |
||
1319 | * @return mixed |
||
1320 | */ |
||
1321 | public function getFkOrganisateur() |
||
1325 | |||
1326 | /** |
||
1327 | * @param mixed $fk_organisateur |
||
1328 | * |
||
1329 | * @return Bbcvols |
||
1330 | */ |
||
1331 | public function setFkOrganisateur($fk_organisateur) |
||
1336 | |||
1337 | /** |
||
1338 | * @return mixed |
||
1339 | */ |
||
1340 | public function getisFacture() |
||
1344 | |||
1345 | /** |
||
1346 | * @param mixed $is_facture |
||
1347 | * |
||
1348 | * @return Bbcvols |
||
1349 | */ |
||
1350 | public function setIsFacture($is_facture) |
||
1355 | |||
1356 | /** |
||
1357 | * @return int |
||
1358 | */ |
||
1359 | public function getCost() |
||
1363 | |||
1364 | /** |
||
1365 | * @param int $cost |
||
1366 | * |
||
1367 | * @return Bbcvols |
||
1368 | */ |
||
1369 | public function setCost($cost) |
||
1374 | |||
1375 | /** |
||
1376 | * @return mixed |
||
1377 | */ |
||
1378 | public function getFkReceiver() |
||
1382 | |||
1383 | /** |
||
1384 | * @param mixed $fk_receiver |
||
1385 | * |
||
1386 | * @return Bbcvols |
||
1387 | */ |
||
1388 | public function setFkReceiver($fk_receiver) |
||
1393 | |||
1394 | /** |
||
1395 | * @return mixed |
||
1396 | */ |
||
1397 | public function getJustifKilometers() |
||
1401 | |||
1402 | /** |
||
1403 | * @param mixed $justif_kilometers |
||
1404 | * |
||
1405 | * @return Bbcvols |
||
1406 | */ |
||
1407 | public function setJustifKilometers($justif_kilometers) |
||
1412 | |||
1413 | |||
1414 | } |
||
1415 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.