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) |
||
| 47 | |||
| 48 | 1 | public function deleteAction(Player $me, Team $team) |
|
| 57 | |||
| 58 | public function joinAction(Team $team, Player $me) |
||
| 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()->getId() == $player->getId()) { |
|
| 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 | View Code Duplication | 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 | 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()->getId() == $me->getId()) { |
|
| 105 | 1 | throw new ForbiddenException("You can't abandon the team you are leading."); |
|
| 106 | } |
||
| 107 | |||
| 108 | View Code Duplication | 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) |
||
| 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) |
||
| 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.