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 day part. |
||
| 270 | * |
||
| 271 | * @return string |
||
| 272 | * |
||
| 273 | * @since 1.0 |
||
| 274 | */ |
||
| 275 | public function getDay() |
||
| 279 | |||
| 280 | /** |
||
| 281 | * Get the textual weekday part, e.g. Monday. |
||
| 282 | * |
||
| 283 | * @return string |
||
| 284 | * |
||
| 285 | * @since 1.0 |
||
| 286 | */ |
||
| 287 | public function getWeekday() |
||
| 291 | |||
| 292 | /** |
||
| 293 | * Accepts a full date string in YYYY-MM-DD format and populates relevent Date attributes. |
||
| 294 | * |
||
| 295 | * @param string $date |
||
| 296 | * |
||
| 297 | * @throws \Alpha\Exception\IllegalArguementException |
||
| 298 | * |
||
| 299 | * @since 1.0 |
||
| 300 | */ |
||
| 301 | public function populateFromString($date) |
||
| 354 | |||
| 355 | /** |
||
| 356 | * Get the validation rule. |
||
| 357 | * |
||
| 358 | * @return string |
||
| 359 | * |
||
| 360 | * @since 1.0 |
||
| 361 | */ |
||
| 362 | public function getRule() |
||
| 366 | |||
| 367 | /** |
||
| 368 | * Set the validation rule. |
||
| 369 | * |
||
| 370 | * @param string $rule |
||
| 371 | * |
||
| 372 | * @since 1.0 |
||
| 373 | */ |
||
| 374 | public function setRule($rule) |
||
| 378 | |||
| 379 | /** |
||
| 380 | * |
||
| 381 | * Increment the cunrrent date by the amount provided |
||
| 382 | * |
||
| 383 | * @param string $amount The amount to increment the date by, e.g. "1 day" |
||
| 384 | * |
||
| 385 | * @since 3.1.0 |
||
| 386 | */ |
||
| 387 | public function increment($amount) |
||
| 392 | |||
| 393 | /** |
||
| 394 | * |
||
| 395 | * Get the start date and the end date of the week of the year provided |
||
| 396 | * |
||
| 397 | * @param int The number of the week (1-52) |
||
| 398 | * @param int The year (YYYY) |
||
| 399 | * |
||
| 400 | * @return array An array containing the "start" date and "end" date. |
||
| 401 | * |
||
| 402 | * @since 3.1.0 |
||
| 403 | */ |
||
| 404 | public static function getStartAndEndDate($week, $year) |
||
| 417 | } |
||
| 418 |
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.