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 |
||
12 | class Player extends UnitContainer { |
||
13 | |||
14 | /** |
||
15 | * Type of this location |
||
16 | * |
||
17 | * @var int $locationType |
||
18 | */ |
||
19 | public static $locationType = LOC_USER; |
||
20 | |||
21 | |||
22 | /** |
||
23 | * Returns location's player owner ID |
||
24 | * |
||
25 | * @return int |
||
26 | */ |
||
27 | public function getPlayerOwnerId() { |
||
30 | |||
31 | |||
32 | public function setLocatedAt($location) { |
||
35 | |||
36 | |||
37 | |||
38 | |||
39 | |||
40 | |||
41 | |||
42 | |||
43 | |||
44 | // Inherited from DBRow |
||
45 | /** |
||
46 | * Table name in DB |
||
47 | * |
||
48 | * @var string |
||
49 | */ |
||
50 | protected static $_table = 'users'; |
||
51 | /** |
||
52 | * Name of ID field in DB |
||
53 | * |
||
54 | * @var string |
||
55 | */ |
||
56 | protected static $_dbIdFieldName = 'id'; |
||
57 | /** |
||
58 | * DB_ROW to Class translation scheme |
||
59 | * |
||
60 | * @var array |
||
61 | */ |
||
62 | protected static $_properties = array( |
||
63 | 'dbId' => array( |
||
64 | P_DB_FIELD => 'id', |
||
65 | // P_FUNC_INPUT => 'floatval', |
||
|
|||
66 | ), |
||
67 | 'name' => array( |
||
68 | P_DB_FIELD => 'username', |
||
69 | ), |
||
70 | 'authLevel' => array( |
||
71 | P_DB_FIELD => 'authlevel', |
||
72 | P_FUNC_INPUT => 'intval', |
||
73 | ), |
||
74 | 'statPointsTotal' => array( |
||
75 | P_DB_FIELD => 'total_points', |
||
76 | P_FUNC_INPUT => 'floatval', |
||
77 | ), |
||
78 | 'capitalPlanetId' => array( |
||
79 | P_DB_FIELD => 'id_planet', |
||
80 | // P_FUNC_INPUT => 'floatval', |
||
81 | ), |
||
82 | ); |
||
83 | |||
84 | |||
85 | // Innate properties |
||
86 | /** |
||
87 | * @var Bonus $player_bonus |
||
88 | */ |
||
89 | public $player_bonus = null; |
||
90 | /** |
||
91 | * @var array |
||
92 | */ |
||
93 | protected $db_row = array(); |
||
94 | |||
95 | protected $_name = ''; |
||
96 | protected $_authLevel = AUTH_LEVEL_REGISTERED; |
||
97 | protected $_capitalPlanetId = 0; |
||
98 | protected $_statPointsTotal = 0; |
||
99 | |||
100 | // public $avatar = 0; |
||
101 | // public $vacation = 0; |
||
102 | // public $banaday = 0; |
||
103 | // public $dark_matter = 0; |
||
104 | // public $dark_matter_total = 0; |
||
105 | // public $player_rpg_explore_xp = 0; |
||
106 | // public $player_rpg_explore_level = 0; |
||
107 | // public $ally_id = 0; |
||
108 | // public $ally_tag = 0; |
||
109 | // public $ally_name = 0; |
||
110 | // public $ally_register_time = 0; |
||
111 | // public $ally_rank_id = 0; |
||
112 | // public $lvl_minier = 0; |
||
113 | // public $xpminier = 0; |
||
114 | // public $player_rpg_tech_xp = 0; |
||
115 | // public $player_rpg_tech_level = 0; |
||
116 | // public $lvl_raid = 0; |
||
117 | // public $xpraid = 0; |
||
118 | // public $raids = 0; |
||
119 | // public $raidsloose = 0; |
||
120 | // public $raidswin = 0; |
||
121 | // public $new_message = 0; |
||
122 | // public $mnl_alliance = 0; |
||
123 | // public $mnl_joueur = 0; |
||
124 | // public $mnl_attaque = 0; |
||
125 | // public $mnl_spy = 0; |
||
126 | // public $mnl_exploit = 0; |
||
127 | // public $mnl_transport = 0; |
||
128 | // public $mnl_expedition = 0; |
||
129 | // public $mnl_buildlist = 0; |
||
130 | // public $msg_admin = 0; |
||
131 | // public $deltime = 0; |
||
132 | // public $news_lastread = 0; |
||
133 | // public $total_rank = 0; |
||
134 | // public $password = 0; |
||
135 | // public $salt = 0; |
||
136 | // public $email = 0; |
||
137 | // public $email_2 = 0; |
||
138 | // public $lang = 0; |
||
139 | // public $sign = 0; |
||
140 | // public $galaxy = 0; |
||
141 | // public $system = 0; |
||
142 | // public $planet = 0; |
||
143 | // public $current_planet = 0; |
||
144 | // public $user_lastip = 0; |
||
145 | // public $user_last_proxy = 0; |
||
146 | // public $user_last_browser_id = 0; |
||
147 | // public $register_time = 0; |
||
148 | // public $onlinetime = 0; |
||
149 | // public $que_processed = 0; |
||
150 | // public $dpath = 0; |
||
151 | // public $design = 0; |
||
152 | // public $noipcheck = 0; |
||
153 | // public $options = 0; |
||
154 | // public $user_as_ally = 0; |
||
155 | // public $metal = 0; |
||
156 | // public $crystal = 0; |
||
157 | // public $deuterium = 0; |
||
158 | // public $user_birthday = 0; |
||
159 | // public $user_birthday_celebrated = 0; |
||
160 | // public $player_race = 0; |
||
161 | // public $vacation_next = 0; |
||
162 | // public $metamatter = 0; |
||
163 | // public $metamatter_total = 0; |
||
164 | // public $admin_protection = 0; |
||
165 | // public $ip_int = 0; |
||
166 | // public $user_bot = 0; |
||
167 | // public $gender = 0; |
||
168 | // public $immortal = 0; |
||
169 | // public $parent_account_id = 0; |
||
170 | // public $server_name = 0; |
||
171 | // public $parent_account_global = 0; |
||
172 | |||
173 | |||
174 | /** |
||
175 | * Player constructor. |
||
176 | */ |
||
177 | public function __construct() { |
||
182 | |||
183 | public function isEmpty() { |
||
186 | |||
187 | /** |
||
188 | * Loading object from DB by primary ID |
||
189 | * |
||
190 | * @param int $dbId |
||
191 | * @param bool $lockSkip |
||
192 | * |
||
193 | * @return |
||
194 | */ |
||
195 | public function dbLoad($dbId, $lockSkip = false) { |
||
201 | |||
202 | /** |
||
203 | * Меняет активную планету игрока на столицу, если активная планета равна $captured_planet_id |
||
204 | * |
||
205 | * @param int $captured_planet_id |
||
206 | * |
||
207 | * @return array|bool|mysqli_result|null |
||
208 | */ |
||
209 | public function db_user_change_active_planet_to_capital($captured_planet_id) { |
||
214 | |||
215 | public function calcColonyCount() { |
||
218 | |||
219 | /** |
||
220 | * @param int $astrotech |
||
221 | * |
||
222 | * @return int|mixed |
||
223 | */ |
||
224 | public function calcColonyMaxCount($astrotech = -1) { |
||
243 | |||
244 | |||
245 | /** |
||
246 | * @return array |
||
247 | */ |
||
248 | public function getDbRow() { |
||
251 | |||
252 | |||
253 | /** |
||
254 | * @return string |
||
255 | */ |
||
256 | protected function getName() { |
||
262 | |||
263 | /** |
||
264 | * @param string $name |
||
265 | */ |
||
266 | protected function setName($name) { |
||
270 | |||
271 | |||
272 | /** |
||
273 | * Lock all fields that belongs to operation |
||
274 | * |
||
275 | * @param int $dbId |
||
276 | * |
||
277 | * @return |
||
278 | * param DBLock $dbRow - Object that accumulates locks |
||
279 | * |
||
280 | */ |
||
281 | public function dbGetLockById($dbId) { |
||
284 | |||
285 | public function dbRowParse($db_row) { |
||
294 | |||
295 | } |
||
296 |
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.