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 Team 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 Team, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
13 | class Team extends AvatarModel |
||
14 | { |
||
15 | /** |
||
16 | * The description of the team written in markdown |
||
17 | * |
||
18 | * @var string |
||
19 | */ |
||
20 | protected $description; |
||
21 | |||
22 | /** |
||
23 | * The creation date of the team |
||
24 | * |
||
25 | * @var TimeDate |
||
26 | */ |
||
27 | protected $created; |
||
28 | |||
29 | /** |
||
30 | * The team's current elo |
||
31 | * |
||
32 | * @var int |
||
33 | */ |
||
34 | protected $elo; |
||
35 | |||
36 | /** |
||
37 | * The team's activity |
||
38 | * |
||
39 | * null if we haven't calculated it yet |
||
40 | * |
||
41 | * @var float|null |
||
42 | */ |
||
43 | protected $activity = null; |
||
44 | |||
45 | /** |
||
46 | * The id of the team leader |
||
47 | * |
||
48 | * @var int |
||
49 | */ |
||
50 | protected $leader; |
||
51 | |||
52 | /** |
||
53 | * The number of matches won |
||
54 | * |
||
55 | * @var int |
||
56 | */ |
||
57 | protected $matches_won; |
||
58 | |||
59 | /** |
||
60 | * The number of matches lost |
||
61 | * |
||
62 | * @var int |
||
63 | */ |
||
64 | protected $matches_lost; |
||
65 | |||
66 | /** |
||
67 | * The number of matches tied |
||
68 | * |
||
69 | * @var int |
||
70 | */ |
||
71 | protected $matches_draw; |
||
72 | |||
73 | /** |
||
74 | * The total number of matches |
||
75 | * |
||
76 | * @var int |
||
77 | */ |
||
78 | protected $matches_total; |
||
79 | |||
80 | /** |
||
81 | * The number of members |
||
82 | * |
||
83 | * @var int |
||
84 | */ |
||
85 | protected $members; |
||
86 | |||
87 | /** |
||
88 | * The team's status |
||
89 | * |
||
90 | * @var string |
||
91 | */ |
||
92 | protected $status; |
||
93 | |||
94 | /** |
||
95 | * A list of cached matches to calculate team activity |
||
96 | * |
||
97 | * @var Match[]|null |
||
98 | */ |
||
99 | public static $cachedMatches = null; |
||
100 | |||
101 | /** |
||
102 | * The name of the database table used for queries |
||
103 | */ |
||
104 | const TABLE = "teams"; |
||
105 | |||
106 | /** |
||
107 | * The location where avatars will be stored |
||
108 | */ |
||
109 | const AVATAR_LOCATION = "/web/assets/imgs/avatars/teams/"; |
||
110 | |||
111 | const CREATE_PERMISSION = Permission::CREATE_TEAM; |
||
112 | const EDIT_PERMISSION = Permission::EDIT_TEAM; |
||
113 | const SOFT_DELETE_PERMISSION = Permission::SOFT_DELETE_TEAM; |
||
114 | const HARD_DELETE_PERMISSION = Permission::HARD_DELETE_TEAM; |
||
115 | |||
116 | /** |
||
117 | * {@inheritdoc} |
||
118 | */ |
||
119 | 18 | protected function assignResult($team) |
|
137 | |||
138 | /** |
||
139 | * Adds a new member to the team |
||
140 | * |
||
141 | * @param int $id The id of the player to add to the team |
||
142 | * |
||
143 | * @return bool|null True if both the player was added to the team AND the team member count was incremented |
||
144 | */ |
||
145 | 18 | public function addMember($id) |
|
156 | |||
157 | /** |
||
158 | * Increase or decrease the ELO of the team |
||
159 | * |
||
160 | * @param int $adjust The value to be added to the current ELO (negative to substract) |
||
161 | */ |
||
162 | 9 | public function changeElo($adjust) |
|
167 | |||
168 | /** |
||
169 | * Change the ELO of the team |
||
170 | * |
||
171 | * @param int $elo The new team ELO |
||
172 | */ |
||
173 | public function setElo($elo) |
||
177 | |||
178 | /** |
||
179 | * Increment the team's match count |
||
180 | * |
||
181 | * @param int $adjust The number to add to the current matches number (negative to substract) |
||
182 | * @param string $type The match count that should be changed. Can be 'win', 'draw' or 'loss' |
||
183 | */ |
||
184 | 9 | public function changeMatchCount($adjust, $type) |
|
205 | |||
206 | /** |
||
207 | * Decrement the team's match count by one |
||
208 | * |
||
209 | * @param string $type The type of the match. Can be 'win', 'draw' or 'loss' |
||
210 | */ |
||
211 | public function decrementMatchCount($type) |
||
215 | |||
216 | /** |
||
217 | * Get the activity of the team |
||
218 | * |
||
219 | * @return float The team's activity |
||
220 | */ |
||
221 | 1 | public function getActivity() |
|
243 | |||
244 | /** |
||
245 | * Get the creation date of the team |
||
246 | * |
||
247 | * @return TimeDate The creation date of the team |
||
248 | */ |
||
249 | 2 | public function getCreationDate() |
|
253 | |||
254 | /** |
||
255 | * Get the description of the team |
||
256 | * |
||
257 | * @param bool $md false for HTML format, true for the original markdown |
||
|
|||
258 | * @return string The description of the team |
||
259 | */ |
||
260 | 1 | public function getDescription() |
|
264 | |||
265 | /** |
||
266 | * Get the current elo of the team |
||
267 | * |
||
268 | * @return int The elo of the team |
||
269 | */ |
||
270 | 9 | public function getElo() |
|
274 | |||
275 | /** |
||
276 | * Get the leader of the team |
||
277 | * |
||
278 | * @return Player The object representing the team leader |
||
279 | */ |
||
280 | 2 | public function getLeader() |
|
284 | |||
285 | /** |
||
286 | * Get the matches this team has participated in |
||
287 | * |
||
288 | * @param string $matchType The filter for match types: "all", "wins", "losses", or "draws" |
||
289 | * @param int $count The amount of matches to be retrieved |
||
290 | * @param int $page The number of the page to return |
||
291 | * |
||
292 | * @return Match[] The array of match IDs this team has participated in |
||
293 | */ |
||
294 | 2 | public function getMatches($matchType = "all", $count = 5, $page = 1) |
|
303 | |||
304 | /** |
||
305 | * Get the number of matches that resulted as a draw |
||
306 | * |
||
307 | * @return int The number of matches, respectively |
||
308 | */ |
||
309 | 1 | public function getMatchesDraw() |
|
313 | |||
314 | /** |
||
315 | * Get the number of matches that the team has lost |
||
316 | * |
||
317 | * @return int The number of matches, respectively |
||
318 | */ |
||
319 | 1 | public function getMatchesLost() |
|
323 | |||
324 | /** |
||
325 | * Get the URL that points to the team's list of matches |
||
326 | * |
||
327 | * @return string The team's list of matches |
||
328 | */ |
||
329 | public function getMatchesURL() |
||
333 | |||
334 | /** |
||
335 | * Get the number of matches that the team has won |
||
336 | * |
||
337 | * @return int The number of matches, respectively |
||
338 | */ |
||
339 | 1 | public function getMatchesWon() |
|
343 | |||
344 | /** |
||
345 | * Get the members on the team |
||
346 | * |
||
347 | * @return Player[] The members on the team |
||
348 | */ |
||
349 | 2 | public function getMembers() |
|
371 | |||
372 | /** |
||
373 | * Get the name of the team |
||
374 | * |
||
375 | * @return string The name of the team |
||
376 | */ |
||
377 | 18 | public function getName() |
|
384 | |||
385 | /** |
||
386 | * Get the name of the team, safe for use in your HTML |
||
387 | * |
||
388 | * @return string The name of the team |
||
389 | */ |
||
390 | 1 | public function getEscapedName() |
|
397 | |||
398 | /** |
||
399 | * Get the number of members on the team |
||
400 | * |
||
401 | * @return int The number of members on the team |
||
402 | */ |
||
403 | 2 | public function getNumMembers() |
|
407 | |||
408 | /** |
||
409 | * Get the total number of matches this team has played |
||
410 | * |
||
411 | * @return int The total number of matches this team has played |
||
412 | */ |
||
413 | 2 | public function getNumTotalMatches() |
|
417 | |||
418 | /** |
||
419 | * Get the rank category a team belongs too based on their ELO |
||
420 | * |
||
421 | * This value is always a multiple of 100 and less than or equal to 2000 |
||
422 | * |
||
423 | * @return int The rank category a team belongs to |
||
424 | */ |
||
425 | 1 | public function getRankValue() |
|
429 | |||
430 | /** |
||
431 | * Get the HTML for an image with the rank symbol |
||
432 | * |
||
433 | * @return string The HTML for a rank image |
||
434 | */ |
||
435 | 1 | public function getRankImageLiteral() |
|
439 | |||
440 | /** |
||
441 | * Increment the team's match count by one |
||
442 | * |
||
443 | * @param string $type The type of the match. Can be 'win', 'draw' or 'loss' |
||
444 | */ |
||
445 | public function incrementMatchCount($type) |
||
449 | |||
450 | /** |
||
451 | * Check if a player is part of this team |
||
452 | * |
||
453 | * @param int $playerID The player to check |
||
454 | * |
||
455 | * @return bool True if the player belongs to this team |
||
456 | */ |
||
457 | 2 | public function isMember($playerID) |
|
463 | |||
464 | /** |
||
465 | * Removes a member from the team |
||
466 | * |
||
467 | * @param int $id The id of the player to remove |
||
468 | * @return void |
||
469 | */ |
||
470 | 2 | public function removeMember($id) |
|
481 | |||
482 | /** |
||
483 | * Update the description of the team |
||
484 | * |
||
485 | * @param string $description The description of the team written as markdown |
||
486 | * @return void |
||
487 | */ |
||
488 | public function setDescription($description) |
||
492 | |||
493 | /** |
||
494 | * Change the status of the team |
||
495 | * |
||
496 | * @param string $newStatus The new status of the team (open, closed, disabled or deleted) |
||
497 | * @return self |
||
498 | */ |
||
499 | public function setStatus($newStatus) |
||
503 | |||
504 | /** |
||
505 | * Change the leader of the team |
||
506 | * |
||
507 | * @param int $leader The ID of the new leader of the team |
||
508 | * @return self |
||
509 | */ |
||
510 | public function setLeader($leader) |
||
514 | |||
515 | /** |
||
516 | * Find if a specific match is the team's last one |
||
517 | * |
||
518 | * @param int $matchID The ID of the match |
||
519 | * @return bool |
||
520 | */ |
||
521 | 1 | public function isLastMatch($matchID) |
|
530 | |||
531 | /** |
||
532 | * {@inheritdoc} |
||
533 | */ |
||
534 | 1 | public function delete() |
|
535 | { |
||
536 | 1 | parent::delete(); |
|
537 | |||
538 | // Remove all the members of a deleted team |
||
539 | 1 | $this->updateProperty($this->members, 'members', 0, 'i'); |
|
540 | 1 | $this->db->query("UPDATE `players` SET `team` = NULL WHERE `team` = ?", |
|
541 | 1 | 'i', $this->id); |
|
542 | 1 | } |
|
543 | |||
544 | /** |
||
545 | * Create a new team |
||
546 | * |
||
547 | * @param string $name The name of the team |
||
548 | * @param int $leader The ID of the person creating the team, also the leader |
||
549 | * @param string $avatar The URL to the team's avatar |
||
550 | * @param string $description The team's description |
||
551 | * @param string $status The team's status (open, closed, disabled or deleted) |
||
552 | * @param string|\TimeDate $created The date the team was created |
||
553 | * |
||
554 | * @return Team An object that represents the newly created team |
||
555 | */ |
||
556 | 19 | public static function createTeam($name, $leader, $avatar, $description, $status = 'closed', $created = "now") |
|
581 | |||
582 | /** |
||
583 | * Get all the teams in the database that are not disabled or deleted |
||
584 | * |
||
585 | * @return Team[] An array of Team IDs |
||
586 | */ |
||
587 | public static function getTeams() |
||
596 | |||
597 | /** |
||
598 | * Get a single team by its name |
||
599 | * |
||
600 | * @param string $name The team name to look for |
||
601 | * @return Team |
||
602 | */ |
||
603 | 1 | public static function getFromName($name) |
|
609 | |||
610 | /** |
||
611 | * Alphabetical order function for use in usort (case-insensitive) |
||
612 | * @return Closure The sort function |
||
613 | */ |
||
614 | public static function getAlphabeticalSort() |
||
620 | |||
621 | /** |
||
622 | * {@inheritdoc} |
||
623 | */ |
||
624 | 1 | public static function getActiveStatuses() |
|
628 | |||
629 | /** |
||
630 | * Get a query builder for teams |
||
631 | * @return QueryBuilder |
||
632 | */ |
||
633 | 1 | View Code Duplication | public static function getQueryBuilder() |
645 | |||
646 | /** |
||
647 | * {@inheritdoc} |
||
648 | */ |
||
649 | 1 | protected function isEditor($player) |
|
653 | } |
||
654 |
This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.
Consider the following example. The parameter
$italy
is not defined by the methodfinale(...)
.The most likely cause is that the parameter was removed, but the annotation was not.