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 Validator 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 Validator, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
18 | class Validator { |
||
19 | |||
20 | /** |
||
21 | * Validate field value by regular expression. |
||
22 | * |
||
23 | * @param string $fieldValue Value of the field. |
||
24 | * @param string $regexp Regular expression. |
||
25 | * |
||
26 | * @return bool |
||
27 | */ |
||
28 | public static function validateRegexp($fieldValue, $regexp) { |
||
31 | |||
32 | /** |
||
33 | * Validate if field value is not empty. |
||
34 | * |
||
35 | * @param string $fieldName Name of the field. |
||
36 | * |
||
37 | * @return bool |
||
38 | */ |
||
39 | public static function validateNotEmpty($fieldName) { |
||
40 | $fieldValue = Request::getFieldValue($fieldName); |
||
41 | if (is_string($fieldValue)) { |
||
42 | $fieldValue = trim($fieldValue); |
||
43 | } |
||
44 | if (empty($fieldValue)) { |
||
45 | Errors::saveErrorFor($fieldName, \__ERRORS::FIELD_CANT_BE_EMPTY); |
||
46 | |||
47 | return false; |
||
48 | } |
||
49 | |||
50 | return true; |
||
51 | } |
||
52 | |||
53 | /** |
||
54 | * Validate if checkbox is checked. |
||
55 | * |
||
56 | * @param string $fieldName Name of the field. |
||
57 | * |
||
58 | * @return bool |
||
59 | */ |
||
60 | View Code Duplication | public static function validateChecked($fieldName) { |
|
|
|||
61 | $fieldValue = trim(Request::getFieldValue($fieldName)); |
||
62 | if (empty($fieldValue)) { |
||
63 | Errors::saveErrorFor($fieldName, \__ERRORS::CHECK_THIS_FIELD); |
||
64 | |||
65 | return false; |
||
66 | } |
||
67 | |||
68 | return true; |
||
69 | } |
||
70 | |||
71 | /** |
||
72 | * Validate if field value is empty. |
||
73 | * |
||
74 | * @param string $fieldName Name of the field. |
||
75 | * |
||
76 | * @return bool |
||
77 | */ |
||
78 | public static function validateEmpty($fieldName) { |
||
79 | $fieldValue = trim(Request::getFieldValue($fieldName)); |
||
80 | if (empty($fieldValue)) { |
||
81 | return true; |
||
82 | } |
||
83 | |||
84 | return false; |
||
85 | } |
||
86 | |||
87 | /** |
||
88 | * Validate if field value haven't spaces. |
||
89 | * |
||
90 | * @param string $fieldName Name of the field. |
||
91 | * |
||
92 | * @return bool |
||
93 | */ |
||
94 | View Code Duplication | public static function validateNoSpaces($fieldName) { |
|
95 | $fieldValue = Request::getFieldValue($fieldName); |
||
96 | if (strpos($fieldValue, " ") !== false) { |
||
97 | Errors::saveErrorFor($fieldName, \__ERRORS::SPACES_INACCEPTABLE); |
||
98 | |||
99 | return false; |
||
100 | } |
||
101 | |||
102 | return true; |
||
103 | } |
||
104 | |||
105 | /** |
||
106 | * Validate login value. |
||
107 | * |
||
108 | * @param string $fieldName Login field name. |
||
109 | * |
||
110 | * @return bool |
||
111 | */ |
||
112 | public static function validateLogin($fieldName) { |
||
119 | |||
120 | /** |
||
121 | * Validate if field value is real password. |
||
122 | * |
||
123 | * @param string $passwordFieldName Name of password field. |
||
124 | * @param string $rePasswordFieldName Name of password repeting field. |
||
125 | * |
||
126 | * @return bool |
||
127 | */ |
||
128 | public static function validatePassword($passwordFieldName, $rePasswordFieldName) { |
||
129 | $password = trim(Request::getFieldValue($passwordFieldName)); |
||
130 | $rePassword = trim(Request::getFieldValue($rePasswordFieldName)); |
||
131 | if (empty($password)) { |
||
132 | Errors::saveErrorFor($passwordFieldName, \__ERRORS::EMPTY_PASSWORD); |
||
133 | |||
134 | return false; |
||
135 | } |
||
136 | |||
137 | if ($password != $rePassword) { |
||
138 | Errors::saveErrorFor($rePasswordFieldName, \__ERRORS::PASSWORDS_ARE_DIFFERENT); |
||
139 | |||
140 | return false; |
||
141 | } |
||
142 | |||
143 | return true; |
||
144 | } |
||
145 | |||
146 | /** |
||
147 | * Validate if captcha code is correct. |
||
148 | * |
||
149 | * @param string $fieldName Name of the captcha field. |
||
150 | * |
||
151 | * @return bool |
||
152 | */ |
||
153 | public static function validateCaptcha($fieldName) { |
||
168 | |||
169 | /** |
||
170 | * Validate if field value is real e-mail address. |
||
171 | * |
||
172 | * @param string $fieldName Name of the field. |
||
173 | * |
||
174 | * @return bool |
||
175 | */ |
||
176 | View Code Duplication | public static function validateEmail($fieldName) { |
|
177 | $fieldValue = trim(Request::getFieldValue($fieldName)); |
||
178 | if (!self::validateNotEmpty($fieldName)) { |
||
179 | Errors::saveErrorFor($fieldName, \__ERRORS::INVALID_EMAIL); |
||
180 | |||
181 | return false; |
||
182 | } elseif (!self::validateRegexp($fieldValue, "#^[A-Za-z0-9\._-]+@([A-Za-z0-9-]+\.)+[A-Za-z0-9-]+$#")) { |
||
183 | Errors::saveErrorFor($fieldName, \__ERRORS::INVALID_EMAIL); |
||
184 | |||
185 | return false; |
||
186 | } |
||
187 | |||
188 | return true; |
||
189 | } |
||
190 | |||
191 | /** |
||
192 | * Validate if variable value is correct e-mail address. |
||
193 | * |
||
194 | * @param string $value Value of the variable. |
||
195 | * |
||
196 | * @return bool |
||
197 | */ |
||
198 | public static function isEmail($value) { |
||
205 | |||
206 | /** |
||
207 | * Normalize URL with protocol prefix. |
||
208 | * |
||
209 | * @param string $url Unnormalized URL. |
||
210 | * @param string $protocol Protocol type (default: http). |
||
211 | * |
||
212 | * @return string Normalized URL. |
||
213 | */ |
||
214 | public static function normalizeUrl($url, $protocol = "http") { |
||
227 | |||
228 | /** |
||
229 | * Validate if field value is correct URL. |
||
230 | * |
||
231 | * @param string $fieldName Name of the field. |
||
232 | * |
||
233 | * @return bool |
||
234 | */ |
||
235 | public static function validateUrl($fieldName) { |
||
250 | |||
251 | /** |
||
252 | * Validate if variable value is correct URL. |
||
253 | * |
||
254 | * @param string $value Value of the variable. |
||
255 | * |
||
256 | * @return bool |
||
257 | */ |
||
258 | public static function isUrl($value) { |
||
284 | |||
285 | /** |
||
286 | * Validate maximum length of the text without HTML tags. |
||
287 | * |
||
288 | * @global array<string> $_ERRORS Global list of form fields validation errors. |
||
289 | * @param string $fieldName Name of the field. |
||
290 | * @param string $maxTextLength Text length limit. |
||
291 | * |
||
292 | * @return bool |
||
293 | */ |
||
294 | View Code Duplication | public static function validateTextMaxLength($fieldName, $maxTextLength) { |
|
307 | |||
308 | /** |
||
309 | * Validate minimum length of the text without HTML tags. |
||
310 | * |
||
311 | * @global array<string> $_ERRORS Global list of form fields validation errors. |
||
312 | * @param string $fieldName Name of the field. |
||
313 | * @param string $minTextLength Text length limit. |
||
314 | * |
||
315 | * @return bool |
||
316 | */ |
||
317 | View Code Duplication | public static function validateTextMinLength($fieldName, $minTextLength) { |
|
330 | |||
331 | /** |
||
332 | * Validate if field value is integer. |
||
333 | * |
||
334 | * @param string $fieldName Name of the field. |
||
335 | * @return bool |
||
336 | */ |
||
337 | View Code Duplication | public static function validateInteger($fieldName) { |
|
351 | |||
352 | /** |
||
353 | * Validate if field value is double(float). |
||
354 | * |
||
355 | * @param string $fieldName Name of the field. |
||
356 | * |
||
357 | * @return bool |
||
358 | */ |
||
359 | public static function validateDouble($fieldName) { |
||
375 | |||
376 | /** |
||
377 | * Validate if field value is double(float). |
||
378 | * |
||
379 | * @param string $fieldName Name of the field. |
||
380 | * |
||
381 | * @return bool |
||
382 | */ |
||
383 | public static function validateFloat($fieldName) { |
||
386 | |||
387 | /** |
||
388 | * Validate if field value is positive numeric value. |
||
389 | * |
||
390 | * @param string $fieldName Name of the field. |
||
391 | * |
||
392 | * @return bool |
||
393 | */ |
||
394 | View Code Duplication | public static function validatePositive($fieldName) { |
|
404 | |||
405 | /** |
||
406 | * Validate if login data is correct. |
||
407 | * |
||
408 | * @param string $loginFieldName Name of the login field. |
||
409 | * @param string $passwordFieldName Name of the password field. |
||
410 | * |
||
411 | * @return bool |
||
412 | */ |
||
413 | public static function validateSignIn($loginFieldName, $passwordFieldName) { |
||
429 | |||
430 | /** |
||
431 | * Validate if file was uploaded. |
||
432 | * |
||
433 | * @global array $_FILES Global array of uploaded files. |
||
434 | * @param string $fileFieldName Name of the file field. |
||
435 | * |
||
436 | * @return bool |
||
437 | */ |
||
438 | public static function validateFileUpload($fileFieldName) { |
||
448 | |||
449 | /** |
||
450 | * Validates if some value is in array. |
||
451 | * |
||
452 | * @param string $fieldName Field name. |
||
453 | * @param array $range Array with values. |
||
454 | * |
||
455 | * @return bool |
||
456 | */ |
||
457 | View Code Duplication | public static function validateRange($fieldName, $range) { |
|
467 | |||
468 | } |
||
469 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.