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 |
||
12 | class Date |
||
13 | { |
||
14 | /** constants */ |
||
15 | const CALENDAR_WINDOWS_1900 = 1900; // Base date of 1st Jan 1900 = 1.0 |
||
16 | const CALENDAR_MAC_1904 = 1904; // Base date of 2nd Jan 1904 = 1.0 |
||
17 | |||
18 | /* |
||
19 | * Names of the months of the year, indexed by shortname |
||
20 | * Planned usage for locale settings |
||
21 | * |
||
22 | * @public |
||
23 | * @var string[] |
||
24 | */ |
||
25 | public static $monthNames = [ |
||
26 | 'Jan' => 'January', |
||
27 | 'Feb' => 'February', |
||
28 | 'Mar' => 'March', |
||
29 | 'Apr' => 'April', |
||
30 | 'May' => 'May', |
||
31 | 'Jun' => 'June', |
||
32 | 'Jul' => 'July', |
||
33 | 'Aug' => 'August', |
||
34 | 'Sep' => 'September', |
||
35 | 'Oct' => 'October', |
||
36 | 'Nov' => 'November', |
||
37 | 'Dec' => 'December', |
||
38 | ]; |
||
39 | |||
40 | /* |
||
41 | * @public |
||
42 | * @var string[] |
||
43 | */ |
||
44 | public static $numberSuffixes = [ |
||
45 | 'st', |
||
46 | 'nd', |
||
47 | 'rd', |
||
48 | 'th', |
||
49 | ]; |
||
50 | |||
51 | /* |
||
52 | * Base calendar year to use for calculations |
||
53 | * Value is either CALENDAR_WINDOWS_1900 (1900) or CALENDAR_MAC_1904 (1904) |
||
54 | * |
||
55 | * @private |
||
56 | * @var int |
||
57 | */ |
||
58 | protected static $excelCalendar = self::CALENDAR_WINDOWS_1900; |
||
59 | |||
60 | /* |
||
61 | * Default timezone to use for DateTime objects |
||
62 | * |
||
63 | * @private |
||
64 | * @var null|\DateTimeZone |
||
65 | */ |
||
66 | protected static $defaultTimeZone; |
||
67 | |||
68 | /** |
||
69 | * Set the Excel calendar (Windows 1900 or Mac 1904). |
||
70 | * |
||
71 | * @param int $baseDate Excel base date (1900 or 1904) |
||
72 | * |
||
73 | * @return bool Success or failure |
||
74 | */ |
||
75 | 90 | public static function setExcelCalendar($baseDate) |
|
76 | { |
||
77 | 90 | if (($baseDate == self::CALENDAR_WINDOWS_1900) || |
|
78 | 90 | ($baseDate == self::CALENDAR_MAC_1904)) { |
|
79 | 89 | self::$excelCalendar = $baseDate; |
|
80 | |||
81 | 89 | return true; |
|
82 | } |
||
83 | |||
84 | 1 | return false; |
|
85 | } |
||
86 | |||
87 | /** |
||
88 | * Return the Excel calendar (Windows 1900 or Mac 1904). |
||
89 | * |
||
90 | * @return int Excel base date (1900 or 1904) |
||
91 | */ |
||
92 | public static function getExcelCalendar() |
||
93 | { |
||
94 | return self::$excelCalendar; |
||
95 | } |
||
96 | |||
97 | /** |
||
98 | * Set the Default timezone to use for dates. |
||
99 | * |
||
100 | * @param DateTimeZone|string $timeZone The timezone to set for all Excel datetimestamp to PHP DateTime Object conversions |
||
101 | * |
||
102 | * @throws \Exception |
||
103 | * |
||
104 | * @return bool Success or failure |
||
105 | * @return bool Success or failure |
||
106 | */ |
||
107 | public static function setDefaultTimezone($timeZone) |
||
108 | { |
||
109 | if ($timeZone = self::validateTimeZone($timeZone)) { |
||
110 | self::$defaultTimeZone = $timeZone; |
||
111 | |||
112 | return true; |
||
113 | } |
||
114 | |||
115 | return false; |
||
116 | } |
||
117 | |||
118 | /** |
||
119 | * Return the Default timezone being used for dates. |
||
120 | * |
||
121 | * @return DateTimeZone The timezone being used as default for Excel timestamp to PHP DateTime object |
||
122 | */ |
||
123 | 23 | public static function getDefaultTimezone() |
|
124 | { |
||
125 | 23 | if (self::$defaultTimeZone === null) { |
|
126 | 1 | self::$defaultTimeZone = new DateTimeZone('UTC'); |
|
127 | } |
||
128 | |||
129 | 23 | return self::$defaultTimeZone; |
|
130 | } |
||
131 | |||
132 | /** |
||
133 | * Validate a timezone. |
||
134 | * |
||
135 | * @param DateTimeZone|string $timeZone The timezone to validate, either as a timezone string or object |
||
136 | * |
||
137 | * @throws \Exception |
||
138 | * |
||
139 | * @return DateTimeZone The timezone as a timezone object |
||
140 | * @return DateTimeZone The timezone as a timezone object |
||
141 | */ |
||
142 | 22 | protected static function validateTimeZone($timeZone) |
|
143 | { |
||
144 | 22 | if (is_object($timeZone) && $timeZone instanceof DateTimeZone) { |
|
145 | return $timeZone; |
||
146 | 22 | } elseif (is_string($timeZone)) { |
|
147 | 22 | return new DateTimeZone($timeZone); |
|
148 | } |
||
149 | |||
150 | throw new \Exception('Invalid timezone'); |
||
151 | } |
||
152 | |||
153 | /** |
||
154 | * Convert a MS serialized datetime value from Excel to a PHP Date/Time object. |
||
155 | * |
||
156 | * @param float|int $excelTimestamp MS Excel serialized date/time value |
||
157 | * @param null|DateTimeZone|string $timeZone The timezone to assume for the Excel timestamp, |
||
158 | * if you don't want to treat it as a UTC value |
||
159 | * Use the default (UST) unless you absolutely need a conversion |
||
160 | * |
||
161 | * @throws \Exception |
||
162 | * |
||
163 | * @return \DateTime PHP date/time object |
||
164 | */ |
||
165 | 45 | public static function excelToDateTimeObject($excelTimestamp, $timeZone = null) |
|
166 | { |
||
167 | 45 | $timeZone = ($timeZone === null) ? self::getDefaultTimezone() : self::validateTimeZone($timeZone); |
|
168 | 45 | if ($excelTimestamp < 1.0) { |
|
169 | // Unix timestamp base date |
||
170 | 12 | $baseDate = new \DateTime('1970-01-01', $timeZone); |
|
171 | } else { |
||
172 | // MS Excel calendar base dates |
||
173 | 33 | if (self::$excelCalendar == self::CALENDAR_WINDOWS_1900) { |
|
174 | // Allow adjustment for 1900 Leap Year in MS Excel |
||
175 | 27 | $baseDate = ($excelTimestamp < 60) ? new \DateTime('1899-12-31', $timeZone) : new \DateTime('1899-12-30', $timeZone); |
|
176 | } else { |
||
177 | 6 | $baseDate = new \DateTime('1904-01-01', $timeZone); |
|
178 | } |
||
179 | } |
||
180 | 45 | $days = floor($excelTimestamp); |
|
181 | 45 | $partDay = $excelTimestamp - $days; |
|
182 | 45 | $hours = floor($partDay * 24); |
|
183 | 45 | $partDay = $partDay * 24 - $hours; |
|
184 | 45 | $minutes = floor($partDay * 60); |
|
185 | 45 | $partDay = $partDay * 60 - $minutes; |
|
186 | 45 | $seconds = round($partDay * 60); |
|
187 | |||
188 | 45 | $interval = '+' . $days . ' days'; |
|
189 | |||
190 | 45 | return $baseDate->modify($interval) |
|
191 | 45 | ->setTime($hours, $minutes, $seconds); |
|
192 | } |
||
193 | |||
194 | /** |
||
195 | * Convert a MS serialized datetime value from Excel to a unix timestamp. |
||
196 | * |
||
197 | * @param float|int $excelTimestamp MS Excel serialized date/time value |
||
198 | * @param null|DateTimeZone|string $timeZone The timezone to assume for the Excel timestamp, |
||
199 | * if you don't want to treat it as a UTC value |
||
200 | * Use the default (UST) unless you absolutely need a conversion |
||
201 | * |
||
202 | * @throws \Exception |
||
203 | * |
||
204 | * @return int Unix timetamp for this date/time |
||
205 | */ |
||
206 | 45 | public static function excelToTimestamp($excelTimestamp, $timeZone = null) |
|
207 | { |
||
208 | 45 | return (int) self::excelToDateTimeObject($excelTimestamp, $timeZone) |
|
209 | 45 | ->format('U'); |
|
210 | } |
||
211 | |||
212 | /** |
||
213 | * Convert a date from PHP to an MS Excel serialized date/time value. |
||
214 | * |
||
215 | * @param mixed $dateValue Unix Timestamp or PHP DateTime object or a string |
||
216 | * |
||
217 | * @return bool|float Excel date/time value |
||
218 | * or boolean FALSE on failure |
||
219 | */ |
||
220 | public static function PHPToExcel($dateValue) |
||
221 | { |
||
222 | if ((is_object($dateValue)) && ($dateValue instanceof DateTimeInterface)) { |
||
223 | return self::dateTimeToExcel($dateValue); |
||
224 | } elseif (is_numeric($dateValue)) { |
||
225 | return self::timestampToExcel($dateValue); |
||
|
|||
226 | } elseif (is_string($dateValue)) { |
||
227 | return self::stringToExcel($dateValue); |
||
228 | } |
||
229 | |||
230 | return false; |
||
231 | } |
||
232 | |||
233 | /** |
||
234 | * Convert a PHP DateTime object to an MS Excel serialized date/time value. |
||
235 | * |
||
236 | * @param DateTimeInterface $dateValue PHP DateTime object |
||
237 | * |
||
238 | * @return float MS Excel serialized date/time value |
||
239 | */ |
||
240 | 32 | public static function dateTimeToExcel(DateTimeInterface $dateValue) |
|
241 | { |
||
242 | 32 | return self::formattedPHPToExcel( |
|
243 | 32 | $dateValue->format('Y'), |
|
244 | 32 | $dateValue->format('m'), |
|
245 | 32 | $dateValue->format('d'), |
|
246 | 32 | $dateValue->format('H'), |
|
247 | 32 | $dateValue->format('i'), |
|
248 | 32 | $dateValue->format('s') |
|
249 | ); |
||
250 | } |
||
251 | |||
252 | /** |
||
253 | * Convert a Unix timestamp to an MS Excel serialized date/time value. |
||
254 | * |
||
255 | * @param DateTimeInterface $dateValue Unix Timestamp |
||
256 | * |
||
257 | * @return float MS Excel serialized date/time value |
||
258 | */ |
||
259 | 17 | public static function timestampToExcel($dateValue) |
|
260 | { |
||
261 | 17 | if (!is_numeric($dateValue)) { |
|
262 | return false; |
||
263 | } |
||
264 | |||
265 | 17 | return self::dateTimeToExcel(new \DateTime('@' . $dateValue)); |
|
266 | } |
||
267 | |||
268 | /** |
||
269 | * formattedPHPToExcel. |
||
270 | * |
||
271 | * @param int $year |
||
272 | * @param int $month |
||
273 | * @param int $day |
||
274 | * @param int $hours |
||
275 | * @param int $minutes |
||
276 | * @param int $seconds |
||
277 | * |
||
278 | * @return float Excel date/time value |
||
279 | */ |
||
280 | 43 | public static function formattedPHPToExcel($year, $month, $day, $hours = 0, $minutes = 0, $seconds = 0) |
|
281 | { |
||
282 | 43 | if (self::$excelCalendar == self::CALENDAR_WINDOWS_1900) { |
|
283 | // |
||
284 | // Fudge factor for the erroneous fact that the year 1900 is treated as a Leap Year in MS Excel |
||
285 | // This affects every date following 28th February 1900 |
||
286 | // |
||
287 | 37 | $excel1900isLeapYear = true; |
|
288 | 37 | if (($year == 1900) && ($month <= 2)) { |
|
289 | 2 | $excel1900isLeapYear = false; |
|
290 | } |
||
291 | 37 | $myexcelBaseDate = 2415020; |
|
292 | } else { |
||
293 | 6 | $myexcelBaseDate = 2416481; |
|
294 | 6 | $excel1900isLeapYear = false; |
|
295 | } |
||
296 | |||
297 | // Julian base date Adjustment |
||
298 | 43 | if ($month > 2) { |
|
299 | 26 | $month -= 3; |
|
300 | } else { |
||
301 | 17 | $month += 9; |
|
302 | 17 | --$year; |
|
303 | } |
||
304 | |||
305 | // Calculate the Julian Date, then subtract the Excel base date (JD 2415020 = 31-Dec-1899 Giving Excel Date of 0) |
||
306 | 43 | $century = substr($year, 0, 2); |
|
307 | 43 | $decade = substr($year, 2, 2); |
|
308 | 43 | $excelDate = floor((146097 * $century) / 4) + floor((1461 * $decade) / 4) + floor((153 * $month + 2) / 5) + $day + 1721119 - $myexcelBaseDate + $excel1900isLeapYear; |
|
309 | |||
310 | 43 | $excelTime = (($hours * 3600) + ($minutes * 60) + $seconds) / 86400; |
|
311 | |||
312 | 43 | return (float) $excelDate + $excelTime; |
|
313 | } |
||
314 | |||
315 | /** |
||
316 | * Is a given cell a date/time? |
||
317 | * |
||
318 | * @param Cell $pCell |
||
319 | * |
||
320 | * @return bool |
||
321 | */ |
||
322 | public static function isDateTime(Cell $pCell) |
||
330 | |||
331 | /** |
||
332 | * Is a given number format a date/time? |
||
333 | * |
||
334 | * @param NumberFormat $pFormat |
||
335 | * |
||
336 | * @return bool |
||
337 | */ |
||
338 | public static function isDateTimeFormat(NumberFormat $pFormat) |
||
339 | { |
||
340 | return self::isDateTimeFormatCode($pFormat->getFormatCode()); |
||
341 | } |
||
342 | |||
343 | private static $possibleDateFormatCharacters = 'eymdHs'; |
||
344 | |||
345 | /** |
||
346 | * Is a given number format code a date/time? |
||
347 | * |
||
348 | * @param string $pFormatCode |
||
349 | * |
||
350 | * @return bool |
||
351 | */ |
||
352 | 36 | public static function isDateTimeFormatCode($pFormatCode) |
|
418 | |||
419 | /** |
||
420 | * Convert a date/time string to Excel time. |
||
421 | * |
||
422 | * @param string $dateValue Examples: '2009-12-31', '2009-12-31 15:59', '2009-12-31 15:59:10' |
||
423 | * |
||
424 | * @return false|float Excel date/time serial value |
||
425 | */ |
||
426 | public static function stringToExcel($dateValue) |
||
451 | |||
452 | /** |
||
453 | * Converts a month name (either a long or a short name) to a month number. |
||
454 | * |
||
455 | * @param string $month Month name or abbreviation |
||
456 | * |
||
457 | * @return int|string Month number (1 - 12), or the original string argument if it isn't a valid month name |
||
458 | */ |
||
459 | public static function monthStringToNumber($month) |
||
471 | |||
472 | /** |
||
473 | * Strips an ordinal froma numeric value. |
||
474 | * |
||
475 | * @param string $day Day number with an ordinal |
||
476 | * |
||
477 | * @return int|string The integer value with any ordinal stripped, or the original string argument if it isn't a valid numeric |
||
478 | */ |
||
479 | public static function dayStringToNumber($day) |
||
488 | } |
||
489 |
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: