Complex classes like DateValidator 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 DateValidator, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
31 | class DateValidator extends Validator |
||
32 | { |
||
33 | /** |
||
34 | * @var string the date format that the value being validated should follow. |
||
35 | * This can be a date time pattern as described in the [ICU manual](http://userguide.icu-project.org/formatparse/datetime#TOC-Date-Time-Format-Syntax). |
||
36 | * |
||
37 | * Alternatively this can be a string prefixed with `php:` representing a format that can be recognized by the PHP Datetime class. |
||
38 | * Please refer to <http://php.net/manual/en/datetime.createfromformat.php> on supported formats. |
||
39 | * |
||
40 | * If this property is not set, the default value will be obtained from `Yii::$app->formatter->dateFormat`, see [[\yii\i18n\Formatter::dateFormat]] for details. |
||
41 | * |
||
42 | * Here are some example values: |
||
43 | * |
||
44 | * ```php |
||
45 | * 'MM/dd/yyyy' // date in ICU format |
||
46 | * 'php:m/d/Y' // the same date in PHP format |
||
47 | * 'MM/dd/yyyy HH:mm' // not only dates but also times can be validated |
||
48 | * ``` |
||
49 | * |
||
50 | * **Note:** the underlying date parsers being used vary dependent on the format. If you use the ICU format and |
||
51 | * the [PHP intl extension](http://php.net/manual/en/book.intl.php) is installed, the [IntlDateFormatter](http://php.net/manual/en/intldateformatter.parse.php) |
||
52 | * is used to parse the input value. In all other cases the PHP [DateTime](http://php.net/manual/en/datetime.createfromformat.php) class |
||
53 | * is used. The IntlDateFormatter has the advantage that it can parse international dates like `12. Mai 2015` or `12 мая 2014`, while the |
||
54 | * PHP parser is limited to English only. The PHP parser however is more strict about the input format as it will not accept |
||
55 | * `12.05.05` for the format `php:d.m.Y`, but the IntlDateFormatter will accept it for the format `dd.MM.yyyy`. |
||
56 | * If you need to use the IntlDateFormatter you can avoid this problem by specifying a [[min|minimum date]]. |
||
57 | */ |
||
58 | public $format; |
||
59 | /** |
||
60 | * @var string the locale ID that is used to localize the date parsing. |
||
61 | * This is only effective when the [PHP intl extension](http://php.net/manual/en/book.intl.php) is installed. |
||
62 | * If not set, the locale of the [[\yii\base\Application::formatter|formatter]] will be used. |
||
63 | * See also [[\yii\i18n\Formatter::locale]]. |
||
64 | */ |
||
65 | public $locale; |
||
66 | /** |
||
67 | * @var string the timezone to use for parsing date and time values. |
||
68 | * This can be any value that may be passed to [date_default_timezone_set()](http://www.php.net/manual/en/function.date-default-timezone-set.php) |
||
69 | * e.g. `UTC`, `Europe/Berlin` or `America/Chicago`. |
||
70 | * Refer to the [php manual](http://www.php.net/manual/en/timezones.php) for available timezones. |
||
71 | * If this property is not set, [[\yii\base\Application::timeZone]] will be used. |
||
72 | */ |
||
73 | public $timeZone; |
||
74 | /** |
||
75 | * @var string the name of the attribute to receive the parsing result. |
||
76 | * When this property is not null and the validation is successful, the named attribute will |
||
77 | * receive the parsing result. |
||
78 | * |
||
79 | * This can be the same attribute as the one being validated. If this is the case, |
||
80 | * the original value will be overwritten with the timestamp value after successful validation. |
||
81 | * @see timestampAttributeFormat |
||
82 | * @see timestampAttributeTimeZone |
||
83 | */ |
||
84 | public $timestampAttribute; |
||
85 | /** |
||
86 | * @var string the format to use when populating the [[timestampAttribute]]. |
||
87 | * The format can be specified in the same way as for [[format]]. |
||
88 | * |
||
89 | * If not set, [[timestampAttribute]] will receive a UNIX timestamp. |
||
90 | * If [[timestampAttribute]] is not set, this property will be ignored. |
||
91 | * @see format |
||
92 | * @see timestampAttribute |
||
93 | * @since 2.0.4 |
||
94 | */ |
||
95 | public $timestampAttributeFormat; |
||
96 | /** |
||
97 | * @var string the timezone to use when populating the [[timestampAttribute]]. Defaults to `UTC`. |
||
98 | * |
||
99 | * This can be any value that may be passed to [date_default_timezone_set()](http://www.php.net/manual/en/function.date-default-timezone-set.php) |
||
100 | * e.g. `UTC`, `Europe/Berlin` or `America/Chicago`. |
||
101 | * Refer to the [php manual](http://www.php.net/manual/en/timezones.php) for available timezones. |
||
102 | * |
||
103 | * If [[timestampAttributeFormat]] is not set, this property will be ignored. |
||
104 | * @see timestampAttributeFormat |
||
105 | * @since 2.0.4 |
||
106 | */ |
||
107 | public $timestampAttributeTimeZone = 'UTC'; |
||
108 | /** |
||
109 | * @var integer|string upper limit of the date. Defaults to null, meaning no upper limit. |
||
110 | * This can be a unix timestamp or a string representing a date time value. |
||
111 | * If this property is a string, [[format]] will be used to parse it. |
||
112 | * @see tooBig for the customized message used when the date is too big. |
||
113 | * @since 2.0.4 |
||
114 | */ |
||
115 | public $max; |
||
116 | /** |
||
117 | * @var integer|string lower limit of the date. Defaults to null, meaning no lower limit. |
||
118 | * This can be a unix timestamp or a string representing a date time value. |
||
119 | * If this property is a string, [[format]] will be used to parse it. |
||
120 | * @see tooSmall for the customized message used when the date is too small. |
||
121 | * @since 2.0.4 |
||
122 | */ |
||
123 | public $min; |
||
124 | /** |
||
125 | * @var string user-defined error message used when the value is bigger than [[max]]. |
||
126 | * @since 2.0.4 |
||
127 | */ |
||
128 | public $tooBig; |
||
129 | /** |
||
130 | * @var string user-defined error message used when the value is smaller than [[min]]. |
||
131 | * @since 2.0.4 |
||
132 | */ |
||
133 | public $tooSmall; |
||
134 | /** |
||
135 | * @var string user friendly value of upper limit to display in the error message. |
||
136 | * If this property is null, the value of [[max]] will be used (before parsing). |
||
137 | * @since 2.0.4 |
||
138 | */ |
||
139 | public $maxString; |
||
140 | /** |
||
141 | * @var string user friendly value of lower limit to display in the error message. |
||
142 | * If this property is null, the value of [[min]] will be used (before parsing). |
||
143 | * @since 2.0.4 |
||
144 | */ |
||
145 | public $minString; |
||
146 | |||
147 | /** |
||
148 | * @var array map of short format names to IntlDateFormatter constant values. |
||
149 | */ |
||
150 | private $_dateFormats = [ |
||
151 | 'short' => 3, // IntlDateFormatter::SHORT, |
||
152 | 'medium' => 2, // IntlDateFormatter::MEDIUM, |
||
153 | 'long' => 1, // IntlDateFormatter::LONG, |
||
154 | 'full' => 0, // IntlDateFormatter::FULL, |
||
155 | ]; |
||
156 | |||
157 | |||
158 | /** |
||
159 | * @inheritdoc |
||
160 | */ |
||
161 | 145 | public function init() |
|
203 | |||
204 | /** |
||
205 | * @inheritdoc |
||
206 | */ |
||
207 | 136 | public function validateAttribute($model, $attribute) |
|
236 | |||
237 | /** |
||
238 | * @inheritdoc |
||
239 | */ |
||
240 | 8 | protected function validateValue($value) |
|
253 | |||
254 | /** |
||
255 | * Parses date string into UNIX timestamp |
||
256 | * |
||
257 | * @param string $value string representing date |
||
258 | * @return integer|false a UNIX timestamp or `false` on failure. |
||
259 | */ |
||
260 | 144 | protected function parseDateValue($value) |
|
265 | |||
266 | /** |
||
267 | * Parses date string into UNIX timestamp |
||
268 | * |
||
269 | * @param string $value string representing date |
||
270 | * @param string $format expected date format |
||
271 | * @return integer|false a UNIX timestamp or `false` on failure. |
||
272 | */ |
||
273 | 144 | private function parseDateValueFormat($value, $format) |
|
290 | |||
291 | /** |
||
292 | * Parses a date value using the IntlDateFormatter::parse() |
||
293 | * @param string $value string representing date |
||
294 | * @param string $format the expected date format |
||
295 | * @return integer|boolean a UNIX timestamp or `false` on failure. |
||
296 | */ |
||
297 | 69 | private function parseDateValueIntl($value, $format) |
|
319 | |||
320 | /** |
||
321 | * Parses a date value using the DateTime::createFromFormat() |
||
322 | * @param string $value string representing date |
||
323 | * @param string $format the expected date format |
||
324 | * @return integer|boolean a UNIX timestamp or `false` on failure. |
||
325 | */ |
||
326 | 78 | private function parseDateValuePHP($value, $format) |
|
342 | |||
343 | /** |
||
344 | * Formats a timestamp using the specified format |
||
345 | * @param integer $timestamp |
||
346 | * @param string $format |
||
347 | * @return string |
||
348 | */ |
||
349 | 102 | private function formatTimestamp($timestamp, $format) |
|
362 | } |
||
363 |
Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.
For example, imagine you have a variable
$accountId
that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to theid
property of an instance of theAccount
class. This class holds a proper account, so the id value must no longer be false.Either this assignment is in error or a type check should be added for that assignment.