Conditions | 17 |
Paths | 28 |
Total Lines | 65 |
Lines | 11 |
Ratio | 16.92 % |
Changes | 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 |
||
28 | function ValidateEmail($email) |
||
29 | { |
||
30 | $is_valid = true; |
||
31 | $at_index = strrpos($email, '@'); |
||
32 | if (is_bool($at_index) && !$at_index) |
||
33 | { |
||
34 | $is_valid = false; |
||
35 | } |
||
36 | else |
||
37 | { |
||
38 | $domain = substr($email, $at_index + 1); |
||
39 | $local = substr($email, 0, $at_index); |
||
40 | $local_len = strlen($local); |
||
41 | $domain_len = strlen($domain); |
||
42 | if ($local_len < 1 || $local_len > 64) |
||
43 | { |
||
44 | // local part length exceeded |
||
45 | $is_valid = false; |
||
46 | } |
||
47 | elseif ($domain_len < 1 || $domain_len > 255) |
||
48 | { |
||
49 | // domain part length exceeded |
||
50 | $is_valid = false; |
||
51 | } |
||
52 | elseif ($local[0] == '.' || $local[$local_len-1] == '.') |
||
53 | { |
||
54 | // local part starts or ends with '.' |
||
55 | $is_valid = false; |
||
56 | } |
||
57 | elseif (preg_match('/\\.\\./', $local)) |
||
58 | { |
||
59 | // local part has two consecutive dots |
||
60 | $is_valid = false; |
||
61 | } |
||
62 | elseif (!preg_match('/^[A-Za-z0-9\\-\\.]+$/', $domain)) |
||
63 | { |
||
64 | // character not valid in domain part |
||
65 | $is_valid = false; |
||
66 | } |
||
67 | elseif (preg_match('/\\.\\./', $domain)) |
||
68 | { |
||
69 | // domain part has two consecutive dots |
||
70 | $is_valid = false; |
||
71 | } |
||
72 | View Code Duplication | elseif (!preg_match('/^(\\\\.|[A-Za-z0-9!#%&`_=\\/$\'*+?^{}|~.-])+$/', |
|
|
|||
73 | str_replace("\\\\", "", $local))) |
||
74 | { |
||
75 | // character not valid in local part unless |
||
76 | // local part is quoted |
||
77 | if (!preg_match('/^"(\\\\"|[^"])+"$/', |
||
78 | str_replace("\\\\", "", $local))) |
||
79 | { |
||
80 | $is_valid = false; |
||
81 | } |
||
82 | } |
||
83 | |||
84 | // :NOTICE: Some network provider will return fake A record if |
||
85 | // a dns query return fail, usually disp some ads. |
||
86 | // So we only check MX record. |
||
87 | if ($is_valid && NixOs()) |
||
88 | if (false == checkdnsrr($domain, "MX")) |
||
89 | $is_valid = false; |
||
90 | } |
||
91 | return $is_valid; |
||
92 | } |
||
93 | |||
131 |
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.