Passed
Push — master ( 6bab95...ec3c3e )
by Zaahid
03:17
created

Base64Stream   A

Complexity

Total Complexity 21

Size/Duplication

Total Lines 185
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
eloc 42
dl 0
loc 185
ccs 52
cts 52
cp 1
rs 10
c 0
b 0
f 0
wmc 21

12 Methods

Rating   Name   Duplication   Size   Complexity  
A write() 0 14 2
A tell() 0 3 1
A close() 0 4 1
A detach() 0 4 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 read() 0 10 3
A fillBuffer() 0 9 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
 * Note that it's expected the underlying stream will only contain valid base64
18
 * characters (normally the stream should be wrapped in a
19
 * PregReplaceFilterStream to filter out non-base64 characters for reading).
20
 *
21
 * ```
22
 * $f = fopen(...);
23
 * $stream = new Base64Stream(new PregReplaceFilterStream(
24
 *      Psr7\stream_for($f), '/[^a-zA-Z0-9\/\+=]/', ''
25
 * ));
26
 * //...
27
 * ```
28
 *
29
 * For writing, a ChunkSplitStream could come in handy so the output is split
30
 * into lines:
31
 *
32
 * ```
33
 * $f = fopen(...);
34
 * $stream = new Base64Stream(new ChunkSplitStream(new PregReplaceFilterStream(
35
 *      Psr7\stream_for($f), '/[^a-zA-Z0-9\/\+=]/', ''
36
 * )));
37
 * //...
38
 * ```
39
 *
40
 * @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...
41
 */
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...
42
class Base64Stream implements StreamInterface
43
{
0 ignored issues
show
Coding Style introduced by
Opening brace should be on the same line as the declaration for class Base64Stream
Loading history...
44
    use StreamDecoratorTrait;
45
46
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
47
     * @var BufferStream buffered bytes
48
     */
49
    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...
50
51
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
52
     * @var string remainder of write operation if the bytes didn't align to 3
53
     *      bytes
54
     */
55
    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...
56
57
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
58
     * @var int current number of read/written bytes (for tell())
59
     */
60
    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...
61
62
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
63
     * @param StreamInterface $stream
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
64
     */
65 9
    public function __construct(StreamInterface $stream)
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines before function; 1 found
Loading history...
66
    {
0 ignored issues
show
Coding Style introduced by
Opening brace should be on the same line as the declaration
Loading history...
67 9
        $this->stream = $stream;
68 9
        $this->buffer = new BufferStream();
69 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...
70
71
    /**
72
     * Returns the current position of the file read/write pointer
73
     *
74
     * @return int
0 ignored issues
show
Coding Style introduced by
Expected "integer" but found "int" for function return type
Loading history...
75
     */
76 1
    public function tell()
77
    {
0 ignored issues
show
Coding Style introduced by
Opening brace should be on the same line as the declaration
Loading history...
78 1
        return $this->position;
79
    }
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...
80
81
    /**
82
     * Returns null, getSize isn't supported
83
     *
84
     * @return null
85
     */
86 3
    public function getSize()
87
    {
0 ignored issues
show
Coding Style introduced by
Opening brace should be on the same line as the declaration
Loading history...
88 3
        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...
89
    }
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...
90
91
    /**
92
     * Not implemented (yet).
93
     *
94
     * Seek position can be calculated.
95
     *
96
     * @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...
97
     * @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...
98
     * @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...
99
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
100 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...
101
    {
0 ignored issues
show
Coding Style introduced by
Opening brace should be on the same line as the declaration
Loading history...
102 1
        throw new RuntimeException('Cannot seek a Base64Stream');
103
    }
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...
104
105
    /**
106
     * Overridden to return false
107
     *
108
     * @return boolean
109
     */
110 1
    public function isSeekable()
111
    {
0 ignored issues
show
Coding Style introduced by
Opening brace should be on the same line as the declaration
Loading history...
112 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...
113
    }
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...
114
115
    /**
116
     * Returns true if the end of stream has been reached.
117
     *
118
     * @return boolean
119
     */
120 7
    public function eof()
121
    {
0 ignored issues
show
Coding Style introduced by
Opening brace should be on the same line as the declaration
Loading history...
122 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...
123
    }
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...
124
125
    /**
126
     * Fills the internal byte buffer after reading and decoding data from the
127
     * underlying stream.
128
     *
129
     * Note that it's expected the underlying stream will only contain valid
130
     * base64 characters (normally the stream should be wrapped in a
131
     * PregReplaceFilterStream to filter out non-base64 characters).
132
     *
133
     * @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...
134
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
135 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...
136
    {
0 ignored issues
show
Coding Style introduced by
Opening brace should be on the same line as the declaration
Loading history...
137 7
        $fill = 8192;
138 7
        while ($this->buffer->getSize() < $length) {
139 7
            $read = $this->stream->read($fill);
140 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...
141 6
                break;
142
            }
143 7
            $this->buffer->write(base64_decode($read));
144
        }
145 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...
146
147
    /**
148
     * Attempts to read $length bytes after decoding them, and returns them.
149
     *
150
     * Note that reading and writing to the same stream may result in wrongly
151
     * encoded data and is not supported.
152
     *
153
     * @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...
154
     * @return string
0 ignored issues
show
Coding Style introduced by
Tag cannot be grouped with parameter tags in a doc comment
Loading history...
155
     */
156 7
    public function read($length)
0 ignored issues
show
Coding Style introduced by
Type hint "int" missing for $length
Loading history...
157
    {
0 ignored issues
show
Coding Style introduced by
Opening brace should be on the same line as the declaration
Loading history...
158
        // 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...
159 7
        if ($length <= 0 || $this->eof()) {
160 2
            return $this->stream->read($length);
161
        }
162 7
        $this->fillBuffer($length);
163 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...
164 7
        $this->position += strlen($ret);
165 7
        return $ret;
166
    }
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...
167
168
    /**
169
     * Writes the passed string to the underlying stream after encoding it to
170
     * base64.
171
     *
172
     * Base64Stream::close or detach must be called.  Failing to do so may
173
     * result in 1-2 bytes missing from the end of the stream if there's a
174
     * remainder.  Note that the default Stream destructor calls close as well.
175
     *
176
     * Note that reading and writing to the same stream may result in wrongly
177
     * encoded data and is not supported.
178
     *
179
     * @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...
180
     * @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...
181
     */
182 2
    public function write($string)
0 ignored issues
show
Coding Style introduced by
Type hint "string" missing for $string
Loading history...
183
    {
0 ignored issues
show
Coding Style introduced by
Opening brace should be on the same line as the declaration
Loading history...
184 2
        $bytes = $this->remainder . $string;
0 ignored issues
show
Coding Style introduced by
Concat operator must not be surrounded by spaces
Loading history...
185 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...
186 2
        if (($len % 3) !== 0) {
187 2
            $this->remainder = substr($bytes, -($len % 3));
188 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...
189
        } else {
190 2
            $this->remainder = '';
191
        }
192 2
        $this->stream->write(base64_encode($bytes));
193 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...
194 2
        $this->position += $len;
195 2
        return $written;
196
    }
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...
197
198
    /**
199
     * Writes out any remaining bytes at the end of the stream and closes.
200
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
201 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...
202
    {
0 ignored issues
show
Coding Style introduced by
Opening brace should be on the same line as the declaration
Loading history...
203 2
        if ($this->isWritable() && $this->remainder !== '') {
204 2
            $this->stream->write(base64_encode($this->remainder));
205 2
            $this->remainder = '';
206
        }
207 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...
208
209
    /**
210
     * Closes the underlying stream after writing out any remaining bytes
211
     * needing to be encoded.
212
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
213 1
    public function close()
214
    {
0 ignored issues
show
Coding Style introduced by
Opening brace should be on the same line as the declaration
Loading history...
215 1
        $this->beforeClose();
216 1
        $this->stream->close();
217 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...
218
219
    /**
220
     * Detaches the underlying stream after writing out any remaining bytes
221
     * needing to be encoded.
222
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
223 1
    public function detach()
224
    {
0 ignored issues
show
Coding Style introduced by
Opening brace should be on the same line as the declaration
Loading history...
225 1
        $this->beforeClose();
226 1
        $this->stream->detach();
227 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...
228
}
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...
229