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. 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 Validate, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
26 | class Validate |
||
27 | { |
||
28 | const REGEX_NOT_NAME_PATTERN = '`(?:[\|<>"\=\]\[\}\{\\\\$£€@%~^#\(\):;\?!¿¡\*])|(?:(?:https?|ftps?)://)|(?:[0-9])`'; |
||
29 | const REGEX_DATE_FORMAT = '`^\d\d/\d\d/\d\d\d\d$`'; |
||
30 | |||
31 | const MAX_INT_NUMBER = 999999999999; |
||
32 | |||
33 | const MIN_NAME_LENGTH = 2; |
||
34 | const MAX_NAME_LENGTH = 20; |
||
35 | |||
36 | const HEX_HASH = '#'; |
||
37 | const MIN_HEX_LENGTH = 3; |
||
38 | const MAX_HEX_LENGTH = 6; |
||
39 | |||
40 | const DEF_MIN_USERNAME_LENGTH = 3; |
||
41 | const DEF_MIN_PASS_LENGTH = 6; |
||
42 | const DEF_MAX_PASS_LENGTH = 60; |
||
43 | const DEF_MIN_AGE = 18; |
||
44 | const DEF_MAX_AGE = 99; |
||
45 | |||
46 | /** @var Str */ |
||
47 | private $oStr; |
||
48 | |||
49 | public function __construct() |
||
53 | |||
54 | /** |
||
55 | * Check the type of a value. |
||
56 | * |
||
57 | * @param string $sValue |
||
58 | * @param string $sType Type whose value should be (case-insensitive). |
||
59 | * @param bool $bRequired Default TRUE |
||
60 | * |
||
61 | * @return bool |
||
62 | * |
||
63 | * @throws PH7InvalidArgumentException If the type doesn't exist. |
||
64 | */ |
||
65 | public static function type($sValue, $sType, $bRequired = true) |
||
118 | |||
119 | /** |
||
120 | * Validate Is String. |
||
121 | * |
||
122 | * @param $sValue |
||
123 | * @param int $iMin Default NULL |
||
124 | * @param int $iMax Default NULL |
||
125 | * |
||
126 | * @return bool |
||
127 | */ |
||
128 | public function str($sValue, $iMin = null, $iMax = null) |
||
144 | |||
145 | /** |
||
146 | * Validate if it's an integer. |
||
147 | * |
||
148 | * @param int $iInt |
||
149 | * @param int $iMin Default 0 |
||
150 | * @param int $iMax Default 999999999999 |
||
151 | * |
||
152 | * @return bool |
||
153 | */ |
||
154 | public function int($iInt, $iMin = 0, $iMax = self::MAX_INT_NUMBER) |
||
161 | |||
162 | /** |
||
163 | * Validate if it's a numeric. |
||
164 | * |
||
165 | * @param string|int (numeric string or integer) $mNumeric |
||
166 | * |
||
167 | * @return bool |
||
168 | */ |
||
169 | public function numeric($mNumeric) |
||
173 | |||
174 | /** |
||
175 | * Validate if it's a digit character. |
||
176 | * |
||
177 | * @param string (numeric string) $sDigit |
||
178 | * |
||
179 | * @return bool |
||
180 | */ |
||
181 | public function digitChar($sDigit) |
||
185 | |||
186 | /** |
||
187 | * Validate if it's a float type. |
||
188 | * |
||
189 | * @param float $fFloat |
||
190 | * @param float|int $mMin Default 0 |
||
191 | * @param float|int $mMax Default 999999999999 |
||
192 | * |
||
193 | * @return bool |
||
194 | */ |
||
195 | public function float($fFloat, $mMin = 0, $mMax = self::MAX_INT_NUMBER) |
||
201 | |||
202 | /** |
||
203 | * Validate if it's a boolean type. |
||
204 | * |
||
205 | * @param bool $bBool |
||
206 | * |
||
207 | * @return bool |
||
208 | */ |
||
209 | public function bool($bBool) |
||
213 | |||
214 | /** |
||
215 | * Validate Username. |
||
216 | * |
||
217 | * @param string $sUsername |
||
218 | * @param int $iMin Default 3 |
||
219 | * @param int $iMax Default 40 |
||
220 | * @param string $sTable Default DbTableName::MEMBER |
||
221 | * |
||
222 | * @return bool |
||
223 | */ |
||
224 | public function username($sUsername, $iMin = self::DEF_MIN_USERNAME_LENGTH, $iMax = PH7_MAX_USERNAME_LENGTH, $sTable = DbTableName::MEMBER) |
||
234 | |||
235 | /** |
||
236 | * Validate Password. |
||
237 | * |
||
238 | * @param string $sPwd |
||
239 | * @param int $iMin Default 6 |
||
240 | * @param int $iMax Default 60 |
||
241 | * |
||
242 | * @return bool |
||
243 | */ |
||
244 | public function password($sPwd, $iMin = self::DEF_MIN_PASS_LENGTH, $iMax = self::DEF_MAX_PASS_LENGTH) |
||
250 | |||
251 | /** |
||
252 | * Validate Email. |
||
253 | * |
||
254 | * @param string $sEmail |
||
255 | * @param bool $bRealHost Checks whether the Email Host is valid. |
||
256 | * |
||
257 | * @return bool |
||
258 | */ |
||
259 | public function email($sEmail, $bRealHost = false) |
||
274 | |||
275 | /** |
||
276 | * Validate Birthday. |
||
277 | * |
||
278 | * @param string $sValue The date format must be formatted like this: mm/dd/yyyy |
||
279 | * @param int $iMin Default 18 |
||
280 | * @param int $iMax Default 99 |
||
281 | * |
||
282 | * @return bool |
||
283 | */ |
||
284 | public function birthDate($sValue, $iMin = self::DEF_MIN_AGE, $iMax = self::DEF_MAX_AGE) |
||
299 | |||
300 | /** |
||
301 | * Validate Date. |
||
302 | * |
||
303 | * @param string $sValue |
||
304 | * |
||
305 | * @return bool |
||
306 | */ |
||
307 | public function date($sValue) |
||
316 | |||
317 | /** |
||
318 | * Validate URL. |
||
319 | * |
||
320 | * @param string $sUrl |
||
321 | * @param bool $bRealUrl Checks if the current URL exists. |
||
322 | * |
||
323 | * @return bool |
||
324 | */ |
||
325 | public function url($sUrl, $bRealUrl = false) |
||
348 | |||
349 | /** |
||
350 | * Validate IP address. |
||
351 | * |
||
352 | * @param string $sIp |
||
353 | * |
||
354 | * @return bool |
||
355 | */ |
||
356 | public function ip($sIp) |
||
360 | |||
361 | /** |
||
362 | * Validate international phone numbers in EPP format. |
||
363 | * |
||
364 | * @param string $sNumber |
||
365 | * |
||
366 | * @return bool |
||
367 | */ |
||
368 | public function phone($sNumber) |
||
372 | |||
373 | /** |
||
374 | * @param string $sHexCode |
||
375 | * |
||
376 | * @return bool |
||
377 | */ |
||
378 | public function hex($sHexCode) |
||
385 | |||
386 | /** |
||
387 | * Validate Name. |
||
388 | * |
||
389 | * @param string $sName |
||
390 | * @param int $iMin Default 2 |
||
391 | * @param int $iMax Default 20 |
||
392 | * |
||
393 | * @return bool |
||
394 | */ |
||
395 | public function name($sName, $iMin = self::MIN_NAME_LENGTH, $iMax = self::MAX_NAME_LENGTH) |
||
409 | |||
410 | /* |
||
411 | /** |
||
412 | * Check Email with test for check if the host email is valid. |
||
413 | * |
||
414 | * @param string $sEmail |
||
415 | * |
||
416 | * @return bool |
||
417 | */ |
||
418 | /* |
||
419 | public function emailHost($sEmail) |
||
420 | { |
||
421 | // The email address must be properly formatted |
||
422 | if (!$this->email($sEmail)) |
||
423 | return false; |
||
424 | |||
425 | // It gets domain |
||
426 | list(, $sDomain ) = explode('@', $sEmail); |
||
427 | // We look for MX records in DNS |
||
428 | if (getmxrr($sDomain, $aMxHost)) |
||
429 | $sConnectAddress = $aMxHost[0]; |
||
430 | else |
||
431 | $sConnectAddress = $sDomain; |
||
432 | // We created the connection on SMTP port (25) |
||
433 | if ($rConnect = @fsockopen($sConnectAddress, 25, $iErrno, $sErrStr)) |
||
434 | { |
||
435 | if (preg_match("/^220/", $sOut = fgets($rConnect, 1024))) |
||
436 | { |
||
437 | fputs($rConnect, "HELO {$_SERVER['HTTP_HOST']}\r\n"); |
||
438 | $sOut = fgets($rConnect, 1024); |
||
439 | fputs($rConnect, "MAIL FROM: <{$sEmail}>\r\n"); |
||
440 | $sFrom = fgets($rConnect, 1024); |
||
441 | fputs($rConnect, "RCPT TO: <{$sEmail}>\r\n"); |
||
442 | $sTo = fgets($rConnect, 1024); |
||
443 | fputs($rConnect, "QUIT\r\n"); |
||
444 | fclose($rConnect); |
||
445 | // If the code returned by the RCPT TO is 250 or 251 (cf: RFC) |
||
446 | // Then the address exists |
||
447 | if (!preg_match("/^250/", $sTo) && !preg_match("/^251/", $sTo)) |
||
448 | // Address rejected by the serve |
||
449 | return false; |
||
450 | else |
||
451 | // Accepted by the server address |
||
452 | return true; |
||
453 | } |
||
454 | else |
||
455 | { |
||
456 | // The server did not respond |
||
457 | return false; |
||
458 | } |
||
459 | } |
||
460 | else |
||
461 | { |
||
462 | // You can display an error message by uncommenting the following two lines or leave the return value of the false boolean. |
||
463 | // echo "Cannot connect to the mail server\n"; |
||
464 | // echo "$iErrno - $sErrStr\n"; |
||
465 | return false; |
||
466 | } |
||
467 | } |
||
468 | //*/ |
||
469 | |||
470 | /** |
||
471 | * Get option for some filter_var(). |
||
472 | * |
||
473 | * @param float|int $mMin Minimum range. |
||
474 | * @param float|int $mMax Maximum range. |
||
475 | * |
||
476 | * @return array |
||
477 | */ |
||
478 | protected static function getFilterOption($mMin, $mMax) |
||
482 | |||
483 | } |
||
484 |
The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.
The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.
To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.