Skip to content

Commit 7466ff4

Browse files
authored
Merge pull request #12 from phpfui/PHP71
Modernize to PHP 7.1
2 parents e1bd072 + 25958d2 commit 7466ff4

22 files changed

Lines changed: 439 additions & 500 deletions

.github/workflows/tests.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ jobs:
99
fail-fast: false
1010
matrix:
1111
os: [ubuntu-latest, windows-latest]
12-
php: [8.2, 8.1, 8.0, 7.4, 7.3, 7.2, 7.1, 7.0, 5.6, 5.5, 5.4]
12+
php: [8.2, 8.1, 8.0, 7.4, 7.3, 7.2, 7.1]
1313
stability: [prefer-stable]
1414

1515
name: P${{ matrix.php }} - ${{ matrix.stability }} - ${{ matrix.os }}

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ composer require zbateson/stream-decorators
2222

2323
## Requirements
2424

25-
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.
25+
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.
2626

2727
## Usage
2828

composer.json

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,14 @@
99
}
1010
],
1111
"require": {
12-
"php": ">=5.4",
13-
"guzzlehttp/psr7": "^1.7.0|^2.0",
12+
"php": ">=7.1",
13+
"guzzlehttp/psr7": "^1.9 | ^2.0",
1414
"zbateson/mb-wrapper": "^1.0.0"
1515
},
1616
"require-dev": {
17-
"sanmai/phpunit-legacy-adapter": "^6.3 || ^8"
17+
"phpunit/phpunit": "<=9.0",
18+
"friendsofphp/php-cs-fixer": "*",
19+
"phpstan/phpstan": "*"
1820
},
1921
"autoload": {
2022
"psr-4": {

phpstan.neon

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
parameters:
2+
level: 6
3+
errorFormat: raw
4+
editorUrl: '%%file%% %%line%% %%column%%: %%error%%'
5+
paths:
6+
- src
7+
- tests
8+
ignoreErrors:
9+
-
10+
message: '#::seek\(\) has no return type specified.#'
11+
paths:
12+
- src/*
13+

src/Base64Stream.php

Lines changed: 24 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,12 @@
44
*
55
* @license http://opensource.org/licenses/bsd-license.php BSD
66
*/
7+
78
namespace ZBateson\StreamDecorators;
89

9-
use Psr\Http\Message\StreamInterface;
10-
use GuzzleHttp\Psr7\StreamDecoratorTrait;
1110
use GuzzleHttp\Psr7\BufferStream;
11+
use GuzzleHttp\Psr7\StreamDecoratorTrait;
12+
use Psr\Http\Message\StreamInterface;
1213
use RuntimeException;
1314

1415
/**
@@ -64,9 +65,6 @@ class Base64Stream implements StreamInterface
6465
*/
6566
private $stream;
6667

67-
/**
68-
* @param StreamInterface $stream
69-
*/
7068
public function __construct(StreamInterface $stream)
7169
{
7270
$this->stream = $stream;
@@ -75,10 +73,8 @@ public function __construct(StreamInterface $stream)
7573

7674
/**
7775
* Returns the current position of the file read/write pointer
78-
*
79-
* @return int
8076
*/
81-
public function tell()
77+
public function tell() : int
8278
{
8379
return $this->position;
8480
}
@@ -88,7 +84,7 @@ public function tell()
8884
*
8985
* @return null
9086
*/
91-
public function getSize()
87+
public function getSize() : ?int
9288
{
9389
return null;
9490
}
@@ -109,20 +105,16 @@ public function seek($offset, $whence = SEEK_SET)
109105

110106
/**
111107
* Overridden to return false
112-
*
113-
* @return boolean
114108
*/
115-
public function isSeekable()
109+
public function isSeekable() : bool
116110
{
117111
return false;
118112
}
119113

120114
/**
121115
* Returns true if the end of stream has been reached.
122-
*
123-
* @return boolean
124116
*/
125-
public function eof()
117+
public function eof() : bool
126118
{
127119
return ($this->buffer->eof() && $this->stream->eof());
128120
}
@@ -134,18 +126,16 @@ public function eof()
134126
* Note that it's expected the underlying stream will only contain valid
135127
* base64 characters (normally the stream should be wrapped in a
136128
* PregReplaceFilterStream to filter out non-base64 characters).
137-
*
138-
* @param int $length
139129
*/
140-
private function fillBuffer($length)
130+
private function fillBuffer(int $length) : void
141131
{
142132
$fill = 8192;
143133
while ($this->buffer->getSize() < $length) {
144134
$read = $this->stream->read($fill);
145-
if ($read === false || $read === '') {
135+
if ($read === '') {
146136
break;
147137
}
148-
$this->buffer->write(base64_decode($read));
138+
$this->buffer->write(\base64_decode($read));
149139
}
150140
}
151141

@@ -166,7 +156,7 @@ public function read($length)
166156
}
167157
$this->fillBuffer($length);
168158
$ret = $this->buffer->read($length);
169-
$this->position += strlen($ret);
159+
$this->position += \strlen($ret);
170160
return $ret;
171161
}
172162

@@ -184,52 +174,50 @@ public function read($length)
184174
* @param string $string
185175
* @return int the number of bytes written
186176
*/
187-
public function write($string)
177+
public function write($string) : int
188178
{
189179
$bytes = $this->remainder . $string;
190-
$len = strlen($bytes);
180+
$len = \strlen($bytes);
191181
if (($len % 3) !== 0) {
192-
$this->remainder = substr($bytes, -($len % 3));
193-
$bytes = substr($bytes, 0, $len - ($len % 3));
182+
$this->remainder = \substr($bytes, -($len % 3));
183+
$bytes = \substr($bytes, 0, $len - ($len % 3));
194184
} else {
195185
$this->remainder = '';
196186
}
197-
$this->stream->write(base64_encode($bytes));
198-
$written = strlen($string);
187+
$this->stream->write(\base64_encode($bytes));
188+
$written = \strlen($string);
199189
$this->position += $len;
200190
return $written;
201191
}
202192

203193
/**
204194
* Writes out any remaining bytes at the end of the stream and closes.
205195
*/
206-
private function beforeClose()
196+
private function beforeClose() : void
207197
{
208198
if ($this->isWritable() && $this->remainder !== '') {
209-
$this->stream->write(base64_encode($this->remainder));
199+
$this->stream->write(\base64_encode($this->remainder));
210200
$this->remainder = '';
211201
}
212202
}
213203

214204
/**
215-
* Closes the underlying stream after writing out any remaining bytes
216-
* needing to be encoded.
217-
* @return void
205+
* @inheritDoc
218206
*/
219-
public function close()
207+
public function close() : void
220208
{
221209
$this->beforeClose();
222210
$this->stream->close();
223211
}
224212

225213
/**
226-
* Detaches the underlying stream after writing out any remaining bytes
227-
* needing to be encoded.
228-
* @return resource|null Underlying PHP stream, if any
214+
* @inheritDoc
229215
*/
230216
public function detach()
231217
{
232218
$this->beforeClose();
233219
$this->stream->detach();
220+
221+
return null;
234222
}
235223
}

src/CharsetStream.php

Lines changed: 13 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,13 @@
44
*
55
* @license http://opensource.org/licenses/bsd-license.php BSD
66
*/
7+
78
namespace ZBateson\StreamDecorators;
89

9-
use Psr\Http\Message\StreamInterface;
1010
use GuzzleHttp\Psr7\StreamDecoratorTrait;
11-
use ZBateson\MbWrapper\MbWrapper;
11+
use Psr\Http\Message\StreamInterface;
1212
use RuntimeException;
13+
use ZBateson\MbWrapper\MbWrapper;
1314

1415
/**
1516
* GuzzleHttp\Psr7 stream decoder extension for charset conversion.
@@ -63,7 +64,7 @@ class CharsetStream implements StreamInterface
6364
* @param string $stringCharset The charset to encode strings to (or
6465
* expected for write)
6566
*/
66-
public function __construct(StreamInterface $stream, $streamCharset = 'ISO-8859-1', $stringCharset = 'UTF-8')
67+
public function __construct(StreamInterface $stream, string $streamCharset = 'ISO-8859-1', string $stringCharset = 'UTF-8')
6768
{
6869
$this->stream = $stream;
6970
$this->converter = new MbWrapper();
@@ -73,10 +74,8 @@ public function __construct(StreamInterface $stream, $streamCharset = 'ISO-8859-
7374

7475
/**
7576
* Overridden to return the position in the target encoding.
76-
*
77-
* @return int
7877
*/
79-
public function tell()
78+
public function tell() : int
8079
{
8180
return $this->position;
8281
}
@@ -86,7 +85,7 @@ public function tell()
8685
*
8786
* @return null
8887
*/
89-
public function getSize()
88+
public function getSize() : ?int
9089
{
9190
return null;
9291
}
@@ -105,10 +104,8 @@ public function seek($offset, $whence = SEEK_SET)
105104

106105
/**
107106
* Overridden to return false
108-
*
109-
* @return boolean
110107
*/
111-
public function isSeekable()
108+
public function isSeekable() : bool
112109
{
113110
return false;
114111
}
@@ -120,15 +117,13 @@ public function isSeekable()
120117
* Aligning to 4 bytes seemed to solve an issue reading from UTF-16LE
121118
* streams and pass testReadUtf16LeToEof, although the buffered string
122119
* should've solved that on its own.
123-
*
124-
* @param int $length
125120
*/
126-
private function readRawCharsIntoBuffer($length)
121+
private function readRawCharsIntoBuffer(int $length) : void
127122
{
128-
$n = (int) ceil(($length + 32) / 4.0) * 4;
123+
$n = (int) \ceil(($length + 32) / 4.0) * 4;
129124
while ($this->bufferLength < $n) {
130125
$raw = $this->stream->read($n + 512);
131-
if ($raw === false || $raw === '') {
126+
if ($raw === '') {
132127
return;
133128
}
134129
$this->buffer .= $raw;
@@ -138,10 +133,8 @@ private function readRawCharsIntoBuffer($length)
138133

139134
/**
140135
* Returns true if the end of stream has been reached.
141-
*
142-
* @return boolean
143136
*/
144-
public function eof()
137+
public function eof() : bool
145138
{
146139
return ($this->bufferLength === 0 && $this->stream->eof());
147140
}
@@ -160,7 +153,7 @@ public function read($length)
160153
return $this->stream->read($length);
161154
}
162155
$this->readRawCharsIntoBuffer($length);
163-
$numChars = min([$this->bufferLength, $length]);
156+
$numChars = \min([$this->bufferLength, $length]);
164157
$chars = $this->converter->getSubstr($this->buffer, $this->streamCharset, 0, $numChars);
165158

166159
$this->position += $numChars;
@@ -177,7 +170,7 @@ public function read($length)
177170
* @param string $string
178171
* @return int the number of bytes written
179172
*/
180-
public function write($string)
173+
public function write($string) : int
181174
{
182175
$converted = $this->converter->convert($string, $this->stringCharset, $this->streamCharset);
183176
$written = $this->converter->getLength($converted, $this->streamCharset);

0 commit comments

Comments
 (0)