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 | class DateHelper |
||
15 | { |
||
16 | use TranslatorAwareTrait; |
||
17 | |||
18 | /** |
||
19 | * @var DateTime $from |
||
20 | */ |
||
21 | protected $from; |
||
22 | |||
23 | /** |
||
24 | * @var DateTime $to |
||
25 | */ |
||
26 | protected $to; |
||
27 | |||
28 | /** |
||
29 | * @var array $dateFormats The date formats options from config. |
||
30 | */ |
||
31 | protected $dateFormats; |
||
32 | |||
33 | /** |
||
34 | * @var array $timeFormats The time formats options from config. |
||
35 | */ |
||
36 | protected $timeFormats; |
||
37 | |||
38 | /** |
||
39 | * @var string $dateFormat The format from dateFormats to use for the date |
||
40 | */ |
||
41 | protected $dateFormat; |
||
42 | |||
43 | /** |
||
44 | * @var string $dateFormat The format from dateFormats to use for the time |
||
45 | */ |
||
46 | protected $timeFormat; |
||
47 | |||
48 | /** |
||
49 | * DateHelper constructor. |
||
50 | * @param array $data DateHelper data. |
||
51 | * @throws Exception When constructor's data missing. |
||
52 | */ |
||
53 | public function __construct(array $data) |
||
69 | |||
70 | /** |
||
71 | * @param mixed $date The date |
||
72 | * [startDate, endDate] |
||
73 | * DateTimeInterface |
||
74 | * string. |
||
75 | * @param string $format The format to use. |
||
76 | * @return string |
||
77 | */ |
||
78 | View Code Duplication | public function formatDate($date, $format = 'default') |
|
92 | |||
93 | /** |
||
94 | * @param mixed $date The date |
||
95 | * [startDate, endDate] |
||
96 | * DateTimeInterface |
||
97 | * string. |
||
98 | * @param string $format The format to use. |
||
99 | * @return string |
||
100 | */ |
||
101 | View Code Duplication | public function formatTime($date, $format = 'default') |
|
115 | |||
116 | /** |
||
117 | * Get the usage case by comparing two dates. |
||
118 | * @return string |
||
119 | */ |
||
120 | private function getDateCase() |
||
149 | |||
150 | /** |
||
151 | * Get the usage case by comparing two hours. |
||
152 | * @return string |
||
153 | */ |
||
154 | private function getTimeCase() |
||
185 | |||
186 | /** |
||
187 | * @param string $case The use case. |
||
188 | * @return string |
||
189 | */ |
||
190 | private function formatDateFromCase($case) |
||
216 | |||
217 | /** |
||
218 | * @param string $case The use case. |
||
219 | * @return string |
||
220 | */ |
||
221 | private function formatTimeFromCase($case) |
||
247 | |||
248 | // ========================================================================== |
||
249 | // UTILS |
||
250 | // ========================================================================== |
||
251 | |||
252 | /** |
||
253 | * @param mixed $date The date to convert. |
||
254 | * @return DateTime |
||
255 | */ |
||
256 | private function parseAsDate($date) |
||
264 | |||
265 | /** |
||
266 | * @param mixed $format DateTime to be formatted. |
||
267 | * @return mixed |
||
268 | */ |
||
269 | private function crossPlatformFormat($format) |
||
277 | } |
||
278 |
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.