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 |
||
12 | abstract class AbstractDay implements DayInterface |
||
13 | { |
||
14 | /** |
||
15 | * The days of the week. |
||
16 | * |
||
17 | * @var array |
||
18 | */ |
||
19 | private $daysOfWeek = array( |
||
20 | DayInterface::WEEK_DAY_MONDAY => 'Monday', |
||
21 | DayInterface::WEEK_DAY_TUESDAY => 'Tuesday', |
||
22 | DayInterface::WEEK_DAY_WEDNESDAY => 'Wednesday', |
||
23 | DayInterface::WEEK_DAY_THURSDAY => 'Thursday', |
||
24 | DayInterface::WEEK_DAY_FRIDAY => 'Friday', |
||
25 | DayInterface::WEEK_DAY_SATURDAY => 'Saturday', |
||
26 | DayInterface::WEEK_DAY_SUNDAY => 'Sunday', |
||
27 | ); |
||
28 | |||
29 | /** |
||
30 | * The day of week. |
||
31 | * |
||
32 | * @var integer |
||
33 | */ |
||
34 | protected $dayOfWeek; |
||
35 | |||
36 | /** |
||
37 | * The time intervals. |
||
38 | * |
||
39 | * @var TimeIntervalInterface[] |
||
40 | */ |
||
41 | protected $openingHoursIntervals; |
||
42 | |||
43 | /** |
||
44 | * Constructor. |
||
45 | * |
||
46 | * @param integer $dayOfWeek The day of week. |
||
47 | * @param TimeIntervalInterface[] $openingHoursIntervals The opening hours intervals. |
||
48 | */ |
||
49 | public function __construct($dayOfWeek, array $openingHoursIntervals) |
||
54 | |||
55 | /** |
||
56 | * {@inheritdoc} |
||
57 | */ |
||
58 | public function getDayOfWeek() |
||
62 | |||
63 | /** |
||
64 | * {@inheritdoc} |
||
65 | */ |
||
66 | public function getDayOfWeekName() |
||
70 | |||
71 | /** |
||
72 | * {@inheritdoc} |
||
73 | */ |
||
74 | public function getOpeningHoursIntervals() |
||
78 | |||
79 | /** |
||
80 | * {@inheritdoc} |
||
81 | */ |
||
82 | public function getClosestPreviousOpeningHoursInterval(Time $time) |
||
92 | |||
93 | /** |
||
94 | * {@inheritdoc} |
||
95 | */ |
||
96 | public function getClosestNextOpeningHoursInterval(Time $time) |
||
106 | |||
107 | /** |
||
108 | * {@inheritdoc} |
||
109 | */ |
||
110 | View Code Duplication | public function getPreviousOpeningHoursInterval(Time $time) |
|
136 | |||
137 | /** |
||
138 | * {@inheritdoc} |
||
139 | */ |
||
140 | View Code Duplication | public function getNextOpeningHoursInterval(Time $time) |
|
165 | |||
166 | /** |
||
167 | * {@inheritdoc} |
||
168 | */ |
||
169 | public function getOpeningTime() |
||
173 | |||
174 | /** |
||
175 | * {@inheritdoc} |
||
176 | */ |
||
177 | public function getClosingTime() |
||
184 | |||
185 | /** |
||
186 | * {@inheritdoc} |
||
187 | */ |
||
188 | public function isWithinOpeningHours(Time $time) |
||
198 | |||
199 | /** |
||
200 | * Set the day of week. |
||
201 | * |
||
202 | * @param integer $dayOfWeek |
||
203 | * @throws \OutOfBoundsException If the given day is invalid. |
||
204 | */ |
||
205 | protected function setDayOfWeek($dayOfWeek) |
||
213 | |||
214 | /** |
||
215 | * Set the opening hours intervals. |
||
216 | * |
||
217 | * @param TimeIntervalInterface[] $openingHoursIntervals The opening hours intervals. |
||
218 | * @throws \InvalidArgumentException If no days are passed or invalid interval is passed. |
||
219 | */ |
||
220 | protected function setOpeningHoursIntervals(array $openingHoursIntervals) |
||
238 | |||
239 | /** |
||
240 | * Flatten the intervals that overlap. |
||
241 | * |
||
242 | * @param TimeIntervalInterface[] $openingHoursIntervals |
||
243 | * @return TimeIntervalInterface[] |
||
244 | */ |
||
245 | protected function flattenOpeningHoursIntervals(array $openingHoursIntervals) |
||
273 | |||
274 | /** |
||
275 | * Handle cloning. |
||
276 | */ |
||
277 | public function __clone() |
||
287 | } |
||
288 |
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.