Total Complexity | 40 |
Total Lines | 251 |
Duplicated Lines | 0 % |
Changes | 2 | ||
Bugs | 1 | Features | 0 |
Complex classes like Validate 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.
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 Validate, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
14 | class Validate |
||
15 | { |
||
16 | /** |
||
17 | * Check whether the given value is a type of 'string' or not. |
||
18 | * |
||
19 | * @param $str |
||
20 | * |
||
21 | * @return bool |
||
22 | */ |
||
23 | public static function isValidString($str): bool |
||
24 | { |
||
25 | return is_string($str); |
||
26 | } |
||
27 | |||
28 | /** |
||
29 | * Check whether the string is empty or not. |
||
30 | * |
||
31 | * @param $str |
||
32 | * |
||
33 | * @return bool |
||
34 | */ |
||
35 | public static function isEmptyString($str): bool |
||
36 | { |
||
37 | if (empty($str) || !self::isValidString($str)) { |
||
38 | return true; |
||
39 | } |
||
40 | |||
41 | return strlen(trim($str)) === 0; |
||
42 | } |
||
43 | |||
44 | /** |
||
45 | * Check whether the given string value length b/w given range. |
||
46 | * |
||
47 | * @param $str |
||
48 | * @param int $minLength |
||
49 | * @param int|null $maxLength |
||
50 | * |
||
51 | * @return bool |
||
52 | */ |
||
53 | public static function isStringWithRange($str, int $minLength = 0, ?int $maxLength = null): bool |
||
54 | { |
||
55 | if (!empty($minLength) && strlen($str) < $minLength) { |
||
56 | return false; |
||
57 | } |
||
58 | |||
59 | if (!empty($maxLength) && strlen($str) > $maxLength) { |
||
60 | return false; |
||
61 | } |
||
62 | |||
63 | return true; |
||
64 | } |
||
65 | |||
66 | /** |
||
67 | * validate string without space (by excluding space). |
||
68 | * |
||
69 | * @param $str |
||
70 | * @param int $minLength |
||
71 | * @param int|null $maxLength |
||
72 | * |
||
73 | * @return bool |
||
74 | */ |
||
75 | public static function isStringWithRangeExcludeSpace(string $str, int $minLength = 0, ?int $maxLength = null): bool |
||
76 | { |
||
77 | return self::isStringWithRange(trim($str), $minLength, $maxLength); |
||
78 | } |
||
79 | |||
80 | /** |
||
81 | * Validate int value. |
||
82 | * |
||
83 | * @param $val |
||
84 | * @param bool $allowNegativeValue |
||
85 | * |
||
86 | * @return bool |
||
87 | */ |
||
88 | public static function isValidInt($val, bool $allowNegativeValue = true): bool |
||
89 | { |
||
90 | if (!filter_var($val, FILTER_VALIDATE_INT)) { |
||
91 | return false; |
||
92 | } |
||
93 | |||
94 | return $allowNegativeValue ? true : $val >= 0; |
||
95 | } |
||
96 | |||
97 | /** |
||
98 | * Validate int value with range. |
||
99 | * |
||
100 | * @param $val |
||
101 | * @param int|null $minLength |
||
102 | * @param int|null $maxLength |
||
103 | * |
||
104 | * @return bool |
||
105 | */ |
||
106 | public static function intWithRange($val, ?int $minLength = null, ?int $maxLength = null) |
||
107 | { |
||
108 | if (!self::isValidInt($val)) { |
||
109 | return false; |
||
110 | } |
||
111 | |||
112 | $val = (int) $val; |
||
113 | |||
114 | if ($minLength !== null && $val < $minLength) { |
||
115 | return false; |
||
116 | } |
||
117 | |||
118 | if ($maxLength !== null && $val > $maxLength) { |
||
119 | return false; |
||
120 | } |
||
121 | |||
122 | return true; |
||
123 | } |
||
124 | |||
125 | /** |
||
126 | * Validate float value. |
||
127 | * |
||
128 | * @param $val |
||
129 | * @param bool $allowNegativeValue |
||
130 | * |
||
131 | * @return bool |
||
132 | */ |
||
133 | public static function isValidFloat($val, bool $allowNegativeValue = true): bool |
||
|
|||
134 | { |
||
135 | return filter_var($val, FILTER_VALIDATE_FLOAT); |
||
136 | } |
||
137 | |||
138 | /** |
||
139 | * Validate float value with range. |
||
140 | * |
||
141 | * @param $val |
||
142 | * @param float|null $minValue |
||
143 | * @param float|null $maxValue |
||
144 | * |
||
145 | * @return bool |
||
146 | */ |
||
147 | public static function floatWithRange($val, ?float $minValue = null, ?float $maxValue = null) |
||
148 | { |
||
149 | if (!self::isValidFloat($val)) { |
||
150 | return false; |
||
151 | } |
||
152 | |||
153 | $val = floatval($val); |
||
154 | |||
155 | if ($minValue !== null && $val < $minValue) { |
||
156 | return false; |
||
157 | } |
||
158 | |||
159 | if ($maxValue !== null && $val > $maxValue) { |
||
160 | return false; |
||
161 | } |
||
162 | |||
163 | return true; |
||
164 | } |
||
165 | |||
166 | /** |
||
167 | * Validate datetime. |
||
168 | * |
||
169 | * @param $givenValue |
||
170 | * @param string $format |
||
171 | * |
||
172 | * @return bool |
||
173 | */ |
||
174 | public static function isValidDate($givenValue, string $format = 'Y-m-d H:i:s') |
||
175 | { |
||
176 | if (self::isEmptyString($givenValue)) { |
||
177 | return false; |
||
178 | } |
||
179 | |||
180 | $generatedDateValue = \DateTime::createFromFormat($format, $givenValue); |
||
181 | |||
182 | return $generatedDateValue && $generatedDateValue->format($format) === $givenValue && \DateTime::getLastErrors()['warning_count'] == 0 && \DateTime::getLastErrors()['error_count'] == 0; |
||
183 | } |
||
184 | |||
185 | /** |
||
186 | * Check whether given date is in the past or not. |
||
187 | * |
||
188 | * @param $date |
||
189 | * @param int|null $now |
||
190 | * @param string $format |
||
191 | * |
||
192 | * @return bool |
||
193 | */ |
||
194 | public static function isPastDate($date, ?int $now = 0, string $format = 'Y-m-d'): bool |
||
195 | { |
||
196 | if (!self::isValidDate($date, $format)) { |
||
197 | return false; |
||
198 | } |
||
199 | if (empty($now)) { |
||
200 | $now = time(); |
||
201 | } |
||
202 | |||
203 | $d = \DateTime::createFromFormat($format, $date); |
||
204 | |||
205 | return $d->getTimestamp() < $now; |
||
206 | } |
||
207 | |||
208 | /** |
||
209 | * Check given date is in the future or not. |
||
210 | * |
||
211 | * @param $date |
||
212 | * @param int|null $now |
||
213 | * @param string $format |
||
214 | * |
||
215 | * @return bool |
||
216 | */ |
||
217 | public static function isFutureDate($date, ?int $now = 0, string $format = 'Y-m-d'): bool |
||
218 | { |
||
219 | if (!self::isValidDate($date, $format)) { |
||
220 | return false; |
||
221 | } |
||
222 | if (empty($now)) { |
||
223 | $now = time(); |
||
224 | } |
||
225 | |||
226 | $d = \DateTime::createFromFormat($format, $date); |
||
227 | |||
228 | return $d->getTimestamp() > $now; |
||
229 | } |
||
230 | |||
231 | /** |
||
232 | * Validate email. |
||
233 | * |
||
234 | * @param string $email |
||
235 | * |
||
236 | * @return bool |
||
237 | */ |
||
238 | public static function isValidEmail(string $email): bool |
||
239 | { |
||
240 | return filter_var($email, FILTER_VALIDATE_EMAIL); |
||
241 | } |
||
242 | |||
243 | /** |
||
244 | * Validate url. |
||
245 | * |
||
246 | * @param string $url |
||
247 | * |
||
248 | * @return bool |
||
249 | */ |
||
250 | public static function isValidURL(string $url): bool |
||
251 | { |
||
252 | return filter_var($url, FILTER_VALIDATE_URL); |
||
253 | } |
||
254 | |||
255 | /** |
||
256 | * validate IP address. |
||
257 | * |
||
258 | * @param string $ip |
||
259 | * |
||
260 | * @return bool |
||
261 | */ |
||
262 | public static function isValidIP(string $ip): bool |
||
265 | } |
||
266 | } |
||
267 |
This check looks for parameters that have been defined for a function or method, but which are not used in the method body.