Completed
Push — master ( 266f95...98e95c )
by Asmir
05:16 queued 02:09
created

HeaderDecoder   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 75
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Test Coverage

Coverage 71.43%

Importance

Changes 0
Metric Value
wmc 13
lcom 1
cbo 0
dl 0
loc 75
ccs 25
cts 35
cp 0.7143
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
C decode() 0 59 11
A convertCharset() 0 5 1
1
<?php
2
3
namespace Goetas\Mail\ToSwiftMailParser\Mime;
4
5
class HeaderDecoder
6
{
7
    private $decodeWindows1252 = false;
8
9 5
    public function __construct($decodeWindows1252 = false)
10
    {
11 5
        $this->decodeWindows1252 = $decodeWindows1252;
12 5
    }
13
14 5
    public function decode(string $string): string
15
    {
16
        /* Take out any spaces between multiple encoded words. */
17 5
        $string = preg_replace('|\?=\s+=\?|', '?==?', $string);
18
19 5
        $out = '';
20 5
        $old_pos = 0;
21
22 5
        while (($pos = strpos($string, '=?', $old_pos)) !== false) {
23
            /* Save any preceding text. */
24 3
            $out .= substr($string, $old_pos, $pos - $old_pos);
25
26
            /* Search for first delimiting question mark (charset). */
27 3
            if (($d1 = strpos($string, '?', $pos + 2)) === false) {
28
                break;
29
            }
30
31 3
            $orig_charset = substr($string, $pos + 2, $d1 - $pos - 2);
32 3
            if ($this->decodeWindows1252 && mb_strtolower($orig_charset) == 'iso-8859-1') {
33
                $orig_charset = 'windows-1252';
34
            }
35
36
            /* Search for second delimiting question mark (encoding). */
37 3
            if (($d2 = strpos($string, '?', $d1 + 1)) === false) {
38
                break;
39
            }
40
41 3
            $encoding = substr($string, $d1 + 1, $d2 - $d1 - 1);
42
43
            /* Search for end of encoded data. */
44 3
            if (($end = strpos($string, '?=', $d2 + 1)) === false) {
45
                break;
46
            }
47
48 3
            $encoded_text = substr($string, $d2 + 1, $end - $d2 - 1);
49
50
            switch ($encoding) {
51 3
                case 'Q' :
0 ignored issues
show
Coding Style introduced by
There must be no space before the colon in a CASE statement

As per the PSR-2 coding standard, there must not be a space in front of the colon in case statements.

switch ($selector) {
    case "A": //right
        doSomething();
        break;
    case "B" : //wrong
        doSomethingElse();
        break;
}

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
52 3
                case 'q' :
0 ignored issues
show
Coding Style introduced by
There must be no space before the colon in a CASE statement

As per the PSR-2 coding standard, there must not be a space in front of the colon in case statements.

switch ($selector) {
    case "A": //right
        doSomething();
        break;
    case "B" : //wrong
        doSomethingElse();
        break;
}

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
53
                    $out .= self::convertCharset(preg_replace_callback('/=([0-9a-f]{2})/i', function ($ord) {
54
                        return chr(hexdec($ord [1]));
55
                    }, str_replace('_', ' ', $encoded_text)), $orig_charset, 'UTF-8');
56
                    break;
57
58 3
                case 'B' :
0 ignored issues
show
Coding Style introduced by
There must be no space before the colon in a CASE statement

As per the PSR-2 coding standard, there must not be a space in front of the colon in case statements.

switch ($selector) {
    case "A": //right
        doSomething();
        break;
    case "B" : //wrong
        doSomethingElse();
        break;
}

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
59
                case 'b' :
0 ignored issues
show
Coding Style introduced by
There must be no space before the colon in a CASE statement

As per the PSR-2 coding standard, there must not be a space in front of the colon in case statements.

switch ($selector) {
    case "A": //right
        doSomething();
        break;
    case "B" : //wrong
        doSomethingElse();
        break;
}

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
60 3
                    $out .= self::convertCharset(base64_decode($encoded_text), $orig_charset, 'UTF-8');
61 3
                    break;
62
63
                default :
0 ignored issues
show
Coding Style introduced by
There must be no space before the colon in a DEFAULT statement

As per the PSR-2 coding standard, there must not be a space in front of the colon in the default statement.

switch ($expr) {
    default : //wrong
        doSomething();
        break;
}

switch ($expr) {
    default: //right
        doSomething();
        break;
}

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
64
                    // Ignore unknown encoding.
65
                    break;
66
            }
67
68 3
            $old_pos = $end + 2;
69
        }
70
71 5
        return $out . substr($string, $old_pos);
72
    }
73
74 3
    private static function convertCharset(string $str, string $orig, string $to)
0 ignored issues
show
Unused Code introduced by
The parameter $orig is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $to is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
75
    {
76
        //@todo convert charset
77 3
        return $str;
78
    }
79
}
80