zbateson /
stream-decorators
| 1 | <?php |
||
|
0 ignored issues
–
show
Coding Style
introduced
by
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
|
|||
| 6 | */ |
||
|
0 ignored issues
–
show
|
|||
| 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
Content of the @author tag must be in the form "Display Name <[email protected]>"
Loading history...
|
|||
| 17 | */ |
||
|
0 ignored issues
–
show
|
|||
| 18 | class ChunkSplitStream implements StreamInterface |
||
| 19 | { |
||
|
0 ignored issues
–
show
|
|||
| 20 | use StreamDecoratorTrait; |
||
| 21 | |||
| 22 | /** |
||
|
0 ignored issues
–
show
|
|||
| 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
|
|||
| 28 | |||
| 29 | /** |
||
|
0 ignored issues
–
show
|
|||
| 30 | * @var int The number of characters in a line before inserting $lineEnding. |
||
| 31 | */ |
||
| 32 | private $lineLength; |
||
|
0 ignored issues
–
show
|
|||
| 33 | |||
| 34 | /** |
||
|
0 ignored issues
–
show
|
|||
| 35 | * @var string The line ending characters to insert. |
||
| 36 | */ |
||
| 37 | private $lineEnding; |
||
|
0 ignored issues
–
show
|
|||
| 38 | |||
| 39 | /** |
||
|
0 ignored issues
–
show
|
|||
| 40 | * @var int The strlen() of $lineEnding |
||
| 41 | */ |
||
| 42 | private $lineEndingLength; |
||
|
0 ignored issues
–
show
|
|||
| 43 | |||
| 44 | /** |
||
|
0 ignored issues
–
show
|
|||
| 45 | * @param StreamInterface $stream |
||
|
0 ignored issues
–
show
|
|||
| 46 | * @param int $lineLength |
||
|
0 ignored issues
–
show
|
|||
| 47 | * @param string $lineEnding |
||
|
0 ignored issues
–
show
|
|||
| 48 | */ |
||
| 49 | 2 | public function __construct(StreamInterface $stream, $lineLength = 76, $lineEnding = "\r\n") |
|
|
0 ignored issues
–
show
|
|||
| 50 | { |
||
|
0 ignored issues
–
show
|
|||
| 51 | 2 | $this->stream = $stream; |
|
|
0 ignored issues
–
show
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
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
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
|
|||
| 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
|
|||
| 63 | * @return string |
||
|
0 ignored issues
–
show
|
|||
| 64 | */ |
||
| 65 | 2 | private function getChunkedString($string) |
|
|
0 ignored issues
–
show
|
|||
| 66 | { |
||
|
0 ignored issues
–
show
|
|||
| 67 | 2 | $firstLine = ''; |
|
| 68 | 2 | if ($this->tell() !== 0) { |
|
| 69 | 2 | $next = $this->lineLength - ($this->position % ($this->lineLength + $this->lineEndingLength)); |
|
|
0 ignored issues
–
show
|
|||
| 70 | 2 | if (strlen($string) > $next) { |
|
| 71 | 2 | $firstLine = substr($string, 0, $next) . $this->lineEnding; |
|
|
0 ignored issues
–
show
|
|||
| 72 | 2 | $string = substr($string, $next); |
|
|
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...
|
|||
| 73 | } |
||
| 74 | } |
||
| 75 | // chunk_split always ends with the passed line ending |
||
|
0 ignored issues
–
show
|
|||
| 76 | 2 | $chunked = $firstLine . chunk_split($string, $this->lineLength, $this->lineEnding); |
|
|
0 ignored issues
–
show
|
|||
| 77 | 2 | return substr($chunked, 0, strlen($chunked) - $this->lineEndingLength); |
|
|
0 ignored issues
–
show
|
|||
| 78 | } |
||
|
0 ignored issues
–
show
|
|||
| 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
|
|||
| 85 | * @return number of bytes written |
||
|
0 ignored issues
–
show
|
|||
| 86 | */ |
||
| 87 | 2 | public function write($string) |
|
|
0 ignored issues
–
show
|
|||
| 88 | { |
||
|
0 ignored issues
–
show
|
|||
| 89 | 2 | $chunked = $this->getChunkedString($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...
|
|||
| 90 | 2 | $this->position += strlen($chunked); |
|
| 91 | 2 | return $this->stream->write($chunked); |
|
| 92 | } |
||
|
0 ignored issues
–
show
|
|||
| 93 | |||
| 94 | /** |
||
| 95 | * Inserts a final line ending character. |
||
| 96 | */ |
||
|
0 ignored issues
–
show
|
|||
| 97 | 2 | private function beforeClose() |
|
|
0 ignored issues
–
show
|
|||
| 98 | { |
||
|
0 ignored issues
–
show
|
|||
| 99 | 2 | if ($this->position !== 0) { |
|
| 100 | 2 | $this->stream->write($this->lineEnding); |
|
| 101 | } |
||
| 102 | 2 | } |
|
|
0 ignored issues
–
show
|
|||
| 103 | |||
| 104 | /** |
||
| 105 | * Closes the stream after ensuring a final line ending character is |
||
| 106 | * inserted. |
||
| 107 | */ |
||
|
0 ignored issues
–
show
|
|||
| 108 | 2 | public function close() |
|
| 109 | { |
||
|
0 ignored issues
–
show
|
|||
| 110 | 2 | $this->beforeClose(); |
|
| 111 | 2 | $this->stream->close(); |
|
| 112 | 2 | } |
|
|
0 ignored issues
–
show
|
|||
| 113 | |||
| 114 | /** |
||
| 115 | * Detaches the stream after ensuring a final line ending character is |
||
| 116 | * inserted. |
||
| 117 | */ |
||
|
0 ignored issues
–
show
|
|||
| 118 | public function detach() |
||
| 119 | { |
||
|
0 ignored issues
–
show
|
|||
| 120 | $this->beforeClose(); |
||
| 121 | $this->stream->detach(); |
||
| 122 | } |
||
|
0 ignored issues
–
show
|
|||
| 123 | } |
||
|
0 ignored issues
–
show
|
|||
| 124 |