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 |
||
14 | abstract class AbstractValuator implements ValuatorInterface { |
||
15 | |||
16 | /** |
||
17 | * The start date of the period over which we should reason about value |
||
18 | * |
||
19 | * @var /DateTime |
||
20 | */ |
||
21 | protected $start_date; |
||
22 | |||
23 | |||
24 | /** |
||
25 | * The end date of the period over which we should reason about value |
||
26 | * @var /DateTime |
||
27 | */ |
||
28 | protected $end_date; |
||
29 | |||
30 | |||
31 | /** |
||
32 | * The unit involved |
||
33 | * @var |
||
34 | */ |
||
35 | protected $unit; |
||
36 | |||
37 | /** |
||
38 | * The store from which to retrieve event value data |
||
39 | * |
||
40 | * @var Store |
||
41 | */ |
||
42 | protected $store; |
||
43 | |||
44 | /** |
||
45 | * AbstractValuator constructor. |
||
46 | * @param \DateTime $start_date |
||
47 | * @param \DateTime $end_date |
||
48 | * @param \Roomify\Bat\Unit\UnitInterface $unit |
||
49 | */ |
||
50 | View Code Duplication | public function __construct(\DateTime $start_date, \DateTime $end_date, UnitInterface $unit, Store $store) { |
|
|
|||
51 | $this->start_date = clone($start_date); |
||
52 | $this->end_date = clone($end_date); |
||
53 | $this->unit = $unit; |
||
54 | $this->store = $store; |
||
55 | } |
||
56 | |||
57 | /** |
||
58 | * @param \DateTime $start_date |
||
59 | */ |
||
60 | public function setStartDate(\DateTime $start_date) { |
||
61 | $this->start_date = clone($start_date); |
||
62 | } |
||
63 | |||
64 | /** |
||
65 | * @return \DateTime |
||
66 | */ |
||
67 | public function getStartDate() { |
||
68 | return $this->start_date; |
||
69 | } |
||
70 | |||
71 | /** |
||
72 | * @param \DateTime $end_date |
||
73 | */ |
||
74 | public function setEndDate(\DateTime $end_date) { |
||
75 | $this->end_date = $end_date; |
||
76 | } |
||
77 | |||
78 | /** |
||
79 | * @return \DateTime |
||
80 | */ |
||
81 | public function getEndDate() { |
||
82 | return $this->end_date; |
||
83 | } |
||
84 | |||
85 | /** |
||
86 | * @param \Roomify\Bat\Unit\UnitInterface $unit |
||
87 | */ |
||
88 | public function setUnit(UnitInterface $unit) { |
||
89 | $this->unit = $unit; |
||
90 | } |
||
91 | |||
92 | /** |
||
93 | * @return \Roomify\Bat\Unit\UnitInterface |
||
94 | */ |
||
95 | public function getUnit() { |
||
96 | return $this->unit; |
||
97 | } |
||
98 | |||
99 | /** |
||
100 | * @param $events |
||
101 | * @return mixed |
||
102 | */ |
||
103 | abstract public function determineValue(); |
||
104 | |||
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.