Complex classes like TimeValue 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 TimeValue, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
15 | class TimeValue extends DataValueObject { |
||
16 | |||
17 | /** |
||
18 | * @deprecated since 0.8, use PRECISION_YEAR1G instead |
||
19 | */ |
||
20 | const PRECISION_Ga = self::PRECISION_YEAR1G; |
||
|
|||
21 | |||
22 | /** |
||
23 | * @deprecated since 0.8, use PRECISION_YEAR100M instead |
||
24 | */ |
||
25 | const PRECISION_100Ma = self::PRECISION_YEAR100M; |
||
26 | |||
27 | /** |
||
28 | * @deprecated since 0.8, use PRECISION_YEAR10M instead |
||
29 | */ |
||
30 | const PRECISION_10Ma = self::PRECISION_YEAR10M; |
||
31 | |||
32 | /** |
||
33 | * @deprecated since 0.8, use PRECISION_YEAR1M instead |
||
34 | */ |
||
35 | const PRECISION_Ma = self::PRECISION_YEAR1M; |
||
36 | |||
37 | /** |
||
38 | * @deprecated since 0.8, use PRECISION_YEAR100K instead |
||
39 | */ |
||
40 | const PRECISION_100ka = self::PRECISION_YEAR100K; |
||
41 | |||
42 | /** |
||
43 | * @deprecated since 0.8, use PRECISION_YEAR10K instead |
||
44 | */ |
||
45 | const PRECISION_10ka = self::PRECISION_YEAR10K; |
||
46 | |||
47 | /** |
||
48 | * @deprecated since 0.8, use PRECISION_YEAR1K instead |
||
49 | */ |
||
50 | const PRECISION_ka = self::PRECISION_YEAR1K; |
||
51 | |||
52 | /** |
||
53 | * @deprecated since 0.8, use PRECISION_YEAR100 instead |
||
54 | */ |
||
55 | const PRECISION_100a = self::PRECISION_YEAR100; |
||
56 | |||
57 | /** |
||
58 | * @deprecated since 0.8, use PRECISION_YEAR10 instead |
||
59 | */ |
||
60 | const PRECISION_10a = self::PRECISION_YEAR10; |
||
61 | |||
62 | /** |
||
63 | * @since 0.8 |
||
64 | */ |
||
65 | const PRECISION_YEAR1G = 0; |
||
66 | |||
67 | /** |
||
68 | * @since 0.8 |
||
69 | */ |
||
70 | const PRECISION_YEAR100M = 1; |
||
71 | |||
72 | /** |
||
73 | * @since 0.8 |
||
74 | */ |
||
75 | const PRECISION_YEAR10M = 2; |
||
76 | |||
77 | /** |
||
78 | * @since 0.8 |
||
79 | */ |
||
80 | const PRECISION_YEAR1M = 3; |
||
81 | |||
82 | /** |
||
83 | * @since 0.8 |
||
84 | */ |
||
85 | const PRECISION_YEAR100K = 4; |
||
86 | |||
87 | /** |
||
88 | * @since 0.8 |
||
89 | */ |
||
90 | const PRECISION_YEAR10K = 5; |
||
91 | |||
92 | /** |
||
93 | * @since 0.8 |
||
94 | */ |
||
95 | const PRECISION_YEAR1K = 6; |
||
96 | |||
97 | /** |
||
98 | * @since 0.8 |
||
99 | */ |
||
100 | const PRECISION_YEAR100 = 7; |
||
101 | |||
102 | /** |
||
103 | * @since 0.8 |
||
104 | */ |
||
105 | const PRECISION_YEAR10 = 8; |
||
106 | |||
107 | const PRECISION_YEAR = 9; |
||
108 | const PRECISION_MONTH = 10; |
||
109 | const PRECISION_DAY = 11; |
||
110 | const PRECISION_HOUR = 12; |
||
111 | const PRECISION_MINUTE = 13; |
||
112 | const PRECISION_SECOND = 14; |
||
113 | |||
114 | /** |
||
115 | * @since 0.7.1 |
||
116 | */ |
||
117 | const CALENDAR_GREGORIAN = 'http://www.wikidata.org/entity/Q1985727'; |
||
118 | |||
119 | /** |
||
120 | * @since 0.7.1 |
||
121 | */ |
||
122 | const CALENDAR_JULIAN = 'http://www.wikidata.org/entity/Q1985786'; |
||
123 | |||
124 | /** |
||
125 | * Timestamp describing a point in time. The actual format depends on the calendar model. |
||
126 | * |
||
127 | * Gregorian and Julian dates use the same YMD ordered format, resembling ISO 8601, e.g. |
||
128 | * +2013-01-01T00:00:00Z. In this format the year is always signed and padded with zero |
||
129 | * characters to have between 4 and 16 digits. Month and day can be zero, indicating they are |
||
130 | * unknown. The timezone suffix Z is meaningless and must be ignored. Use getTimezone() instead. |
||
131 | * |
||
132 | * @see $timezone |
||
133 | * @see $calendarModel |
||
134 | * |
||
135 | * @var string |
||
136 | */ |
||
137 | private $timestamp; |
||
138 | |||
139 | /** |
||
140 | * Unit used for the getBefore() and getAfter() values. Use one of the self::PRECISION_... |
||
141 | * constants. |
||
142 | * |
||
143 | * @var int |
||
144 | */ |
||
145 | private $precision; |
||
146 | |||
147 | /** |
||
148 | * If the date is uncertain, how many units after the given time could it be? |
||
149 | * The unit is given by the precision. |
||
150 | * |
||
151 | * @var int Amount |
||
152 | */ |
||
153 | private $after; |
||
154 | |||
155 | /** |
||
156 | * If the date is uncertain, how many units before the given time could it be? |
||
157 | * The unit is given by the precision. |
||
158 | * |
||
159 | * @var int Amount |
||
160 | */ |
||
161 | private $before; |
||
162 | |||
163 | /** |
||
164 | * Time zone information as an offset from UTC in minutes. |
||
165 | * |
||
166 | * @var int Minutes |
||
167 | */ |
||
168 | private $timezone; |
||
169 | |||
170 | /** |
||
171 | * URI identifying the calendar model. The actual timestamp should be in this calendar model, |
||
172 | * but note that there is nothing this class can do to enforce this convention. |
||
173 | * |
||
174 | * @var string URI |
||
175 | */ |
||
176 | private $calendarModel; |
||
177 | |||
178 | /** |
||
179 | * @since 0.1 |
||
180 | * |
||
181 | * @param string $timestamp Timestamp in a format resembling ISO 8601. |
||
182 | * @param int $timezone Time zone offset from UTC in minutes. |
||
183 | * @param int $before Number of units given by the precision. |
||
184 | * @param int $after Number of units given by the precision. |
||
185 | * @param int $precision One of the self::PRECISION_... constants. |
||
186 | * @param string $calendarModel An URI identifying the calendar model. |
||
187 | * |
||
188 | * @throws IllegalValueException |
||
189 | */ |
||
190 | public function __construct( $timestamp, $timezone, $before, $after, $precision, $calendarModel ) { |
||
227 | |||
228 | /** |
||
229 | * @param string $timestamp |
||
230 | * |
||
231 | * @throws IllegalValueException |
||
232 | * @return string |
||
233 | */ |
||
234 | private function normalizeIsoTimestamp( $timestamp ) { |
||
271 | |||
272 | /** |
||
273 | * @see $timestamp |
||
274 | * |
||
275 | * @since 0.1 |
||
276 | * |
||
277 | * @return string |
||
278 | */ |
||
279 | public function getTime() { |
||
282 | |||
283 | /** |
||
284 | * @see $calendarModel |
||
285 | * |
||
286 | * @since 0.1 |
||
287 | * |
||
288 | * @return string URI |
||
289 | */ |
||
290 | public function getCalendarModel() { |
||
293 | |||
294 | /** |
||
295 | * @see $before |
||
296 | * |
||
297 | * @since 0.1 |
||
298 | * |
||
299 | * @return int Amount |
||
300 | */ |
||
301 | public function getBefore() { |
||
304 | |||
305 | /** |
||
306 | * @see $after |
||
307 | * |
||
308 | * @since 0.1 |
||
309 | * |
||
310 | * @return int Amount |
||
311 | */ |
||
312 | public function getAfter() { |
||
315 | |||
316 | /** |
||
317 | * @see $precision |
||
318 | * |
||
319 | * @since 0.1 |
||
320 | * |
||
321 | * @return int one of the self::PRECISION_... constants |
||
322 | */ |
||
323 | public function getPrecision() { |
||
326 | |||
327 | /** |
||
328 | * @see $timezone |
||
329 | * |
||
330 | * @since 0.1 |
||
331 | * |
||
332 | * @return int Minutes |
||
333 | */ |
||
334 | public function getTimezone() { |
||
337 | |||
338 | /** |
||
339 | * @see DataValue::getType |
||
340 | * |
||
341 | * @since 0.1 |
||
342 | * |
||
343 | * @return string |
||
344 | */ |
||
345 | public static function getType() { |
||
348 | |||
349 | /** |
||
350 | * @see DataValue::getSortKey |
||
351 | * |
||
352 | * @since 0.1 |
||
353 | * |
||
354 | * @return string |
||
355 | */ |
||
356 | public function getSortKey() { |
||
359 | |||
360 | /** |
||
361 | * @see DataValue::getValue |
||
362 | * |
||
363 | * @since 0.1 |
||
364 | * |
||
365 | * @return TimeValue |
||
366 | */ |
||
367 | public function getValue() { |
||
370 | |||
371 | /** |
||
372 | * @see Serializable::serialize |
||
373 | * |
||
374 | * @since 0.1 |
||
375 | * |
||
376 | * @return string |
||
377 | */ |
||
378 | public function serialize() { |
||
381 | |||
382 | /** |
||
383 | * @see Serializable::unserialize |
||
384 | * |
||
385 | * @since 0.1 |
||
386 | * |
||
387 | * @param string $value |
||
388 | * |
||
389 | * @throws IllegalValueException |
||
390 | */ |
||
391 | public function unserialize( $value ) { |
||
395 | |||
396 | /** |
||
397 | * @see DataValue::getArrayValue |
||
398 | * |
||
399 | * @since 0.1 |
||
400 | * |
||
401 | * @return array |
||
402 | */ |
||
403 | public function getArrayValue() { |
||
413 | |||
414 | /** |
||
415 | * Constructs a new instance of the DataValue from the provided data. |
||
416 | * This can round-trip with @see getArrayValue |
||
417 | * |
||
418 | * @since 0.1 |
||
419 | * |
||
420 | * @param mixed $data |
||
421 | * |
||
422 | * @return TimeValue |
||
423 | * @throws IllegalValueException |
||
424 | */ |
||
425 | public static function newFromArray( $data ) { |
||
437 | |||
438 | public function __toString() { |
||
441 | |||
442 | } |
||
443 |