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:
Complex classes like Date often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use Date, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
31 | class Date |
||
32 | { |
||
33 | /** |
||
34 | * set name of image |
||
35 | * |
||
36 | * @access public |
||
37 | * @param int $iWeek number of week |
||
38 | * @param int $iYear year |
||
39 | * @param string $sFormat |
||
40 | * @return Date |
||
41 | */ |
||
42 | public static function getWeek(int $iWeek, int $iYear, string $sFormat = "Y-m-d") : Date |
||
43 | { |
||
44 | $iFirstDayInYear = date("N",mktime(0, 0, 0, 1, 1, $iYear)); |
||
45 | |||
46 | View Code Duplication | if ($iFirstDayInYear < 5) { $iShift = -($iFirstDayInYear - 1) * 86400; } else { $iShift = (8 - $iFirstDayInYear) * 86400; } |
|
|
|||
47 | |||
48 | if ($iWeek > 1) { $iWeekInSeconds = ($iWeek-1) * 604800; } else { $iWeekInSeconds = 0; } |
||
49 | |||
50 | $iTimestamp = mktime(0, 0, 0, 1, 1, $iYear) + $iWeekInSeconds + $iShift; |
||
51 | $iTimestampLastDay = mktime(0, 0, 0, 1, 6, $iYear) + $iWeekInSeconds + $iShift + 604800; |
||
52 | |||
53 | return array(date($sFormat, $iTimestamp), date($sFormat, $iTimestampLastDay)); |
||
54 | } |
||
55 | |||
56 | /** |
||
57 | * set name of image |
||
58 | * |
||
59 | * @access public |
||
60 | * @return \Venus\lib\Date |
||
61 | */ |
||
62 | public static function getActualWeek() : Date |
||
63 | { |
||
64 | return self::getWeek(date('W'), date('Y')); |
||
65 | } |
||
66 | |||
67 | /** |
||
68 | * set name of image |
||
69 | * |
||
70 | * @access public |
||
71 | * @param string $sMonth number of week |
||
72 | * @param string $sLanguage language |
||
73 | * @return \Venus\lib\Date |
||
74 | */ |
||
75 | public static function getMonthInWord(string $sMonth, string $sLanguage = 'fr') : Date |
||
76 | { |
||
77 | if ($sLanguage == 'fr') { |
||
78 | |||
79 | if ($sMonth == '01' || $sMonth == 1) { return 'Janvier'; } |
||
80 | else if ($sMonth == '02' || $sMonth == 2) { return 'Février'; } |
||
81 | else if ($sMonth == '03' || $sMonth == 3) { return 'Mars'; } |
||
82 | else if ($sMonth == '04' || $sMonth == 4) { return 'Avril'; } |
||
83 | else if ($sMonth == '05' || $sMonth == 5) { return 'Mai'; } |
||
84 | else if ($sMonth == '06' || $sMonth == 6) { return 'Juin'; } |
||
85 | else if ($sMonth == '07' || $sMonth == 7) { return 'Juillet'; } |
||
86 | else if ($sMonth == '08' || $sMonth == 8) { return 'Août'; } |
||
87 | else if ($sMonth == '09' || $sMonth == 9) { return 'Septembre'; } |
||
88 | else if ($sMonth == 10) { return 'Octobre'; } |
||
89 | else if ($sMonth == 11) { return 'Novembre'; } |
||
90 | else if ($sMonth == 12) { return 'Décembre'; } |
||
91 | } |
||
92 | } |
||
93 | |||
94 | /** |
||
95 | * set name of image |
||
96 | * |
||
97 | * @access public |
||
98 | * @param mixed $sDay number of day |
||
99 | * @param string $sLanguage language |
||
100 | * @return \Venus\lib\Date |
||
101 | */ |
||
102 | public static function getDayInWord(string $sDay, string $sLanguage = 'fr') : Date |
||
103 | { |
||
104 | if ($sLanguage == 'fr') { |
||
105 | |||
106 | if ($sDay == 0) { return 'dimanche'; } |
||
107 | else if ($sDay == 1) { return 'lundi'; } |
||
108 | else if ($sDay == 2) { return 'mardi'; } |
||
109 | else if ($sDay == 3) { return 'mercredi'; } |
||
110 | else if ($sDay == 4) { return 'jeudi'; } |
||
111 | else if ($sDay == 5) { return 'vendredi'; } |
||
112 | else if ($sDay == 6) { return 'samedi'; } |
||
113 | } |
||
114 | } |
||
115 | |||
116 | /** |
||
117 | * get age by date |
||
118 | * |
||
119 | * @access public |
||
120 | * @param string $sBirthday |
||
121 | * @return int |
||
122 | */ |
||
123 | public static function getAgeByDate(string $sBirthday) : int |
||
124 | { |
||
125 | list($iYear, $iMonth, $iDay) = preg_split('/[-.]/', $sBirthday); |
||
126 | |||
127 | $aToday = array(); |
||
128 | $aToday['mois'] = date('n'); |
||
129 | $aToday['jour'] = date('j'); |
||
130 | $aToday['annee'] = date('Y'); |
||
131 | |||
132 | $iYears = $aToday['annee'] - $iYear; |
||
133 | |||
134 | if ($aToday['mois'] <= $iMonth) { |
||
135 | |||
136 | if ($iMonth == $aToday['mois']) { |
||
137 | |||
138 | if ($iDay > $aToday['jour']) { $iYears--; } |
||
139 | } |
||
140 | else { |
||
141 | |||
142 | $iYears--; |
||
143 | } |
||
144 | } |
||
145 | |||
146 | return $iYears; |
||
147 | } |
||
148 | |||
149 | /** |
||
150 | * set name of image |
||
151 | * |
||
152 | * @access public |
||
153 | * @param int $iWeek number of week |
||
154 | * @param int $iYear year |
||
155 | * @param string $sFormat |
||
156 | * @return array|Date |
||
157 | */ |
||
158 | public static function getMiddleWeek(int $iWeek, int $iYear, string $sFormat = "Y-m-d") : array |
||
159 | { |
||
160 | $iFirstDayInYear = date("N",mktime(0, 0, 0, 1, 1, $iYear)); |
||
161 | |||
162 | View Code Duplication | if ($iFirstDayInYear < 5) { $iShift = -($iFirstDayInYear - 1) * 86400; } |
|
163 | else { $iShift = (8 - $iFirstDayInYear) * 86400; } |
||
164 | |||
165 | if ($iWeek > 1) { $iWeekInSeconds = ($iWeek-1) * 604800; } |
||
166 | else { $iWeekInSeconds = 0; } |
||
167 | |||
168 | if (date('N') > 2) { |
||
169 | |||
170 | $iTimestamp = mktime(0, 0, 0, 1, 1, $iYear) + $iWeekInSeconds + $iShift + 172800; |
||
171 | $iTimestampLastDay = $iTimestamp + 604800; |
||
172 | } |
||
173 | else { |
||
174 | |||
175 | $iTimestamp = mktime(0, 0, 0, 1, 1, $iYear) + $iWeekInSeconds + $iShift - 432000; |
||
176 | $iTimestampLastDay = $iTimestamp + 604800; |
||
177 | } |
||
178 | |||
179 | $aDates = array(date($sFormat, $iTimestamp), date($sFormat, $iTimestampLastDay)); |
||
180 | |||
181 | if (preg_replace('/^([0-9]+)-[0-9]+-[0-9]+$/', '$1', $aDates[0]) != date('Y')) { |
||
182 | |||
183 | $aDates[0] = preg_replace('/^[0-9]+(-[0-9]+-[0-9]+)$/', date('Y').'$1', $aDates[0]); |
||
184 | $aDates[1] = preg_replace('/^[0-9]+(-[0-9]+-[0-9]+)$/', (date('Y')+1).'$1', $aDates[1]); |
||
185 | } |
||
186 | |||
187 | return $aDates; |
||
188 | } |
||
189 | |||
190 | /** |
||
191 | * set name of image |
||
192 | * |
||
193 | * @access public |
||
194 | * @return array |
||
195 | */ |
||
196 | public static function getActualMiddleWeek() : array |
||
197 | { |
||
198 | return self::getMiddleWeek(date('W'), date('Y')); |
||
199 | } |
||
200 | |||
201 | /** |
||
202 | * get time of kind "X hour ago" |
||
203 | * |
||
204 | * @access public |
||
205 | * @param string $sDateTime datetime to convert |
||
206 | * @param string $sLanguage language |
||
207 | * @return string |
||
208 | */ |
||
209 | public static function getTimeAgoInString(string $sDateTime, string $sLanguage = 'fr') : string |
||
239 | } |
||
240 |
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.