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 |
||
28 | class Charge implements \JsonSerializable |
||
29 | { |
||
30 | /** |
||
31 | * @var int |
||
32 | */ |
||
33 | protected $id; |
||
34 | |||
35 | /** |
||
36 | * @var ActionInterface |
||
37 | */ |
||
38 | protected $action; |
||
39 | |||
40 | /** |
||
41 | * @var PriceInterface |
||
42 | */ |
||
43 | protected $price; |
||
44 | |||
45 | /** |
||
46 | * @var TargetInterface |
||
47 | */ |
||
48 | protected $target; |
||
49 | |||
50 | /** |
||
51 | * @var QuantityInterface |
||
52 | */ |
||
53 | protected $usage; |
||
54 | |||
55 | /** |
||
56 | * @var Money |
||
57 | */ |
||
58 | protected $sum; |
||
59 | |||
60 | /** |
||
61 | * @var DateTime |
||
62 | */ |
||
63 | protected $time; |
||
64 | |||
65 | 3 | View Code Duplication | public function __construct( |
82 | |||
83 | /** |
||
84 | * Returns charge that is sum of given charges. |
||
85 | * @param Charge[] $charges |
||
86 | * @return Charge |
||
87 | */ |
||
88 | public static function sumUp(array $charges) |
||
101 | |||
102 | public function getId() |
||
106 | |||
107 | 3 | public function getAction() |
|
111 | |||
112 | 3 | public function getTarget() |
|
116 | |||
117 | /** |
||
118 | * @return PriceInterface |
||
119 | */ |
||
120 | 3 | public function getPrice() |
|
124 | |||
125 | 3 | public function getUsage() |
|
129 | |||
130 | 3 | public function getSum() |
|
134 | |||
135 | public function calculatePrice() |
||
141 | |||
142 | public function getTime() |
||
146 | |||
147 | public function jsonSerialize() |
||
151 | } |
||
152 |
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.