Total Complexity | 56 |
Total Lines | 323 |
Duplicated Lines | 0 % |
Changes | 2 | ||
Bugs | 0 | Features | 0 |
Complex classes like Rfc5322Validator 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.
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 Rfc5322Validator, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
20 | class Rfc5322Validator extends AValidator |
||
21 | { |
||
22 | // Maximum lengths defined by RFC 5322 |
||
23 | private const MAX_LOCAL_PART_LENGTH = 64; |
||
24 | private const MAX_DOMAIN_LABEL_LENGTH = 63; |
||
25 | private const MAX_DOMAIN_LENGTH = 255; |
||
26 | |||
27 | // Character sets for unquoted local part |
||
28 | private const LOCAL_PART_ALLOWED_CHARS = '!#$%&\'*+-/=?^_`{|}~.'; |
||
29 | |||
30 | /** |
||
31 | * Validates an email address according to RFC 5322 rules |
||
32 | * |
||
33 | * @param EmailAddress $email The email address to validate |
||
34 | * @return bool True if the email address is valid according to RFC 5322 |
||
35 | */ |
||
36 | public function validate(EmailAddress $email): bool |
||
37 | { |
||
38 | $localPart = $email->getLocalPart(); |
||
39 | $domain = $email->getDomain(); |
||
40 | |||
41 | if ($localPart === null || $domain === null) { |
||
42 | return false; |
||
43 | } |
||
44 | |||
45 | return $this->validateLocalPart($localPart) && $this->validateDomain($domain); |
||
46 | } |
||
47 | |||
48 | /** |
||
49 | * Validates the local part of an email address |
||
50 | * |
||
51 | * @param string $localPart The local part to validate |
||
52 | * @return bool True if the local part is valid |
||
53 | */ |
||
54 | private function validateLocalPart(string $localPart): bool |
||
55 | { |
||
56 | // Check length |
||
57 | if (strlen($localPart) > self::MAX_LOCAL_PART_LENGTH) { |
||
58 | return false; |
||
59 | } |
||
60 | |||
61 | // Empty local part is invalid |
||
62 | if ($localPart === '') { |
||
63 | return false; |
||
64 | } |
||
65 | |||
66 | // Handle quoted string |
||
67 | if ($localPart[0] === '"') { |
||
68 | return $this->validateQuotedString($localPart); |
||
69 | } |
||
70 | |||
71 | // Handle dot-atom format |
||
72 | return $this->validateDotAtom($localPart); |
||
73 | } |
||
74 | |||
75 | /** |
||
76 | * Validates a dot-atom format local part |
||
77 | * |
||
78 | * @param string $localPart The unquoted local part to validate |
||
79 | * @return bool True if the unquoted local part is valid |
||
80 | */ |
||
81 | private function validateDotAtom(string $localPart): bool |
||
82 | { |
||
83 | // Split into atoms |
||
84 | $atoms = explode('.', $localPart); |
||
85 | |||
86 | // Check each atom |
||
87 | foreach ($atoms as $atom) { |
||
88 | if ($atom === '') { |
||
89 | return false; |
||
90 | } |
||
91 | |||
92 | // Check for valid characters in each atom |
||
93 | if (!preg_match('/^[a-zA-Z0-9!#$%&\'*+\-\/=?^_`{|}~]+$/', $atom)) { |
||
94 | return false; |
||
95 | } |
||
96 | } |
||
97 | |||
98 | return true; |
||
99 | } |
||
100 | |||
101 | /** |
||
102 | * Validates a quoted string local part |
||
103 | * |
||
104 | * @param string $localPart The quoted string to validate |
||
105 | * @return bool True if the quoted string is valid |
||
106 | */ |
||
107 | private function validateQuotedString(string $localPart): bool |
||
152 | } |
||
153 | |||
154 | /** |
||
155 | * Validates the domain part of an email address |
||
156 | * |
||
157 | * @param string $domain The domain to validate |
||
158 | * @return bool True if the domain is valid |
||
159 | */ |
||
160 | private function validateDomain(string $domain): bool |
||
161 | { |
||
162 | // Check for empty domain |
||
163 | if ($domain === '') { |
||
164 | return false; |
||
165 | } |
||
166 | |||
167 | // Check total length |
||
168 | if (strlen($domain) > self::MAX_DOMAIN_LENGTH) { |
||
169 | return false; |
||
170 | } |
||
171 | |||
172 | // Handle domain literal |
||
173 | if ($domain[0] === '[') { |
||
174 | return $this->validateDomainLiteral($domain); |
||
175 | } |
||
176 | |||
177 | // Validate regular domain |
||
178 | return $this->validateDomainName($domain); |
||
179 | } |
||
180 | |||
181 | /** |
||
182 | * Validates a domain literal (IP address in brackets) |
||
183 | * |
||
184 | * @param string $domain The domain literal to validate |
||
185 | * @return bool True if the domain literal is valid |
||
186 | */ |
||
187 | private function validateDomainLiteral(string $domain): bool |
||
286 | } |
||
287 | |||
288 | /** |
||
289 | * Validates a domain name |
||
290 | * |
||
291 | * @param string $domain The domain name to validate |
||
292 | * @return bool True if the domain name is valid |
||
293 | */ |
||
294 | private function validateDomainName(string $domain): bool |
||
295 | { |
||
296 | // Split into labels |
||
297 | $labels = explode('.', $domain); |
||
298 | |||
299 | // Must have at least two labels |
||
300 | if (count($labels) < 2) { |
||
301 | return false; |
||
302 | } |
||
303 | |||
304 | // Validate each label |
||
305 | foreach ($labels as $label) { |
||
306 | if (!$this->validateDomainLabel($label)) { |
||
307 | return false; |
||
308 | } |
||
309 | } |
||
310 | |||
311 | return true; |
||
312 | } |
||
313 | |||
314 | /** |
||
315 | * Validates a single domain label |
||
316 | * |
||
317 | * @param string $label The domain label to validate |
||
318 | * @return bool True if the domain label is valid |
||
319 | */ |
||
320 | private function validateDomainLabel(string $label): bool |
||
343 | } |
||
344 | } |
||
345 |