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

ChunkSplitStream::write()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 1
dl 0
loc 5
ccs 4
cts 4
cp 1
crap 1
rs 10
c 0
b 0
f 0
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 "ChunkSplitStream.php" doesn't match the expected filename "chunksplitstream.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
12
/**
13
 * Inserts line ending characters after the set number of characters have been
14
 * written to the underlying stream.
15
 *
16
 * @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...
17
 */
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...
18
class ChunkSplitStream implements StreamInterface
19
{
0 ignored issues
show
Coding Style introduced by
Opening brace should be on the same line as the declaration for class ChunkSplitStream
Loading history...
20
    use StreamDecoratorTrait;
21
22
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
23
     * @var int Number of bytes written, and importantly, if non-zero, writes a
24
     *      final $lineEnding on close (and so maintained instead of using
25
     *      tell() directly)
26
     */
27
    private $position;
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...
28
29
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
30
     * @var int The number of characters in a line before inserting $lineEnding.
31
     */
32
    private $lineLength;
0 ignored issues
show
Coding Style introduced by
Private member variable "lineLength" must contain a leading underscore
Loading history...
Coding Style introduced by
Private member variable "lineLength" 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 string The line ending characters to insert.
36
     */
37
    private $lineEnding;
0 ignored issues
show
Coding Style introduced by
Private member variable "lineEnding" must contain a leading underscore
Loading history...
Coding Style introduced by
Private member variable "lineEnding" 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
     * @var int The strlen() of $lineEnding
41
     */
42
    private $lineEndingLength;
0 ignored issues
show
Coding Style introduced by
Private member variable "lineEndingLength" must contain a leading underscore
Loading history...
Coding Style introduced by
Private member variable "lineEndingLength" must be prefixed with an underscore
Loading history...
43
44
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
45
     * @param StreamInterface $stream
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
46
     * @param int $lineLength
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
Coding Style introduced by
Expected 13 spaces after parameter type; 1 found
Loading history...
Coding Style introduced by
Expected "integer" but found "int" for parameter type
Loading history...
47
     * @param string $lineEnding
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
Coding Style introduced by
Expected 10 spaces after parameter type; 1 found
Loading history...
48
     */
49 2
    public function __construct(StreamInterface $stream, $lineLength = 76, $lineEnding = "\r\n")
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines before function; 1 found
Loading history...
Coding Style introduced by
Type hint "int" missing for $lineLength
Loading history...
Coding Style introduced by
Type hint "string" missing for $lineEnding
Loading history...
50
    {
0 ignored issues
show
Coding Style introduced by
Opening brace should be on the same line as the declaration
Loading history...
51 2
        $this->stream = $stream;
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...
52 2
        $this->lineLength = $lineLength;
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 7 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...
53 2
        $this->lineEnding = $lineEnding;
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 7 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...
54 2
        $this->lineEndingLength = strlen($this->lineEnding);
55 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 __construct()
Loading history...
56
57
    /**
58
     * Inserts the line ending character after each line length characters in
59
     * the passed string, making sure previously written bytes are taken into
60
     * account.
61
     *
62
     * @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...
63
     * @return string
0 ignored issues
show
Coding Style introduced by
Tag cannot be grouped with parameter tags in a doc comment
Loading history...
64
     */
65 2
    private function getChunkedString($string)
0 ignored issues
show
Coding Style introduced by
Private method name "ChunkSplitStream::getChunkedString" must be prefixed with an underscore
Loading history...
Coding Style introduced by
Type hint "string" missing for $string
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 2
        $firstLine = '';
68 2
        if ($this->tell() !== 0) {
69 2
            $next = $this->lineLength - ($this->position % ($this->lineLength + $this->lineEndingLength));
0 ignored issues
show
Coding Style introduced by
Arithmetic operation must be bracketed
Loading history...
70 2
            if (strlen($string) > $next) {
71 2
                $firstLine = substr($string, 0, $next) . $this->lineEnding;
0 ignored issues
show
Coding Style introduced by
Concat operator must not be surrounded by spaces
Loading history...
72 2
                $string = substr($string, $next);
0 ignored issues
show
Coding Style introduced by
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...
73
            }
74
        }
75
        // chunk_split always ends with the passed line ending
0 ignored issues
show
Coding Style Documentation introduced by
Inline comments must start with a capital letter
Loading history...
Coding Style introduced by
Inline comments must end in full-stops, exclamation marks, or question marks
Loading history...
76 2
        $chunked = $firstLine . chunk_split($string, $this->lineLength, $this->lineEnding);
0 ignored issues
show
Coding Style introduced by
Concat operator must not be surrounded by spaces
Loading history...
77 2
        return substr($chunked, 0, strlen($chunked) - $this->lineEndingLength);
0 ignored issues
show
Coding Style introduced by
Arithmetic operation must be bracketed
Loading history...
78
    }
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 getChunkedString()
Loading history...
79
80
    /**
81
     * Writes the passed string to the underlying stream, ensuring line endings
82
     * are inserted every "line length" characters in the string.
83
     *
84
     * @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...
85
     * @return 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...
86
     */
87 2
    public function write($string)
0 ignored issues
show
Coding Style introduced by
Type hint "string" missing for $string
Loading history...
88
    {
0 ignored issues
show
Coding Style introduced by
Opening brace should be on the same line as the declaration
Loading history...
89 2
        $chunked = $this->getChunkedString($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...
90 2
        $this->position += strlen($chunked);
91 2
        return $this->stream->write($chunked);
92
    }
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...
93
94
    /**
95
     * Inserts a final line ending character.
96
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
97 2
    private function beforeClose()
0 ignored issues
show
Coding Style introduced by
Private method name "ChunkSplitStream::beforeClose" must be prefixed with an underscore
Loading history...
98
    {
0 ignored issues
show
Coding Style introduced by
Opening brace should be on the same line as the declaration
Loading history...
99 2
        if ($this->position !== 0) {
100 2
            $this->stream->write($this->lineEnding);
101
        }
102 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...
103
104
    /**
105
     * Closes the stream after ensuring a final line ending character is
106
     * inserted.
107
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
108 2
    public function close()
109
    {
0 ignored issues
show
Coding Style introduced by
Opening brace should be on the same line as the declaration
Loading history...
110 2
        $this->beforeClose();
111 2
        $this->stream->close();
112 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 close()
Loading history...
113
114
    /**
115
     * Detaches the stream after ensuring a final line ending character is
116
     * inserted.
117
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
118
    public function detach()
119
    {
0 ignored issues
show
Coding Style introduced by
Opening brace should be on the same line as the declaration
Loading history...
120
        $this->beforeClose();
121
        $this->stream->detach();
122
    }
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...
123
}
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...
124