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) |
|
| 34 | |||
| 35 | 1 | public function listAction() |
|
| 36 | { |
||
| 37 | 1 | Team::$cachedMatches = Match::getQueryBuilder() |
|
| 38 | 1 | ->where('time')->isAfter(TimeDate::from('45 days ago')) |
|
| 39 | 1 | ->active() |
|
| 40 | 1 | ->getModels($fast = true); |
|
| 41 | |||
| 42 | 1 | $teams = $this->getQueryBuilder() |
|
| 43 | 1 | ->sortBy('elo')->reverse() |
|
| 44 | 1 | ->getModels($fast = true); |
|
| 45 | |||
| 46 | return array( |
||
| 47 | 1 | "teams" => $teams |
|
| 48 | ); |
||
| 49 | } |
||
| 50 | |||
| 51 | public function createAction(Player $me) |
||
| 55 | |||
| 56 | View Code Duplication | public function editAction(Player $me, Team $team) |
|
|
|
|||
| 57 | { |
||
| 58 | // TODO: Generating this response is unnecessary |
||
| 59 | $response = $this->edit($team, $me, "team"); |
||
| 60 | |||
| 61 | if ($this->newLeader) { |
||
| 62 | // Redirect to a confirmation form if we are assigning a new leader |
||
| 63 | $url = Service::getGenerator()->generate('team_assign_leader', array( |
||
| 64 | 'team' => $team->getAlias(), |
||
| 65 | 'player' => $this->newLeader->getAlias() |
||
| 66 | )); |
||
| 67 | |||
| 68 | return new RedirectResponse($url); |
||
| 69 | } |
||
| 70 | |||
| 71 | return $response; |
||
| 72 | } |
||
| 73 | |||
| 74 | 1 | public function deleteAction(Player $me, Team $team) |
|
| 75 | { |
||
| 76 | 1 | $members = $team->getMembers(); |
|
| 77 | |||
| 78 | return $this->delete($team, $me, function () use ($team, $me, $members) { |
||
| 79 | 1 | $event = new Event\TeamDeleteEvent($team, $me, $members); |
|
| 80 | 1 | Service::getDispatcher()->dispatch(Events::TEAM_DELETE, $event); |
|
| 81 | 1 | }); |
|
| 82 | } |
||
| 83 | |||
| 84 | View Code Duplication | public function joinAction(Team $team, Player $me) |
|
| 85 | { |
||
| 86 | $this->requireLogin(); |
||
| 87 | if (!$me->isTeamless()) { |
||
| 88 | throw new ForbiddenException("You are already a member of a team"); |
||
| 89 | } elseif ($team->getStatus() != 'open') { |
||
| 90 | throw new ForbiddenException("This team is not accepting new members without an invitation"); |
||
| 91 | } |
||
| 92 | |||
| 93 | return $this->showConfirmationForm(function () use ($team, $me) { |
||
| 94 | $team->addMember($me->getId()); |
||
| 95 | Service::getDispatcher()->dispatch(Events::TEAM_JOIN, new Event\TeamJoinEvent($team, $me)); |
||
| 96 | |||
| 97 | return new RedirectResponse($team->getUrl()); |
||
| 98 | }, "Are you sure you want to join {$team->getEscapedName()}?", |
||
| 99 | "You are now a member of {$team->getName()}"); |
||
| 100 | } |
||
| 101 | |||
| 102 | 1 | public function kickAction(Team $team, Player $player, Player $me) |
|
| 123 | |||
| 124 | 1 | View Code Duplication | public function abandonAction(Team $team, Player $me) |
| 125 | { |
||
| 126 | 1 | if (!$team->isMember($me->getId())) { |
|
| 127 | throw new ForbiddenException("You are not a member of that team!"); |
||
| 128 | } |
||
| 129 | |||
| 130 | 1 | if ($team->getLeader()->isSameAs($me)) { |
|
| 131 | 1 | throw new ForbiddenException("You can't abandon the team you are leading."); |
|
| 132 | } |
||
| 133 | |||
| 134 | return $this->showConfirmationForm(function () use ($team, $me) { |
||
| 135 | 1 | $team->removeMember($me->getId()); |
|
| 136 | 1 | Service::getDispatcher()->dispatch(Events::TEAM_ABANDON, new Event\TeamAbandonEvent($team, $me)); |
|
| 137 | |||
| 138 | 1 | return new RedirectResponse($team->getUrl()); |
|
| 139 | 1 | }, "Are you sure you want to abandon {$team->getEscapedName()}?", |
|
| 140 | 1 | "You have left {$team->getName()}", "Abandon"); |
|
| 141 | } |
||
| 142 | |||
| 143 | public function assignLeaderAction(Team $team, Player $me, Player $player) |
||
| 144 | { |
||
| 145 | $this->assertCanEdit($me, $team, "You are not allowed to change the leader of this team."); |
||
| 146 | |||
| 147 | if (!$team->isMember($player->getId())) { |
||
| 148 | throw new ForbiddenException("The specified player is not a member of {$team->getName()}"); |
||
| 149 | } elseif ($team->getLeader()->isSameAs($player)) { |
||
| 150 | throw new ForbiddenException("{$player->getUsername()} is already the leader of {$team->getName()}"); |
||
| 151 | } |
||
| 152 | |||
| 153 | return $this->showConfirmationForm(function () use ($player, $team) { |
||
| 154 | $event = new Event\TeamLeaderChangeEvent($team, $player, $team->getLeader()); |
||
| 155 | $team->setLeader($player->getId()); |
||
| 156 | Service::getDispatcher()->dispatch(Events::TEAM_LEADER_CHANGE, $event); |
||
| 157 | |||
| 158 | return new RedirectResponse($team->getUrl()); |
||
| 159 | }, "Are you sure you want to transfer the leadership of the team to <strong>{$player->getEscapedUsername()}</strong>?", |
||
| 160 | "{$player->getUsername()} is now leading {$team->getName()}", |
||
| 161 | "Appoint leadership"); |
||
| 162 | } |
||
| 163 | |||
| 164 | /** |
||
| 165 | * Show a confirmation form to confirm that the user wants to assign a new |
||
| 166 | * leader to a team |
||
| 167 | * |
||
| 168 | * @param Player $leader The new leader |
||
| 169 | */ |
||
| 170 | public function newLeader($leader) |
||
| 174 | |||
| 175 | protected function validateNew($form) |
||
| 186 | |||
| 187 | protected function canCreate($player) |
||
| 188 | { |
||
| 189 | if ($player->getTeam()->isValid()) { |
||
| 190 | throw new ForbiddenException("You need to abandon your current team before you can create a new one"); |
||
| 195 | |||
| 196 | /** |
||
| 197 | * Make sure that a player can edit a team |
||
| 198 | * |
||
| 199 | * Throws an exception if a player is not an admin or the leader of a team |
||
| 200 | * @param Player $player The player to test |
||
| 201 | * @param Team $team The team |
||
| 202 | * @param string $message The error message to show |
||
| 203 | * @throws HTTPException |
||
| 204 | * @return void |
||
| 205 | */ |
||
| 206 | 1 | private function assertCanEdit(Player $player, Team $team, $message = "You are not allowed to edit that team") |
|
| 212 | } |
||
| 213 |
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.