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 |
||
26 | class PersianCalendar implements CalendarInterface |
||
27 | { |
||
28 | /** |
||
29 | * In each 128 year cycle, the following years are leap years. |
||
30 | * |
||
31 | * @var int[] |
||
32 | */ |
||
33 | private static $LEAP_YEAR_CYCLE = array( |
||
34 | 0, 5, 9, 13, 17, 21, 25, 29, 34, 38, 42, 46, 50, 54, 58, 62, 67, 71, 75, 79, 83, 87, 91, 95, 100, 104, 108, 112, 116, 120, 124 |
||
35 | ); |
||
36 | |||
37 | /** |
||
38 | * Determine the number of days in a specified month, allowing for leap years, etc. |
||
39 | * |
||
40 | * @param int $year |
||
41 | * @param int $month |
||
42 | * |
||
43 | * @return int |
||
44 | */ |
||
45 | public function daysInMonth($year, $month) |
||
55 | |||
56 | /** |
||
57 | * Determine the number of days in a week. |
||
58 | * |
||
59 | * @return int |
||
60 | */ |
||
61 | public function daysInWeek() |
||
65 | |||
66 | /** |
||
67 | * The escape sequence used to indicate this calendar in GEDCOM files. |
||
68 | * |
||
69 | * @return string |
||
70 | */ |
||
71 | public function gedcomCalendarEscape() |
||
75 | |||
76 | /** |
||
77 | * Determine whether or not a given year is a leap-year. |
||
78 | * |
||
79 | * @param int $year |
||
80 | * |
||
81 | * @return bool |
||
82 | */ |
||
83 | public function isLeapYear($year) |
||
87 | |||
88 | /** |
||
89 | * What is the highest Julian day number that can be converted into this calendar. |
||
90 | * |
||
91 | * @return int |
||
92 | */ |
||
93 | public function jdEnd() |
||
97 | |||
98 | /** |
||
99 | * What is the lowest Julian day number that can be converted into this calendar. |
||
100 | * |
||
101 | * @return int |
||
102 | */ |
||
103 | public function jdStart() |
||
107 | |||
108 | /** |
||
109 | * Convert a Julian day number into a year/month/day. |
||
110 | * |
||
111 | * @param int $julian_day |
||
112 | * |
||
113 | * @return int[] |
||
114 | */ |
||
115 | public function jdToYmd($julian_day) |
||
136 | |||
137 | /** |
||
138 | * Determine the number of months in a year (if given), |
||
139 | * or the maximum number of months in any year. |
||
140 | * |
||
141 | * @param int|null $year |
||
142 | * |
||
143 | * @return int |
||
144 | */ |
||
145 | public function monthsInYear($year = null) |
||
149 | |||
150 | /** |
||
151 | * Convert a year/month/day to a Julian day number. |
||
152 | * |
||
153 | * @param int $year |
||
154 | * @param int $month |
||
155 | * @param int $day |
||
156 | * |
||
157 | * @return int |
||
158 | */ |
||
159 | public function ymdToJd($year, $month, $day) |
||
176 | |||
177 | /** |
||
178 | * The PHP modulus function returns a negative modulus for a negative dividend. |
||
179 | * This algorithm requires a "traditional" modulus function where the modulus is |
||
180 | * always positive. |
||
181 | * |
||
182 | * @param int $dividend |
||
183 | * @param int $divisor |
||
184 | * |
||
185 | * @return int |
||
186 | */ |
||
187 | public function mod($dividend, $divisor) |
||
200 | } |
||
201 |
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.