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 | class FeedLootValueObject |
||
11 | { |
||
12 | /** |
||
13 | * @var string |
||
14 | */ |
||
15 | private $type; |
||
16 | |||
17 | /** |
||
18 | * @var int |
||
19 | */ |
||
20 | private $timestamp; |
||
21 | |||
22 | /** |
||
23 | * @var int |
||
24 | */ |
||
25 | private $itemId; |
||
26 | |||
27 | /** |
||
28 | * @var string |
||
29 | */ |
||
30 | private $context; |
||
31 | |||
32 | /** |
||
33 | * @var int[] |
||
34 | */ |
||
35 | private $bonusLists; |
||
36 | |||
37 | /** |
||
38 | * LootValueObject constructor. |
||
39 | * @param string $type |
||
40 | * @param int $timestamp |
||
41 | * @param int $itemId |
||
42 | * @param string $context |
||
43 | * @param int[] $bonusLists |
||
44 | */ |
||
45 | View Code Duplication | public function __construct( |
|
58 | |||
59 | /** |
||
60 | * @return string |
||
61 | */ |
||
62 | public function getType() |
||
66 | |||
67 | /** |
||
68 | * @return int |
||
69 | */ |
||
70 | public function getTimestamp() |
||
74 | |||
75 | /** |
||
76 | * @return int |
||
77 | */ |
||
78 | public function getItemId() |
||
82 | |||
83 | /** |
||
84 | * @return string |
||
85 | */ |
||
86 | public function getContext() |
||
90 | |||
91 | /** |
||
92 | * @return int[] |
||
93 | */ |
||
94 | public function getBonusLists() |
||
98 | } |
||
99 |
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.