Completed
Push — master ( 487db1...396d03 )
by Zaahid
02:25
created

QuotedPrintableDecodeStreamFilter::getLines()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 18
Code Lines 13

Duplication

Lines 18
Ratio 100 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 18
loc 18
rs 9.4285
cc 3
eloc 13
nc 4
nop 1
1
<?php
2
/**
3
 * This file is part of the ZBateson\MailMimeParser project.
4
 *
5
 * @license http://opensource.org/licenses/bsd-license.php BSD
6
 */
7
namespace ZBateson\MailMimeParser\Stream;
8
9
use php_user_filter;
10
11
/**
12
 * @author Zaahid Bateson
13
 */
14
class QuotedPrintableDecodeStreamFilter extends php_user_filter
15
{
16
    /**
17
     * Name used when registering with stream_filter_register.
18
     */
19
    const STREAM_FILTER_NAME = 'convert.quoted-printable-decode';
20
    
21
    /**
22
     * @var string Leftovers from the last incomplete line that was parsed, to
23
     *      be prepended to the next line read.
24
     */
25
    private $leftover = '';
26
    
27
    /**
28
     * Returns an array of complete lines (including line endings) from the 
29
     * passed $bucket object.
30
     * 
31
     * If the last line on $bucket is incomplete, it's assigned to
32
     * $this->leftover and prepended to the first element of the first line in
33
     * the next call to getLines.
34
     * 
35
     * @param object $bucket
36
     * @return string[]
37
     */
38 View Code Duplication
    private function getLines($bucket)
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...
39
    {
40
        $lines = preg_split(
41
            '/([\r\n]+)/',
42
            $bucket->data,
43
            -1,
44
            PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_NO_EMPTY
45
        );
46
        if (!empty($this->leftover)) {
47
            $lines[0] = $this->leftover . $lines[0];
48
            $this->leftover = '';
49
        }
50
        $last = end($lines);
51
        if ($last[strlen($last) - 1] !== "\n") {
52
            $this->leftover = array_pop($lines);
53
        }
54
        return $lines;
55
    }
56
    
57
    /**
58
     * Filters the lines in the passed $lines array, returning a concatenated
59
     * string of decoded lines.
60
     * 
61
     * @param array $lines
62
     * @param int $consumed
63
     * @return string
64
     */
65 View Code Duplication
    private function filterBucketLines(array $lines, &$consumed)
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...
66
    {
67
        $data = '';
68
        foreach ($lines as $line) {
69
            $consumed += strlen($line);
70
            $data .= $line;
71
        }
72
        $decoded = quoted_printable_decode($data);
73
        return $decoded;
74
    }
75
    
76
    /**
77
     * Filter implementation converts encoding before returning PSFS_PASS_ON.
78
     * 
79
     * @param resource $in
80
     * @param resource $out
81
     * @param int $consumed
82
     * @param bool $closing
83
     * @return int
84
     */
85 View Code Duplication
    public function filter($in, $out, &$consumed, $closing)
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...
86
    {
87
        while ($bucket = stream_bucket_make_writeable($in)) {
88
            $lines = $this->getLines($bucket);
89
            $converted = $this->filterBucketLines($lines, $consumed);
90
            stream_bucket_append($out, stream_bucket_new($this->stream, $converted));
0 ignored issues
show
Bug introduced by
The property stream does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
91
        }
92
        return PSFS_PASS_ON;
93
    }
94
}
95