| Conditions | 18 | 
| Paths | 20 | 
| Total Lines | 61 | 
| Code Lines | 37 | 
| Lines | 0 | 
| Ratio | 0 % | 
| Changes | 2 | ||
| Bugs | 1 | Features | 0 | 
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | <?php | ||
| 14 | function valid_email($email) | ||
| 15 | { | ||
| 16 | $input = (string) $email; | ||
|  | |||
| 17 |     if (empty($email)) { | ||
| 18 | return false; | ||
| 19 | } | ||
| 20 | $isValid = true; | ||
| 21 | $atIndex = strrpos($email, '@'); | ||
| 22 |     if (is_bool($atIndex) && !$atIndex) { | ||
| 23 | $isValid = false; | ||
| 24 |     } else { | ||
| 25 | $domain = substr($email, $atIndex + 1); | ||
| 26 | $local = substr($email, 0, $atIndex); | ||
| 27 | $localLen = strlen($local); | ||
| 28 | $domainLen = strlen($domain); | ||
| 29 |         if ($localLen < 1 || $localLen > 64) { | ||
| 30 | // local part length exceeded | ||
| 31 | $isValid = false; | ||
| 32 |         } else { | ||
| 33 |             if ($domainLen < 1 || $domainLen > 255) { | ||
| 34 | // domain part length exceeded | ||
| 35 | $isValid = false; | ||
| 36 |             } else { | ||
| 37 |                 if ('.' == $local[0] || '.' == $local[$localLen - 1]) { | ||
| 38 | // local part starts or ends with '.' | ||
| 39 | $isValid = false; | ||
| 40 |                 } else { | ||
| 41 |                     if (preg_match('/\.\./', $local)) { | ||
| 42 | // local part has two consecutive dots | ||
| 43 | $isValid = false; | ||
| 44 |                     } else { | ||
| 45 |                         if (!preg_match('/^[A-Za-z0-9\-\.]+$/', $domain)) { | ||
| 46 | // character not valid in domain part | ||
| 47 | $isValid = false; | ||
| 48 |                         } else { | ||
| 49 |                             if (preg_match('/\.\./', $domain)) { | ||
| 50 | // domain part has two consecutive dots | ||
| 51 | $isValid = false; | ||
| 52 |                             } else { | ||
| 53 |                                 if (!preg_match('/^(\.|[A-Za-z0-9!#%&`_=\/$\'*+?^{}|~.-])+$/', | ||
| 54 |                                     str_replace('\\', '', $local)) | ||
| 55 |                                 ) { | ||
| 56 | // character not valid in local part unless | ||
| 57 | // local part is quoted | ||
| 58 |                                     if (!preg_match('/^"(\"|[^"])+"$/', str_replace('\\', '', $local))) { | ||
| 59 | $isValid = false; | ||
| 60 | } | ||
| 61 | } | ||
| 62 | } | ||
| 63 | } | ||
| 64 | } | ||
| 65 | } | ||
| 66 | } | ||
| 67 | } | ||
| 68 |         if ($isValid && !(checkdnsrr($domain, 'MX') || checkdnsrr($domain, 'A'))) { | ||
| 69 | // domain not found in DNS | ||
| 70 | $isValid = false; | ||
| 71 | } | ||
| 72 | } | ||
| 73 | |||
| 74 | return $isValid; | ||
| 75 | } | ||
| 164 |