Conditions | 12 |
Paths | 7 |
Total Lines | 39 |
Code Lines | 21 |
Lines | 0 |
Ratio | 0 % |
Changes | 3 | ||
Bugs | 0 | Features | 1 |
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 |
||
47 | public function validate() |
||
48 | { |
||
49 | // @codeCoverageIgnoreStart |
||
50 | if (!function_exists('idn_to_ascii') && !class_exists('\idna_convert')) { |
||
51 | throw new Exception('Unable to convert punycode. Either install the PHP Intl extension, or the mabrahamde/idna-converter Composer package.'); |
||
|
|||
52 | } |
||
53 | |||
54 | // Use PHP Intl first |
||
55 | if (function_exists('idn_to_ascii')) { |
||
56 | $email = idn_to_ascii($this->value); |
||
57 | |||
58 | // Fall back to the idna_convert class |
||
59 | } elseif (class_exists('\idna_convert')) { |
||
60 | $idn = new \idna_convert([ |
||
61 | 'idn_version' => 2008 |
||
62 | ]); |
||
63 | |||
64 | $email = $idn->encode($this->value); |
||
65 | |||
66 | // Fall back to no conversion |
||
67 | } else { |
||
68 | $email = $this->value; |
||
69 | } |
||
70 | // @codeCoverageIgnoreEnd |
||
71 | |||
72 | if (!( |
||
73 | filter_var($email, FILTER_VALIDATE_EMAIL) && // RFC 822 (obsolete). Exceptions: no comments, no whitespace |
||
74 | preg_match('/@.+\./', $email) && // Standard email formatting |
||
75 | !preg_match('/@\[/', $email) && // Disallow IPv6 domains |
||
76 | !preg_match('/".+@/', $email) && // Disallow quotes |
||
77 | !preg_match('/=.+@/', $email) && // Disallow equals |
||
78 | !preg_match('/localhost/i', $email) && // Disallow localhost |
||
79 | !preg_match('/localdomain/i', $email) // Disallow localdomain |
||
80 | )) { |
||
81 | throw new UnexpectedValueException( |
||
82 | sprintf('The value "%s" is not a valid email address.', $this->value) |
||
83 | ); |
||
84 | } |
||
85 | } |
||
86 | } |
||
87 |
Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.