Complex classes like TimeField 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 TimeField, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 20 | class TimeField extends TextField |
||
| 21 | { |
||
| 22 | protected $schemaDataType = FormField::SCHEMA_DATA_TYPE_TIME; |
||
| 23 | |||
| 24 | /** |
||
| 25 | * Override locale. If empty will default to current locale |
||
| 26 | * |
||
| 27 | * @var string |
||
| 28 | */ |
||
| 29 | protected $locale = null; |
||
| 30 | |||
| 31 | /** |
||
| 32 | * Override time format. If empty will default to that used by the current locale. |
||
| 33 | * |
||
| 34 | * @var string |
||
| 35 | */ |
||
| 36 | protected $timeFormat = null; |
||
| 37 | |||
| 38 | /** |
||
| 39 | * Length of this date (full, short, etc). |
||
| 40 | * |
||
| 41 | * @see http://php.net/manual/en/class.intldateformatter.php#intl.intldateformatter-constants |
||
| 42 | * @var int |
||
| 43 | */ |
||
| 44 | protected $timeLength = null; |
||
| 45 | |||
| 46 | /** |
||
| 47 | * Unparsed value, used exclusively for comparing with internal value |
||
| 48 | * to detect invalid values. |
||
| 49 | * |
||
| 50 | * @var mixed |
||
| 51 | */ |
||
| 52 | protected $rawValue = null; |
||
| 53 | |||
| 54 | /** |
||
| 55 | * Set custom timezone |
||
| 56 | * |
||
| 57 | * @var string |
||
| 58 | */ |
||
| 59 | protected $timezone = null; |
||
| 60 | |||
| 61 | /** |
||
| 62 | * Use HTML5-based input fields (and force ISO 8601 time formats). |
||
| 63 | * |
||
| 64 | * @var bool |
||
| 65 | */ |
||
| 66 | protected $html5 = true; |
||
| 67 | |||
| 68 | /** |
||
| 69 | * @return bool |
||
| 70 | */ |
||
| 71 | public function getHTML5() |
||
| 75 | |||
| 76 | /** |
||
| 77 | * @param boolean $bool |
||
| 78 | * @return $this |
||
| 79 | */ |
||
| 80 | public function setHTML5($bool) |
||
| 85 | |||
| 86 | /** |
||
| 87 | * Get time format in CLDR standard format |
||
| 88 | * |
||
| 89 | * This can be set explicitly. If not, this will be generated from the current locale |
||
| 90 | * with the current time length. |
||
| 91 | * |
||
| 92 | * @see http://userguide.icu-project.org/formatparse/datetime#TOC-Date-Field-Symbol-Table |
||
| 93 | */ |
||
| 94 | public function getTimeFormat() |
||
| 108 | |||
| 109 | /** |
||
| 110 | * Set time format in CLDR standard format. |
||
| 111 | * Only applicable with {@link setHTML5(false)}. |
||
| 112 | * |
||
| 113 | * @see http://userguide.icu-project.org/formatparse/datetime#TOC-Date-Field-Symbol-Table |
||
| 114 | * @param string $format |
||
| 115 | * @return $this |
||
| 116 | */ |
||
| 117 | public function setTimeFormat($format) |
||
| 122 | |||
| 123 | /** |
||
| 124 | * Get length of the time format to use. One of: |
||
| 125 | * |
||
| 126 | * - IntlDateFormatter::SHORT E.g. '6:31 PM' |
||
| 127 | * - IntlDateFormatter::MEDIUM E.g. '6:30:48 PM' |
||
| 128 | * - IntlDateFormatter::LONG E.g. '6:32:09 PM NZDT' |
||
| 129 | * - IntlDateFormatter::FULL E.g. '6:32:24 PM New Zealand Daylight Time' |
||
| 130 | * |
||
| 131 | * @see http://php.net/manual/en/class.intldateformatter.php#intl.intldateformatter-constants |
||
| 132 | * @return int |
||
| 133 | */ |
||
| 134 | public function getTimeLength() |
||
| 141 | |||
| 142 | /** |
||
| 143 | * Get length of the time format to use. |
||
| 144 | * Only applicable with {@link setHTML5(false)}. |
||
| 145 | * |
||
| 146 | * @see http://php.net/manual/en/class.intldateformatter.php#intl.intldateformatter-constants |
||
| 147 | * |
||
| 148 | * @param int $length |
||
| 149 | * @return $this |
||
| 150 | */ |
||
| 151 | public function setTimeLength($length) |
||
| 156 | |||
| 157 | /** |
||
| 158 | * Get time formatter with the standard locale / date format |
||
| 159 | * |
||
| 160 | * @return IntlDateFormatter |
||
| 161 | */ |
||
| 162 | protected function getFormatter() |
||
| 201 | |||
| 202 | /** |
||
| 203 | * Get a time formatter for the ISO 8601 format |
||
| 204 | * |
||
| 205 | * @return IntlDateFormatter |
||
| 206 | */ |
||
| 207 | protected function getISO8601Formatter() |
||
| 222 | |||
| 223 | public function getAttributes() |
||
| 233 | |||
| 234 | public function Type() |
||
| 238 | |||
| 239 | /** |
||
| 240 | * Assign value posted from form submission |
||
| 241 | * |
||
| 242 | * @param mixed $value |
||
| 243 | * @param mixed $data |
||
| 244 | * @return $this |
||
| 245 | */ |
||
| 246 | public function setSubmittedValue($value, $data = null) |
||
| 255 | |||
| 256 | /** |
||
| 257 | * Set time assigned from database value |
||
| 258 | * |
||
| 259 | * @param mixed $value |
||
| 260 | * @param mixed $data |
||
| 261 | * @return $this |
||
| 262 | */ |
||
| 263 | public function setValue($value, $data = null) |
||
| 278 | |||
| 279 | public function Value() |
||
| 289 | |||
| 290 | /** |
||
| 291 | * Show midnight in current format (adjusts for timezone) |
||
| 292 | * |
||
| 293 | * @return string |
||
| 294 | */ |
||
| 295 | public function getMidnight() |
||
| 303 | |||
| 304 | /** |
||
| 305 | * Validate this field |
||
| 306 | * |
||
| 307 | * @param Validator $validator |
||
| 308 | * @return bool |
||
| 309 | */ |
||
| 310 | public function validate($validator) |
||
| 331 | |||
| 332 | /** |
||
| 333 | * @return string |
||
| 334 | */ |
||
| 335 | public function getLocale() |
||
| 339 | |||
| 340 | /** |
||
| 341 | * Determines the presented/processed format based on locale defaults, |
||
| 342 | * instead of explicitly setting {@link setTimeFormat()}. |
||
| 343 | * Only applicable with {@link setHTML5(false)}. |
||
| 344 | * |
||
| 345 | * @param string $locale |
||
| 346 | * @return $this |
||
| 347 | */ |
||
| 348 | public function setLocale($locale) |
||
| 353 | |||
| 354 | /** |
||
| 355 | * Creates a new readonly field specified below |
||
| 356 | * |
||
| 357 | * @return TimeField_Readonly |
||
| 358 | */ |
||
| 359 | public function performReadonlyTransformation() |
||
| 365 | |||
| 366 | /** |
||
| 367 | * Convert time localised in the current locale to ISO 8601 time |
||
| 368 | * |
||
| 369 | * @param string $time |
||
| 370 | * @return string The formatted time, or null if not a valid time |
||
| 371 | */ |
||
| 372 | public function localisedToISO8601($time) |
||
| 395 | |||
| 396 | /** |
||
| 397 | * Format iso time to localised form |
||
| 398 | * |
||
| 399 | * @param string $time |
||
| 400 | * @return string |
||
| 401 | */ |
||
| 402 | public function iso8601ToLocalised($time) |
||
| 416 | |||
| 417 | |||
| 418 | |||
| 419 | /** |
||
| 420 | * Tidy up iso8601-ish time, or approximation |
||
| 421 | * |
||
| 422 | * @param string $time Time in iso8601 or approximate form |
||
| 423 | * @return string iso8601 time, or null if not valid |
||
| 424 | */ |
||
| 425 | public function tidyISO8601($time) |
||
| 442 | |||
| 443 | /** |
||
| 444 | * @return string |
||
| 445 | */ |
||
| 446 | public function getTimezone() |
||
| 450 | |||
| 451 | /** |
||
| 452 | * @param string $timezone |
||
| 453 | * @return $this |
||
| 454 | */ |
||
| 455 | public function setTimezone($timezone) |
||
| 463 | |||
| 464 | |||
| 465 | /** |
||
| 466 | * Run a callback within a specific timezone |
||
| 467 | * |
||
| 468 | * @param string $timezone |
||
| 469 | * @param callable $callback |
||
| 470 | */ |
||
| 471 | protected function withTimezone($timezone, $callback) |
||
| 486 | } |
||
| 487 |
In PHP, under loose comparison (like
==, or!=, orswitchconditions), values of different types might be equal.For
stringvalues, the empty string''is a special case, in particular the following results might be unexpected: