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 | "team" => $team |
||
| 30 | 1 | ); |
|
| 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 | "teams" => $teams |
||
| 46 | 1 | ); |
|
| 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 | public function deleteAction(Player $me, Team $team) |
||
| 73 | { |
||
| 74 | $members = $team->getMembers(); |
||
| 75 | |||
| 76 | return $this->delete($team, $me, function () use ($team, $me, $members) { |
||
| 77 | $event = new Event\TeamDeleteEvent($team, $me, $members); |
||
| 78 | Service::getDispatcher()->dispatch(Events::TEAM_DELETE, $event); |
||
| 79 | }); |
||
| 80 | } |
||
| 81 | |||
| 82 | View Code Duplication | public function joinAction(Team $team, Player $me) |
|
| 99 | |||
| 100 | public function kickAction(Team $team, Player $player, Player $me) |
||
| 101 | { |
||
| 102 | $this->assertCanEdit($me, $team, "You are not allowed to kick a player off that team!"); |
||
| 103 | |||
| 104 | if ($team->getLeader()->isSameAs($player)) { |
||
| 105 | throw new ForbiddenException("You can't kick the leader off their team."); |
||
| 106 | } |
||
| 107 | |||
| 108 | if (!$team->isMember($player->getId())) { |
||
| 109 | throw new ForbiddenException("The specified player is not a member of that team."); |
||
| 110 | } |
||
| 111 | |||
| 112 | return $this->showConfirmationForm(function () use ($me, $team, $player) { |
||
| 113 | $team->removeMember($player->getId()); |
||
| 114 | $event = new Event\TeamKickEvent($team, $player, $me); |
||
| 115 | Service::getDispatcher()->dispatch(Events::TEAM_KICK, $event); |
||
| 116 | |||
| 117 | return new RedirectResponse($team->getUrl()); |
||
| 118 | }, "Are you sure you want to kick {$player->getEscapedUsername()} from {$team->getEscapedName()}?", |
||
| 119 | "Player {$player->getUsername()} has been kicked from {$team->getName()}", "Kick"); |
||
| 120 | } |
||
| 121 | |||
| 122 | View Code Duplication | public function abandonAction(Team $team, Player $me) |
|
| 123 | { |
||
| 124 | if (!$team->isMember($me->getId())) { |
||
| 125 | throw new ForbiddenException("You are not a member of that team!"); |
||
| 126 | } |
||
| 127 | |||
| 128 | if ($team->getLeader()->isSameAs($me)) { |
||
| 129 | throw new ForbiddenException("You can't abandon the team you are leading."); |
||
| 130 | } |
||
| 131 | |||
| 132 | return $this->showConfirmationForm(function () use ($team, $me) { |
||
| 133 | $team->removeMember($me->getId()); |
||
| 134 | Service::getDispatcher()->dispatch(Events::TEAM_ABANDON, new Event\TeamAbandonEvent($team, $me)); |
||
| 135 | |||
| 136 | return new RedirectResponse($team->getUrl()); |
||
| 137 | }, "Are you sure you want to abandon {$team->getEscapedName()}?", |
||
| 138 | "You have left {$team->getName()}", "Abandon"); |
||
| 139 | } |
||
| 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 | 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: