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 |
||
25 | abstract class AbstractPrice implements PriceInterface, EntityInterface |
||
26 | { |
||
27 | /** |
||
28 | * @var integer |
||
29 | */ |
||
30 | protected $id; |
||
31 | |||
32 | /** |
||
33 | * @var TypeInterface |
||
34 | */ |
||
35 | protected $type; |
||
36 | |||
37 | /** |
||
38 | * @var TargetInterface |
||
39 | */ |
||
40 | protected $target; |
||
41 | |||
42 | /** |
||
43 | * @var PlanInterface |
||
44 | */ |
||
45 | protected $plan; |
||
46 | |||
47 | 7 | View Code Duplication | public function __construct( |
58 | |||
59 | /** |
||
60 | * {@inheritdoc} |
||
61 | */ |
||
62 | 2 | public function getId() |
|
66 | |||
67 | /** |
||
68 | * {@inheritdoc} |
||
69 | */ |
||
70 | 7 | public function getType() |
|
74 | |||
75 | /** |
||
76 | * {@inheritdoc} |
||
77 | */ |
||
78 | 7 | public function getTarget() |
|
82 | |||
83 | /** |
||
84 | * {@inheritdoc} |
||
85 | */ |
||
86 | 1 | public function getPlan() |
|
87 | { |
||
88 | 1 | return $this->plan; |
|
89 | } |
||
90 | |||
91 | /** |
||
92 | * {@inheritdoc} |
||
93 | * Default sum calculation method: sum = price * usage. |
||
94 | */ |
||
95 | 1 | View Code Duplication | public function calculateSum(QuantityInterface $quantity) |
109 | |||
110 | public static function create(array $data) |
||
114 | |||
115 | public function jsonSerialize() |
||
123 | |||
124 | /** |
||
125 | * {@inheritdoc} |
||
126 | */ |
||
127 | abstract public function calculateUsage(QuantityInterface $quantity); |
||
128 | |||
129 | /** |
||
130 | * {@inheritdoc} |
||
131 | */ |
||
132 | abstract public function calculatePrice(QuantityInterface $action); |
||
133 | } |
||
134 |
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.