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 TeamAbandonEvent extends Event |
|
|
|||
14 | { |
||
15 | /** |
||
16 | * @var \Team |
||
17 | */ |
||
18 | protected $team; |
||
19 | |||
20 | /** |
||
21 | * @var \Player |
||
22 | */ |
||
23 | protected $player; |
||
24 | |||
25 | /** |
||
26 | * Create a new event |
||
27 | * |
||
28 | * @param \Team $team The team that the player left |
||
29 | * @param \Player $player The player who abandoned the team |
||
30 | */ |
||
31 | 1 | public function __construct(\Team $team, \Player $player) |
|
36 | |||
37 | /** |
||
38 | * Get the team that the player abandoned |
||
39 | * |
||
40 | * @return \Team |
||
41 | */ |
||
42 | 1 | public function getTeam() |
|
46 | |||
47 | /** |
||
48 | * Get the player who left the team |
||
49 | * |
||
50 | * @return \Player |
||
51 | */ |
||
52 | 1 | public function getPlayer() |
|
56 | |||
57 | /** |
||
58 | * {@inheritdoc} |
||
59 | */ |
||
60 | 1 | public function notify($type) |
|
64 | } |
||
65 |
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.