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 | class DayBuilder |
||
13 | { |
||
14 | /** |
||
15 | * Create a new Day. |
||
16 | * |
||
17 | * @param integer $dayOfWeek The day of week. |
||
18 | * @param array $openingIntervals The opening intervals. |
||
19 | * @return Day |
||
20 | */ |
||
21 | public static function fromArray($dayOfWeek, array $openingIntervals) |
||
33 | |||
34 | /** |
||
35 | * Create a DayInterface object from an array. |
||
36 | * |
||
37 | * @param array $data The day data. |
||
38 | * @return DayInterface |
||
39 | */ |
||
40 | public static function fromAssociativeArray(array $data) |
||
59 | |||
60 | /** |
||
61 | * Check if an interval array is all day. |
||
62 | * |
||
63 | * @param Time $start The start time. |
||
64 | * @param Time $end The end time. |
||
65 | * @return boolean |
||
66 | */ |
||
67 | private static function isIntervalAllDay(Time $start, Time $end) |
||
79 | } |
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.