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 | 54 | public function __construct($year) |
|
82 | |||
83 | 54 | protected function normalizeDescription() |
|
94 | |||
95 | /** |
||
96 | * @param $year |
||
97 | * @param $month |
||
98 | * @param $day |
||
99 | * |
||
100 | * @return \DateTime |
||
101 | */ |
||
102 | 16 | protected function generateDateTime($year, $month, $day) |
|
112 | |||
113 | 16 | protected function setupDateTimeObjects(\DateTime $start, \DateTime $end = null) |
|
127 | |||
128 | /** |
||
129 | * @return \DateTime |
||
130 | */ |
||
131 | 54 | public function getStartDate() |
|
139 | |||
140 | /** |
||
141 | * @return \DateTime |
||
142 | */ |
||
143 | 51 | public function getEndDate() |
|
151 | |||
152 | /** |
||
153 | * @return string |
||
154 | */ |
||
155 | 51 | public function getDescription() |
|
159 | |||
160 | /** |
||
161 | * @return int |
||
162 | */ |
||
163 | 51 | public function getTotalLength() |
|
167 | |||
168 | /** |
||
169 | * @return bool |
||
170 | */ |
||
171 | 51 | public function isValid() |
|
175 | |||
176 | /** |
||
177 | * @return boolean |
||
178 | */ |
||
179 | 51 | 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.