Skip to content

Commit

Permalink
Merge pull request #12 from phpfui/PHP71
Browse files Browse the repository at this point in the history
Modernize to PHP 7.1
  • Loading branch information
zbateson committed Jan 11, 2023
2 parents e1bd072 + 25958d2 commit 7466ff4
Show file tree
Hide file tree
Showing 22 changed files with 439 additions and 500 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ jobs:
fail-fast: false
matrix:
os: [ubuntu-latest, windows-latest]
php: [8.2, 8.1, 8.0, 7.4, 7.3, 7.2, 7.1, 7.0, 5.6, 5.5, 5.4]
php: [8.2, 8.1, 8.0, 7.4, 7.3, 7.2, 7.1]
stability: [prefer-stable]

name: P${{ matrix.php }} - ${{ matrix.stability }} - ${{ matrix.os }}
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ composer require zbateson/stream-decorators

## Requirements

StreamDecorators requires PHP 5.4 or newer. Tested on PHP 5.4, 5.5, 5.6, 7, 7.1, 7.2, 7.3, 7.4 and 8.0.
StreamDecorators requires PHP 7.1 or newer. Tested on PHP 7.1, 7.2, 7.3, 7.4, 8.0, 8.1 and 8.2.

## Usage

Expand Down
8 changes: 5 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,14 @@
}
],
"require": {
"php": ">=5.4",
"guzzlehttp/psr7": "^1.7.0|^2.0",
"php": ">=7.1",
"guzzlehttp/psr7": "^1.9 | ^2.0",
"zbateson/mb-wrapper": "^1.0.0"
},
"require-dev": {
"sanmai/phpunit-legacy-adapter": "^6.3 || ^8"
"phpunit/phpunit": "<=9.0",
"friendsofphp/php-cs-fixer": "*",
"phpstan/phpstan": "*"
},
"autoload": {
"psr-4": {
Expand Down
13 changes: 13 additions & 0 deletions phpstan.neon
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
parameters:
level: 6
errorFormat: raw
editorUrl: '%%file%% %%line%% %%column%%: %%error%%'
paths:
- src
- tests
ignoreErrors:
-
message: '#::seek\(\) has no return type specified.#'
paths:
- src/*

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
Loading

0 comments on commit 7466ff4

Please sign in to comment.