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 |
||
5 | abstract class AbstractSpecialDate implements SpecialDateInterface |
||
6 | { |
||
7 | /** |
||
8 | * @var \DateTime |
||
9 | */ |
||
10 | protected $startDate; |
||
11 | |||
12 | /** |
||
13 | * @var \DateTime |
||
14 | */ |
||
15 | protected $endDate; |
||
16 | |||
17 | /** |
||
18 | * @var \DateTime |
||
19 | */ |
||
20 | protected $zeroDate; |
||
21 | |||
22 | /** |
||
23 | * @var string |
||
24 | */ |
||
25 | protected $description; |
||
26 | |||
27 | /** |
||
28 | * @var int |
||
29 | */ |
||
30 | protected $totalLength = 0; |
||
31 | |||
32 | /** |
||
33 | * @var bool |
||
34 | */ |
||
35 | protected $valid = true; |
||
36 | |||
37 | /** |
||
38 | * @var bool |
||
39 | */ |
||
40 | protected $bankHoliday = false; |
||
41 | |||
42 | /** |
||
43 | * @var bool |
||
44 | */ |
||
45 | protected $nationalAcceptedParty = false; |
||
|
|||
46 | |||
47 | /** |
||
48 | * @var string |
||
49 | */ |
||
50 | protected $normalizedDescription = false; |
||
51 | /** |
||
52 | * @var int |
||
53 | */ |
||
54 | protected $year; |
||
55 | |||
56 | 69 | public function __construct($year) |
|
57 | { |
||
58 | 69 | $this->year = $year; |
|
59 | 69 | $this->zeroDate = new \DateTime(); |
|
60 | 69 | $this->zeroDate->setDate(1, 1, 1); |
|
61 | |||
62 | 69 | $this->startDate = new \DateTime(); |
|
63 | 69 | $this->startDate->setDate(1, 1, 1); |
|
64 | |||
65 | 69 | $this->endDate = new \DateTime(); |
|
66 | 69 | $this->endDate->setDate(1, 1, 1); |
|
67 | |||
68 | 69 | $this->generate(); |
|
69 | |||
70 | 69 | View Code Duplication | if ($this->startDate->format('Y') . '-' . $this->startDate->format('m') . '-' . $this->startDate->format('d') === '0001-01-01') { |
71 | 28 | $this->valid = false; |
|
72 | 28 | $this->totalLength = 0; |
|
73 | } |
||
74 | |||
75 | 69 | View Code Duplication | if ($this->endDate->format('Y') . '-' . $this->endDate->format('m') . '-' . $this->endDate->format('d') === '0001-01-01') { |
76 | 28 | $this->valid = false; |
|
77 | 28 | $this->totalLength = 0; |
|
78 | } |
||
79 | |||
80 | 69 | $this->normalizeDescription(); |
|
81 | 69 | } |
|
82 | |||
83 | 69 | protected function normalizeDescription() |
|
94 | |||
95 | /** |
||
96 | * @param $year |
||
97 | * @param $month |
||
98 | * @param $day |
||
99 | * |
||
100 | * @return \DateTime |
||
101 | */ |
||
102 | 19 | protected function generateDateTime($year, $month, $day) |
|
112 | |||
113 | 25 | protected function setupDateTimeObjects(\DateTime $start, \DateTime $end = null) |
|
114 | { |
||
115 | 25 | $this->startDate = $start; |
|
116 | |||
117 | 25 | if ($end === null) { |
|
118 | 21 | $this->endDate = $start; |
|
119 | } else { |
||
120 | 4 | $this->endDate = $end; |
|
121 | } |
||
122 | |||
123 | 25 | $interval = $this->startDate->diff($this->endDate); |
|
124 | |||
125 | 25 | $this->totalLength = (int)$interval->format('%R%a') + 1; |
|
126 | 25 | } |
|
127 | |||
128 | /** |
||
129 | * @return \DateTime |
||
130 | */ |
||
131 | 69 | public function getStartDate() |
|
139 | |||
140 | /** |
||
141 | * @return \DateTime |
||
142 | */ |
||
143 | 66 | public function getEndDate() |
|
151 | |||
152 | /** |
||
153 | * @return string |
||
154 | */ |
||
155 | 66 | public function getDescription() |
|
159 | |||
160 | /** |
||
161 | * @return int |
||
162 | */ |
||
163 | 66 | public function getTotalLength() |
|
167 | |||
168 | /** |
||
169 | * @return bool |
||
170 | */ |
||
171 | 66 | public function isValid() |
|
175 | |||
176 | /** |
||
177 | * @return boolean |
||
178 | */ |
||
179 | 66 | public function isBankHoliday() |
|
183 | |||
184 | /** |
||
185 | * @return boolean |
||
186 | */ |
||
187 | public function isNationalAcceptedParty() |
||
191 | |||
192 | /** |
||
193 | * @return int |
||
194 | */ |
||
195 | public function getYear() |
||
199 | |||
200 | /** |
||
201 | * @return string |
||
202 | */ |
||
203 | 1 | public function getNormalizedDescription() |
|
207 | |||
208 | abstract protected function generate(); |
||
209 | } |
||
210 |
Very long variable names usually make code harder to read. It is therefore recommended not to make variable names too verbose.