Complex classes like Timestamp 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 Timestamp, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
51 | class Timestamp extends Type implements TypeInterface |
||
52 | { |
||
53 | /** |
||
54 | * The year part. |
||
55 | * |
||
56 | * @var string |
||
57 | * |
||
58 | * @since 1.0 |
||
59 | */ |
||
60 | private $year; |
||
61 | |||
62 | /** |
||
63 | * The month part. |
||
64 | * |
||
65 | * @var string |
||
66 | * |
||
67 | * @since 1.0 |
||
68 | */ |
||
69 | private $month; |
||
70 | |||
71 | /** |
||
72 | * The day part. |
||
73 | * |
||
74 | * @var string |
||
75 | * |
||
76 | * @since 1.0 |
||
77 | */ |
||
78 | private $day; |
||
79 | |||
80 | /** |
||
81 | * The hour part. |
||
82 | * |
||
83 | * @var string |
||
84 | * |
||
85 | * @since 1.0 |
||
86 | */ |
||
87 | private $hour; |
||
88 | |||
89 | /** |
||
90 | * The minute part. |
||
91 | * |
||
92 | * @var string |
||
93 | * |
||
94 | * @since 1.0 |
||
95 | */ |
||
96 | private $minute; |
||
97 | |||
98 | /** |
||
99 | * The second part. |
||
100 | * |
||
101 | * @var string |
||
102 | * |
||
103 | * @since 1.0 |
||
104 | */ |
||
105 | private $second; |
||
106 | |||
107 | /** |
||
108 | * The textual version of the day, e.g. Monday. |
||
109 | * |
||
110 | * @var string |
||
111 | * |
||
112 | * @since 1.0 |
||
113 | */ |
||
114 | private $weekday; |
||
115 | |||
116 | /** |
||
117 | * The validation rule (reg-ex) applied to Date values. |
||
118 | * |
||
119 | * @var string |
||
120 | * |
||
121 | * @since 1.0 |
||
122 | */ |
||
123 | private $validationRule; |
||
124 | |||
125 | /** |
||
126 | * The error message returned for invalid values. |
||
127 | * |
||
128 | * @var string |
||
129 | * |
||
130 | * @since 1.0 |
||
131 | */ |
||
132 | protected $helper = 'Not a valid timestamp value! A timestamp should be in the format YYYY-MM-DD HH:MM:SS.'; |
||
133 | |||
134 | /** |
||
135 | * Constructor. |
||
136 | * |
||
137 | * @since 1.0 |
||
138 | * |
||
139 | * @throws \Alpha\Exception\IllegalArguementException |
||
140 | */ |
||
141 | public function __construct($timestamp = '') |
||
172 | |||
173 | /** |
||
174 | * Accepts a full date/time string in YYYY-mm-dd hh:ii:ss format. |
||
175 | * |
||
176 | * @param string $dateTime |
||
177 | * |
||
178 | * @since 1.0 |
||
179 | */ |
||
180 | public function setValue($dateTime) |
||
184 | |||
185 | /** |
||
186 | * Setter for the timestamp value. |
||
187 | * |
||
188 | * @param int $year |
||
189 | * @param int $month |
||
190 | * @param int $day |
||
191 | * @param int $hour |
||
192 | * @param int $minute |
||
193 | * @param int $second |
||
194 | * |
||
195 | * @since 1.0 |
||
196 | * |
||
197 | * @throws \Alpha\Exception\IllegalArguementException |
||
198 | */ |
||
199 | public function setTimestampValue($year, $month, $day, $hour, $minute, $second) |
||
238 | |||
239 | /** |
||
240 | * Getter for the Timestamp value. |
||
241 | * |
||
242 | * @return string |
||
243 | * |
||
244 | * @since 1.0 |
||
245 | */ |
||
246 | public function getValue() |
||
250 | |||
251 | /** |
||
252 | * Return the value in UNIX timestamp format. |
||
253 | * |
||
254 | * @return int |
||
255 | * |
||
256 | * @since 1.0 |
||
257 | */ |
||
258 | public function getUnixValue() |
||
262 | |||
263 | /** |
||
264 | * Getter for the date part. |
||
265 | * |
||
266 | * @return string |
||
267 | * |
||
268 | * @since 1.0 |
||
269 | */ |
||
270 | public function getDate() |
||
274 | |||
275 | /** |
||
276 | * Get the date value as a string in the format "DD/MM/YYYY". |
||
277 | * |
||
278 | * @return string |
||
279 | * |
||
280 | * @since 1.0 |
||
281 | */ |
||
282 | public function getEuroValue() |
||
286 | |||
287 | /** |
||
288 | * Setter for the date part. |
||
289 | * |
||
290 | * @param int $year |
||
291 | * @param int $month |
||
292 | * @param int $day |
||
293 | * |
||
294 | * @since 1.0 |
||
295 | * |
||
296 | * @throws \Alpha\Exception\IllegalArguementException |
||
297 | */ |
||
298 | public function setDate($year, $month, $day) |
||
325 | |||
326 | /** |
||
327 | * Getter for the time part. |
||
328 | * |
||
329 | * @return string |
||
330 | * |
||
331 | * @since 1.0 |
||
332 | */ |
||
333 | public function getTime() |
||
337 | |||
338 | /** |
||
339 | * Getter for the year part. |
||
340 | * |
||
341 | * @return string |
||
342 | * |
||
343 | * @since 1.0 |
||
344 | */ |
||
345 | public function getYear() |
||
349 | |||
350 | /** |
||
351 | * Getter for the month part. |
||
352 | * |
||
353 | * @return string |
||
354 | * |
||
355 | * @since 1.0 |
||
356 | */ |
||
357 | public function getMonth() |
||
361 | |||
362 | /** |
||
363 | * Getter for the day part. |
||
364 | * |
||
365 | * @return string |
||
366 | * |
||
367 | * @since 1.0 |
||
368 | */ |
||
369 | public function getDay() |
||
373 | |||
374 | /** |
||
375 | * Get the textual weekday part, e.g. Monday. |
||
376 | * |
||
377 | * @return string |
||
378 | * |
||
379 | * @since 1.0 |
||
380 | */ |
||
381 | public function getWeekday() |
||
385 | |||
386 | /** |
||
387 | * Getter for the hour part. |
||
388 | * |
||
389 | * @return string |
||
390 | * |
||
391 | * @since 1.0 |
||
392 | */ |
||
393 | public function getHour() |
||
397 | |||
398 | /** |
||
399 | * Getter for the minute part. |
||
400 | * |
||
401 | * @return string |
||
402 | * |
||
403 | * @since 1.0 |
||
404 | */ |
||
405 | public function getMinute() |
||
409 | |||
410 | /** |
||
411 | * Getter for the second part. |
||
412 | * |
||
413 | * @return string |
||
414 | * |
||
415 | * @since 1.0 |
||
416 | */ |
||
417 | public function getSecond() |
||
421 | |||
422 | /** |
||
423 | * Setter for the time part. |
||
424 | * |
||
425 | * @param int $hour |
||
426 | * @param int $minute |
||
427 | * @param int $second |
||
428 | * |
||
429 | * @since 1.0 |
||
430 | * |
||
431 | * @throws \Alpha\Exception\IllegalArguementException |
||
432 | */ |
||
433 | public function setTime($hour, $minute, $second) |
||
455 | |||
456 | /** |
||
457 | * Accepts a full date/time string in YYYY-mm-dd hh:ii:ss format. |
||
458 | * |
||
459 | * @param string $dateTime |
||
460 | * |
||
461 | * @since 1.0 |
||
462 | * |
||
463 | * @throws \Alpha\Exception\IllegalArguementException |
||
464 | */ |
||
465 | public function populateFromString($dateTime) |
||
567 | |||
568 | /** |
||
569 | * Get the validation rule. |
||
570 | * |
||
571 | * @return string |
||
572 | * |
||
573 | * @since 1.0 |
||
574 | */ |
||
575 | public function getRule() |
||
579 | |||
580 | /** |
||
581 | * Set the validation rule. |
||
582 | * |
||
583 | * @param string $rule |
||
584 | * |
||
585 | * @since 1.0 |
||
586 | */ |
||
587 | public function setRule($rule) |
||
591 | |||
592 | /** |
||
593 | * Get the validation helper text. |
||
594 | * |
||
595 | * @return string |
||
596 | * |
||
597 | * @since 1.0 |
||
598 | */ |
||
599 | public function getHelper() |
||
603 | |||
604 | /** |
||
605 | * Set the validation helper text. |
||
606 | * |
||
607 | * @param string $helper |
||
608 | * |
||
609 | * @since 1.0 |
||
610 | */ |
||
611 | public function setHelper($helper) |
||
615 | |||
616 | /** |
||
617 | * Returns the difference between now and this timestamp value, in a human-readable format, e.g: 3 days ago, 3 days from now. |
||
618 | * |
||
619 | * @return string |
||
620 | * |
||
621 | * @since 1.2.4 |
||
622 | */ |
||
623 | public function getTimeAway() |
||
651 | } |
||
652 |
This check looks for assignments to scalar types that may be of the wrong type.
To ensure the code behaves as expected, it may be a good idea to add an explicit type cast.