Complex classes like Date 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 Date, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
52 | class Date extends Type implements TypeInterface |
||
53 | { |
||
54 | /** |
||
55 | * The year part. |
||
56 | * |
||
57 | * @var string |
||
58 | * |
||
59 | * @since 1.0 |
||
60 | */ |
||
61 | private $year; |
||
62 | |||
63 | /** |
||
64 | * The month part. |
||
65 | * |
||
66 | * @var string |
||
67 | * |
||
68 | * @since 1.0 |
||
69 | */ |
||
70 | private $month; |
||
71 | |||
72 | /** |
||
73 | * The textual version of the month, e.g. July. |
||
74 | * |
||
75 | * @var string |
||
76 | * |
||
77 | * @since 3.1 |
||
78 | */ |
||
79 | private $monthName; |
||
80 | |||
81 | /** |
||
82 | * The day part. |
||
83 | * |
||
84 | * @var string |
||
85 | * |
||
86 | * @since 1.0 |
||
87 | */ |
||
88 | private $day; |
||
89 | |||
90 | /** |
||
91 | * The textual version of the day, e.g. Monday. |
||
92 | * |
||
93 | * @var string |
||
94 | * |
||
95 | * @since 1.0 |
||
96 | */ |
||
97 | private $weekday; |
||
98 | |||
99 | /** |
||
100 | * The validation rule (reg-ex) applied to Date values. |
||
101 | * |
||
102 | * @var string |
||
103 | * |
||
104 | * @since 1.0 |
||
105 | */ |
||
106 | private $validationRule; |
||
107 | |||
108 | /** |
||
109 | * The error message returned for invalid values. |
||
110 | * |
||
111 | * @var string |
||
112 | * |
||
113 | * @since 1.0 |
||
114 | */ |
||
115 | protected $helper = 'Not a valid date value! A date should be in the ISO format YYYY-MM-DD.'; |
||
116 | |||
117 | /** |
||
118 | * Constructor. Leave $date param empty to default to now. |
||
119 | * |
||
120 | * @param string $date Date string in the ISO format YYYY-MM-DD. |
||
121 | * |
||
122 | * @since 1.0 |
||
123 | * |
||
124 | * @throws \Alpha\Exception\IllegalArguementException |
||
125 | */ |
||
126 | public function __construct($date = '') |
||
152 | |||
153 | /** |
||
154 | * Accepts a full date string in ISO YYYY-mm-dd format and populates relevent Date attributes. |
||
155 | * |
||
156 | * @param string $date |
||
157 | * |
||
158 | * @since 1.0 |
||
159 | * |
||
160 | * @throws \Alpha\Exception\IllegalArguementException |
||
161 | */ |
||
162 | public function setValue($date) |
||
166 | |||
167 | /** |
||
168 | * Set the Date attributes to match the three values provided. |
||
169 | * |
||
170 | * @param int $year |
||
171 | * @param int $month |
||
172 | * @param int $day |
||
173 | * |
||
174 | * @throws \Alpha\Exception\IllegalArguementException |
||
175 | * |
||
176 | * @since 1.0 |
||
177 | */ |
||
178 | public function setDateValue($year, $month, $day) |
||
206 | |||
207 | /** |
||
208 | * Get the date value as a string in the format "YYYY-MM-DD". |
||
209 | * |
||
210 | * @return string |
||
211 | * |
||
212 | * @since 1.0 |
||
213 | */ |
||
214 | public function getValue() |
||
218 | |||
219 | /** |
||
220 | * Return the value in UNIX timestamp format. |
||
221 | * |
||
222 | * @return int |
||
223 | * |
||
224 | * @since 1.0 |
||
225 | */ |
||
226 | public function getUnixValue() |
||
230 | |||
231 | /** |
||
232 | * Get the date value as a string in the format "DD/MM/YYYY". |
||
233 | * |
||
234 | * @return string |
||
235 | * |
||
236 | * @since 1.0 |
||
237 | */ |
||
238 | public function getEuroValue() |
||
242 | |||
243 | /** |
||
244 | * Get the date value as a string in the format "MM/DD/YYYY". |
||
245 | * |
||
246 | * @return string |
||
247 | * |
||
248 | * @since 1.0 |
||
249 | */ |
||
250 | public function getUSValue() |
||
254 | |||
255 | /** |
||
256 | * Get the year part. |
||
257 | * |
||
258 | * @return string |
||
259 | * |
||
260 | * @since 1.0 |
||
261 | */ |
||
262 | public function getYear() |
||
266 | |||
267 | /** |
||
268 | * Get the month part. |
||
269 | * |
||
270 | * @return string |
||
271 | * |
||
272 | * @since 1.0 |
||
273 | */ |
||
274 | public function getMonth() |
||
278 | |||
279 | /** |
||
280 | * Get the month part. |
||
281 | * |
||
282 | * @return string |
||
283 | * |
||
284 | * @since 3.1 |
||
285 | */ |
||
286 | public function getMonthName() |
||
290 | |||
291 | /** |
||
292 | * Get the day part. |
||
293 | * |
||
294 | * @return string |
||
295 | * |
||
296 | * @since 1.0 |
||
297 | */ |
||
298 | public function getDay() |
||
302 | |||
303 | /** |
||
304 | * Get the textual weekday part, e.g. Monday. |
||
305 | * |
||
306 | * @return string |
||
307 | * |
||
308 | * @since 1.0 |
||
309 | */ |
||
310 | public function getWeekday() |
||
314 | |||
315 | /** |
||
316 | * Accepts a full date string in YYYY-MM-DD format and populates relevent Date attributes. |
||
317 | * |
||
318 | * @param string $date |
||
319 | * |
||
320 | * @throws \Alpha\Exception\IllegalArguementException |
||
321 | * |
||
322 | * @since 1.0 |
||
323 | */ |
||
324 | public function populateFromString($date) |
||
378 | |||
379 | /** |
||
380 | * Get the validation rule. |
||
381 | * |
||
382 | * @return string |
||
383 | * |
||
384 | * @since 1.0 |
||
385 | */ |
||
386 | public function getRule() |
||
390 | |||
391 | /** |
||
392 | * Set the validation rule. |
||
393 | * |
||
394 | * @param string $rule |
||
395 | * |
||
396 | * @since 1.0 |
||
397 | */ |
||
398 | public function setRule($rule) |
||
402 | |||
403 | /** |
||
404 | * |
||
405 | * Increment the cunrrent date by the amount provided |
||
406 | * |
||
407 | * @param string $amount The amount to increment the date by, e.g. "1 day" |
||
408 | * |
||
409 | * @since 3.1.0 |
||
410 | */ |
||
411 | public function increment($amount) |
||
416 | |||
417 | /** |
||
418 | * |
||
419 | * Get the start date and the end date of the week of the year provided |
||
420 | * |
||
421 | * @param int The number of the week (1-52) |
||
422 | * @param int The year (YYYY) |
||
423 | * |
||
424 | * @return array An array containing the "start" date and "end" date. |
||
425 | * |
||
426 | * @since 3.1.0 |
||
427 | */ |
||
428 | public static function getStartAndEndDate($week, $year) |
||
441 | } |
||
442 |
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.