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 day part. |
||
74 | * |
||
75 | * @var string |
||
76 | * |
||
77 | * @since 1.0 |
||
78 | */ |
||
79 | private $day; |
||
80 | |||
81 | /** |
||
82 | * The textual version of the day, e.g. Monday. |
||
83 | * |
||
84 | * @var string |
||
85 | * |
||
86 | * @since 1.0 |
||
87 | */ |
||
88 | private $weekday; |
||
89 | |||
90 | /** |
||
91 | * The validation rule (reg-ex) applied to Date values. |
||
92 | * |
||
93 | * @var string |
||
94 | * |
||
95 | * @since 1.0 |
||
96 | */ |
||
97 | private $validationRule; |
||
98 | |||
99 | /** |
||
100 | * The error message returned for invalid values. |
||
101 | * |
||
102 | * @var string |
||
103 | * |
||
104 | * @since 1.0 |
||
105 | */ |
||
106 | protected $helper = 'Not a valid date value! A date should be in the ISO format YYYY-MM-DD.'; |
||
107 | |||
108 | /** |
||
109 | * Constructor. Leave $date param empty to default to now. |
||
110 | * |
||
111 | * @param string $date Date string in the ISO format YYYY-MM-DD. |
||
112 | * |
||
113 | * @since 1.0 |
||
114 | * |
||
115 | * @throws \Alpha\Exception\IllegalArguementException |
||
116 | */ |
||
117 | public function __construct($date = '') |
||
142 | |||
143 | /** |
||
144 | * Accepts a full date string in ISO YYYY-mm-dd format and populates relevent Date attributes. |
||
145 | * |
||
146 | * @param string $date |
||
147 | * |
||
148 | * @since 1.0 |
||
149 | * |
||
150 | * @throws \Alpha\Exception\IllegalArguementException |
||
151 | */ |
||
152 | public function setValue($date) |
||
156 | |||
157 | /** |
||
158 | * Set the Date attributes to match the three values provided. |
||
159 | * |
||
160 | * @param int $year |
||
161 | * @param int $month |
||
162 | * @param int $day |
||
163 | * |
||
164 | * @throws \Alpha\Exception\IllegalArguementException |
||
165 | * |
||
166 | * @since 1.0 |
||
167 | */ |
||
168 | public function setDateValue($year, $month, $day) |
||
195 | |||
196 | /** |
||
197 | * Get the date value as a string in the format "YYYY-MM-DD". |
||
198 | * |
||
199 | * @return string |
||
200 | * |
||
201 | * @since 1.0 |
||
202 | */ |
||
203 | public function getValue() |
||
207 | |||
208 | /** |
||
209 | * Return the value in UNIX timestamp format. |
||
210 | * |
||
211 | * @return int |
||
212 | * |
||
213 | * @since 1.0 |
||
214 | */ |
||
215 | public function getUnixValue() |
||
219 | |||
220 | /** |
||
221 | * Get the date value as a string in the format "DD/MM/YYYY". |
||
222 | * |
||
223 | * @return string |
||
224 | * |
||
225 | * @since 1.0 |
||
226 | */ |
||
227 | public function getEuroValue() |
||
231 | |||
232 | /** |
||
233 | * Get the date value as a string in the format "MM/DD/YYYY". |
||
234 | * |
||
235 | * @return string |
||
236 | * |
||
237 | * @since 1.0 |
||
238 | */ |
||
239 | public function getUSValue() |
||
243 | |||
244 | /** |
||
245 | * Get the year part. |
||
246 | * |
||
247 | * @return string |
||
248 | * |
||
249 | * @since 1.0 |
||
250 | */ |
||
251 | public function getYear() |
||
255 | |||
256 | /** |
||
257 | * Get the month part. |
||
258 | * |
||
259 | * @return string |
||
260 | * |
||
261 | * @since 1.0 |
||
262 | */ |
||
263 | public function getMonth() |
||
267 | |||
268 | /** |
||
269 | * Get the month part. |
||
270 | * |
||
271 | * @return string |
||
272 | * |
||
273 | * @since 3.1 |
||
274 | */ |
||
275 | public function getMonthName() |
||
279 | |||
280 | /** |
||
281 | * Get the day part. |
||
282 | * |
||
283 | * @return string |
||
284 | * |
||
285 | * @since 1.0 |
||
286 | */ |
||
287 | public function getDay() |
||
291 | |||
292 | /** |
||
293 | * Get the textual weekday part, e.g. Monday. |
||
294 | * |
||
295 | * @return string |
||
296 | * |
||
297 | * @since 1.0 |
||
298 | */ |
||
299 | public function getWeekday() |
||
303 | |||
304 | /** |
||
305 | * Accepts a full date string in YYYY-MM-DD format and populates relevent Date attributes. |
||
306 | * |
||
307 | * @param string $date |
||
308 | * |
||
309 | * @throws \Alpha\Exception\IllegalArguementException |
||
310 | * |
||
311 | * @since 1.0 |
||
312 | */ |
||
313 | public function populateFromString($date) |
||
366 | |||
367 | /** |
||
368 | * Get the validation rule. |
||
369 | * |
||
370 | * @return string |
||
371 | * |
||
372 | * @since 1.0 |
||
373 | */ |
||
374 | public function getRule() |
||
378 | |||
379 | /** |
||
380 | * Set the validation rule. |
||
381 | * |
||
382 | * @param string $rule |
||
383 | * |
||
384 | * @since 1.0 |
||
385 | */ |
||
386 | public function setRule($rule) |
||
390 | |||
391 | /** |
||
392 | * |
||
393 | * Increment the cunrrent date by the amount provided |
||
394 | * |
||
395 | * @param string $amount The amount to increment the date by, e.g. "1 day" |
||
396 | * |
||
397 | * @since 3.1.0 |
||
398 | */ |
||
399 | public function increment($amount) |
||
404 | |||
405 | /** |
||
406 | * |
||
407 | * Get the start date and the end date of the week of the year provided |
||
408 | * |
||
409 | * @param int The number of the week (1-52) |
||
410 | * @param int The year (YYYY) |
||
411 | * |
||
412 | * @return array An array containing the "start" date and "end" date. |
||
413 | * |
||
414 | * @since 3.1.0 |
||
415 | */ |
||
416 | public static function getStartAndEndDate($week, $year) |
||
429 | } |
||
430 |
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.