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 |
||
10 | View Code Duplication | class Character |
|
|
|||
11 | { |
||
12 | use ImmutableTrait, SerializableTrait; |
||
13 | |||
14 | /** |
||
15 | * @var string |
||
16 | */ |
||
17 | private $name; |
||
18 | |||
19 | /** |
||
20 | * @var string |
||
21 | */ |
||
22 | private $nick; |
||
23 | |||
24 | /** |
||
25 | * @var int |
||
26 | */ |
||
27 | private $level; |
||
28 | |||
29 | /** |
||
30 | * @var string |
||
31 | */ |
||
32 | private $vocation; |
||
33 | |||
34 | /** |
||
35 | * @var Carbon |
||
36 | */ |
||
37 | private $joined; |
||
38 | |||
39 | /** |
||
40 | * @var string |
||
41 | */ |
||
42 | private $status; |
||
43 | |||
44 | /** |
||
45 | * Character constructor. |
||
46 | * |
||
47 | * @param string $name |
||
48 | * @param string $nick |
||
49 | * @param int $level |
||
50 | * @param string $vocation |
||
51 | * @param Carbon $joined |
||
52 | * @param string $status |
||
53 | * @throws ImmutableException |
||
54 | */ |
||
55 | public function __construct( |
||
72 | |||
73 | /** |
||
74 | * @return string |
||
75 | */ |
||
76 | public function getName(): string |
||
80 | |||
81 | /** |
||
82 | * @return string |
||
83 | */ |
||
84 | public function getNick(): string |
||
88 | |||
89 | /** |
||
90 | * @return int |
||
91 | */ |
||
92 | public function getLevel(): int |
||
96 | |||
97 | /** |
||
98 | * @return string |
||
99 | */ |
||
100 | public function getVocation(): string |
||
104 | |||
105 | /** |
||
106 | * @return Carbon |
||
107 | */ |
||
108 | public function getJoined(): Carbon |
||
112 | |||
113 | /** |
||
114 | * @return string |
||
115 | */ |
||
116 | public function getStatus(): string |
||
120 | } |
||
121 |
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.