Complex classes like TimeZone 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 TimeZone, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
43 | abstract class TimeZone |
||
44 | { |
||
45 | /** |
||
46 | * The translation manager instance. |
||
47 | * |
||
48 | * @var TranslationManager |
||
49 | */ |
||
50 | protected $translationManager = null; |
||
51 | |||
52 | /** |
||
53 | * The id of this time zone. |
||
54 | * |
||
55 | * @var string |
||
56 | */ |
||
57 | protected $id; |
||
58 | |||
59 | /** |
||
60 | * @var string The "resolved" id. This means if the original id pointed |
||
61 | * to a link timezone this will contain the id of the |
||
62 | * timezone the link resolved to. |
||
63 | */ |
||
64 | protected $resolvedId = null; |
||
65 | |||
66 | /** |
||
67 | * Returns the translation manager for this TimeZone. |
||
68 | * |
||
69 | * @return TranslationManager The translation manager. |
||
70 | * |
||
71 | * @author Dominik del Bondio <[email protected]> |
||
72 | * @since 0.11.0 |
||
73 | */ |
||
74 | public function getTranslationManager() |
||
78 | |||
79 | /** |
||
80 | * The GMT time zone has a raw offset of zero and does not use daylight |
||
81 | * savings time. This is a commonly used time zone. |
||
82 | * |
||
83 | * @param TranslationManager $tm The translation manager |
||
84 | * |
||
85 | * @return TimeZone The GMT time zone. |
||
86 | * |
||
87 | * @author Dominik del Bondio <[email protected]> |
||
88 | * @author The ICU Project |
||
89 | * @since 0.11.0 |
||
90 | */ |
||
91 | public static function getGMT(TranslationManager $tm) |
||
95 | |||
96 | /** |
||
97 | * Overloaded. |
||
98 | * |
||
99 | * @see TimeZone::getOffsetIIIIII() |
||
100 | * @see TimeZone::getOffsetIIIIIII() |
||
101 | * |
||
102 | * @author Dominik del Bondio <[email protected]> |
||
103 | * @since 0.11.0 |
||
104 | */ |
||
105 | public function getOffset() |
||
119 | |||
120 | /** |
||
121 | * Returns the time zone raw and GMT offset for the given moment |
||
122 | * in time. Upon return, local-millis = GMT-millis + rawOffset + |
||
123 | * dstOffset. All computations are performed in the proleptic |
||
124 | * Gregorian calendar. The default implementation in the TimeZone |
||
125 | * class delegates to the 8-argument getOffset(). |
||
126 | * |
||
127 | * @param float $date Moment in time for which to return offsets, in units of |
||
128 | * milliseconds from January 1, 1970 0:00 GMT, either GMT |
||
129 | * time or local wall time, depending on `local'. |
||
130 | * @param bool $local If true, `date' is local wall time; otherwise it |
||
131 | * is in GMT time. |
||
132 | * @param int $rawOffset Output parameter to receive the raw offset, that is, the |
||
133 | * offset not including DST adjustments |
||
134 | * @param int $dstOffset Output parameter to receive the DST offset, that is, the |
||
135 | * offset to be added to `rawOffset' to obtain the total |
||
136 | * offset between local and GMT time. If DST is not in |
||
137 | * effect, this value is zero; otherwise it is a positive |
||
138 | * value, typically one hour. |
||
139 | * |
||
140 | * @author Dominik del Bondio <[email protected]> |
||
141 | * @author The ICU Project |
||
142 | * @since 0.11.0 |
||
143 | */ |
||
144 | public function getOffsetRef($date, $local, &$rawOffset, &$dstOffset) |
||
176 | |||
177 | /** |
||
178 | * Sets the TimeZone's raw GMT offset (i.e., the number of milliseconds to |
||
179 | * add to GMT to get local time, before taking daylight savings time into |
||
180 | * account). |
||
181 | * |
||
182 | * @param int $offsetMillis The new raw GMT offset for this time zone. |
||
183 | * |
||
184 | * @author Dominik del Bondio <[email protected]> |
||
185 | * @author The ICU Project |
||
186 | * @since 0.11.0 |
||
187 | */ |
||
188 | abstract public function setRawOffset($offsetMillis); |
||
189 | |||
190 | /** |
||
191 | * Returns the TimeZone's raw GMT offset (i.e., the number of milliseconds to |
||
192 | * add to GMT to get local time, before taking daylight savings time into |
||
193 | * account). |
||
194 | * |
||
195 | * @return int The TimeZone's raw GMT offset. |
||
196 | * |
||
197 | * @author Dominik del Bondio <[email protected]> |
||
198 | * @author The ICU Project |
||
199 | * @since 0.11.0 |
||
200 | */ |
||
201 | abstract public function getRawOffset(); |
||
202 | |||
203 | /** |
||
204 | * Returns the TimeZone's ID. |
||
205 | * |
||
206 | * @return string This TimeZone's ID. |
||
207 | * |
||
208 | * @author Dominik del Bondio <[email protected]> |
||
209 | * @author The ICU Project |
||
210 | * @since 0.11.0 |
||
211 | */ |
||
212 | public function getId() |
||
216 | |||
217 | /** |
||
218 | * Sets the TimeZone's ID to the specified value. This doesn't affect any |
||
219 | * other fields (for example, if you say |
||
220 | * <code> |
||
221 | * $foo = $tm->createTimeZone('America/New_York'); |
||
222 | * $foo->setId('America/Los_Angeles'); |
||
223 | * </code> |
||
224 | * the time zone's GMT offset and daylight-savings rules don't change to those |
||
225 | * for Los Angeles. They're still those for New York. Only the ID has |
||
226 | * changed.) |
||
227 | * |
||
228 | * @param string $id The new timezone ID. |
||
229 | * |
||
230 | * @author Dominik del Bondio <[email protected]> |
||
231 | * @author The ICU Project |
||
232 | * @since 0.11.0 |
||
233 | */ |
||
234 | public function setId($id) |
||
238 | |||
239 | /** |
||
240 | * Returns the resolved TimeZone's ID. |
||
241 | * |
||
242 | * @return string This TimeZone's ID. |
||
243 | * |
||
244 | * @author Dominik del Bondio <[email protected]> |
||
245 | * @author The ICU Project |
||
246 | * @since 0.11.0 |
||
247 | */ |
||
248 | public function getResolvedId() |
||
256 | |||
257 | /** |
||
258 | * Sets the resolved TimeZone's ID. |
||
259 | * |
||
260 | * @param string $id The resolved timezone ID. |
||
261 | * |
||
262 | * @author Dominik del Bondio <[email protected]> |
||
263 | * @author The ICU Project |
||
264 | * @since 0.11.0 |
||
265 | */ |
||
266 | public function setResolvedId($id) |
||
270 | |||
271 | /** |
||
272 | * Enum for use with getDisplayName |
||
273 | * @stable ICU 2.4 |
||
274 | */ |
||
275 | /** |
||
276 | * Selector for short display name |
||
277 | * @stable ICU 2.4 |
||
278 | */ |
||
279 | const SHORT = 1; |
||
280 | /** |
||
281 | * Selector for long display name |
||
282 | * @stable ICU 2.4 |
||
283 | */ |
||
284 | const LONG = 2; |
||
285 | |||
286 | /** |
||
287 | * Returns a name of this time zone suitable for presentation to the user |
||
288 | * in the specified locale. |
||
289 | * If the display name is not available for the locale, |
||
290 | * then this method returns a string in the format |
||
291 | * <code>GMT[+-]hh:mm</code>. |
||
292 | * |
||
293 | * @param bool $daylight If true, return the daylight savings name. |
||
294 | * @param int $style Either <code>self::LONG</code> or <code>self::SHORT</code> |
||
295 | * @param Locale $locale The locale in which to supply the display name. |
||
296 | * |
||
297 | * @return string the human-readable name of this time zone in the given |
||
298 | * locale or in the default locale if the given locale is |
||
299 | * not recognized. |
||
300 | * |
||
301 | * @author Dominik del Bondio <[email protected]> |
||
302 | * @author The ICU Project |
||
303 | * @since 0.11.0 |
||
304 | */ |
||
305 | public function getDisplayName($daylight = null, $style = null, Locale $locale = null) |
||
345 | |||
346 | /** |
||
347 | * Returns the GMT+-hh:mm representation of this timezone. |
||
348 | * |
||
349 | * @param bool $daylight Whether dst is active. |
||
350 | * @param string $separator The hour/minute and minute/second separator. |
||
351 | * @param string $prefix A prefix to be added in front of the string. |
||
352 | * |
||
353 | * @return string The formatted representation. |
||
354 | * |
||
355 | * @author Dominik del Bondio <[email protected]> |
||
356 | * @since 1.0.2 |
||
357 | */ |
||
358 | public function formatOffset($daylight, $separator = ':', $prefix = 'GMT') |
||
378 | |||
379 | /** |
||
380 | * Queries if this time zone uses daylight savings time. |
||
381 | * |
||
382 | * @return bool If this time zone uses daylight savings time, |
||
383 | * false, otherwise. |
||
384 | * |
||
385 | * @author Dominik del Bondio <[email protected]> |
||
386 | * @author The ICU Project |
||
387 | * @since 0.11.0 |
||
388 | */ |
||
389 | abstract public function useDaylightTime(); |
||
390 | |||
391 | /** |
||
392 | * Returns true if this zone has the same rule and offset as another zone. |
||
393 | * That is, if this zone differs only in ID, if at all. |
||
394 | * |
||
395 | * @param TimeZone $other The object to be compared with |
||
396 | * |
||
397 | * @return bool True if the given zone is the same as this one, |
||
398 | * with the possible exception of the ID |
||
399 | * |
||
400 | * @author Dominik del Bondio <[email protected]> |
||
401 | * @author The ICU Project |
||
402 | * @since 0.11.0 |
||
403 | */ |
||
404 | public function hasSameRules(TimeZone $other) |
||
409 | |||
410 | /** |
||
411 | * Returns the amount of time to be added to local standard time |
||
412 | * to get local wall clock time. |
||
413 | * <p> |
||
414 | * The default implementation always returns 3600000 milliseconds |
||
415 | * (i.e., one hour) if this time zone observes Daylight Saving |
||
416 | * Time. Otherwise, 0 (zero) is returned. |
||
417 | * <p> |
||
418 | * If an underlying TimeZone implementation subclass supports |
||
419 | * historical Daylight Saving Time changes, this method returns |
||
420 | * the known latest daylight saving value. |
||
421 | * |
||
422 | * @return int The amount of saving time in milliseconds |
||
423 | * |
||
424 | * @author Dominik del Bondio <[email protected]> |
||
425 | * @author The ICU Project |
||
426 | * @since 0.11.0 |
||
427 | */ |
||
428 | public function getDSTSavings() |
||
435 | |||
436 | /** |
||
437 | * Construct a timezone with a given ID. |
||
438 | * |
||
439 | * @param TranslationManager $tm The translation Manager |
||
440 | * @param string $id A system time zone ID |
||
441 | * |
||
442 | * @author Dominik del Bondio <[email protected]> |
||
443 | * @author The ICU Project |
||
444 | * @since 0.11.0 |
||
445 | */ |
||
446 | protected function __construct(TranslationManager $tm, $id = '') |
||
451 | |||
452 | /** |
||
453 | * Returns the TimeZone's adjusted GMT offset (i.e., the number of |
||
454 | * milliseconds to add to GMT to get local time in this time zone, taking |
||
455 | * daylight savings time into account) as of a particular reference date. |
||
456 | * The reference date is used to determine whether daylight savings time is |
||
457 | * in effect and needs to be figured into the offset that is returned (in |
||
458 | * other words, what is the adjusted GMT offset in this time zone at this |
||
459 | * particular date and time?). For the time zones produced by |
||
460 | * createTimeZone(), the reference data is specified according to the |
||
461 | * Gregorian calendar, and the date and time fields are local standard time. |
||
462 | * |
||
463 | * <p>Note: Don't call this method. Instead, call the getOffsetRef() which |
||
464 | * returns both the raw and the DST offset for a given time. This method |
||
465 | * is retained only for backward compatibility. |
||
466 | * |
||
467 | * @param int $era The reference date's era |
||
468 | * @param int $year The reference date's year |
||
469 | * @param int $month The reference date's month (0-based; 0 is January) |
||
470 | * @param int $day The reference date's day-in-month (1-based) |
||
471 | * @param int $dayOfWeek The reference date's day-of-week (1-based; 1 is Sunday) |
||
472 | * @param int $millis The reference date's milliseconds in day, local standard |
||
473 | * time |
||
474 | * |
||
475 | * @return int The offset in milliseconds to add to GMT to get local time. |
||
476 | * |
||
477 | * @author Dominik del Bondio <[email protected]> |
||
478 | * @author The ICU Project |
||
479 | * @since 0.11.0 |
||
480 | */ |
||
481 | abstract protected function getOffsetIIIIII($era, $year, $month, $day, $dayOfWeek, $millis); |
||
482 | |||
483 | /** |
||
484 | * Gets the time zone offset, for current date, modified in case of |
||
485 | * daylight savings. This is the offset to add *to* UTC to get local time. |
||
486 | * |
||
487 | * <p>Note: Don't call this method. Instead, call the getOffsetRef(), which |
||
488 | * returns both the raw and the DST offset for a given time. This method |
||
489 | * is retained only for backward compatibility. |
||
490 | * |
||
491 | * @param int $era The era of the given date. |
||
492 | * @param int $year The year in the given date. |
||
493 | * @param int $month The month in the given date. |
||
494 | * Month is 0-based. e.g., 0 for January. |
||
495 | * @param int $day The day-in-month of the given date. |
||
496 | * @param int $dayOfWeek The day-of-week of the given date. |
||
497 | * @param int $milliseconds The millis in day in <em>standard</em> local time. |
||
498 | * @param int $monthLength The length of the given month in days. |
||
499 | * |
||
500 | * @return int The offset to add *to* GMT to get local time. |
||
501 | * |
||
502 | * @author Dominik del Bondio <[email protected]> |
||
503 | * @author The ICU Project |
||
504 | * @since 0.11.0 |
||
505 | */ |
||
506 | abstract protected function getOffsetIIIIIII($era, $year, $month, $day, $dayOfWeek, $milliseconds, $monthLength); |
||
507 | |||
508 | /** |
||
509 | * Parse a custom time zone identifier and return a corresponding zone. |
||
510 | * |
||
511 | * @param TranslationManager $tm The translation manager |
||
512 | * @param string $id A string of the form GMT[+-]hh:mm, GMT[+-]hhmm, or |
||
513 | * GMT[+-]hh. |
||
514 | * |
||
515 | * @return TimeZone A newly created SimpleTimeZone with the |
||
516 | * given offset and no Daylight Savings Time, or |
||
517 | * null if the id cannot be parsed. |
||
518 | * |
||
519 | * @author Dominik del Bondio <[email protected]> |
||
520 | * @author The ICU Project |
||
521 | * @since 0.11.0 |
||
522 | */ |
||
523 | public static function createCustomTimeZone(TranslationManager $tm, $id) |
||
585 | |||
586 | /** |
||
587 | * Returns true if the two TimeZones are equal. (The TimeZone version |
||
588 | * only compares IDs, but subclasses are expected to also compare the fields |
||
589 | * they add.) |
||
590 | * |
||
591 | * @param TimeZone $that The object to be compared with. |
||
592 | * |
||
593 | * @return bool True if the given TimeZone is equal to this |
||
594 | * TimeZone; false otherwise. |
||
595 | * |
||
596 | * @author Dominik del Bondio <[email protected]> |
||
597 | * @author The ICU Project |
||
598 | * @since 0.11.0 |
||
599 | */ |
||
600 | public function __is_equal(TimeZone $that) |
||
604 | |||
605 | /** |
||
606 | * Returns true if the two TimeZones are NOT equal; that is, if operator==() |
||
607 | * returns false. |
||
608 | * |
||
609 | * @param TimeZone $that The object to be compared with. |
||
610 | * |
||
611 | * @return bool True if the given TimeZone is not equal to this |
||
612 | * TimeZone; false otherwise. |
||
613 | * |
||
614 | * @author Dominik del Bondio <[email protected]> |
||
615 | * @author The ICU Project |
||
616 | * @since 0.11.0 |
||
617 | */ |
||
618 | public function __is_not_equal(TimeZone $that) |
||
622 | |||
623 | /** |
||
624 | * Queries if the given date is in daylight savings time in |
||
625 | * this time zone. |
||
626 | * This method is wasteful since it creates a new GregorianCalendar and |
||
627 | * deletes it each time it is called. |
||
628 | * |
||
629 | * @param float $date The given time |
||
630 | * |
||
631 | * @return bool True if the given date is in daylight savings time, |
||
632 | * false, otherwise. |
||
633 | * |
||
634 | * @author Dominik del Bondio <[email protected]> |
||
635 | * @author The ICU Project |
||
636 | * @since 0.11.0 |
||
637 | */ |
||
638 | public function inDaylightTime($date) |
||
644 | } |
||
645 |
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVar
assignment in line 1 and the$higher
assignment in line 2 are dead. The first because$myVar
is never used and the second because$higher
is always overwritten for every possible time line.