Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
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 |
||
57 | class DateField extends TextField |
||
58 | { |
||
59 | protected $schemaDataType = FormField::SCHEMA_DATA_TYPE_DATE; |
||
60 | |||
61 | /** |
||
62 | * Override locale. If empty will default to current locale |
||
63 | * |
||
64 | * @var string |
||
65 | */ |
||
66 | protected $locale = null; |
||
67 | |||
68 | /** |
||
69 | * Override date format. If empty will default to that used by the current locale. |
||
70 | * |
||
71 | * @var null |
||
72 | */ |
||
73 | protected $dateFormat = null; |
||
74 | |||
75 | /** |
||
76 | * Set if js calendar should popup |
||
77 | * |
||
78 | * @var bool |
||
79 | */ |
||
80 | protected $showCalendar = false; |
||
81 | |||
82 | /** |
||
83 | * Length of this date (full, short, etc). |
||
84 | * |
||
85 | * @see http://php.net/manual/en/class.intldateformatter.php#intl.intldateformatter-constants |
||
86 | * @var int |
||
87 | */ |
||
88 | protected $dateLength = null; |
||
89 | |||
90 | /** |
||
91 | * Override locale for client side. |
||
92 | * |
||
93 | * @var string |
||
94 | */ |
||
95 | protected $clientLocale = null; |
||
96 | |||
97 | /** |
||
98 | * Min date |
||
99 | * |
||
100 | * @var string ISO 8601 date for min date |
||
101 | */ |
||
102 | protected $minDate = null; |
||
103 | |||
104 | /** |
||
105 | * Max date |
||
106 | * |
||
107 | * @var string ISO 860 date for max date |
||
108 | */ |
||
109 | protected $maxDate = null; |
||
110 | |||
111 | /** |
||
112 | * Unparsed value, used exclusively for comparing with internal value |
||
113 | * to detect invalid values. |
||
114 | * |
||
115 | * @var mixed |
||
116 | */ |
||
117 | protected $rawValue = null; |
||
118 | |||
119 | /** |
||
120 | * Use HTML5-based input fields (and force ISO 8601 date formats). |
||
121 | * |
||
122 | * @var bool |
||
123 | */ |
||
124 | protected $html5 = true; |
||
125 | |||
126 | /** |
||
127 | * @return bool |
||
128 | */ |
||
129 | public function getHTML5() |
||
133 | |||
134 | /** |
||
135 | * @param boolean $bool |
||
136 | * @return $this |
||
137 | */ |
||
138 | public function setHTML5($bool) |
||
143 | |||
144 | /** |
||
145 | * Get length of the date format to use. One of: |
||
146 | * |
||
147 | * - IntlDateFormatter::SHORT |
||
148 | * - IntlDateFormatter::MEDIUM |
||
149 | * - IntlDateFormatter::LONG |
||
150 | * - IntlDateFormatter::FULL |
||
151 | * |
||
152 | * @see http://php.net/manual/en/class.intldateformatter.php#intl.intldateformatter-constants |
||
153 | * @return int |
||
154 | */ |
||
155 | public function getDateLength() |
||
162 | |||
163 | /** |
||
164 | * Get length of the date format to use. |
||
165 | * Only applicable with {@link setHTML5(false)}. |
||
166 | * |
||
167 | * @see http://php.net/manual/en/class.intldateformatter.php#intl.intldateformatter-constants |
||
168 | * |
||
169 | * @param int $length |
||
170 | * @return $this |
||
171 | */ |
||
172 | public function setDateLength($length) |
||
177 | |||
178 | /** |
||
179 | * Get date format in CLDR standard format |
||
180 | * |
||
181 | * This can be set explicitly. If not, this will be generated from the current locale |
||
182 | * with the current date length. |
||
183 | * |
||
184 | * @see http://userguide.icu-project.org/formatparse/datetime#TOC-Date-Field-Symbol-Table |
||
185 | */ |
||
186 | public function getDateFormat() |
||
200 | |||
201 | /** |
||
202 | * Set date format in CLDR standard format. |
||
203 | * Only applicable with {@link setHTML5(false)}. |
||
204 | * |
||
205 | * @see http://userguide.icu-project.org/formatparse/datetime#TOC-Date-Field-Symbol-Table |
||
206 | * @param string $format |
||
207 | * @return $this |
||
208 | */ |
||
209 | public function setDateFormat($format) |
||
214 | |||
215 | /** |
||
216 | * Get date formatter with the standard locale / date format |
||
217 | * |
||
218 | * @throws \LogicException |
||
219 | * @return IntlDateFormatter |
||
220 | */ |
||
221 | View Code Duplication | protected function getFrontendFormatter() |
|
259 | |||
260 | /** |
||
261 | * Get a date formatter for the ISO 8601 format |
||
262 | * |
||
263 | * @return IntlDateFormatter |
||
264 | */ |
||
265 | View Code Duplication | protected function getInternalFormatter() |
|
278 | |||
279 | View Code Duplication | public function getAttributes() |
|
293 | |||
294 | View Code Duplication | public function getSchemaDataDefaults() |
|
306 | |||
307 | public function Type() |
||
311 | |||
312 | /** |
||
313 | * Assign value posted from form submission |
||
314 | * |
||
315 | * @param mixed $value |
||
316 | * @param mixed $data |
||
317 | * @return $this |
||
318 | */ |
||
319 | View Code Duplication | public function setSubmittedValue($value, $data = null) |
|
334 | |||
335 | /** |
||
336 | * Assign value based on {@link $datetimeFormat}, which might be localised. |
||
337 | * |
||
338 | * When $html5=true, assign value from ISO 8601 string. |
||
339 | * |
||
340 | * @param mixed $value |
||
341 | * @param mixed $data |
||
342 | * @return $this |
||
343 | */ |
||
344 | View Code Duplication | public function setValue($value, $data = null) |
|
359 | |||
360 | public function Value() |
||
364 | |||
365 | public function performReadonlyTransformation() |
||
372 | |||
373 | /** |
||
374 | * @param Validator $validator |
||
375 | * @return bool |
||
376 | */ |
||
377 | View Code Duplication | public function validate($validator) |
|
449 | |||
450 | /** |
||
451 | * Get locale to use for this field |
||
452 | * |
||
453 | * @return string |
||
454 | */ |
||
455 | public function getLocale() |
||
459 | |||
460 | /** |
||
461 | * Determines the presented/processed format based on locale defaults, |
||
462 | * instead of explicitly setting {@link setDateFormat()}. |
||
463 | * Only applicable with {@link setHTML5(false)}. |
||
464 | * |
||
465 | * @param string $locale |
||
466 | * @return $this |
||
467 | */ |
||
468 | public function setLocale($locale) |
||
473 | |||
474 | public function getSchemaValidation() |
||
480 | |||
481 | /** |
||
482 | * @return string |
||
483 | */ |
||
484 | public function getMinDate() |
||
488 | |||
489 | /** |
||
490 | * @param string $minDate |
||
491 | * @return $this |
||
492 | */ |
||
493 | public function setMinDate($minDate) |
||
498 | |||
499 | /** |
||
500 | * @return string |
||
501 | */ |
||
502 | public function getMaxDate() |
||
506 | |||
507 | /** |
||
508 | * @param string $maxDate |
||
509 | * @return $this |
||
510 | */ |
||
511 | public function setMaxDate($maxDate) |
||
516 | |||
517 | /** |
||
518 | * Convert frontend date to the internal representation (ISO 8601). |
||
519 | * The frontend date is also in ISO 8601 when $html5=true. |
||
520 | * |
||
521 | * @param string $date |
||
522 | * @return string The formatted date, or null if not a valid date |
||
523 | */ |
||
524 | View Code Duplication | protected function frontendToInternal($date) |
|
537 | |||
538 | /** |
||
539 | * Convert the internal date representation (ISO 8601) to a format used by the frontend, |
||
540 | * as defined by {@link $dateFormat}. With $html5=true, the frontend date will also be |
||
541 | * in ISO 8601. |
||
542 | * |
||
543 | * @param string $date |
||
544 | * @return string The formatted date, or null if not a valid date |
||
545 | */ |
||
546 | View Code Duplication | protected function internalToFrontend($date) |
|
560 | |||
561 | /** |
||
562 | * Tidy up the internal date representation (ISO 8601), |
||
563 | * and fall back to strtotime() if there's parsing errors. |
||
564 | * |
||
565 | * @param string $date Date in ISO 8601 or approximate form |
||
566 | * @return string ISO 8601 date, or null if not valid |
||
567 | */ |
||
568 | View Code Duplication | protected function tidyInternal($date) |
|
585 | } |
||
586 |
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..