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 |
||
8 | class TeamController extends CRUDController |
||
9 | { |
||
10 | /** |
||
11 | * The new leader if we're changing them |
||
12 | * @var null|Player |
||
13 | */ |
||
14 | private $newLeader = null; |
||
15 | |||
16 | 1 | public function showAction(Team $team) |
|
17 | { |
||
18 | 1 | $matches = $this->getQueryBuilder('Match') |
|
|
|||
19 | 1 | ->with($team) |
|
20 | 1 | ->getSummary($team); |
|
21 | |||
22 | 1 | $wins = $this->getQueryBuilder('Match') |
|
23 | 1 | ->with($team, "win") |
|
24 | 1 | ->getSummary($team); |
|
25 | |||
26 | return array( |
||
27 | 1 | "matches" => $matches, |
|
28 | 1 | "wins" => $wins, |
|
29 | 1 | "team" => $team |
|
30 | ); |
||
31 | } |
||
32 | |||
33 | 1 | public function listAction() |
|
34 | { |
||
35 | 1 | Team::$cachedMatches = $this->getQueryBuilder('Match') |
|
36 | 1 | ->where('time')->isAfter(TimeDate::from('45 days ago')) |
|
37 | 1 | ->active() |
|
38 | 1 | ->getModels($fast = true); |
|
39 | |||
40 | 1 | $teams = $this->getQueryBuilder() |
|
41 | 1 | ->sortBy('elo')->reverse() |
|
42 | 1 | ->getModels($fast = true); |
|
43 | |||
44 | return array( |
||
45 | 1 | "teams" => $teams |
|
46 | ); |
||
47 | } |
||
48 | |||
49 | public function createAction(Player $me) |
||
53 | |||
54 | View Code Duplication | public function editAction(Player $me, Team $team) |
|
55 | { |
||
56 | // TODO: Generating this response is unnecessary |
||
57 | $response = $this->edit($team, $me, "team"); |
||
58 | |||
59 | if ($this->newLeader) { |
||
60 | // Redirect to a confirmation form if we are assigning a new leader |
||
61 | $url = Service::getGenerator()->generate('team_assign_leader', array( |
||
62 | 'team' => $team->getAlias(), |
||
63 | 'player' => $this->newLeader->getAlias() |
||
64 | )); |
||
65 | |||
66 | return new RedirectResponse($url); |
||
67 | } |
||
68 | |||
69 | return $response; |
||
70 | } |
||
71 | |||
72 | 1 | public function deleteAction(Player $me, Team $team) |
|
81 | |||
82 | View Code Duplication | public function joinAction(Team $team, Player $me) |
|
99 | |||
100 | 1 | public function kickAction(Team $team, Player $player, Player $me) |
|
121 | |||
122 | 1 | View Code Duplication | public function abandonAction(Team $team, Player $me) |
140 | |||
141 | public function assignLeaderAction(Team $team, Player $me, Player $player) |
||
161 | |||
162 | /** |
||
163 | * Show a confirmation form to confirm that the user wants to assign a new |
||
164 | * leader to a team |
||
165 | * |
||
166 | * @param Player $leader The new leader |
||
167 | */ |
||
168 | public function newLeader($leader) |
||
172 | |||
173 | protected function validateNew($form) |
||
184 | |||
185 | protected function canCreate($player) |
||
193 | |||
194 | /** |
||
195 | * Make sure that a player can edit a team |
||
196 | * |
||
197 | * Throws an exception if a player is not an admin or the leader of a team |
||
198 | * @param Player $player The player to test |
||
199 | * @param Team $team The team |
||
200 | * @param string $message The error message to show |
||
201 | * @throws HTTPException |
||
202 | * @return void |
||
203 | */ |
||
204 | 1 | private function assertCanEdit(Player $player, Team $team, $message = "You are not allowed to edit that team") |
|
210 | } |
||
211 |
Let’s take a look at an example:
In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different sub-classes of User which does not have a getDisplayName() method, the code will break.
Available Fixes
Change the type-hint for the parameter:
Add an additional type-check:
Add the method to the parent class: