Complex classes like Dates 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 Dates, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
12 | class Dates extends DateTime |
||
13 | { |
||
14 | /** |
||
15 | * @var string[] $humanReadableI18n Words used in method to transform |
||
16 | * date difference to human readable. |
||
17 | */ |
||
18 | protected static $humanReadableI18n = [ |
||
19 | 'now' => 'now', |
||
20 | 'since' => 'since', |
||
21 | 'in' => 'in', |
||
22 | 'yesterday' => 'yesterday', |
||
23 | 'the' => 'the', |
||
24 | 'at' => 'at' |
||
25 | ]; |
||
26 | |||
27 | /** |
||
28 | * @var string[] $humanReadableFormats Date and time formats used in |
||
29 | * method to transform date difference to human readable. |
||
30 | */ |
||
31 | protected static $humanReadableFormats = [ |
||
32 | 'dateSameYear' => 'm-d', |
||
33 | 'dateDifferentYear' => 'Y-m-d', |
||
34 | 'time' => 'H:i' |
||
35 | ]; |
||
36 | |||
37 | /** |
||
38 | * Return the value of the humanReadableI18n property |
||
39 | * |
||
40 | * @return string[] |
||
41 | */ |
||
42 | public static function getHumanReadableI18n() |
||
46 | |||
47 | /** |
||
48 | * Define a new value for a key of the humanReadableI18n property |
||
49 | * |
||
50 | * @param string $key The key in humanReadableI18n |
||
51 | * @param string $value The new value for the key |
||
52 | * |
||
53 | * @return void |
||
54 | */ |
||
55 | public static function setHumanReadableI18nKey($key, $value) |
||
59 | |||
60 | /** |
||
61 | * Define a new value to the property humanReadableI18n |
||
62 | * |
||
63 | * @param string[] $value The new value for the property |
||
64 | * |
||
65 | * @return void |
||
66 | */ |
||
67 | public static function setHumanReadableI18n($value) |
||
71 | |||
72 | /** |
||
73 | * Return the value of the humanReadableFormats property |
||
74 | * |
||
75 | * @return string[] |
||
76 | */ |
||
77 | public static function getHumanReadableFormats() |
||
81 | |||
82 | /** |
||
83 | * Define a new value for a key of the humanReadableFormats property |
||
84 | * |
||
85 | * @param string $key The key in humanReadableFormats |
||
86 | * @param string $value The new value for the key |
||
87 | * |
||
88 | * @return void |
||
89 | */ |
||
90 | public static function setHumanReadableFormatsKey($key, $value) |
||
94 | |||
95 | /** |
||
96 | * Define a new value to the property humanReadableFormats |
||
97 | * |
||
98 | * @param string[] $value The new value for the property |
||
99 | * |
||
100 | * @return void |
||
101 | */ |
||
102 | public static function setHumanReadableFormats($value) |
||
106 | |||
107 | /** |
||
108 | * Return the date. Format is Y-m-d H:i:sO |
||
109 | * |
||
110 | * @return string |
||
111 | */ |
||
112 | public function getDate() |
||
116 | |||
117 | /** |
||
118 | * Return a numeric representation of a year, 4 digits. |
||
119 | * |
||
120 | * @return int |
||
121 | */ |
||
122 | public function getYear() |
||
126 | |||
127 | /** |
||
128 | * Return the numeric representation of a month, without leading zeros. |
||
129 | * The returned int format can not have leading zeros. |
||
130 | * |
||
131 | * @return int |
||
132 | */ |
||
133 | public function getMonth() |
||
137 | |||
138 | /** |
||
139 | * Return the day of the month without leading zeros. |
||
140 | * The returned int format can not have leading zeros. |
||
141 | * |
||
142 | * @return int |
||
143 | */ |
||
144 | public function getDay() |
||
148 | |||
149 | /** |
||
150 | * Return 24-hour format without leading zeros. |
||
151 | * The returned int format can not have leading zeros. |
||
152 | * |
||
153 | * @return int |
||
154 | */ |
||
155 | public function getHour() |
||
159 | |||
160 | /** |
||
161 | * Return minutes, without leading zeros. |
||
162 | * The returned int format can not have leading zeros. |
||
163 | * |
||
164 | * @return int |
||
165 | */ |
||
166 | public function getMinute() |
||
170 | |||
171 | /** |
||
172 | * Return second, without leading zeros. |
||
173 | * The returned int format can not have leading zeros. |
||
174 | * |
||
175 | * @return int |
||
176 | */ |
||
177 | public function getSecond() |
||
181 | |||
182 | /** |
||
183 | * Return the difference to Greenwich time (GMT) |
||
184 | * with colon between hours and minutes |
||
185 | * |
||
186 | * @return string |
||
187 | */ |
||
188 | public function getZone() |
||
192 | |||
193 | /** |
||
194 | * Override modify DateTime method to allow personal keywords |
||
195 | * |
||
196 | * @param string $modify A date/time string |
||
197 | * |
||
198 | * @return \BFW\Dates |
||
199 | */ |
||
200 | public function modify($modify) |
||
214 | |||
215 | /** |
||
216 | * Get DateTime equivalent keyword for a personal keyword |
||
217 | * |
||
218 | * @return \stdClass |
||
219 | */ |
||
220 | protected function getNewKeywordsForModify() |
||
247 | |||
248 | /** |
||
249 | * Use personal keyword on modify method |
||
250 | * |
||
251 | * @param string $modify A date/time string |
||
252 | * |
||
253 | * @throws Exception If bad pattern or unknown keyword |
||
254 | */ |
||
255 | protected function modifyWithOthersKeywords($modify) |
||
282 | |||
283 | /** |
||
284 | * Return date's SQL format (postgresql format). |
||
285 | * The return can be an array or a string. |
||
286 | * |
||
287 | * @param boolean $returnArray (default false) True to return an array. |
||
288 | * |
||
289 | * @return string[]|string |
||
290 | */ |
||
291 | public function getSqlFormat($returnArray = false) |
||
302 | |||
303 | /** |
||
304 | * List all timezone existing in current php version |
||
305 | * |
||
306 | * @return string[] |
||
307 | */ |
||
308 | public function lstTimeZone() |
||
312 | |||
313 | /** |
||
314 | * List all continent define in php DateTimeZone. |
||
315 | * |
||
316 | * @return string[] |
||
317 | */ |
||
318 | public function lstTimeZoneContinent() |
||
333 | |||
334 | /** |
||
335 | * List all available country for a continent |
||
336 | * |
||
337 | * @param string $continent The continent for which we want |
||
338 | * the countries list |
||
339 | * |
||
340 | * @return string[] |
||
341 | */ |
||
342 | public function lstTimeZonePays($continent) |
||
355 | |||
356 | /** |
||
357 | * Transform a date to a human readable format |
||
358 | * |
||
359 | * @param boolean $returnDateAndTime (default true) True to return date and |
||
360 | * time concatenated with a space. False to have only date. |
||
361 | * |
||
362 | * @return string |
||
363 | */ |
||
364 | public function humanReadable($returnDateAndTime = true) |
||
394 | |||
395 | /** |
||
396 | * Format date to human readable when the date is now |
||
397 | * |
||
398 | * @param \stdClass &$parsedTxt Texts returned by humanReadable method |
||
399 | * |
||
400 | * @return void |
||
401 | */ |
||
402 | protected function humanDateNow(&$parsedTxt) |
||
407 | |||
408 | /** |
||
409 | * Format date to human readable when date is today |
||
410 | * |
||
411 | * @param \stdClass &$parsedTxt Texts returned by humanReadable method |
||
412 | * @param \DateInterval $diff Interval between now and date to read |
||
413 | * |
||
414 | * @return void |
||
415 | */ |
||
416 | protected function humanDateToday(&$parsedTxt, $diff) |
||
434 | |||
435 | /** |
||
436 | * Format date to human readable when date is yesterday |
||
437 | * |
||
438 | * @param \stdClass &$parsedTxt Texts returned by humanReadable method |
||
439 | * |
||
440 | * @return void |
||
441 | */ |
||
442 | protected function humanDateYesterday(&$parsedTxt) |
||
452 | |||
453 | /** |
||
454 | * Format date to human readable when date is not now, today or yesterday |
||
455 | * |
||
456 | * @param \stdClass &$parsedTxt Texts returned by humanReadable method |
||
457 | * @param \DateTime $current DateTime object for now |
||
458 | * |
||
459 | * @return void |
||
460 | */ |
||
461 | protected function humanDateOther(&$parsedTxt, $current) |
||
480 | } |
||
481 |
If you suppress an error, we recommend checking for the error condition explicitly: