Total Complexity | 137 |
Total Lines | 678 |
Duplicated Lines | 0 % |
Changes | 15 | ||
Bugs | 2 | Features | 6 |
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 Attribute[]|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 | * Convert parsed values to attributes |
||
57 | * |
||
58 | * @var bool |
||
59 | */ |
||
60 | protected $attributize = false; |
||
61 | |||
62 | /** |
||
63 | * Header constructor. |
||
64 | * @param $raw_header |
||
65 | * @param boolean $attributize |
||
66 | * |
||
67 | * @throws InvalidMessageDateException |
||
68 | */ |
||
69 | public function __construct($raw_header, $attributize = true) { |
||
74 | } |
||
75 | |||
76 | /** |
||
77 | * Call dynamic attribute setter and getter methods |
||
78 | * @param string $method |
||
79 | * @param array $arguments |
||
80 | * |
||
81 | * @return Attribute|mixed |
||
82 | * @throws MethodNotFoundException |
||
83 | */ |
||
84 | public function __call($method, $arguments) { |
||
85 | if(strtolower(substr($method, 0, 3)) === 'get') { |
||
86 | $name = preg_replace('/(.)(?=[A-Z])/u', '$1_', substr(strtolower($method), 3)); |
||
87 | |||
88 | if(in_array($name, array_keys($this->attributes))) { |
||
89 | return $this->attributes[$name]; |
||
90 | } |
||
91 | |||
92 | } |
||
93 | |||
94 | throw new MethodNotFoundException("Method ".self::class.'::'.$method.'() is not supported'); |
||
95 | } |
||
96 | |||
97 | /** |
||
98 | * Magic getter |
||
99 | * @param $name |
||
100 | * |
||
101 | * @return Attribute|null |
||
102 | */ |
||
103 | public function __get($name) { |
||
105 | } |
||
106 | |||
107 | /** |
||
108 | * Get a specific header attribute |
||
109 | * @param $name |
||
110 | * |
||
111 | * @return Attribute|mixed |
||
112 | */ |
||
113 | public function get($name) { |
||
119 | } |
||
120 | |||
121 | /** |
||
122 | * Set a specific attribute |
||
123 | * @param string $name |
||
124 | * @param array|mixed $value |
||
125 | * @param boolean $strict |
||
126 | * |
||
127 | * @return Attribute |
||
128 | */ |
||
129 | public function set($name, $value, $strict = false) { |
||
130 | if(isset($this->attributes[$name]) && $strict === false) { |
||
131 | if ($this->attributize) { |
||
132 | $this->attributes[$name]->add($value, true); |
||
133 | }else{ |
||
134 | if(isset($this->attributes[$name])) { |
||
135 | if (is_array($this->attributes[$name]) == false) { |
||
|
|||
136 | $this->attributes[$name] = [$this->attributes[$name], $value]; |
||
137 | }else{ |
||
138 | $this->attributes[$name][] = $value; |
||
139 | } |
||
140 | }else{ |
||
141 | $this->attributes[$name] = $value; |
||
142 | } |
||
143 | } |
||
144 | }elseif($this->attributize == false){ |
||
145 | $this->attributes[$name] = $value; |
||
146 | }else{ |
||
147 | $this->attributes[$name] = new Attribute($name, $value); |
||
148 | } |
||
149 | |||
150 | return $this->attributes[$name]; |
||
151 | } |
||
152 | |||
153 | /** |
||
154 | * Perform a regex match all on the raw header and return the first result |
||
155 | * @param $pattern |
||
156 | * |
||
157 | * @return mixed|null |
||
158 | */ |
||
159 | public function find($pattern) { |
||
160 | if (preg_match_all($pattern, $this->raw, $matches)) { |
||
161 | if (isset($matches[1])) { |
||
162 | if(count($matches[1]) > 0) { |
||
163 | return $matches[1][0]; |
||
164 | } |
||
165 | } |
||
166 | } |
||
167 | return null; |
||
168 | } |
||
169 | |||
170 | /** |
||
171 | * Parse the raw headers |
||
172 | * |
||
173 | * @throws InvalidMessageDateException |
||
174 | */ |
||
175 | protected function parse(){ |
||
200 | } |
||
201 | |||
202 | /** |
||
203 | * Parse mail headers from a string |
||
204 | * @link https://php.net/manual/en/function.imap-rfc822-parse-headers.php |
||
205 | * @param $raw_headers |
||
206 | * |
||
207 | * @return object |
||
208 | */ |
||
209 | public function rfc822_parse_headers($raw_headers){ |
||
302 | } |
||
303 | |||
304 | /** |
||
305 | * Decode MIME header elements |
||
306 | * @link https://php.net/manual/en/function.imap-mime-header-decode.php |
||
307 | * @param string $text The MIME text |
||
308 | * |
||
309 | * @return array The decoded elements are returned in an array of objects, where each |
||
310 | * object has two properties, charset and text. |
||
311 | */ |
||
312 | public function mime_header_decode($text){ |
||
313 | if (extension_loaded('imap')) { |
||
314 | return \imap_mime_header_decode($text); |
||
315 | } |
||
316 | $charset = $this->getEncoding($text); |
||
317 | return [(object)[ |
||
318 | "charset" => $charset, |
||
319 | "text" => $this->convertEncoding($text, $charset) |
||
320 | ]]; |
||
321 | } |
||
322 | |||
323 | /** |
||
324 | * Check if a given pair of strings has ben decoded |
||
325 | * @param $encoded |
||
326 | * @param $decoded |
||
327 | * |
||
328 | * @return bool |
||
329 | */ |
||
330 | private function notDecoded($encoded, $decoded) { |
||
331 | return 0 === strpos($decoded, '=?') |
||
332 | && strlen($decoded) - 2 === strpos($decoded, '?=') |
||
333 | && false !== strpos($encoded, $decoded); |
||
334 | } |
||
335 | |||
336 | /** |
||
337 | * Convert the encoding |
||
338 | * @param $str |
||
339 | * @param string $from |
||
340 | * @param string $to |
||
341 | * |
||
342 | * @return mixed|string |
||
343 | */ |
||
344 | public function convertEncoding($str, $from = "ISO-8859-2", $to = "UTF-8") { |
||
383 | } |
||
384 | } |
||
385 | } |
||
386 | |||
387 | /** |
||
388 | * Get the encoding of a given abject |
||
389 | * @param object|string $structure |
||
390 | * |
||
391 | * @return string |
||
392 | */ |
||
393 | public function getEncoding($structure) { |
||
407 | } |
||
408 | |||
409 | /** |
||
410 | * Test if a given value is utf-8 encoded |
||
411 | * @param $value |
||
412 | * |
||
413 | * @return bool |
||
414 | */ |
||
415 | private function is_uft8($value) { |
||
416 | return strpos(strtolower($value), '=?utf-8?') === 0; |
||
417 | } |
||
418 | |||
419 | /** |
||
420 | * Try to decode a specific header |
||
421 | * @param mixed $value |
||
422 | * |
||
423 | * @return mixed |
||
424 | */ |
||
425 | private function decode($value) { |
||
426 | if (is_array($value)) { |
||
427 | return $this->decodeArray($value); |
||
428 | } |
||
429 | $original_value = $value; |
||
430 | $decoder = $this->config['decoder']['message']; |
||
431 | |||
432 | if ($value !== null) { |
||
433 | $is_utf8_base = $this->is_uft8($value); |
||
434 | |||
435 | if($decoder === 'utf-8' && extension_loaded('imap')) { |
||
436 | $value = \imap_utf8($value); |
||
437 | $is_utf8_base = $this->is_uft8($value); |
||
438 | if ($is_utf8_base) { |
||
439 | $value = mb_decode_mimeheader($value); |
||
440 | } |
||
441 | if ($this->notDecoded($original_value, $value)) { |
||
442 | $decoded_value = $this->mime_header_decode($value); |
||
443 | if (count($decoded_value) > 0) { |
||
444 | if(property_exists($decoded_value[0], "text")) { |
||
445 | $value = $decoded_value[0]->text; |
||
446 | } |
||
447 | } |
||
448 | } |
||
449 | }elseif($decoder === 'iconv' && $is_utf8_base) { |
||
450 | $value = iconv_mime_decode($value); |
||
451 | }elseif($is_utf8_base){ |
||
452 | $value = mb_decode_mimeheader($value); |
||
453 | } |
||
454 | |||
455 | if ($this->is_uft8($value)) { |
||
456 | $value = mb_decode_mimeheader($value); |
||
457 | } |
||
458 | |||
459 | if ($this->notDecoded($original_value, $value)) { |
||
460 | $value = $this->convertEncoding($original_value, $this->getEncoding($original_value)); |
||
461 | } |
||
462 | } |
||
463 | |||
464 | return $value; |
||
465 | } |
||
466 | |||
467 | /** |
||
468 | * Decode a given array |
||
469 | * @param array $values |
||
470 | * |
||
471 | * @return array |
||
472 | */ |
||
473 | private function decodeArray($values) { |
||
474 | foreach($values as $key => $value) { |
||
475 | $values[$key] = $this->decode($value); |
||
476 | } |
||
477 | return $values; |
||
478 | } |
||
479 | |||
480 | /** |
||
481 | * Try to extract the priority from a given raw header string |
||
482 | */ |
||
483 | private function findPriority() { |
||
507 | } |
||
508 | |||
509 | /** |
||
510 | * Extract a given part as address array from a given header |
||
511 | * @param $values |
||
512 | * |
||
513 | * @return array |
||
514 | */ |
||
515 | private function decodeAddresses($values) { |
||
538 | } |
||
539 | |||
540 | /** |
||
541 | * Extract a given part as address array from a given header |
||
542 | * @param object $header |
||
543 | */ |
||
544 | private function extractAddresses($header) { |
||
545 | foreach(['from', 'to', 'cc', 'bcc', 'reply_to', 'sender'] as $key){ |
||
546 | if (property_exists($header, $key)) { |
||
547 | $this->set($key, $this->parseAddresses($header->$key)); |
||
548 | } |
||
549 | } |
||
550 | } |
||
551 | |||
552 | /** |
||
553 | * Parse Addresses |
||
554 | * @param $list |
||
555 | * |
||
556 | * @return array |
||
557 | */ |
||
558 | private function parseAddresses($list) { |
||
598 | } |
||
599 | |||
600 | /** |
||
601 | * Search and extract potential header extensions |
||
602 | */ |
||
603 | private function extractHeaderExtensions(){ |
||
629 | } |
||
630 | } |
||
631 | } |
||
632 | } |
||
633 | } |
||
634 | } |
||
635 | } |
||
636 | |||
637 | /** |
||
638 | * Exception handling for invalid dates |
||
639 | * |
||
640 | * Currently known invalid formats: |
||
641 | * ^ Datetime ^ Problem ^ Cause |
||
642 | * | Mon, 20 Nov 2017 20:31:31 +0800 (GMT+8:00) | Double timezone specification | A Windows feature |
||
643 | * | Thu, 8 Nov 2018 08:54:58 -0200 (-02) | |
||
644 | * | | and invalid timezone (max 6 char) | |
||
645 | * | 04 Jan 2018 10:12:47 UT | Missing letter "C" | Unknown |
||
646 | * | Thu, 31 May 2018 18:15:00 +0800 (added by) | Non-standard details added by the | Unknown |
||
647 | * | | mail server | |
||
648 | * | Sat, 31 Aug 2013 20:08:23 +0580 | Invalid timezone | PHPMailer bug https://sourceforge.net/p/phpmailer/mailman/message/6132703/ |
||
649 | * |
||
650 | * Please report any new invalid timestamps to [#45](https://github.com/Webklex/php-imap/issues) |
||
651 | * |
||
652 | * @param object $header |
||
653 | * |
||
654 | * @throws InvalidMessageDateException |
||
655 | */ |
||
656 | private function parseDate($header) { |
||
693 | } |
||
694 | } |
||
695 | |||
696 | /** |
||
697 | * Get all available attributes |
||
698 | * |
||
699 | * @return array |
||
700 | */ |
||
701 | public function getAttributes() { |
||
703 | } |
||
704 | |||
706 |
When comparing two booleans, it is generally considered safer to use the strict comparison operator.