Completed
Push — master ( 7a0fb6...891643 )
by Asmir
24:50 queued 22:48
created

HeaderDecoder::convertCharset()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 5
c 0
b 0
f 0
ccs 0
cts 2
cp 0
rs 9.4285
cc 1
eloc 2
nc 1
nop 3
crap 2
1
<?php
2
3
namespace Goetas\Mail\ToSwiftMailParser\Mime;
4
5
class HeaderDecoder
6
{
7
    public static $decodeWindows1252 = false;
8
9
    public function decode($string)
10
    {
11
        /* Take out any spaces between multiple encoded words. */
12
        $string = preg_replace('|\?=\s+=\?|', '?==?', $string);
13
14
        $out = '';
15
        $old_pos = 0;
16
17
        while (($pos = strpos($string, '=?', $old_pos)) !== false) {
18
            /* Save any preceding text. */
19
            $out .= substr($string, $old_pos, $pos - $old_pos);
20
21
            /* Search for first delimiting question mark (charset). */
22
            if (($d1 = strpos($string, '?', $pos + 2)) === false) {
23
                break;
24
            }
25
26
            $orig_charset = substr($string, $pos + 2, $d1 - $pos - 2);
27
            if (self::$decodeWindows1252 && mb_strtolower($orig_charset) == 'iso-8859-1') {
28
                $orig_charset = 'windows-1252';
29
            }
30
31
            /* Search for second delimiting question mark (encoding). */
32
            if (($d2 = strpos($string, '?', $d1 + 1)) === false) {
33
                break;
34
            }
35
36
            $encoding = substr($string, $d1 + 1, $d2 - $d1 - 1);
37
38
            /* Search for end of encoded data. */
39
            if (($end = strpos($string, '?=', $d2 + 1)) === false) {
40
                break;
41
            }
42
43
            $encoded_text = substr($string, $d2 + 1, $end - $d2 - 1);
44
45
            switch ($encoding) {
46
                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...
47
                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...
48
                    $out .= self::convertCharset(preg_replace_callback('/=([0-9a-f]{2})/i', function ($ord) {
49
                        return chr(hexdec($ord [1]));
50
                    }, str_replace('_', ' ', $encoded_text)), $orig_charset, 'UTF-8');
51
                    break;
52
53
                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...
54
                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...
55
                    $out .= self::convertCharset(base64_decode($encoded_text), $orig_charset, 'UTF-8');
56
                    break;
57
58
                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...
59
                    // Ignore unknown encoding.
60
                    break;
61
            }
62
63
            $old_pos = $end + 2;
64
        }
65
66
        return $out . substr($string, $old_pos);
67
    }
68
69
    public static function convertCharset($str, $orig, $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...
70
    {
71
        //@todo convert charset
72
        return $str;
73
    }
74
}
75