Skip to content

Commit

Permalink
Modernize to PHP 7.1
Browse files Browse the repository at this point in the history
Run PHPStan and PHPCSFixer on src
  • Loading branch information
phpfui committed Jan 11, 2023
1 parent 642797f commit 25958d2
Show file tree
Hide file tree
Showing 8 changed files with 170 additions and 254 deletions.
60 changes: 24 additions & 36 deletions src/Base64Stream.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,12 @@
*
* @license http://opensource.org/licenses/bsd-license.php BSD
*/

namespace ZBateson\StreamDecorators;

use Psr\Http\Message\StreamInterface;
use GuzzleHttp\Psr7\StreamDecoratorTrait;
use GuzzleHttp\Psr7\BufferStream;
use GuzzleHttp\Psr7\StreamDecoratorTrait;
use Psr\Http\Message\StreamInterface;
use RuntimeException;

/**
Expand Down Expand Up @@ -64,9 +65,6 @@ class Base64Stream implements StreamInterface
*/
private $stream;

/**
* @param StreamInterface $stream
*/
public function __construct(StreamInterface $stream)
{
$this->stream = $stream;
Expand All @@ -75,10 +73,8 @@ public function __construct(StreamInterface $stream)

/**
* Returns the current position of the file read/write pointer
*
* @return int
*/
public function tell()
public function tell() : int
{
return $this->position;
}
Expand All @@ -88,7 +84,7 @@ public function tell()
*
* @return null
*/
public function getSize()
public function getSize() : ?int
{
return null;
}
Expand All @@ -109,20 +105,16 @@ public function seek($offset, $whence = SEEK_SET)

/**
* Overridden to return false
*
* @return boolean
*/
public function isSeekable()
public function isSeekable() : bool
{
return false;
}

/**
* Returns true if the end of stream has been reached.
*
* @return boolean
*/
public function eof()
public function eof() : bool
{
return ($this->buffer->eof() && $this->stream->eof());
}
Expand All @@ -134,18 +126,16 @@ public function eof()
* Note that it's expected the underlying stream will only contain valid
* base64 characters (normally the stream should be wrapped in a
* PregReplaceFilterStream to filter out non-base64 characters).
*
* @param int $length
*/
private function fillBuffer($length)
private function fillBuffer(int $length) : void
{
$fill = 8192;
while ($this->buffer->getSize() < $length) {
$read = $this->stream->read($fill);
if ($read === false || $read === '') {
if ($read === '') {
break;
}
$this->buffer->write(base64_decode($read));
$this->buffer->write(\base64_decode($read));
}
}

Expand All @@ -166,7 +156,7 @@ public function read($length)
}
$this->fillBuffer($length);
$ret = $this->buffer->read($length);
$this->position += strlen($ret);
$this->position += \strlen($ret);
return $ret;
}

Expand All @@ -184,52 +174,50 @@ public function read($length)
* @param string $string
* @return int the number of bytes written
*/
public function write($string)
public function write($string) : int
{
$bytes = $this->remainder . $string;
$len = strlen($bytes);
$len = \strlen($bytes);
if (($len % 3) !== 0) {
$this->remainder = substr($bytes, -($len % 3));
$bytes = substr($bytes, 0, $len - ($len % 3));
$this->remainder = \substr($bytes, -($len % 3));
$bytes = \substr($bytes, 0, $len - ($len % 3));
} else {
$this->remainder = '';
}
$this->stream->write(base64_encode($bytes));
$written = strlen($string);
$this->stream->write(\base64_encode($bytes));
$written = \strlen($string);
$this->position += $len;
return $written;
}

/**
* Writes out any remaining bytes at the end of the stream and closes.
*/
private function beforeClose()
private function beforeClose() : void
{
if ($this->isWritable() && $this->remainder !== '') {
$this->stream->write(base64_encode($this->remainder));
$this->stream->write(\base64_encode($this->remainder));
$this->remainder = '';
}
}

/**
* Closes the underlying stream after writing out any remaining bytes
* needing to be encoded.
* @return void
* @inheritDoc
*/
public function close()
public function close() : void
{
$this->beforeClose();
$this->stream->close();
}

/**
* Detaches the underlying stream after writing out any remaining bytes
* needing to be encoded.
* @return resource|null Underlying PHP stream, if any
* @inheritDoc
*/
public function detach()
{
$this->beforeClose();
$this->stream->detach();

return null;
}
}
33 changes: 13 additions & 20 deletions src/CharsetStream.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,13 @@
*
* @license http://opensource.org/licenses/bsd-license.php BSD
*/

namespace ZBateson\StreamDecorators;

use Psr\Http\Message\StreamInterface;
use GuzzleHttp\Psr7\StreamDecoratorTrait;
use ZBateson\MbWrapper\MbWrapper;
use Psr\Http\Message\StreamInterface;
use RuntimeException;
use ZBateson\MbWrapper\MbWrapper;

/**
* GuzzleHttp\Psr7 stream decoder extension for charset conversion.
Expand Down Expand Up @@ -63,7 +64,7 @@ class CharsetStream implements StreamInterface
* @param string $stringCharset The charset to encode strings to (or
* expected for write)
*/
public function __construct(StreamInterface $stream, $streamCharset = 'ISO-8859-1', $stringCharset = 'UTF-8')
public function __construct(StreamInterface $stream, string $streamCharset = 'ISO-8859-1', string $stringCharset = 'UTF-8')
{
$this->stream = $stream;
$this->converter = new MbWrapper();
Expand All @@ -73,10 +74,8 @@ public function __construct(StreamInterface $stream, $streamCharset = 'ISO-8859-

/**
* Overridden to return the position in the target encoding.
*
* @return int
*/
public function tell()
public function tell() : int
{
return $this->position;
}
Expand All @@ -86,7 +85,7 @@ public function tell()
*
* @return null
*/
public function getSize()
public function getSize() : ?int
{
return null;
}
Expand All @@ -105,10 +104,8 @@ public function seek($offset, $whence = SEEK_SET)

/**
* Overridden to return false
*
* @return boolean
*/
public function isSeekable()
public function isSeekable() : bool
{
return false;
}
Expand All @@ -120,15 +117,13 @@ public function isSeekable()
* Aligning to 4 bytes seemed to solve an issue reading from UTF-16LE
* streams and pass testReadUtf16LeToEof, although the buffered string
* should've solved that on its own.
*
* @param int $length
*/
private function readRawCharsIntoBuffer($length)
private function readRawCharsIntoBuffer(int $length) : void
{
$n = (int) ceil(($length + 32) / 4.0) * 4;
$n = (int) \ceil(($length + 32) / 4.0) * 4;
while ($this->bufferLength < $n) {
$raw = $this->stream->read($n + 512);
if ($raw === false || $raw === '') {
if ($raw === '') {
return;
}
$this->buffer .= $raw;
Expand All @@ -138,10 +133,8 @@ private function readRawCharsIntoBuffer($length)

/**
* Returns true if the end of stream has been reached.
*
* @return boolean
*/
public function eof()
public function eof() : bool
{
return ($this->bufferLength === 0 && $this->stream->eof());
}
Expand All @@ -160,7 +153,7 @@ public function read($length)
return $this->stream->read($length);
}
$this->readRawCharsIntoBuffer($length);
$numChars = min([$this->bufferLength, $length]);
$numChars = \min([$this->bufferLength, $length]);
$chars = $this->converter->getSubstr($this->buffer, $this->streamCharset, 0, $numChars);

$this->position += $numChars;
Expand All @@ -177,7 +170,7 @@ public function read($length)
* @param string $string
* @return int the number of bytes written
*/
public function write($string)
public function write($string) : int
{
$converted = $this->converter->convert($string, $this->stringCharset, $this->streamCharset);
$written = $this->converter->getLength($converted, $this->streamCharset);
Expand Down
45 changes: 18 additions & 27 deletions src/ChunkSplitStream.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,11 @@
*
* @license http://opensource.org/licenses/bsd-license.php BSD
*/

namespace ZBateson\StreamDecorators;

use Psr\Http\Message\StreamInterface;
use GuzzleHttp\Psr7\StreamDecoratorTrait;
use Psr\Http\Message\StreamInterface;

/**
* Inserts line ending characters after the set number of characters have been
Expand Down Expand Up @@ -46,40 +47,32 @@ class ChunkSplitStream implements StreamInterface
*/
private $stream;

/**
* @param StreamInterface $stream
* @param int $lineLength
* @param string $lineEnding
*/
public function __construct(StreamInterface $stream, $lineLength = 76, $lineEnding = "\r\n")
public function __construct(StreamInterface $stream, int $lineLength = 76, string $lineEnding = "\r\n")
{
$this->stream = $stream;
$this->lineLength = $lineLength;
$this->lineEnding = $lineEnding;
$this->lineEndingLength = strlen($this->lineEnding);
$this->lineEndingLength = \strlen($this->lineEnding);
}

/**
* Inserts the line ending character after each line length characters in
* the passed string, making sure previously written bytes are taken into
* account.
*
* @param string $string
* @return string
*/
private function getChunkedString($string)
private function getChunkedString(string $string) : string
{
$firstLine = '';
if ($this->tell() !== 0) {
$next = $this->lineLength - ($this->position % ($this->lineLength + $this->lineEndingLength));
if (strlen($string) > $next) {
$firstLine = substr($string, 0, $next) . $this->lineEnding;
$string = substr($string, $next);
if (\strlen($string) > $next) {
$firstLine = \substr($string, 0, $next) . $this->lineEnding;
$string = \substr($string, $next);
}
}
// chunk_split always ends with the passed line ending
$chunked = $firstLine . chunk_split($string, $this->lineLength, $this->lineEnding);
return substr($chunked, 0, strlen($chunked) - $this->lineEndingLength);
$chunked = $firstLine . \chunk_split($string, $this->lineLength, $this->lineEnding);
return \substr($chunked, 0, \strlen($chunked) - $this->lineEndingLength);
}

/**
Expand All @@ -89,42 +82,40 @@ private function getChunkedString($string)
* @param string $string
* @return int number of bytes written
*/
public function write($string)
public function write($string) : int
{
$chunked = $this->getChunkedString($string);
$this->position += strlen($chunked);
$this->position += \strlen($chunked);
return $this->stream->write($chunked);
}

/**
* Inserts a final line ending character.
*/
private function beforeClose()
private function beforeClose() : void
{
if ($this->position !== 0) {
$this->stream->write($this->lineEnding);
}
}

/**
* Closes the stream after ensuring a final line ending character is
* inserted.
* @return void
* @inheritDoc
*/
public function close()
public function close() : void
{
$this->beforeClose();
$this->stream->close();
}

/**
* Detaches the stream after ensuring a final line ending character is
* inserted.
* @return resource|null Underlying PHP stream, if any
* @inheritDoc
*/
public function detach()
{
$this->beforeClose();
$this->stream->detach();

return null;
}
}
Loading

0 comments on commit 25958d2

Please # to comment.