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

Base64DecodeStreamFilter::filterLine()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 10
Code Lines 7

Duplication

Lines 10
Ratio 100 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 10
loc 10
rs 9.4285
cc 3
eloc 7
nc 3
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 Base64DecodeStreamFilter extends php_user_filter
15
{
16
    /**
17
     * Name used when registering with stream_filter_register.
18
     */
19
    const STREAM_FILTER_NAME = 'convert.base64-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
    private function getRawBytes($bucket)
39
    {
40
        $raw = preg_replace('/\s+/', '', $bucket->data);
41
        if (!empty($this->leftover)) {
42
            $raw = $this->leftover . $raw;
43
            $this->leftover = '';
44
        }
45
        $nLeftover = strlen($raw) % 3;
46
        if ($nLeftover !== 0) {
47
            $this->leftover = substr($nLeftover, -$nLeftover);
48
        }
49
        return $raw;
50
    }
51
52
    /**
53
     * Filters a single line of encoded input.  Returns NULL if the end has been
54
     * reached.
55
     * 
56
     * @param string $line
57
     * @return string the decoded line
58
     */
59 View Code Duplication
    private function filterLine($line)
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...
60
    {
61
        $cur = trim($line);
62
        if ($this->isEmptyOrStartLine($cur)) {
0 ignored issues
show
Bug introduced by
The method isEmptyOrStartLine() does not seem to exist on object<ZBateson\MailMime...se64DecodeStreamFilter>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
63
            return '';
64
        } elseif ($this->isEndLine($cur)) {
0 ignored issues
show
Bug introduced by
The method isEndLine() does not seem to exist on object<ZBateson\MailMime...se64DecodeStreamFilter>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
65
            return null;
66
        }
67
        return convert_uudecode($cur);
68
    }
69
    
70
    /**
71
     * Filters the lines in the passed $lines array, returning a concatenated
72
     * string of decoded lines.
73
     * 
74
     * @param array $lines
75
     * @param int $consumed
76
     * @return string
77
     */
78 View Code Duplication
    private function filterBucketBytes(array $lines, &$consumed)
0 ignored issues
show
Unused Code introduced by
This method is not used, and could be removed.
Loading history...
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...
79
    {
80
        $data = '';
81
        foreach ($lines as $line) {
82
            $consumed += strlen($line);
83
            $filtered = $this->filterLine($line);
84
            if ($filtered === null) {
85
                break;
86
            }
87
            $data .= $filtered;
88
        }
89
        return $data;
90
    }
91
    
92
    /**
93
     * Filter implementation converts encoding before returning PSFS_PASS_ON.
94
     * 
95
     * @param resource $in
96
     * @param resource $out
97
     * @param int $consumed
98
     * @param bool $closing
99
     * @return int
100
     */
101
    public function filter($in, $out, &$consumed, $closing)
102
    {
103
        while ($bucket = stream_bucket_make_writeable($in)) {
104
            $bytes = $this->getRawBytes($bucket);
105
            $nConsumed = strlen($bucket->data);
106
            if ($this->leftover !== '') {
107
                $nConsumed -= $nConsumed - strlen(rtrim($bucket->data));
108
                $nConsumed -= strlen($this->leftover);
109
            }
110
            $consumed += $nConsumed;
111
            $converted = base64_decode($bytes);
112
            stream_bucket_append($out, stream_bucket_new($this->stream, $converted));
1 ignored issue
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...
113
        }
114
        return PSFS_PASS_ON;
115
    }
116
}
117