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 OlsonTimeZone 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 OlsonTimeZone, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
41 | class OlsonTimeZone extends TimeZone |
||
42 | { |
||
43 | /** |
||
44 | * The transitions |
||
45 | * |
||
46 | * @var array |
||
47 | * @since 0.11.0 |
||
48 | */ |
||
49 | protected $transitions; |
||
50 | |||
51 | /** |
||
52 | * The types, 1..255 |
||
53 | * |
||
54 | * @var array |
||
55 | * @since 0.11.0 |
||
56 | */ |
||
57 | protected $types; |
||
58 | |||
59 | /** |
||
60 | * The last year for which the transitions data are to be used |
||
61 | * rather than the finalZone. If there is no finalZone, then this |
||
62 | * is set to INT32_MAX. NOTE: This corresponds to the year _before_ |
||
63 | * the one indicated by finalMillis. |
||
64 | * |
||
65 | * @var int |
||
66 | * @since 0.11.0 |
||
67 | */ |
||
68 | protected $finalYear; |
||
69 | |||
70 | /** |
||
71 | * The millis for the start of the first year for which finalZone |
||
72 | * is to be used, or DBL_MAX if finalZone is 0. NOTE: This is |
||
73 | * 0:00 GMT Jan 1, <finalYear + 1> (not <finalMillis>). |
||
74 | * |
||
75 | * @var float |
||
76 | * @since 0.11.0 |
||
77 | */ |
||
78 | protected $finalMillis; |
||
79 | |||
80 | /** |
||
81 | * A SimpleTimeZone that governs the behavior for years > finalYear. |
||
82 | * If and only if finalYear == INT32_MAX then finalZone == 0. |
||
83 | * |
||
84 | * @var SimpleTimeZone |
||
85 | * @since 0.11.0 |
||
86 | */ |
||
87 | protected $finalZone; // owned, may be NULL |
||
88 | |||
89 | const MAX_INT = 2147483647; |
||
90 | const MAX_DBL = Calendar::MAX_MILLIS; |
||
91 | |||
92 | /** |
||
93 | * Constructor |
||
94 | * |
||
95 | * @see AgaviOlsonTimeZone::constructor() |
||
96 | * @see AgaviOlsonTimeZone::constructorOSA() |
||
97 | * |
||
98 | * @author Dominik del Bondio <[email protected]> |
||
99 | * @author The ICU Project |
||
100 | * @since 0.11.0 |
||
101 | */ |
||
102 | public function __construct() |
||
117 | |||
118 | /** |
||
119 | * Default constructor. Creates a time zone with an empty ID and |
||
120 | * a fixed GMT offset of zero. |
||
121 | * |
||
122 | * @author Dominik del Bondio <[email protected]> |
||
123 | * @author The ICU Project |
||
124 | * @since 0.11.0 |
||
125 | */ |
||
126 | protected function constructor() |
||
134 | |||
135 | /** |
||
136 | * Construct a GMT+0 zone with no transitions. This is done when a |
||
137 | * constructor fails so the resultant object is well-behaved. |
||
138 | * |
||
139 | * @author Dominik del Bondio <[email protected]> |
||
140 | * @author The ICU Project |
||
141 | * @since 0.11.0 |
||
142 | */ |
||
143 | protected function constructEmpty() |
||
150 | |||
151 | /** |
||
152 | * Construct with info from an array. |
||
153 | * |
||
154 | * @param TranslationManager $tm The translation manager. |
||
155 | * @param string $id The id. |
||
156 | * @param array $zoneInfo The zone info data. |
||
157 | * |
||
158 | * @author Dominik del Bondio <[email protected]> |
||
159 | * @author The ICU Project |
||
160 | * @since 0.11.0 |
||
161 | */ |
||
162 | protected function constructorOSA(TranslationManager $tm, $id, array $zoneInfo) |
||
200 | |||
201 | /** |
||
202 | * Returns true if the two TimeZone objects are equal. |
||
203 | * |
||
204 | * @param TimeZone $that The timezone to compare against. |
||
205 | * |
||
206 | * @author Dominik del Bondio <[email protected]> |
||
207 | * @author The ICU Project |
||
208 | * @since 0.11.0 |
||
209 | */ |
||
210 | View Code Duplication | function __is_equal(TimeZone $that) |
|
218 | |||
219 | /** |
||
220 | * TimeZone API. |
||
221 | * |
||
222 | * @see TimeZone::getOffsetIIIIII() |
||
223 | * |
||
224 | * @author Dominik del Bondio <[email protected]> |
||
225 | * @author The ICU Project |
||
226 | * @since 0.11.0 |
||
227 | */ |
||
228 | View Code Duplication | protected function getOffsetIIIIII($era, $year, $month, $dom, $dow, $millis) |
|
236 | |||
237 | /** |
||
238 | * TimeZone API. |
||
239 | * |
||
240 | * @see TimeZone::getOffsetIIIIIII() |
||
241 | * |
||
242 | * @author Dominik del Bondio <[email protected]> |
||
243 | * @author The ICU Project |
||
244 | * @since 0.11.0 |
||
245 | */ |
||
246 | protected function getOffsetIIIIIII($era, $year, $month, $dom, $dow, $millis, $monthLength) |
||
276 | |||
277 | /** |
||
278 | * TimeZone API. |
||
279 | * |
||
280 | * @see TimeZone::getOffsetRef() |
||
281 | * |
||
282 | * @author Dominik del Bondio <[email protected]> |
||
283 | * @author The ICU Project |
||
284 | * @since 0.11.0 |
||
285 | */ |
||
286 | public function getOffsetRef($date, $local, &$rawoff, &$dstoff) |
||
322 | |||
323 | /** |
||
324 | * TimeZone API. |
||
325 | * |
||
326 | * @see TimeZone::setRawOffset() |
||
327 | * |
||
328 | * @author Dominik del Bondio <[email protected]> |
||
329 | * @author The ICU Project |
||
330 | * @since 0.11.0 |
||
331 | */ |
||
332 | public function setRawOffset($offsetMillis) |
||
339 | |||
340 | /** |
||
341 | * TimeZone API. |
||
342 | * |
||
343 | * @see TimeZone::getRawOffset() |
||
344 | * |
||
345 | * @author Dominik del Bondio <[email protected]> |
||
346 | * @author The ICU Project |
||
347 | * @since 0.11.0 |
||
348 | */ |
||
349 | public function getRawOffset() |
||
356 | |||
357 | /** |
||
358 | * Find the smallest i (in 0..transitionCount-1) such that time >= |
||
359 | * transition(i), where transition(i) is either the GMT or the local |
||
360 | * transition time, as specified by `local'. |
||
361 | * |
||
362 | * @param float $time epoch seconds, either GMT or local wall |
||
363 | * @param bool $local if TRUE, `time' is in local wall units, otherwise it |
||
364 | * is GMT |
||
365 | * |
||
366 | * @return int an index i, where 0 <= i < transitionCount, and |
||
367 | * transition(i) <= time < transition(i+1), or i == 0 if |
||
368 | * transitionCount == 0 or time < transition(0). |
||
369 | * |
||
370 | * @author Dominik del Bondio <[email protected]> |
||
371 | * @author The ICU Project |
||
372 | * @since 0.11.0 |
||
373 | */ |
||
374 | protected function findTransition($time, $local) |
||
415 | |||
416 | /** |
||
417 | * TimeZone API. |
||
418 | * |
||
419 | * @see TimeZone::useDaylightTime() |
||
420 | * |
||
421 | * @author Dominik del Bondio <[email protected]> |
||
422 | * @author The ICU Project |
||
423 | * @since 0.11.0 |
||
424 | */ |
||
425 | public function useDaylightTime() |
||
466 | |||
467 | /** |
||
468 | * TimeZone API. |
||
469 | * |
||
470 | * @see TimeZone::getDSTSavings() |
||
471 | * |
||
472 | * @author Dominik del Bondio <[email protected]> |
||
473 | * @author The ICU Project |
||
474 | * @since 0.11.0 |
||
475 | */ |
||
476 | public function getDSTSavings() |
||
483 | |||
484 | /** |
||
485 | * TimeZone API. |
||
486 | * |
||
487 | * @see TimeZone::inDaylightTime() |
||
488 | * |
||
489 | * @author Dominik del Bondio <[email protected]> |
||
490 | * @author The ICU Project |
||
491 | * @since 0.11.0 |
||
492 | */ |
||
493 | public function inDaylightTime($date) |
||
500 | } |
||
501 |
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: