Completed
Push — master ( 425e93...6bab95 )
by Zaahid
04:57
created

Base64Stream   A

Complexity

Total Complexity 25

Size/Duplication

Total Lines 203
Duplicated Lines 0 %

Test Coverage

Coverage 98.36%

Importance

Changes 0
Metric Value
eloc 50
dl 0
loc 203
ccs 60
cts 61
cp 0.9836
rs 10
c 0
b 0
f 0
wmc 25

13 Methods

Rating   Name   Duplication   Size   Complexity  
A tell() 0 3 1
A eof() 0 3 2
A getSize() 0 3 1
A __construct() 0 4 1
A isSeekable() 0 3 1
A seek() 0 3 1
A readToBase64Offset() 0 11 4
A write() 0 14 2
A close() 0 4 1
A detach() 0 4 1
A read() 0 10 3
A fillBuffer() 0 10 4
A beforeClose() 0 5 3
1
<?php
0 ignored issues
show
Coding Style introduced by
The PHP open tag does not have a corresponding PHP close tag
Loading history...
Coding Style introduced by
Filename "Base64Stream.php" doesn't match the expected filename "base64stream.php"
Loading history...
2
/**
3
 * This file is part of the ZBateson\StreamDecorators project.
4
 *
5
 * @license http://opensource.org/licenses/bsd-license.php BSD
0 ignored issues
show
Coding Style introduced by
The tag in position 1 should be the @package tag
Loading history...
6
 */
0 ignored issues
show
Coding Style introduced by
PHP version not specified
Loading history...
Coding Style introduced by
There must be exactly one blank line after the file comment
Loading history...
Coding Style Documentation introduced by
Missing @package tag in file comment
Loading history...
Coding Style Documentation introduced by
Missing @subpackage tag in file comment
Loading history...
Coding Style introduced by
Missing @category tag in file comment
Loading history...
Coding Style introduced by
Missing @author tag in file comment
Loading history...
Coding Style introduced by
Missing @link tag in file comment
Loading history...
7
namespace ZBateson\StreamDecorators;
8
9
use Psr\Http\Message\StreamInterface;
10
use GuzzleHttp\Psr7\StreamDecoratorTrait;
11
use GuzzleHttp\Psr7\BufferStream;
12
use RuntimeException;
13
14
/**
15
 * GuzzleHttp\Psr7 stream decoder extension for base64 streams.
16
 *
17
 * @author Zaahid Bateson
0 ignored issues
show
Coding Style Documentation introduced by
@author tag is not allowed in class comment
Loading history...
Coding Style introduced by
Content of the @author tag must be in the form "Display Name <[email protected]>"
Loading history...
18
 */
0 ignored issues
show
Coding Style introduced by
Missing @category tag in class comment
Loading history...
Coding Style introduced by
Missing @package tag in class comment
Loading history...
Coding Style introduced by
Missing @license tag in class comment
Loading history...
Coding Style introduced by
Missing @link tag in class comment
Loading history...
19
class Base64Stream implements StreamInterface
20
{
0 ignored issues
show
Coding Style introduced by
Opening brace should be on the same line as the declaration for class Base64Stream
Loading history...
21
    use StreamDecoratorTrait;
22
23
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
24
     * @var BufferStream buffered bytes
25
     */
26
    private $buffer;
0 ignored issues
show
Coding Style introduced by
Private member variable "buffer" must contain a leading underscore
Loading history...
Coding Style introduced by
Private member variable "buffer" must be prefixed with an underscore
Loading history...
27
28
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
29
     * @var string remainder of write operation if the bytes didn't align to 3
30
     *      bytes
31
     */
32
    private $remainder = '';
0 ignored issues
show
Coding Style introduced by
Private member variable "remainder" must contain a leading underscore
Loading history...
Coding Style introduced by
Private member variable "remainder" must be prefixed with an underscore
Loading history...
33
34
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
35
     * @var int current number of read/written bytes (for tell())
36
     */
37
    private $position = 0;
0 ignored issues
show
Coding Style introduced by
Private member variable "position" must contain a leading underscore
Loading history...
Coding Style introduced by
Private member variable "position" must be prefixed with an underscore
Loading history...
38
39
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
40
     * @param StreamInterface $stream
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
41
     */
42 9
    public function __construct(StreamInterface $stream)
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines before function; 1 found
Loading history...
43
    {
0 ignored issues
show
Coding Style introduced by
Opening brace should be on the same line as the declaration
Loading history...
44 9
        $this->stream = $stream;
45 9
        $this->buffer = new BufferStream();
46 9
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
Coding Style introduced by
Expected 1 blank line before closing function brace; 0 found
Loading history...
Coding Style introduced by
Expected //end __construct()
Loading history...
47
48
    /**
49
     * Returns the current position of the file read/write pointer
50
     *
51
     * @return int
0 ignored issues
show
Coding Style introduced by
Expected "integer" but found "int" for function return type
Loading history...
52
     */
53 1
    public function tell()
54
    {
0 ignored issues
show
Coding Style introduced by
Opening brace should be on the same line as the declaration
Loading history...
55 1
        return $this->position;
56
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
Coding Style introduced by
Expected 1 blank line before closing function brace; 0 found
Loading history...
Coding Style introduced by
Expected //end tell()
Loading history...
57
58
    /**
59
     * Returns null, getSize isn't supported
60
     *
61
     * @return null
62
     */
63 2
    public function getSize()
64
    {
0 ignored issues
show
Coding Style introduced by
Opening brace should be on the same line as the declaration
Loading history...
65 2
        return null;
0 ignored issues
show
Coding Style introduced by
TRUE, FALSE and NULL should be uppercase as per the configured coding-style; instead of null please use NULL.
Loading history...
66
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
Coding Style introduced by
Expected 1 blank line before closing function brace; 0 found
Loading history...
Coding Style introduced by
Expected //end getSize()
Loading history...
67
68
    /**
69
     * Not implemented (yet).
70
     *
71
     * Seek position can be calculated.
72
     *
73
     * @param int $offset
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
Coding Style introduced by
Expected "integer" but found "int" for parameter type
Loading history...
Coding Style introduced by
Tag value indented incorrectly; expected 2 spaces but found 1
Loading history...
74
     * @param int $whence
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
Coding Style introduced by
Expected "integer" but found "int" for parameter type
Loading history...
Coding Style introduced by
Tag value indented incorrectly; expected 2 spaces but found 1
Loading history...
75
     * @throws RuntimeException
0 ignored issues
show
Coding Style introduced by
Tag cannot be grouped with parameter tags in a doc comment
Loading history...
introduced by
Comment missing for @throws tag in function comment
Loading history...
76
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
77 1
    public function seek($offset, $whence = SEEK_SET)
0 ignored issues
show
Coding Style introduced by
Type hint "int" missing for $offset
Loading history...
Coding Style introduced by
Type hint "int" missing for $whence
Loading history...
78
    {
0 ignored issues
show
Coding Style introduced by
Opening brace should be on the same line as the declaration
Loading history...
79 1
        throw new RuntimeException('Cannot seek a Base64Stream');
80
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
Coding Style introduced by
Expected 1 blank line before closing function brace; 0 found
Loading history...
Coding Style introduced by
Expected //end seek()
Loading history...
81
82
    /**
83
     * Overridden to return false
84
     *
85
     * @return boolean
86
     */
87 1
    public function isSeekable()
88
    {
0 ignored issues
show
Coding Style introduced by
Opening brace should be on the same line as the declaration
Loading history...
89 1
        return false;
0 ignored issues
show
Coding Style introduced by
TRUE, FALSE and NULL should be uppercase as per the configured coding-style; instead of false please use FALSE.
Loading history...
90
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
Coding Style introduced by
Expected 1 blank line before closing function brace; 0 found
Loading history...
Coding Style introduced by
Expected //end isSeekable()
Loading history...
91
92
    /**
93
     * Returns true if the end of stream has been reached.
94
     *
95
     * @return boolean
96
     */
97 7
    public function eof()
98
    {
0 ignored issues
show
Coding Style introduced by
Opening brace should be on the same line as the declaration
Loading history...
99 7
        return ($this->buffer->eof() && $this->stream->eof());
0 ignored issues
show
Coding Style introduced by
Boolean operators are not allowed outside of control structure conditions
Loading history...
100
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
Coding Style introduced by
Expected 1 blank line before closing function brace; 0 found
Loading history...
Coding Style introduced by
Expected //end eof()
Loading history...
101
102
    /**
103
     * Checks if the passed bytes contain a multiple of four valid base64 bytes
104
     * after stripping invalid characters, and reads as many additional bytes as
105
     * needed until a multiple of four or the end of stream is reached.
106
     * 
107
     * @param string $bytes
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
Coding Style introduced by
Tag value indented incorrectly; expected 2 spaces but found 1
Loading history...
108
     * @return string
0 ignored issues
show
Coding Style introduced by
Tag cannot be grouped with parameter tags in a doc comment
Loading history...
109
     */
110 7
    private function readToBase64Offset($bytes)
0 ignored issues
show
Coding Style introduced by
Private method name "Base64Stream::readToBase64Offset" must be prefixed with an underscore
Loading history...
Coding Style introduced by
Type hint "string" missing for $bytes
Loading history...
111
    {
0 ignored issues
show
Coding Style introduced by
Opening brace should be on the same line as the declaration
Loading history...
112 7
        $raw = preg_replace('~[^A-Za-z0-9\+/\=]+~', '', $bytes);
113 7
        while (strlen($raw) % 4 !== 0) {
0 ignored issues
show
Coding Style Performance introduced by
The use of strlen() inside a loop condition is not allowed; assign the return value to a variable and use the variable in the loop condition instead
Loading history...
Coding Style introduced by
Arithmetic operation must be bracketed
Loading history...
114 1
            $b = $this->stream->read(1);
115 1
            if ($b === false || $b === '') {
0 ignored issues
show
Coding Style introduced by
TRUE, FALSE and NULL should be uppercase as per the configured coding-style; instead of false please use FALSE.
Loading history...
116
                return $raw;
117
            }
118 1
            $raw .= preg_replace('~[^A-Za-z0-9\+/\=]+~', '', $b);
119
        }
120 7
        return $raw;
121
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
Coding Style introduced by
Expected 1 blank line before closing function brace; 0 found
Loading history...
Coding Style introduced by
Expected //end readToBase64Offset()
Loading history...
122
123
    /**
124
     * Fills the internal byte buffer after reading and decoding data from the
125
     * underlying stream.
126
     *
127
     * @param int $length
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
Coding Style introduced by
Expected "integer" but found "int" for parameter type
Loading history...
128
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
129 7
    private function fillBuffer($length)
0 ignored issues
show
Coding Style introduced by
Private method name "Base64Stream::fillBuffer" must be prefixed with an underscore
Loading history...
Coding Style introduced by
Type hint "int" missing for $length
Loading history...
130
    {
0 ignored issues
show
Coding Style introduced by
Opening brace should be on the same line as the declaration
Loading history...
131 7
        $fill = 8192;
132 7
        while ($this->buffer->getSize() < $length) {
133 7
            $read = $this->stream->read($fill);
134 7
            if ($read === false || $read === '') {
0 ignored issues
show
Coding Style introduced by
TRUE, FALSE and NULL should be uppercase as per the configured coding-style; instead of false please use FALSE.
Loading history...
135 6
                break;
136
            }
137 7
            $decoded = base64_decode($this->readToBase64Offset($read));
138 7
            $this->buffer->write($decoded);
139
        }
140 7
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
Coding Style introduced by
Expected 1 blank line before closing function brace; 0 found
Loading history...
Coding Style introduced by
Expected //end fillBuffer()
Loading history...
141
142
    /**
143
     * Attempts to read $length bytes after decoding them, and returns them.
144
     *
145
     * Note that reading and writing to the same stream may result in wrongly
146
     * encoded data and is not supported.
147
     *
148
     * @param int $length
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
Coding Style introduced by
Expected "integer" but found "int" for parameter type
Loading history...
Coding Style introduced by
Tag value indented incorrectly; expected 2 spaces but found 1
Loading history...
149
     * @return string
0 ignored issues
show
Coding Style introduced by
Tag cannot be grouped with parameter tags in a doc comment
Loading history...
150
     */
151 7
    public function read($length)
0 ignored issues
show
Coding Style introduced by
Type hint "int" missing for $length
Loading history...
152
    {
0 ignored issues
show
Coding Style introduced by
Opening brace should be on the same line as the declaration
Loading history...
153
        // let Guzzle decide what to do.
0 ignored issues
show
Coding Style Documentation introduced by
Inline comments must start with a capital letter
Loading history...
154 7
        if ($length <= 0 || $this->eof()) {
155 1
            return $this->stream->read($length);
156
        }
157 7
        $this->fillBuffer($length);
158 7
        $ret = $this->buffer->read($length);
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 13 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
159 7
        $this->position += strlen($ret);
160 7
        return $ret;
161
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
Coding Style introduced by
Expected 1 blank line before closing function brace; 0 found
Loading history...
Coding Style introduced by
Expected //end read()
Loading history...
162
163
    /**
164
     * Writes the passed string to the underlying stream after encoding it to
165
     * base64.
166
     *
167
     * Base64Stream::close or detach must be called.  Failing to do so may
168
     * result in 1-2 bytes missing from the end of the stream if there's a
169
     * remainder.  Note that the default Stream destructor calls close as well.
170
     *
171
     * Note that reading and writing to the same stream may result in wrongly
172
     * encoded data and is not supported.
173
     *
174
     * @param string $string
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
Coding Style introduced by
Tag value indented incorrectly; expected 2 spaces but found 1
Loading history...
175
     * @return int the number of bytes written
0 ignored issues
show
Coding Style introduced by
Tag cannot be grouped with parameter tags in a doc comment
Loading history...
Coding Style introduced by
Expected "integer" but found "int" for function return type
Loading history...
176
     */
177 2
    public function write($string)
0 ignored issues
show
Coding Style introduced by
Type hint "string" missing for $string
Loading history...
178
    {
0 ignored issues
show
Coding Style introduced by
Opening brace should be on the same line as the declaration
Loading history...
179 2
        $bytes = $this->remainder . $string;
0 ignored issues
show
Coding Style introduced by
Concat operator must not be surrounded by spaces
Loading history...
180 2
        $len = strlen($bytes);
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 3 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
181 2
        if (($len % 3) !== 0) {
182 2
            $this->remainder = substr($bytes, -($len % 3));
183 2
            $bytes = substr($bytes, 0, $len - ($len % 3));
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 11 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
Coding Style introduced by
Arithmetic operation must be bracketed
Loading history...
184
        } else {
185 2
            $this->remainder = '';
186
        }
187 2
        $this->stream->write(base64_encode($bytes));
188 2
        $written = strlen($string);
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 9 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
189 2
        $this->position += $len;
190 2
        return $written;
191
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
Coding Style introduced by
Expected 1 blank line before closing function brace; 0 found
Loading history...
Coding Style introduced by
Expected //end write()
Loading history...
192
193
    /**
194
     * Writes out any remaining bytes at the end of the stream and closes.
195
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
196 2
    private function beforeClose()
0 ignored issues
show
Coding Style introduced by
Private method name "Base64Stream::beforeClose" must be prefixed with an underscore
Loading history...
197
    {
0 ignored issues
show
Coding Style introduced by
Opening brace should be on the same line as the declaration
Loading history...
198 2
        if ($this->isWritable() && $this->remainder !== '') {
199 2
            $this->stream->write(base64_encode($this->remainder));
200 2
            $this->remainder = '';
201
        }
202 2
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
Coding Style introduced by
Expected 1 blank line before closing function brace; 0 found
Loading history...
Coding Style introduced by
Expected //end beforeClose()
Loading history...
203
204
    /**
205
     * Closes the underlying stream after writing out any remaining bytes
206
     * needing to be encoded.
207
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
208 1
    public function close()
209
    {
0 ignored issues
show
Coding Style introduced by
Opening brace should be on the same line as the declaration
Loading history...
210 1
        $this->beforeClose();
211 1
        $this->stream->close();
212 1
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
Coding Style introduced by
Expected 1 blank line before closing function brace; 0 found
Loading history...
Coding Style introduced by
Expected //end close()
Loading history...
213
214
    /**
215
     * Detaches the underlying stream after writing out any remaining bytes
216
     * needing to be encoded.
217
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
218 1
    public function detach()
219
    {
0 ignored issues
show
Coding Style introduced by
Opening brace should be on the same line as the declaration
Loading history...
220 1
        $this->beforeClose();
221 1
        $this->stream->detach();
222 1
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 0 found
Loading history...
Coding Style introduced by
Expected 1 blank line before closing function brace; 0 found
Loading history...
Coding Style introduced by
Expected //end detach()
Loading history...
223
}
0 ignored issues
show
Coding Style introduced by
Expected //end class
Loading history...
Coding Style introduced by
As per coding style, files should not end with a newline character.

This check marks files that end in a newline character, i.e. an empy line.

Loading history...
224