Total Complexity | 125 |
Total Lines | 604 |
Duplicated Lines | 0 % |
Changes | 9 | ||
Bugs | 1 | Features | 3 |
Complex classes like Header 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 Header, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
25 | class Header { |
||
26 | |||
27 | /** |
||
28 | * Raw header |
||
29 | * |
||
30 | * @var string $raw |
||
31 | */ |
||
32 | public $raw = ""; |
||
33 | |||
34 | /** |
||
35 | * Attribute holder |
||
36 | * |
||
37 | * @var array $attributes |
||
38 | */ |
||
39 | protected $attributes = []; |
||
40 | |||
41 | /** |
||
42 | * Config holder |
||
43 | * |
||
44 | * @var array $config |
||
45 | */ |
||
46 | protected $config = []; |
||
47 | |||
48 | /** |
||
49 | * Fallback Encoding |
||
50 | * |
||
51 | * @var string |
||
52 | */ |
||
53 | public $fallback_encoding = 'UTF-8'; |
||
54 | |||
55 | /** |
||
56 | * Header constructor. |
||
57 | * @param $raw_header |
||
58 | * |
||
59 | * @throws InvalidMessageDateException |
||
60 | */ |
||
61 | public function __construct($raw_header) { |
||
65 | } |
||
66 | |||
67 | /** |
||
68 | * Call dynamic attribute setter and getter methods |
||
69 | * @param string $method |
||
70 | * @param array $arguments |
||
71 | * |
||
72 | * @return mixed |
||
73 | * @throws MethodNotFoundException |
||
74 | */ |
||
75 | public function __call($method, $arguments) { |
||
76 | if(strtolower(substr($method, 0, 3)) === 'get') { |
||
77 | $name = preg_replace('/(.)(?=[A-Z])/u', '$1_', substr(strtolower($method), 3)); |
||
78 | |||
79 | if(in_array($name, array_keys($this->attributes))) { |
||
80 | return $this->attributes[$name]; |
||
81 | } |
||
82 | |||
83 | } |
||
84 | |||
85 | throw new MethodNotFoundException("Method ".self::class.'::'.$method.'() is not supported'); |
||
86 | } |
||
87 | |||
88 | /** |
||
89 | * Magic getter |
||
90 | * @param $name |
||
91 | * |
||
92 | * @return mixed|null |
||
93 | */ |
||
94 | public function __get($name) { |
||
96 | } |
||
97 | |||
98 | /** |
||
99 | * Get a specific header attribute |
||
100 | * @param $name |
||
101 | * |
||
102 | * @return mixed|null |
||
103 | */ |
||
104 | public function get($name) { |
||
110 | } |
||
111 | |||
112 | /** |
||
113 | * Perform a regex match all on the raw header and return the first result |
||
114 | * @param $pattern |
||
115 | * |
||
116 | * @return mixed|null |
||
117 | */ |
||
118 | public function find($pattern) { |
||
127 | } |
||
128 | |||
129 | /** |
||
130 | * Parse the raw headers |
||
131 | * |
||
132 | * @throws InvalidMessageDateException |
||
133 | */ |
||
134 | protected function parse(){ |
||
162 | } |
||
163 | |||
164 | /** |
||
165 | * Parse mail headers from a string |
||
166 | * @link https://php.net/manual/en/function.imap-rfc822-parse-headers.php |
||
167 | * @param $raw_headers |
||
168 | * |
||
169 | * @return object |
||
170 | */ |
||
171 | public function rfc822_parse_headers($raw_headers){ |
||
254 | } |
||
255 | |||
256 | /** |
||
257 | * Decode MIME header elements |
||
258 | * @link https://php.net/manual/en/function.imap-mime-header-decode.php |
||
259 | * @param string $text The MIME text |
||
260 | * |
||
261 | * @return array The decoded elements are returned in an array of objects, where each |
||
262 | * object has two properties, charset and text. |
||
263 | */ |
||
264 | public function mime_header_decode($text){ |
||
265 | if (extension_loaded('imap')) { |
||
266 | return \imap_mime_header_decode($text); |
||
267 | } |
||
268 | $charset = $this->getEncoding($text); |
||
269 | return [(object)[ |
||
270 | "charset" => $charset, |
||
271 | "text" => $this->convertEncoding($text, $charset) |
||
272 | ]]; |
||
273 | } |
||
274 | |||
275 | /** |
||
276 | * Check if a given pair of strings has ben decoded |
||
277 | * @param $encoded |
||
278 | * @param $decoded |
||
279 | * |
||
280 | * @return bool |
||
281 | */ |
||
282 | private function notDecoded($encoded, $decoded) { |
||
283 | return 0 === strpos($decoded, '=?') |
||
284 | && strlen($decoded) - 2 === strpos($decoded, '?=') |
||
285 | && false !== strpos($encoded, $decoded); |
||
286 | } |
||
287 | |||
288 | /** |
||
289 | * Convert the encoding |
||
290 | * @param $str |
||
291 | * @param string $from |
||
292 | * @param string $to |
||
293 | * |
||
294 | * @return mixed|string |
||
295 | */ |
||
296 | public function convertEncoding($str, $from = "ISO-8859-2", $to = "UTF-8") { |
||
335 | } |
||
336 | } |
||
337 | } |
||
338 | |||
339 | /** |
||
340 | * Get the encoding of a given abject |
||
341 | * @param object|string $structure |
||
342 | * |
||
343 | * @return string |
||
344 | */ |
||
345 | public function getEncoding($structure) { |
||
359 | } |
||
360 | |||
361 | /** |
||
362 | * Try to decode a specific header |
||
363 | * @param mixed $value |
||
364 | * |
||
365 | * @return mixed |
||
366 | */ |
||
367 | private function decode($value) { |
||
368 | if (is_array($value)) { |
||
369 | return $this->decodeArray($value); |
||
370 | } |
||
371 | $original_value = $value; |
||
372 | $decoder = $this->config['decoder']['message']; |
||
373 | |||
374 | if ($value !== null) { |
||
375 | if($decoder === 'utf-8' && extension_loaded('imap')) { |
||
376 | $value = \imap_utf8($value); |
||
377 | if (strpos(strtolower($value), '=?utf-8?') === 0) { |
||
378 | $value = mb_decode_mimeheader($value); |
||
379 | } |
||
380 | if ($this->notDecoded($original_value, $value)) { |
||
381 | $decoded_value = $this->mime_header_decode($value); |
||
382 | if (count($decoded_value) > 0) { |
||
383 | if(property_exists($decoded_value[0], "text")) { |
||
384 | $value = $decoded_value[0]->text; |
||
385 | } |
||
386 | } |
||
387 | } |
||
388 | }elseif($decoder === 'iconv') { |
||
389 | $value = iconv_mime_decode($value); |
||
390 | }else{ |
||
391 | $value = mb_decode_mimeheader($value); |
||
392 | } |
||
393 | |||
394 | if (strpos(strtolower($value), '=?utf-8?') === 0) { |
||
395 | $value = mb_decode_mimeheader($value); |
||
396 | } |
||
397 | |||
398 | if ($this->notDecoded($original_value, $value)) { |
||
399 | $value = $this->convertEncoding($original_value, $this->getEncoding($original_value)); |
||
400 | } |
||
401 | } |
||
402 | |||
403 | return $value; |
||
404 | } |
||
405 | |||
406 | /** |
||
407 | * Decode a given array |
||
408 | * @param array $values |
||
409 | * |
||
410 | * @return array |
||
411 | */ |
||
412 | private function decodeArray($values) { |
||
413 | foreach($values as $key => $value) { |
||
414 | $values[$key] = $this->decode($value); |
||
415 | } |
||
416 | return $values; |
||
417 | } |
||
418 | |||
419 | /** |
||
420 | * Try to extract the priority from a given raw header string |
||
421 | */ |
||
422 | private function findPriority() { |
||
446 | } |
||
447 | |||
448 | /** |
||
449 | * Extract a given part as address array from a given header |
||
450 | * @param $values |
||
451 | * |
||
452 | * @return array |
||
453 | */ |
||
454 | private function decodeAddresses($values) { |
||
473 | } |
||
474 | |||
475 | /** |
||
476 | * Extract a given part as address array from a given header |
||
477 | * @param object $header |
||
478 | */ |
||
479 | private function extractAddresses($header) { |
||
480 | foreach(['from', 'to', 'cc', 'bcc', 'reply_to', 'sender'] as $key){ |
||
481 | if (property_exists($header, $key)) { |
||
482 | $this->attributes[$key] = $this->parseAddresses($header->$key); |
||
483 | } |
||
484 | } |
||
485 | } |
||
486 | |||
487 | /** |
||
488 | * Parse Addresses |
||
489 | * @param $list |
||
490 | * |
||
491 | * @return array |
||
492 | */ |
||
493 | private function parseAddresses($list) { |
||
529 | } |
||
530 | |||
531 | /** |
||
532 | * Search and extract potential header extensions |
||
533 | */ |
||
534 | private function extractHeaderExtensions(){ |
||
555 | } |
||
556 | } |
||
557 | } |
||
558 | } |
||
559 | } |
||
560 | } |
||
561 | } |
||
562 | |||
563 | /** |
||
564 | * Exception handling for invalid dates |
||
565 | * |
||
566 | * Currently known invalid formats: |
||
567 | * ^ Datetime ^ Problem ^ Cause |
||
568 | * | Mon, 20 Nov 2017 20:31:31 +0800 (GMT+8:00) | Double timezone specification | A Windows feature |
||
569 | * | Thu, 8 Nov 2018 08:54:58 -0200 (-02) | |
||
570 | * | | and invalid timezone (max 6 char) | |
||
571 | * | 04 Jan 2018 10:12:47 UT | Missing letter "C" | Unknown |
||
572 | * | Thu, 31 May 2018 18:15:00 +0800 (added by) | Non-standard details added by the | Unknown |
||
573 | * | | mail server | |
||
574 | * | Sat, 31 Aug 2013 20:08:23 +0580 | Invalid timezone | PHPMailer bug https://sourceforge.net/p/phpmailer/mailman/message/6132703/ |
||
575 | * |
||
576 | * Please report any new invalid timestamps to [#45](https://github.com/Webklex/php-imap/issues) |
||
577 | * |
||
578 | * @param object $header |
||
579 | * |
||
580 | * @throws InvalidMessageDateException |
||
581 | */ |
||
582 | private function parseDate($header) { |
||
619 | } |
||
620 | } |
||
621 | |||
622 | /** |
||
623 | * Get all available attributes |
||
624 | * |
||
625 | * @return array |
||
626 | */ |
||
627 | public function getAttributes() { |
||
629 | } |
||
630 | |||
632 |