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) |
|
32 | |||
33 | 1 | public function listAction() |
|
48 | |||
49 | public function createAction(Player $me) |
||
50 | { |
||
51 | return $this->create($me); |
||
52 | } |
||
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) |
|
83 | { |
||
84 | $this->requireLogin(); |
||
85 | if (!$me->isTeamless()) { |
||
86 | throw new ForbiddenException("You are already a member of a team"); |
||
87 | } elseif ($team->getStatus() != 'open') { |
||
88 | throw new ForbiddenException("This team is not accepting new members without an invitation"); |
||
89 | } |
||
90 | |||
91 | return $this->showConfirmationForm(function () use ($team, $me) { |
||
92 | $team->addMember($me->getId()); |
||
93 | Service::getDispatcher()->dispatch(Events::TEAM_JOIN, new Event\TeamJoinEvent($team, $me)); |
||
94 | |||
95 | return new RedirectResponse($team->getUrl()); |
||
96 | }, "Are you sure you want to join {$team->getEscapedName()}?", |
||
97 | "You are now a member of {$team->getName()}"); |
||
98 | } |
||
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) |
||
142 | { |
||
143 | $this->assertCanEdit($me, $team, "You are not allowed to change the leader of this team."); |
||
144 | |||
145 | if (!$team->isMember($player->getId())) { |
||
146 | throw new ForbiddenException("The specified player is not a member of {$team->getName()}"); |
||
147 | } elseif ($team->getLeader()->isSameAs($player)) { |
||
148 | throw new ForbiddenException("{$player->getUsername()} is already the leader of {$team->getName()}"); |
||
149 | } |
||
150 | |||
151 | return $this->showConfirmationForm(function () use ($player, $team) { |
||
152 | $event = new Event\TeamLeaderChangeEvent($team, $player, $team->getLeader()); |
||
153 | $team->setLeader($player->getId()); |
||
154 | Service::getDispatcher()->dispatch(Events::TEAM_LEADER_CHANGE, $event); |
||
155 | |||
156 | return new RedirectResponse($team->getUrl()); |
||
157 | }, "Are you sure you want to transfer the leadership of the team to <strong>{$player->getEscapedUsername()}</strong>?", |
||
158 | "{$player->getUsername()} is now leading {$team->getName()}", |
||
159 | "Appoint leadership"); |
||
160 | } |
||
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) |
||
169 | { |
||
170 | $this->newLeader = $leader; |
||
171 | } |
||
172 | |||
173 | protected function validateNew($form) |
||
174 | { |
||
175 | $name = $form->get('name'); |
||
176 | $team = Team::getFromName($name->getData()); |
||
177 | |||
178 | // The name for the team that the user gave us already exists |
||
179 | // TODO: This takes deleted teams into account, do we want that? |
||
180 | if ($team->isValid()) { |
||
181 | $name->addError(new FormError("A team called {$team->getName()} already exists")); |
||
182 | } |
||
183 | } |
||
184 | |||
185 | protected function canCreate($player) |
||
186 | { |
||
187 | if ($player->getTeam()->isValid()) { |
||
188 | throw new ForbiddenException("You need to abandon your current team before you can create a new one"); |
||
189 | } |
||
190 | |||
191 | return parent::canCreate($player); |
||
192 | } |
||
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: