Complex classes like DateField 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 DateField, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 56 | class DateField extends TextField |
||
| 57 | { |
||
| 58 | protected $schemaDataType = FormField::SCHEMA_DATA_TYPE_DATE; |
||
| 59 | |||
| 60 | /** |
||
| 61 | * Override locale. If empty will default to current locale |
||
| 62 | * |
||
| 63 | * @var string |
||
| 64 | */ |
||
| 65 | protected $locale = null; |
||
| 66 | |||
| 67 | /** |
||
| 68 | * Override date format. If empty will default to that used by the current locale. |
||
| 69 | * |
||
| 70 | * @var null |
||
| 71 | */ |
||
| 72 | protected $dateFormat = null; |
||
| 73 | |||
| 74 | /** |
||
| 75 | * Set if js calendar should popup |
||
| 76 | * |
||
| 77 | * @var bool |
||
| 78 | */ |
||
| 79 | protected $showCalendar = false; |
||
| 80 | |||
| 81 | /** |
||
| 82 | * Length of this date (full, short, etc). |
||
| 83 | * |
||
| 84 | * @see http://php.net/manual/en/class.intldateformatter.php#intl.intldateformatter-constants |
||
| 85 | * @var int |
||
| 86 | */ |
||
| 87 | protected $dateLength = null; |
||
| 88 | |||
| 89 | /** |
||
| 90 | * Override locale for client side. |
||
| 91 | * |
||
| 92 | * @var string |
||
| 93 | */ |
||
| 94 | protected $clientLocale = null; |
||
| 95 | |||
| 96 | /** |
||
| 97 | * Min date |
||
| 98 | * |
||
| 99 | * @var string ISO 8601 date for min date |
||
| 100 | */ |
||
| 101 | protected $minDate = null; |
||
| 102 | |||
| 103 | /** |
||
| 104 | * Max date |
||
| 105 | * |
||
| 106 | * @var string ISO 860 date for max date |
||
| 107 | */ |
||
| 108 | protected $maxDate = null; |
||
| 109 | |||
| 110 | /** |
||
| 111 | * Unparsed value, used exclusively for comparing with internal value |
||
| 112 | * to detect invalid values. |
||
| 113 | * |
||
| 114 | * @var mixed |
||
| 115 | */ |
||
| 116 | protected $rawValue = null; |
||
| 117 | |||
| 118 | /** |
||
| 119 | * Use HTML5-based input fields (and force ISO 8601 date formats). |
||
| 120 | * |
||
| 121 | * @var bool |
||
| 122 | */ |
||
| 123 | protected $html5 = true; |
||
| 124 | |||
| 125 | /** |
||
| 126 | * @return bool |
||
| 127 | */ |
||
| 128 | public function getHTML5() |
||
| 132 | |||
| 133 | /** |
||
| 134 | * @param boolean $bool |
||
| 135 | * @return $this |
||
| 136 | */ |
||
| 137 | public function setHTML5($bool) |
||
| 142 | |||
| 143 | /** |
||
| 144 | * Get length of the date format to use. One of: |
||
| 145 | * |
||
| 146 | * - IntlDateFormatter::SHORT |
||
| 147 | * - IntlDateFormatter::MEDIUM |
||
| 148 | * - IntlDateFormatter::LONG |
||
| 149 | * - IntlDateFormatter::FULL |
||
| 150 | * |
||
| 151 | * @see http://php.net/manual/en/class.intldateformatter.php#intl.intldateformatter-constants |
||
| 152 | * @return int |
||
| 153 | */ |
||
| 154 | public function getDateLength() |
||
| 161 | |||
| 162 | /** |
||
| 163 | * Get length of the date format to use. |
||
| 164 | * Only applicable with {@link setHTML5(false)}. |
||
| 165 | * |
||
| 166 | * @see http://php.net/manual/en/class.intldateformatter.php#intl.intldateformatter-constants |
||
| 167 | * |
||
| 168 | * @param int $length |
||
| 169 | * @return $this |
||
| 170 | */ |
||
| 171 | public function setDateLength($length) |
||
| 176 | |||
| 177 | /** |
||
| 178 | * Get date format in CLDR standard format |
||
| 179 | * |
||
| 180 | * This can be set explicitly. If not, this will be generated from the current locale |
||
| 181 | * with the current date length. |
||
| 182 | * |
||
| 183 | * @see http://userguide.icu-project.org/formatparse/datetime#TOC-Date-Field-Symbol-Table |
||
| 184 | */ |
||
| 185 | public function getDateFormat() |
||
| 199 | |||
| 200 | /** |
||
| 201 | * Set date format in CLDR standard format. |
||
| 202 | * Only applicable with {@link setHTML5(false)}. |
||
| 203 | * |
||
| 204 | * @see http://userguide.icu-project.org/formatparse/datetime#TOC-Date-Field-Symbol-Table |
||
| 205 | * @param string $format |
||
| 206 | * @return $this |
||
| 207 | */ |
||
| 208 | public function setDateFormat($format) |
||
| 213 | |||
| 214 | /** |
||
| 215 | * Get date formatter with the standard locale / date format |
||
| 216 | * |
||
| 217 | * @throws \LogicException |
||
| 218 | * @return IntlDateFormatter |
||
| 219 | */ |
||
| 220 | protected function getFormatter() |
||
| 258 | |||
| 259 | /** |
||
| 260 | * Get a date formatter for the ISO 8601 format |
||
| 261 | * |
||
| 262 | * @return IntlDateFormatter |
||
| 263 | */ |
||
| 264 | protected function getISO8601Formatter() |
||
| 277 | |||
| 278 | public function getAttributes() |
||
| 292 | |||
| 293 | public function Type() |
||
| 297 | |||
| 298 | /** |
||
| 299 | * Assign value posted from form submission |
||
| 300 | * |
||
| 301 | * @param mixed $value |
||
| 302 | * @param mixed $data |
||
| 303 | * @return $this |
||
| 304 | */ |
||
| 305 | public function setSubmittedValue($value, $data = null) |
||
| 320 | |||
| 321 | public function setValue($value, $data = null) |
||
| 340 | |||
| 341 | public function Value() |
||
| 345 | |||
| 346 | public function performReadonlyTransformation() |
||
| 353 | |||
| 354 | /** |
||
| 355 | * @param Validator $validator |
||
| 356 | * @return bool |
||
| 357 | */ |
||
| 358 | public function validate($validator) |
||
| 414 | |||
| 415 | /** |
||
| 416 | * Get locale to use for this field |
||
| 417 | * |
||
| 418 | * @return string |
||
| 419 | */ |
||
| 420 | public function getLocale() |
||
| 424 | |||
| 425 | /** |
||
| 426 | * Determines the presented/processed format based on locale defaults, |
||
| 427 | * instead of explicitly setting {@link setDateFormat()}. |
||
| 428 | * Only applicable with {@link setHTML5(false)}. |
||
| 429 | * |
||
| 430 | * @param string $locale |
||
| 431 | * @return $this |
||
| 432 | */ |
||
| 433 | public function setLocale($locale) |
||
| 438 | |||
| 439 | public function getSchemaValidation() |
||
| 445 | |||
| 446 | /** |
||
| 447 | * @return string |
||
| 448 | */ |
||
| 449 | public function getMinDate() |
||
| 453 | |||
| 454 | /** |
||
| 455 | * @param string $minDate |
||
| 456 | * @return $this |
||
| 457 | */ |
||
| 458 | public function setMinDate($minDate) |
||
| 463 | |||
| 464 | /** |
||
| 465 | * @return string |
||
| 466 | */ |
||
| 467 | public function getMaxDate() |
||
| 471 | |||
| 472 | /** |
||
| 473 | * @param string $maxDate |
||
| 474 | * @return $this |
||
| 475 | */ |
||
| 476 | public function setMaxDate($maxDate) |
||
| 481 | |||
| 482 | /** |
||
| 483 | * Convert date localised in the current locale to ISO 8601 date |
||
| 484 | * |
||
| 485 | * @param string $date |
||
| 486 | * @return string The formatted date, or null if not a valid date |
||
| 487 | */ |
||
| 488 | public function localisedToISO8601($date) |
||
| 501 | |||
| 502 | /** |
||
| 503 | * Convert an ISO 8601 localised date into the format specified by the |
||
| 504 | * current date format. |
||
| 505 | * |
||
| 506 | * @param string $date |
||
| 507 | * @return string The formatted date, or null if not a valid date |
||
| 508 | */ |
||
| 509 | public function iso8601ToLocalised($date) |
||
| 523 | |||
| 524 | /** |
||
| 525 | * Tidy up iso8601-ish date, or approximation |
||
| 526 | * |
||
| 527 | * @param string $date Date in iso8601 or approximate form |
||
| 528 | * @return string iso8601 date, or null if not valid |
||
| 529 | */ |
||
| 530 | public function tidyISO8601($date) |
||
| 547 | } |
||
| 548 |
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..