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 |
||
| 13 | View Code Duplication | class TeamKickEvent extends Event |
|
|
|
|||
| 14 | { |
||
| 15 | /** |
||
| 16 | * @var \Team |
||
| 17 | */ |
||
| 18 | protected $team; |
||
| 19 | |||
| 20 | /** |
||
| 21 | * @var \Player |
||
| 22 | */ |
||
| 23 | protected $kicked; |
||
| 24 | |||
| 25 | /** |
||
| 26 | * @var \Player |
||
| 27 | */ |
||
| 28 | protected $kicker; |
||
| 29 | |||
| 30 | /** |
||
| 31 | * Create a new event |
||
| 32 | * |
||
| 33 | * @param \Team $team The team from which the player was kicked |
||
| 34 | * @param \Player $kicked The player who was kicked |
||
| 35 | * @param \Player $kicker The player who issued the kick |
||
| 36 | */ |
||
| 37 | 1 | public function __construct(\Team $team, \Player $kicked, \Player $kicker) |
|
| 38 | { |
||
| 39 | 1 | $this->team = $team; |
|
| 40 | 1 | $this->kicked = $kicked; |
|
| 41 | 1 | $this->kicker = $kicker; |
|
| 42 | 1 | } |
|
| 43 | |||
| 44 | /** |
||
| 45 | * Get the team from which the player was kicked |
||
| 46 | * |
||
| 47 | * @return \Team |
||
| 48 | */ |
||
| 49 | 1 | public function getTeam() |
|
| 50 | { |
||
| 51 | 1 | return $this->team; |
|
| 52 | } |
||
| 53 | |||
| 54 | /** |
||
| 55 | * Get the player who was kicked |
||
| 56 | * |
||
| 57 | * @return \Player |
||
| 58 | */ |
||
| 59 | public function getKicked() |
||
| 63 | |||
| 64 | /** |
||
| 65 | * Get the player who issued the kick |
||
| 66 | * |
||
| 67 | * @return \Player |
||
| 68 | */ |
||
| 69 | public function getKicker() |
||
| 73 | |||
| 74 | /** |
||
| 75 | * Get the player who was kicked |
||
| 76 | * |
||
| 77 | 1 | * Alias for TeamKickEvent::getKicked() |
|
| 78 | * |
||
| 79 | 1 | * @return \Player $player |
|
| 80 | 1 | */ |
|
| 81 | public function getPlayer() |
||
| 85 | |||
| 86 | /** |
||
| 87 | * {@inheritdoc} |
||
| 88 | */ |
||
| 89 | public function notify($type) |
||
| 93 | } |
||
| 94 |
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.