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) |
|
| 20 | |||
| 21 | 1 | public function listAction() |
|
| 25 | |||
| 26 | public function createAction(Player $me) |
||
| 30 | |||
| 31 | public function editAction(Player $me, Team $team) |
||
| 32 | { |
||
| 33 | $response = $this->edit($team, $me, "team"); |
||
| 34 | |||
| 35 | if ($this->newLeader) { |
||
| 36 | // Redirect to a confirmation form if we are assigning a new leader |
||
| 37 | $url = Service::getGenerator()->generate('team_assign_leader', array( |
||
| 38 | 'team' => $team->getAlias(), |
||
| 39 | 'player' => $this->newLeader->getAlias() |
||
| 40 | )); |
||
| 41 | |||
| 42 | return new RedirectResponse($url); |
||
| 43 | } |
||
| 44 | |||
| 45 | return $response; |
||
| 46 | } |
||
| 47 | |||
| 48 | 1 | public function deleteAction(Player $me, Team $team) |
|
| 49 | { |
||
| 50 | 1 | $members = $team->getMembers(); |
|
| 51 | |||
| 52 | return $this->delete($team, $me, function () use ($team, $me, $members) { |
||
| 53 | 1 | $event = new Event\TeamDeleteEvent($team, $me, $members); |
|
| 54 | 1 | Service::getDispatcher()->dispatch(Events::TEAM_DELETE, $event); |
|
| 55 | 1 | }); |
|
| 56 | } |
||
| 57 | |||
| 58 | View Code Duplication | public function joinAction(Team $team, Player $me) |
|
|
|
|||
| 59 | { |
||
| 60 | $this->requireLogin(); |
||
| 61 | if (!$me->isTeamless()) { |
||
| 62 | throw new ForbiddenException("You are already a member of a team"); |
||
| 63 | } elseif ($team->getStatus() != 'open') { |
||
| 64 | throw new ForbiddenException("This team is not accepting new members without an invitation"); |
||
| 65 | } |
||
| 66 | |||
| 67 | return $this->showConfirmationForm(function () use ($team, $me) { |
||
| 68 | $team->addMember($me->getId()); |
||
| 69 | Service::getDispatcher()->dispatch(Events::TEAM_JOIN, new Event\TeamJoinEvent($team, $me)); |
||
| 70 | |||
| 71 | return new RedirectResponse($team->getUrl()); |
||
| 72 | }, "Are you sure you want to join {$team->getEscapedName()}?", |
||
| 73 | "You are now a member of {$team->getName()}"); |
||
| 74 | } |
||
| 75 | |||
| 76 | 1 | public function kickAction(Team $team, Player $player, Player $me) |
|
| 77 | { |
||
| 78 | 1 | $this->assertCanEdit($me, $team, "You are not allowed to kick a player off that team!"); |
|
| 79 | |||
| 80 | 1 | if ($team->getLeader()->isSameAs($player)) { |
|
| 81 | throw new ForbiddenException("You can't kick the leader off their team."); |
||
| 82 | } |
||
| 83 | |||
| 84 | 1 | if (!$team->isMember($player->getId())) { |
|
| 85 | throw new ForbiddenException("The specified player is not a member of that team."); |
||
| 86 | } |
||
| 87 | |||
| 88 | return $this->showConfirmationForm(function () use ($me, $team, $player) { |
||
| 89 | 1 | $team->removeMember($player->getId()); |
|
| 90 | 1 | $event = new Event\TeamKickEvent($team, $player, $me); |
|
| 91 | 1 | Service::getDispatcher()->dispatch(Events::TEAM_KICK, $event); |
|
| 92 | |||
| 93 | 1 | return new RedirectResponse($team->getUrl()); |
|
| 94 | 1 | }, "Are you sure you want to kick {$player->getEscapedUsername()} from {$team->getEscapedName()}?", |
|
| 95 | 1 | "Player {$player->getUsername()} has been kicked from {$team->getName()}", "Kick"); |
|
| 96 | } |
||
| 97 | |||
| 98 | 1 | View Code Duplication | public function abandonAction(Team $team, Player $me) |
| 99 | { |
||
| 100 | 1 | if (!$team->isMember($me->getId())) { |
|
| 101 | throw new ForbiddenException("You are not a member of that team!"); |
||
| 102 | } |
||
| 103 | |||
| 104 | 1 | if ($team->getLeader()->isSameAs($me)) { |
|
| 105 | 1 | throw new ForbiddenException("You can't abandon the team you are leading."); |
|
| 106 | } |
||
| 107 | |||
| 108 | return $this->showConfirmationForm(function () use ($team, $me) { |
||
| 109 | 1 | $team->removeMember($me->getId()); |
|
| 110 | 1 | Service::getDispatcher()->dispatch(Events::TEAM_ABANDON, new Event\TeamAbandonEvent($team, $me)); |
|
| 111 | |||
| 112 | 1 | return new RedirectResponse($team->getUrl()); |
|
| 113 | 1 | }, "Are you sure you want to abandon {$team->getEscapedName()}?", |
|
| 114 | 1 | "You have left {$team->getName()}", "Abandon"); |
|
| 115 | } |
||
| 116 | |||
| 117 | public function assignLeaderAction(Team $team, Player $me, Player $player) |
||
| 118 | { |
||
| 119 | $this->assertCanEdit($me, $team, "You are not allowed to change the leader of this team."); |
||
| 120 | |||
| 121 | if (!$team->isMember($player->getId())) { |
||
| 122 | throw new ForbiddenException("The specified player is not a member of {$team->getName()}"); |
||
| 123 | } elseif ($team->getLeader()->isSameAs($player)) { |
||
| 124 | throw new ForbiddenException("{$player->getUsername()} is already the leader of {$team->getName()}"); |
||
| 125 | } |
||
| 126 | |||
| 127 | return $this->showConfirmationForm(function () use ($player, $team) { |
||
| 128 | $event = new Event\TeamLeaderChangeEvent($team, $player, $team->getLeader()); |
||
| 129 | $team->setLeader($player->getId()); |
||
| 130 | Service::getDispatcher()->dispatch(Events::TEAM_LEADER_CHANGE, $event); |
||
| 131 | |||
| 132 | return new RedirectResponse($team->getUrl()); |
||
| 133 | }, "Are you sure you want to transfer the leadership of the team to <strong>{$player->getEscapedUsername()}</strong>?", |
||
| 134 | "{$player->getUsername()} is now leading {$team->getName()}", |
||
| 135 | "Appoint leadership"); |
||
| 136 | } |
||
| 137 | |||
| 138 | /** |
||
| 139 | * Show a confirmation form to confirm that the user wants to assign a new |
||
| 140 | * leader to a team |
||
| 141 | * |
||
| 142 | * @param Player $leader The new leader |
||
| 143 | */ |
||
| 144 | public function newLeader($leader) |
||
| 148 | |||
| 149 | protected function validateNew($form) |
||
| 150 | { |
||
| 151 | $name = $form->get('name'); |
||
| 152 | $team = Team::getFromName($name->getData()); |
||
| 153 | |||
| 154 | // The name for the team that the user gave us already exists |
||
| 160 | |||
| 161 | protected function canCreate($player) |
||
| 169 | |||
| 170 | /** |
||
| 171 | * Make sure that a player can edit a team |
||
| 172 | * |
||
| 173 | * Throws an exception if a player is not an admin or the leader of a team |
||
| 174 | * @param Player $player The player to test |
||
| 175 | * @param Team $team The team |
||
| 176 | * @param string $message The error message to show |
||
| 177 | * @throws HTTPException |
||
| 178 | * @return void |
||
| 179 | */ |
||
| 180 | 1 | private function assertCanEdit(Player $player, Team $team, $message = "You are not allowed to edit that team") |
|
| 186 | } |
||
| 187 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.