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:
1 | <?php |
||
22 | class V2FleetModel extends KeyedModel { |
||
23 | protected $location; |
||
24 | |||
25 | /** |
||
26 | * Name of table for this entity |
||
27 | * |
||
28 | * @var string $tableName |
||
29 | */ |
||
30 | protected $tableName = 'fleets'; |
||
31 | |||
32 | protected $exceptionClass = 'Entity\EntityException'; |
||
33 | protected $entityContainerClass = 'V2Fleet\V2FleetContainer'; |
||
34 | |||
35 | protected $newProperties = array( |
||
36 | 'dbId' => array( |
||
37 | P_DB_FIELD => 'fleet_id', |
||
38 | ), |
||
39 | 'ownerId' => array(P_DB_FIELD => 'fleet_owner',), |
||
40 | 'arriveOwnerId' => array(P_DB_FIELD => 'fleet_target_owner'), |
||
41 | 'departurePlanetId' => array(P_DB_FIELD => 'fleet_start_planet_id'), |
||
42 | 'arrivePlanetId' => array(P_DB_FIELD => 'fleet_end_planet_id'), |
||
43 | |||
44 | 'missionType' => array( |
||
45 | P_DB_FIELD => 'fleet_mission', |
||
46 | P_DB_FIELD_TYPE => TYPE_INTEGER, |
||
47 | ), |
||
48 | 'status' => array( |
||
49 | P_DB_FIELD => 'fleet_mess', |
||
50 | P_DB_FIELD_TYPE => TYPE_INTEGER, |
||
51 | ), |
||
52 | 'groupId' => array(P_DB_FIELD => 'fleet_group',), |
||
53 | |||
54 | |||
55 | // 'fleet_start_galaxy' => array(P_DB_FIELD => 'fleet_start_galaxy'), |
||
|
|||
56 | // 'fleet_start_system' => array(P_DB_FIELD => 'fleet_start_system'), |
||
57 | // 'fleet_start_planet' => array(P_DB_FIELD => 'fleet_start_planet'), |
||
58 | // 'fleet_start_type' => array(P_DB_FIELD => 'fleet_start_type'), |
||
59 | // 'fleet_end_galaxy' => array(P_DB_FIELD => 'fleet_end_galaxy'), |
||
60 | // 'fleet_end_system' => array(P_DB_FIELD => 'fleet_end_system'), |
||
61 | // 'fleet_end_planet' => array(P_DB_FIELD => 'fleet_end_planet'), |
||
62 | // 'fleet_end_type' => array(P_DB_FIELD => 'fleet_end_type'), |
||
63 | // 'fleet_resource_metal' => array(P_DB_FIELD => 'fleet_resource_metal'), |
||
64 | // 'fleet_resource_crystal' => array(P_DB_FIELD => 'fleet_resource_crystal'), |
||
65 | // 'fleet_resource_deuterium' => array(P_DB_FIELD => 'fleet_resource_deuterium'), |
||
66 | |||
67 | |||
68 | 'vectorDeparture' => array(P_DB_FIELD => 'fleet_start_galaxy'), |
||
69 | 'vectorArrive' => array(P_DB_FIELD => 'fleet_end_galaxy'), |
||
70 | |||
71 | 'timeDeparture' => array( |
||
72 | P_DB_FIELD => 'start_time', |
||
73 | P_DB_FIELD_TYPE => TYPE_INTEGER, |
||
74 | ), |
||
75 | 'timeArrive' => array( |
||
76 | P_DB_FIELD => 'fleet_start_time', |
||
77 | P_DB_FIELD_TYPE => TYPE_INTEGER, |
||
78 | ), |
||
79 | 'timeComplete' => array( |
||
80 | P_DB_FIELD => 'fleet_end_stay', |
||
81 | P_DB_FIELD_TYPE => TYPE_INTEGER, |
||
82 | ), |
||
83 | 'timeReturn' => array( |
||
84 | P_DB_FIELD => 'fleet_end_time', |
||
85 | P_DB_FIELD_TYPE => TYPE_INTEGER, |
||
86 | ), |
||
87 | |||
88 | 'shipsCount' => array(P_DB_FIELD => 'fleet_amount'), |
||
89 | 'units' => array(), |
||
90 | 'isReturning' => array(), |
||
91 | ); |
||
92 | |||
93 | public function __construct(\Common\GlobalContainer $gc) { |
||
135 | |||
136 | public function importVector(V2FleetContainer $that, $propertyName) { |
||
143 | |||
144 | public function exportVector(V2FleetContainer $that, $propertyName) { |
||
151 | |||
152 | /** |
||
153 | * @param array $array |
||
154 | * |
||
155 | * @return V2FleetContainer |
||
156 | */ |
||
157 | public function fromArray($array) { |
||
175 | |||
176 | protected function save(V2FleetContainer $cFleet) { |
||
179 | |||
180 | public function isEmpty(V2FleetContainer $cFleet) { |
||
188 | |||
189 | /** |
||
190 | * Forcibly returns fleet before time outs |
||
191 | */ |
||
192 | protected function doReturn(V2FleetContainer $cFleet) { |
||
225 | |||
226 | public function commandReturn($userId, $fleet_id) { |
||
238 | |||
239 | } |
||
240 |
Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.
The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.
This check looks for comments that seem to be mostly valid code and reports them.