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 |
||
9 | View Code Duplication | class House |
|
|
|||
10 | { |
||
11 | use ImmutableTrait, SerializableTrait; |
||
12 | |||
13 | /** |
||
14 | * @var int |
||
15 | */ |
||
16 | private $houseId; |
||
17 | |||
18 | /** |
||
19 | * @var string |
||
20 | */ |
||
21 | private $name; |
||
22 | |||
23 | /** |
||
24 | * @var int |
||
25 | */ |
||
26 | private $size; |
||
27 | |||
28 | /** |
||
29 | * @var int |
||
30 | */ |
||
31 | private $rent; |
||
32 | |||
33 | /** |
||
34 | * @var string |
||
35 | */ |
||
36 | private $status; |
||
37 | |||
38 | /** |
||
39 | * House constructor. |
||
40 | * @param int $id |
||
41 | * @param string $name |
||
42 | * @param int $size |
||
43 | * @param int $rent |
||
44 | * @param string $status |
||
45 | * @throws ImmutableException |
||
46 | */ |
||
47 | public function __construct(int $id, string $name, int $size, int $rent, string $status) |
||
57 | |||
58 | /** |
||
59 | * @return int |
||
60 | */ |
||
61 | public function getHouseId(): int |
||
65 | |||
66 | /** |
||
67 | * @return string |
||
68 | */ |
||
69 | public function getName(): string |
||
73 | |||
74 | /** |
||
75 | * @return int |
||
76 | */ |
||
77 | public function getSize(): int |
||
81 | |||
82 | /** |
||
83 | * @return int |
||
84 | */ |
||
85 | public function getRent(): int |
||
89 | |||
90 | /** |
||
91 | * @return string |
||
92 | */ |
||
93 | public function getStatus(): string |
||
97 | |||
98 | /** |
||
99 | * @return bool |
||
100 | */ |
||
101 | public function isRented() |
||
105 | } |
||
106 |
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.