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 Guildhall |
|
|
|||
11 | { |
||
12 | use ImmutableTrait, SerializableTrait; |
||
13 | |||
14 | /** |
||
15 | * @var string |
||
16 | */ |
||
17 | private $name; |
||
18 | |||
19 | /** |
||
20 | * @var string |
||
21 | */ |
||
22 | private $town; |
||
23 | |||
24 | /** |
||
25 | * @var Carbon |
||
26 | */ |
||
27 | private $paid; |
||
28 | |||
29 | /** |
||
30 | * @var string |
||
31 | */ |
||
32 | private $world; |
||
33 | |||
34 | /** |
||
35 | * @var int |
||
36 | */ |
||
37 | private $houseId; |
||
38 | |||
39 | /** |
||
40 | * Guildhall constructor. |
||
41 | * |
||
42 | * @param string $name |
||
43 | * @param string $town |
||
44 | * @param Carbon $paid |
||
45 | * @param string $world |
||
46 | * @param int $houseId |
||
47 | * @throws ImmutableException |
||
48 | */ |
||
49 | public function __construct(string $name, string $town, Carbon $paid, string $world, int $houseId) |
||
59 | |||
60 | /** |
||
61 | * @return string |
||
62 | */ |
||
63 | public function getName(): string |
||
67 | |||
68 | /** |
||
69 | * @return string |
||
70 | */ |
||
71 | public function getTown(): string |
||
75 | |||
76 | /** |
||
77 | * @return Carbon |
||
78 | */ |
||
79 | public function getPaid(): Carbon |
||
83 | |||
84 | /** |
||
85 | * @return string |
||
86 | */ |
||
87 | public function getWorld(): string |
||
91 | |||
92 | /** |
||
93 | * @return int |
||
94 | */ |
||
95 | public function getHouseId(): int |
||
99 | } |
||
100 |
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.