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 |
||
8 | class Business implements BusinessInterface |
||
9 | { |
||
10 | /** |
||
11 | * The days. |
||
12 | * |
||
13 | * @var DayInterface[] |
||
14 | */ |
||
15 | protected $days; |
||
16 | |||
17 | /** |
||
18 | * The time zone. |
||
19 | * |
||
20 | * @var \DateTimeZone |
||
21 | */ |
||
22 | protected $timezone; |
||
23 | |||
24 | /** |
||
25 | * Creates a new business. |
||
26 | * |
||
27 | * @param DayInterface[] $days |
||
28 | * @param \DateTimeZone|null $timezone |
||
29 | */ |
||
30 | public function __construct(array $days, \DateTimeZone $timezone = null) |
||
35 | |||
36 | /** |
||
37 | * Get the days. |
||
38 | * |
||
39 | * @return DayInterface[] |
||
40 | */ |
||
41 | public function getDays() |
||
45 | |||
46 | /** |
||
47 | * Add a set of days. |
||
48 | * |
||
49 | * @param DayInterface[] $days The days. |
||
50 | * @throws \InvalidArgumentException If no days are passed. |
||
51 | */ |
||
52 | public function setDays(array $days) |
||
64 | |||
65 | /** |
||
66 | * {@inheritdoc} |
||
67 | */ |
||
68 | public function within(\DateTime $date) |
||
79 | |||
80 | /** |
||
81 | * {@inheritdoc} |
||
82 | */ |
||
83 | public function getNextChangeDateTime(\DateTime $date = null) |
||
97 | |||
98 | /** |
||
99 | * {@inheritdoc} |
||
100 | */ |
||
101 | public function closestDateInterval(\DateTime $date) |
||
108 | |||
109 | /** |
||
110 | * {@inheritdoc} |
||
111 | */ |
||
112 | public function jsonSerialize() |
||
119 | |||
120 | /** |
||
121 | * Gets the closest business date interval after the given date. |
||
122 | * |
||
123 | * @param \DateTime $date |
||
124 | * @return DateTimeInterval |
||
125 | */ |
||
126 | private function getClosestDateIntervalAfter(\DateTime $date) |
||
175 | |||
176 | /** |
||
177 | * Gets the business date after the given date (excluding holidays). |
||
178 | * |
||
179 | * @param \DateTime $date |
||
180 | * @return \DateTime |
||
181 | */ |
||
182 | private function getDateAfter(\DateTime $date) |
||
196 | |||
197 | /** |
||
198 | * Gets the closest interval endpoint after the given date. |
||
199 | * |
||
200 | * @param \DateTime $date |
||
201 | * @return DateTimeInterval |
||
202 | */ |
||
203 | private function getClosestInterval(\DateTime $date) |
||
231 | |||
232 | /** |
||
233 | * Gets the closest business day before a given day number (including it). |
||
234 | * |
||
235 | * @param integer $dayNumber |
||
236 | * @return DayInterface|null |
||
237 | */ |
||
238 | private function getClosestDayBefore($dayNumber) |
||
246 | |||
247 | /** |
||
248 | * Gets the closest business day after a given day number (including it). |
||
249 | * |
||
250 | * @param integer $dayNumber |
||
251 | * @return DayInterface|null |
||
252 | */ |
||
253 | private function getClosestDayAfter($dayNumber) |
||
261 | |||
262 | /** |
||
263 | * Gets the business day before the day number. |
||
264 | * |
||
265 | * @param integer $dayNumber |
||
266 | * @return DayInterface|null |
||
267 | */ |
||
268 | View Code Duplication | private function getDayBefore($dayNumber) |
|
282 | |||
283 | /** |
||
284 | * Gets the business day after the day number. |
||
285 | * |
||
286 | * @param integer $dayNumber |
||
287 | * @return DayInterface|null |
||
288 | */ |
||
289 | View Code Duplication | private function getDayAfter($dayNumber) |
|
303 | |||
304 | /** |
||
305 | * Get the day corresponding to the day number. |
||
306 | * |
||
307 | * @param integer $dayNumber |
||
308 | * @return DayInterface|null |
||
309 | */ |
||
310 | private function getDay($dayNumber) |
||
314 | |||
315 | /** |
||
316 | * Add a day. |
||
317 | * |
||
318 | * @param DayInterface $day The day. |
||
319 | */ |
||
320 | private function addDay(DayInterface $day) |
||
324 | } |
||
325 |
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.