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 |
||
12 | class AchievementValueObject |
||
13 | { |
||
14 | /** |
||
15 | * @var int |
||
16 | */ |
||
17 | private $id; |
||
18 | |||
19 | /** |
||
20 | * @var string |
||
21 | */ |
||
22 | private $title; |
||
23 | |||
24 | /** |
||
25 | * @var int |
||
26 | */ |
||
27 | private $points; |
||
28 | |||
29 | /** |
||
30 | * @var string |
||
31 | */ |
||
32 | private $description; |
||
33 | |||
34 | /** |
||
35 | * @var string[] |
||
36 | */ |
||
37 | private $rewardItems; |
||
38 | |||
39 | /** |
||
40 | * @var string |
||
41 | */ |
||
42 | private $icon; |
||
43 | |||
44 | /** |
||
45 | * @var CriteriaValueObject[] |
||
46 | */ |
||
47 | private $criteria; |
||
48 | |||
49 | /** |
||
50 | * @var bool |
||
51 | */ |
||
52 | private $accountWide; |
||
53 | |||
54 | /** |
||
55 | * @var int |
||
56 | */ |
||
57 | private $factionId; |
||
58 | |||
59 | /** |
||
60 | * AchievementValueObject constructor. |
||
61 | * @param int $id |
||
62 | * @param string $title |
||
63 | * @param int $points |
||
64 | * @param string $description |
||
65 | * @param string[] $rewardItems |
||
66 | * @param string $icon |
||
67 | * @param CriteriaValueObject[] $criteria |
||
68 | * @param bool $accountWide |
||
69 | * @param int $factionId |
||
70 | */ |
||
71 | View Code Duplication | public function __construct( |
|
92 | |||
93 | /** |
||
94 | * @return int |
||
95 | */ |
||
96 | public function getId() |
||
100 | |||
101 | /** |
||
102 | * @return string |
||
103 | */ |
||
104 | public function getTitle() |
||
108 | |||
109 | /** |
||
110 | * @return int |
||
111 | */ |
||
112 | public function getPoints() |
||
116 | |||
117 | /** |
||
118 | * @return string |
||
119 | */ |
||
120 | public function getDescription() |
||
124 | |||
125 | /** |
||
126 | * @return string[] |
||
127 | */ |
||
128 | public function getRewardItems() |
||
132 | |||
133 | /** |
||
134 | * @return string |
||
135 | */ |
||
136 | public function getIcon() |
||
140 | |||
141 | /** |
||
142 | * @return CriteriaValueObject[] |
||
143 | */ |
||
144 | public function getCriteria() |
||
148 | |||
149 | /** |
||
150 | * @return bool |
||
151 | */ |
||
152 | public function isAccountWide() |
||
156 | |||
157 | /** |
||
158 | * @return int |
||
159 | */ |
||
160 | public function getFactionId() |
||
164 | } |
||
165 |
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.