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 |
||
10 | View Code Duplication | abstract class DateTimeUtil |
|
|
|||
11 | { |
||
12 | /** |
||
13 | * Returns true if the given date time is a Saturday or Sunday. |
||
14 | * |
||
15 | * @param DateTimeInterface $dateTime The date/time object. |
||
16 | * |
||
17 | * @return bool |
||
18 | */ |
||
19 | final public static function isWeekendDay(DateTimeInterface $dateTime) : bool |
||
24 | |||
25 | /** |
||
26 | * Returns true if the given date time is not a Saturday or Sunday. |
||
27 | * |
||
28 | * @param DateTimeInterface $dateTime The date/time object. |
||
29 | * |
||
30 | * @return bool |
||
31 | */ |
||
32 | final public static function isWeekDay(DateTimeInterface $dateTime) : bool |
||
37 | |||
38 | /** |
||
39 | * Returns true if the given dates occur on the same year, month and day. |
||
40 | * |
||
41 | * @param DateTimeInterface $thisDate A date to compare. |
||
42 | * @param DateTimeInterface $thatDate A date to compare. |
||
43 | * |
||
44 | * @return bool |
||
45 | */ |
||
46 | final public static function isSameDay(DateTimeInterface $thisDate, DateTimeInterface $thatDate) : bool |
||
51 | |||
52 | /** |
||
53 | * Indicates whether the given instance of DateTime is within the daylight saving time range for the current time |
||
54 | * zone. |
||
55 | * |
||
56 | * @param DateTimeInterface $dateTime The date/time object. |
||
57 | * |
||
58 | * @return bool |
||
59 | */ |
||
60 | final public static function isDaylightSavings(DateTimeInterface $dateTime) : bool |
||
64 | |||
65 | /** |
||
66 | * Returns true if the given date is between the provided date range. |
||
67 | * |
||
68 | * @param DateTimeInterface $subjectDate The date/time object being checked. |
||
69 | * @param DateTimeInterface $startDate The start date/time object. |
||
70 | * @param DateTimeInterface $endDate The end date/time object. |
||
71 | * |
||
72 | * @return bool |
||
73 | * |
||
74 | * @throws \DomainException Thrown when an invalid date range is provided. |
||
75 | */ |
||
76 | final public static function isInRange( |
||
90 | |||
91 | /** |
||
92 | * Returns the given DateTime instance as a "time ago" string. |
||
93 | * |
||
94 | * @param DateTimeInterface $dateTime The DateTime object to present as an ago string. |
||
95 | * |
||
96 | * @return string |
||
97 | * |
||
98 | * @throws \DomainException Thrown if the given $dateTime is in the future. |
||
99 | */ |
||
100 | final public static function asAgoString(DateTimeInterface $dateTime) : string |
||
129 | } |
||
130 |
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.