Skip to content

Commit 67196e8

Browse files
committed
Improve type definitions and update to PHPStan level max
1 parent 9234da0 commit 67196e8

24 files changed

+508
-260
lines changed

README.md

+16-16
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ from this source stream.
111111
The event receives a single mixed argument for incoming data.
112112

113113
```php
114-
$stream->on('data', function ($data) {
114+
$stream->on('data', function (mixed $data): void {
115115
echo $data;
116116
});
117117
```
@@ -142,7 +142,7 @@ The `end` event will be emitted once the source stream has successfully
142142
reached the end of the stream (EOF).
143143

144144
```php
145-
$stream->on('end', function () {
145+
$stream->on('end', function (): void {
146146
echo 'END';
147147
});
148148
```
@@ -180,7 +180,7 @@ trying to read from this stream.
180180
The event receives a single `Exception` argument for the error instance.
181181

182182
```php
183-
$server->on('error', function (Exception $e) {
183+
$server->on('error', function (Exception $e): void {
184184
echo 'Error: ' . $e->getMessage() . PHP_EOL;
185185
});
186186
```
@@ -213,7 +213,7 @@ stream which should result in the same error processing.
213213
The `close` event will be emitted once the stream closes (terminates).
214214

215215
```php
216-
$stream->on('close', function () {
216+
$stream->on('close', function (): void {
217217
echo 'CLOSED';
218218
});
219219
```
@@ -312,7 +312,7 @@ Re-attach the data source after a previous `pause()`.
312312
```php
313313
$stream->pause();
314314

315-
Loop::addTimer(1.0, function () use ($stream) {
315+
Loop::addTimer(1.0, function () use ($stream): void {
316316
$stream->resume();
317317
});
318318
```
@@ -362,7 +362,7 @@ you'll have to manually close the destination stream:
362362

363363
```php
364364
$source->pipe($dest);
365-
$source->on('close', function () use ($dest) {
365+
$source->on('close', function () use ($dest): void {
366366
$dest->end('BYE!');
367367
});
368368
```
@@ -456,7 +456,7 @@ The `drain` event will be emitted whenever the write buffer became full
456456
previously and is now ready to accept more data.
457457

458458
```php
459-
$stream->on('drain', function () use ($stream) {
459+
$stream->on('drain', function () use ($stream): void {
460460
echo 'Stream is now ready to accept more data';
461461
});
462462
```
@@ -478,11 +478,11 @@ The event receives a single `ReadableStreamInterface` argument for the
478478
source stream.
479479

480480
```php
481-
$stream->on('pipe', function (ReadableStreamInterface $source) use ($stream) {
481+
$stream->on('pipe', function (ReadableStreamInterface $source) use ($stream): void {
482482
echo 'Now receiving piped data';
483483

484484
// explicitly close target if source emits an error
485-
$source->on('error', function () use ($stream) {
485+
$source->on('error', function () use ($stream): void {
486486
$stream->close();
487487
});
488488
});
@@ -536,7 +536,7 @@ stream which should result in the same error processing.
536536
The `close` event will be emitted once the stream closes (terminates).
537537

538538
```php
539-
$stream->on('close', function () {
539+
$stream->on('close', function (): void {
540540
echo 'CLOSED';
541541
});
542542
```
@@ -746,7 +746,7 @@ stream in order to stop waiting for the stream to flush its final data.
746746

747747
```php
748748
$stream->end();
749-
Loop::addTimer(1.0, function () use ($stream) {
749+
Loop::addTimer(1.0, function () use ($stream): void {
750750
$stream->close();
751751
});
752752
```
@@ -831,10 +831,10 @@ readable mode or a stream such as `STDIN`:
831831

832832
```php
833833
$stream = new ReadableResourceStream(STDIN);
834-
$stream->on('data', function ($chunk) {
834+
$stream->on('data', function (string $chunk): void {
835835
echo $chunk;
836836
});
837-
$stream->on('end', function () {
837+
$stream->on('end', function (): void {
838838
echo 'END';
839839
});
840840
```
@@ -1133,7 +1133,7 @@ The callback function is allowed to throw an `Exception`. In this case,
11331133
the stream will emit an `error` event and then [`close()`](#close-1) the stream.
11341134

11351135
```php
1136-
$through = new ThroughStream(function ($data) {
1136+
$through = new ThroughStream(function (mixed $data): string {
11371137
if (!is_string($data)) {
11381138
throw new \UnexpectedValueException('Only strings allowed');
11391139
}
@@ -1164,7 +1164,7 @@ $stdout = new WritableResourceStream(STDOUT);
11641164

11651165
$stdio = new CompositeStream($stdin, $stdout);
11661166

1167-
$stdio->on('data', function ($chunk) use ($stdio) {
1167+
$stdio->on('data', function (string $chunk) use ($stdio): void {
11681168
$stdio->write('You said: ' . $chunk);
11691169
});
11701170
```
@@ -1243,7 +1243,7 @@ If you do not want to run these, they can simply be skipped like this:
12431243
vendor/bin/phpunit --exclude-group internet
12441244
```
12451245

1246-
On top of this, we use PHPStan on level 5 to ensure type safety across the project:
1246+
On top of this, we use PHPStan on max level to ensure type safety across the project:
12471247

12481248
```bash
12491249
vendor/bin/phpstan

examples/01-http.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,10 @@
2626

2727
$stream = new DuplexResourceStream($resource);
2828

29-
$stream->on('data', function ($chunk) {
29+
$stream->on('data', function (string $chunk): void {
3030
echo $chunk;
3131
});
32-
$stream->on('close', function () {
32+
$stream->on('close', function (): void {
3333
echo '[CLOSED]' . PHP_EOL;
3434
});
3535

examples/02-https.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,10 @@
2626

2727
$stream = new DuplexResourceStream($resource);
2828

29-
$stream->on('data', function ($chunk) {
29+
$stream->on('data', function (string $chunk): void {
3030
echo $chunk;
3131
});
32-
$stream->on('close', function () {
32+
$stream->on('close', function (): void {
3333
echo '[CLOSED]' . PHP_EOL;
3434
});
3535

examples/91-benchmark-throughput.php

+9-3
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,11 @@
2222

2323
$args = getopt('i:o:t:');
2424
$if = $args['i'] ?? '/dev/zero';
25+
assert(is_string($if));
2526
$of = $args['o'] ?? '/dev/null';
27+
assert(is_string($of));
2628
$t = $args['t'] ?? 1;
29+
assert(is_numeric($t));
2730

2831
// passing file descriptors requires mapping paths (https://bugs.php.net/bug.php?id=53465)
2932
$if = str_replace('/dev/fd/', 'php://fd/', $if);
@@ -38,18 +41,21 @@
3841

3942
// setup input and output streams and pipe inbetween
4043
$fh = fopen($if, 'r');
44+
assert(is_resource($fh));
45+
$fo = fopen($of, 'w');
46+
assert(is_resource($fo));
4147
$in = new React\Stream\ReadableResourceStream($fh);
42-
$out = new React\Stream\WritableResourceStream(fopen($of, 'w'));
48+
$out = new React\Stream\WritableResourceStream($fo);
4349
$in->pipe($out);
4450

4551
// stop input stream in $t seconds
4652
$start = microtime(true);
47-
$timeout = Loop::addTimer($t, function () use ($in) {
53+
$timeout = Loop::addTimer((float) $t, function () use ($in): void {
4854
$in->close();
4955
});
5056

5157
// print stream position once stream closes
52-
$in->on('close', function () use ($fh, $start, $timeout, $info) {
58+
$in->on('close', function () use ($fh, $start, $timeout, $info): void {
5359
$t = microtime(true) - $start;
5460
Loop::cancelTimer($timeout);
5561

phpstan.neon.dist

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
parameters:
2-
level: 5
2+
level: max
33

44
paths:
55
- examples/

src/CompositeStream.php

+5
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,13 @@
66

77
final class CompositeStream extends EventEmitter implements DuplexStreamInterface
88
{
9+
/** @var ReadableStreamInterface */
910
private $readable;
11+
12+
/** @var WritableStreamInterface */
1013
private $writable;
14+
15+
/** @var bool */
1116
private $closed = false;
1217

1318
public function __construct(ReadableStreamInterface $readable, WritableStreamInterface $writable)

src/DuplexResourceStream.php

+18-5
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99

1010
final class DuplexResourceStream extends EventEmitter implements DuplexStreamInterface
1111
{
12+
/** @var resource */
1213
private $stream;
1314

1415
/** @var LoopInterface */
@@ -31,11 +32,20 @@ final class DuplexResourceStream extends EventEmitter implements DuplexStreamInt
3132
* @var int
3233
*/
3334
private $bufferSize;
35+
36+
/** @var WritableStreamInterface */
3437
private $buffer;
3538

39+
/** @var bool */
3640
private $readable = true;
41+
42+
/** @var bool */
3743
private $writable = true;
44+
45+
/** @var bool */
3846
private $closing = false;
47+
48+
/** @var bool */
3949
private $listening = false;
4050

4151
/**
@@ -78,13 +88,13 @@ public function __construct($stream, ?LoopInterface $loop = null, ?int $readChun
7888
$this->bufferSize = $readChunkSize ?? 65536;
7989
$this->buffer = $buffer;
8090

81-
$this->buffer->on('error', function ($error) {
91+
$this->buffer->on('error', function (\Exception $error): void {
8292
$this->emit('error', [$error]);
8393
});
8494

8595
$this->buffer->on('close', [$this, 'close']);
8696

87-
$this->buffer->on('drain', function () {
97+
$this->buffer->on('drain', function (): void {
8898
$this->emit('drain');
8999
});
90100

@@ -167,11 +177,14 @@ public function pipe(WritableStreamInterface $dest, array $options = []): Writab
167177
return Util::pipe($this, $dest, $options);
168178
}
169179

170-
/** @internal */
171-
public function handleData($stream)
180+
/**
181+
* @internal
182+
* @param resource $stream
183+
*/
184+
public function handleData($stream): void
172185
{
173186
$error = null;
174-
\set_error_handler(function ($errno, $errstr, $errfile, $errline) use (&$error): bool {
187+
\set_error_handler(function (int $errno, string $errstr, string $errfile, int $errline) use (&$error): bool {
175188
$error = new \ErrorException(
176189
$errstr,
177190
0,

src/ReadableResourceStream.php

+5-2
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,10 @@ final class ReadableResourceStream extends EventEmitter implements ReadableStrea
3737
*/
3838
private $bufferSize;
3939

40+
/** @var bool */
4041
private $closed = false;
42+
43+
/** @var bool */
4144
private $listening = false;
4245

4346
/**
@@ -121,10 +124,10 @@ public function close(): void
121124
}
122125

123126
/** @internal */
124-
public function handleData()
127+
public function handleData(): void
125128
{
126129
$error = null;
127-
\set_error_handler(function ($errno, $errstr, $errfile, $errline) use (&$error): bool {
130+
\set_error_handler(function (int $errno, string $errstr, string $errfile, int $errline) use (&$error): bool {
128131
$error = new \ErrorException(
129132
$errstr,
130133
0,

src/ReadableStreamInterface.php

+7-7
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
* The event receives a single mixed argument for incoming data.
1818
*
1919
* ```php
20-
* $stream->on('data', function ($data) {
20+
* $stream->on('data', function (mixed $data): void {
2121
* echo $data;
2222
* });
2323
* ```
@@ -47,7 +47,7 @@
4747
* reached the end of the stream (EOF).
4848
*
4949
* ```php
50-
* $stream->on('end', function () {
50+
* $stream->on('end', function (): void {
5151
* echo 'END';
5252
* });
5353
* ```
@@ -84,7 +84,7 @@
8484
* The event receives a single `Exception` argument for the error instance.
8585
*
8686
* ```php
87-
* $stream->on('error', function (Exception $e) {
87+
* $stream->on('error', function (Exception $e): void {
8888
* echo 'Error: ' . $e->getMessage() . PHP_EOL;
8989
* });
9090
* ```
@@ -116,7 +116,7 @@
116116
* The `close` event will be emitted once the stream closes (terminates).
117117
*
118118
* ```php
119-
* $stream->on('close', function () {
119+
* $stream->on('close', function (): void {
120120
* echo 'CLOSED';
121121
* });
122122
* ```
@@ -236,7 +236,7 @@ public function pause(): void;
236236
* ```php
237237
* $stream->pause();
238238
*
239-
* Loop::addTimer(1.0, function () use ($stream) {
239+
* Loop::addTimer(1.0, function () use ($stream): void {
240240
* $stream->resume();
241241
* });
242242
* ```
@@ -287,7 +287,7 @@ public function resume(): void;
287287
*
288288
* ```php
289289
* $source->pipe($dest);
290-
* $source->on('close', function () use ($dest) {
290+
* $source->on('close', function () use ($dest): void {
291291
* $dest->end('BYE!');
292292
* });
293293
* ```
@@ -319,7 +319,7 @@ public function resume(): void;
319319
* a `pipe` event with this source stream an event argument.
320320
*
321321
* @param WritableStreamInterface $dest
322-
* @param array $options
322+
* @param array{end?:bool} $options
323323
* @return WritableStreamInterface $dest stream as-is
324324
*/
325325
public function pipe(WritableStreamInterface $dest, array $options = []): WritableStreamInterface;

0 commit comments

Comments
 (0)