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

src/QuotedPrintableStream.php (149 issues)

1
<?php
0 ignored issues
show
The PHP open tag does not have a corresponding PHP close tag
Loading history...
Filename "QuotedPrintableStream.php" doesn't match the expected filename "quotedprintablestream.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
The tag in position 1 should be the @package tag
Loading history...
6
 */
0 ignored issues
show
PHP version not specified
Loading history...
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...
Missing @category tag in file comment
Loading history...
Missing @author tag in file comment
Loading history...
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 RuntimeException;
12
13
/**
14
 * GuzzleHttp\Psr7 stream decoder decorator for quoted printable streams.
15
 *
16
 * @author Zaahid Bateson
0 ignored issues
show
Coding Style Documentation introduced by
@author tag is not allowed in class comment
Loading history...
Content of the @author tag must be in the form "Display Name <[email protected]>"
Loading history...
17
 */
0 ignored issues
show
Missing @category tag in class comment
Loading history...
Missing @package tag in class comment
Loading history...
Missing @license tag in class comment
Loading history...
Missing @link tag in class comment
Loading history...
18
class QuotedPrintableStream implements StreamInterface
19
{
0 ignored issues
show
Opening brace should be on the same line as the declaration for class QuotedPrintableStream
Loading history...
20
    use StreamDecoratorTrait;
21
22
    /**
0 ignored issues
show
Missing short description in doc comment
Loading history...
23
     * @var int current read/write position
24
     */
25
    private $position = 0;
0 ignored issues
show
Private member variable "position" must contain a leading underscore
Loading history...
Private member variable "position" must be prefixed with an underscore
Loading history...
26
27
    /**
0 ignored issues
show
Missing short description in doc comment
Loading history...
28
     * @var string Last line of written text (used to maintain good line-breaks)
29
     */
30
    private $lastLine = '';
0 ignored issues
show
Private member variable "lastLine" must contain a leading underscore
Loading history...
Private member variable "lastLine" must be prefixed with an underscore
Loading history...
31
32
    /**
33
     * Overridden to return the position in the target encoding.
34
     *
35
     * @return int
0 ignored issues
show
Expected "integer" but found "int" for function return type
Loading history...
36
     */
37 3
    public function tell()
0 ignored issues
show
Expected 2 blank lines before function; 1 found
Loading history...
38
    {
0 ignored issues
show
Opening brace should be on the same line as the declaration
Loading history...
39 3
        return $this->position;
40
    }
0 ignored issues
show
Expected 2 blank lines after function; 1 found
Loading history...
Expected 1 blank line before closing function brace; 0 found
Loading history...
Expected //end tell()
Loading history...
41
42
    /**
43
     * Returns null, getSize isn't supported
44
     *
45
     * @return null
46
     */
47 1
    public function getSize()
48
    {
0 ignored issues
show
Opening brace should be on the same line as the declaration
Loading history...
49 1
        return null;
0 ignored issues
show
TRUE, FALSE and NULL should be uppercase as per the configured coding-style; instead of null please use NULL.
Loading history...
50
    }
0 ignored issues
show
Expected 2 blank lines after function; 1 found
Loading history...
Expected 1 blank line before closing function brace; 0 found
Loading history...
Expected //end getSize()
Loading history...
51
52
    /**
53
     * Not supported.
54
     *
55
     * @param int $offset
0 ignored issues
show
Missing parameter comment
Loading history...
Expected "integer" but found "int" for parameter type
Loading history...
Tag value indented incorrectly; expected 2 spaces but found 1
Loading history...
56
     * @param int $whence
0 ignored issues
show
Missing parameter comment
Loading history...
Expected "integer" but found "int" for parameter type
Loading history...
Tag value indented incorrectly; expected 2 spaces but found 1
Loading history...
57
     * @throws RuntimeException
0 ignored issues
show
Tag cannot be grouped with parameter tags in a doc comment
Loading history...
Comment missing for @throws tag in function comment
Loading history...
58
     */
0 ignored issues
show
Missing @return tag in function comment
Loading history...
59 1
    public function seek($offset, $whence = SEEK_SET)
0 ignored issues
show
Type hint "int" missing for $offset
Loading history...
Type hint "int" missing for $whence
Loading history...
60
    {
0 ignored issues
show
Opening brace should be on the same line as the declaration
Loading history...
61 1
        throw new RuntimeException('Cannot seek a QuotedPrintableStream');
62
    }
0 ignored issues
show
Expected 2 blank lines after function; 1 found
Loading history...
Expected 1 blank line before closing function brace; 0 found
Loading history...
Expected //end seek()
Loading history...
63
64
    /**
65
     * Overridden to return false
66
     *
67
     * @return boolean
68
     */
69 1
    public function isSeekable()
70
    {
0 ignored issues
show
Opening brace should be on the same line as the declaration
Loading history...
71 1
        return false;
0 ignored issues
show
TRUE, FALSE and NULL should be uppercase as per the configured coding-style; instead of false please use FALSE.
Loading history...
72
    }
0 ignored issues
show
Expected 2 blank lines after function; 1 found
Loading history...
Expected 1 blank line before closing function brace; 0 found
Loading history...
Expected //end isSeekable()
Loading history...
73
74
    /**
75
     * Reads $length chars from the underlying stream, prepending the past $pre
76
     * to it first.
77
     *
78
     * If the characters read (including the prepended $pre) contain invalid
79
     * quoted-printable characters, the underlying stream is rewound by the
80
     * total number of characters ($length + strlen($pre)).
81
     *
82
     * The quoted-printable encoded characters are returned.  If the characters
83
     * read are invalid, '3D' is returned indicating an '=' character.
84
     *
85
     * @param int $length
0 ignored issues
show
Missing parameter comment
Loading history...
Expected 4 spaces after parameter type; 1 found
Loading history...
Expected "integer" but found "int" for parameter type
Loading history...
Tag value indented incorrectly; expected 2 spaces but found 1
Loading history...
86
     * @param string $pre
0 ignored issues
show
Missing parameter comment
Loading history...
Tag value indented incorrectly; expected 2 spaces but found 1
Loading history...
87
     * @return string
0 ignored issues
show
Tag cannot be grouped with parameter tags in a doc comment
Loading history...
88
     */
89 5
    private function readEncodedChars($length, $pre = '')
0 ignored issues
show
Private method name "QuotedPrintableStream::readEncodedChars" must be prefixed with an underscore
Loading history...
Type hint "int" missing for $length
Loading history...
Type hint "string" missing for $pre
Loading history...
90
    {
0 ignored issues
show
Opening brace should be on the same line as the declaration
Loading history...
91 5
        $str = $pre . $this->stream->read($length);
0 ignored issues
show
Concat operator must not be surrounded by spaces
Loading history...
92 5
        $len = strlen($str);
93 5
        if ($len > 0 && !preg_match('/^[0-9a-f]{2}$|^[\r\n].$/is', $str) && $this->stream->isSeekable()) {
0 ignored issues
show
There must be a single space after a NOT operator; 0 found
Loading history...
94 1
            $this->stream->seek(-$len, SEEK_CUR);
95 1
            return '3D';    // '=' character
0 ignored issues
show
Comments may not appear after statements
Loading history...
96
        }
97 5
        return $str;
98
    }
0 ignored issues
show
Expected 2 blank lines after function; 1 found
Loading history...
Expected 1 blank line before closing function brace; 0 found
Loading history...
Expected //end readEncodedChars()
Loading history...
99
100
    /**
101
     * Decodes the passed $block of text.
102
     *
103
     * If the last or before last character is an '=' char, indicating the
104
     * beginning of a quoted-printable encoded char, 1 or 2 additional bytes are
105
     * read from the underlying stream respectively.
106
     *
107
     * The decoded string is returned.
108
     *
109
     * @param string $block
0 ignored issues
show
Missing parameter comment
Loading history...
Tag value indented incorrectly; expected 2 spaces but found 1
Loading history...
110
     * @return string
0 ignored issues
show
Tag cannot be grouped with parameter tags in a doc comment
Loading history...
111
     */
112 8
    private function decodeBlock($block)
0 ignored issues
show
Private method name "QuotedPrintableStream::decodeBlock" must be prefixed with an underscore
Loading history...
Type hint "string" missing for $block
Loading history...
113
    {
0 ignored issues
show
Opening brace should be on the same line as the declaration
Loading history...
114 8
        if (substr($block, -1) === '=') {
115 5
            $block .= $this->readEncodedChars(2);
116 8
        } elseif (substr($block, -2, 1) === '=') {
0 ignored issues
show
Usage of ELSEIF not allowed; use ELSE IF instead
Loading history...
117 4
            $first = substr($block, -1);
0 ignored issues
show
Equals sign not aligned with surrounding assignments; expected 2 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...
118 4
            $block = substr($block, 0, -1);
0 ignored issues
show
Equals sign not aligned with surrounding assignments; expected 2 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...
119 4
            $block .= $this->readEncodedChars(1, $first);
120
        }
121 8
        return quoted_printable_decode($block);
122
    }
0 ignored issues
show
Expected 2 blank lines after function; 1 found
Loading history...
Expected 1 blank line before closing function brace; 0 found
Loading history...
Expected //end decodeBlock()
Loading history...
123
124
    /**
0 ignored issues
show
Parameter $str should have a doc-comment as per coding-style.
Loading history...
125
     * Reads up to $length characters, appends them to the passed $str string,
126
     * and returns the total number of characters read.
127
     *
128
     * -1 is returned if there are no more bytes to read.
129
     *
130
     * @param int $length
0 ignored issues
show
Missing parameter comment
Loading history...
Expected 4 spaces after parameter type; 1 found
Loading history...
Expected "integer" but found "int" for parameter type
Loading history...
Tag value indented incorrectly; expected 2 spaces but found 1
Loading history...
131
     * @param string $append
0 ignored issues
show
Missing parameter comment
Loading history...
Doc comment for parameter $append does not match actual variable name $str
Loading history...
Tag value indented incorrectly; expected 2 spaces but found 1
Loading history...
132
     * @return int
0 ignored issues
show
Tag cannot be grouped with parameter tags in a doc comment
Loading history...
Expected "integer" but found "int" for function return type
Loading history...
133
     */
134 8
    private function readRawDecodeAndAppend($length, &$str)
0 ignored issues
show
Private method name "QuotedPrintableStream::readRawDecodeAndAppend" must be prefixed with an underscore
Loading history...
Type hint "int" missing for $length
Loading history...
Type hint "string" missing for $append
Loading history...
135
    {
0 ignored issues
show
Opening brace should be on the same line as the declaration
Loading history...
136 8
        $block = $this->stream->read($length);
137 8
        if ($block === false || $block === '') {
0 ignored issues
show
TRUE, FALSE and NULL should be uppercase as per the configured coding-style; instead of false please use FALSE.
Loading history...
138 8
            return -1;
139
        }
140 8
        $decoded = $this->decodeBlock($block);
141 8
        $count = strlen($decoded);
0 ignored issues
show
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...
142 8
        $str .= $decoded;
0 ignored issues
show
Equals sign not aligned with surrounding assignments; expected 4 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...
143 8
        return $count;
144
    }
0 ignored issues
show
Expected 2 blank lines after function; 1 found
Loading history...
Expected 1 blank line before closing function brace; 0 found
Loading history...
Expected //end readRawDecodeAndAppend()
Loading history...
145
146
    /**
147
     * Reads up to $length decoded bytes from the underlying quoted-printable
148
     * encoded stream and returns them.
149
     * 
150
     * @param int $length
0 ignored issues
show
Missing parameter comment
Loading history...
Expected "integer" but found "int" for parameter type
Loading history...
Tag value indented incorrectly; expected 2 spaces but found 1
Loading history...
151
     * @return string
0 ignored issues
show
Tag cannot be grouped with parameter tags in a doc comment
Loading history...
152
     */
153 8
    public function read($length)
0 ignored issues
show
Type hint "int" missing for $length
Loading history...
154
    {
0 ignored issues
show
Opening brace should be on the same line as the declaration
Loading history...
155
        // 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...
156 8
        if ($length <= 0 || $this->eof()) {
157
            return $this->stream->read($length);
158
        }
159 8
        $count = 0;
160 8
        $bytes = '';
161 8
        while ($count < $length) {
162 8
            $nRead = $this->readRawDecodeAndAppend($length - $count, $bytes);
0 ignored issues
show
Arithmetic operation must be bracketed
Loading history...
163 8
            if ($nRead === -1) {
164 8
                break;
165
            }
166 8
            $this->position += $nRead;
167 8
            $count += $nRead;
0 ignored issues
show
Equals sign not aligned with surrounding assignments; expected 10 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...
168
        }
169 8
        return $bytes;
170
    }
0 ignored issues
show
Expected 2 blank lines after function; 1 found
Loading history...
Expected 1 blank line before closing function brace; 0 found
Loading history...
Expected //end read()
Loading history...
171
172
    /**
173
     * Writes the passed string to the underlying stream after encoding it as
174
     * quoted-printable.
175
     *
176
     * Note that reading and writing to the same stream without rewinding is not
177
     * supported.
178
     *
179
     * @param string $string
0 ignored issues
show
Missing parameter comment
Loading history...
Tag value indented incorrectly; expected 2 spaces but found 1
Loading history...
180
     * @return int the number of bytes written
0 ignored issues
show
Tag cannot be grouped with parameter tags in a doc comment
Loading history...
Expected "integer" but found "int" for function return type
Loading history...
181
     */
182 1
    public function write($string)
0 ignored issues
show
Type hint "string" missing for $string
Loading history...
183
    {
0 ignored issues
show
Opening brace should be on the same line as the declaration
Loading history...
184 1
        $encodedLine = quoted_printable_encode($this->lastLine);
0 ignored issues
show
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...
185 1
        $lineAndString = rtrim(quoted_printable_encode($this->lastLine . $string), "\r\n");
0 ignored issues
show
Concat operator must not be surrounded by spaces
Loading history...
186 1
        $write = substr($lineAndString, strlen($encodedLine));
0 ignored issues
show
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...
187 1
        $this->stream->write($write);
188 1
        $written = strlen($string);
0 ignored issues
show
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 1
        $this->position += $written;
190
191 1
        $lpos = strrpos($lineAndString, "\n");
0 ignored issues
show
Equals sign not aligned with surrounding assignments; expected 5 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...
192 1
        $lastLine = $lineAndString;
193 1
        if ($lpos !== false) {
0 ignored issues
show
TRUE, FALSE and NULL should be uppercase as per the configured coding-style; instead of false please use FALSE.
Loading history...
194 1
            $lastLine = substr($lineAndString, $lpos + 1);
0 ignored issues
show
Arithmetic operation must be bracketed
Loading history...
195
        }
196 1
        $this->lastLine = quoted_printable_decode($lastLine);
197 1
        return $written;
198
    }
0 ignored issues
show
Expected 2 blank lines after function; 1 found
Loading history...
Expected 1 blank line before closing function brace; 0 found
Loading history...
Expected //end write()
Loading history...
199
200
    /**
201
     * Writes out a final CRLF if the current line isn't empty.
202
     */
0 ignored issues
show
Missing @return tag in function comment
Loading history...
203 1
    private function beforeClose()
0 ignored issues
show
Private method name "QuotedPrintableStream::beforeClose" must be prefixed with an underscore
Loading history...
204
    {
0 ignored issues
show
Opening brace should be on the same line as the declaration
Loading history...
205 1
        if ($this->isWritable() && $this->lastLine !== '') {
206 1
            $this->stream->write("\r\n");
207 1
            $this->lastLine = '';
208
        }
209 1
    }
0 ignored issues
show
Expected 2 blank lines after function; 1 found
Loading history...
Expected 1 blank line before closing function brace; 0 found
Loading history...
Expected //end beforeClose()
Loading history...
210
211
    /**
212
     * Closes the underlying stream and writes a final CRLF if the current line
213
     * isn't empty.
214
     */
0 ignored issues
show
Missing @return tag in function comment
Loading history...
215 1
    public function close()
216
    {
0 ignored issues
show
Opening brace should be on the same line as the declaration
Loading history...
217 1
        $this->beforeClose();
218 1
        $this->stream->close();
219 1
    }
0 ignored issues
show
Expected 2 blank lines after function; 1 found
Loading history...
Expected 1 blank line before closing function brace; 0 found
Loading history...
Expected //end close()
Loading history...
220
221
    /**
222
     * Closes the underlying stream and writes a final CRLF if the current line
223
     * isn't empty.
224
     */
0 ignored issues
show
Missing @return tag in function comment
Loading history...
225
    public function detach()
226
    {
0 ignored issues
show
Opening brace should be on the same line as the declaration
Loading history...
227
        $this->beforeClose();
228
        $this->stream->detach();
229
    }
0 ignored issues
show
Expected 2 blank lines after function; 0 found
Loading history...
Expected 1 blank line before closing function brace; 0 found
Loading history...
Expected //end detach()
Loading history...
230
}
0 ignored issues
show
Expected //end class
Loading history...
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...
231