Rfc2047::decode()   B
last analyzed

Complexity

Conditions 7
Paths 14

Size

Total Lines 35

Duplication

Lines 35
Ratio 100 %

Importance

Changes 0
Metric Value
cc 7
nc 14
nop 2
dl 35
loc 35
rs 8.4266
c 0
b 0
f 0
1
<?php
2
namespace Fwlib\Util\Algorithm;
3
4
/**
5
 * Rfc2047 util
6
 *
7
 * RFC 2047 - MIME (Multipurpose Internet Mail Extensions) Part Thr
8
 * @link    http://www.faqs.org/rfcs/rfc2047
9
 * @link    http://www.php.net/imap_utf8
10
 *
11
 * Usually used in mail header, attachment name etc.
12
 *
13
 * @copyright   Copyright 2013-2015 Fwolf
14
 * @license     http://www.gnu.org/licenses/lgpl.html LGPL-3.0+
15
 */
16
class Rfc2047
17
{
18
    /**
19
     * Decode string
20
     *
21
     * @param   string  $str
22
     * @param   string  $encoding
23
     * @return  string
24
     */
25 View Code Duplication
    public function decode($str, $encoding = 'utf-8')
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
26
    {
27
        // Find string encoding
28
        $ar = [];
29
        preg_match_all('/=\?(.{3,13})\?([B|Q])\?([^\?]*)\?\=/i', $str, $ar);
30
        // 0 is all-string pattern, 1 is encoding, 2 is string to base64_decode
31
        $i = count($ar[0]);
32
        if (0 < $i) {
33
            // Got match, process
34
            for ($j = 0; $j < count($i); $j++) {
0 ignored issues
show
Performance Best Practice introduced by
It seems like you are calling the size function count() as part of the test condition. You might want to compute the size beforehand, and not on each iteration.

If the size of the collection does not change during the iteration, it is generally a good practice to compute it beforehand, and not on each iteration:

for ($i=0; $i<count($array); $i++) { // calls count() on each iteration
}

// Better
for ($i=0, $c=count($array); $i<$c; $i++) { // calls count() just once
}
Loading history...
35
                $s = '';
36
                if ('B' == strtoupper($ar[2][$j])) {
37
                    // Decode base64 first
38
                    $s = base64_decode($ar[3][$j]);
39
                } elseif ('Q' == strtoupper($ar[2][$j])) {
40
                    // quoted-printable encoding ? its format like '=0D=0A'
41
                    $s = quoted_printable_decode($ar[3][$j]);
42
                }
43
44
                // Then convert string to charset ordered
45
                if ($encoding != strtolower($ar[1][$j])) {
46
                    $s = mb_convert_encoding($s, $encoding, $ar[1][$j]);
47
                }
48
49
                // Then replace into original string
50
                if (!empty($s)) {
51
                    $str = str_replace($ar[0][$j], $s, $str);
52
                }
53
            }
54
            return $str;
55
        } else {
56
            // No match, return original string
57
            return $str;
58
        }
59
    }
60
61
62
    /**
63
     * Encode string
64
     *
65
     * No break in string(B encoding mode instead of Q, see
66
     * phpmailer::EncodeHeader, line 1156), because that possible
67
     * break chinese chars.
68
     *
69
     * @param   string  $str
70
     * @param   string  $encoding
71
     * @return  string
72
     */
73
    public function encode($str, $encoding = 'utf-8')
74
    {
75
        return "=?" . $encoding . "?B?" . base64_encode($str) . "?=";
76
    }
77
}
78