Skip to content

Commit c621bb3

Browse files
authored
Merge pull request #14 from sirn-se/phpdoc
phpdoc
2 parents 909ac13 + ec5841d commit c621bb3

File tree

10 files changed

+48
-22
lines changed

10 files changed

+48
-22
lines changed

phpstan.neon

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,8 @@ parameters:
33
paths:
44
- src
55
- tests
6-
treatPhpDocTypesAsCertain: false
6+
treatPhpDocTypesAsCertain: false
7+
exceptions:
8+
check:
9+
tooWideThrowType: true
10+
implicitThrows: false

src/Context.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,8 @@ class Context
2121
/**
2222
* Create exception.
2323
* @param open-resource|null $stream
24-
* @throws InvalidArgumentException if incorrect resource
24+
* @throws InvalidArgumentException if not a resource
25+
* @throws InvalidArgumentException if wrong resource type
2526
*/
2627
public function __construct(mixed $stream = null)
2728
{
@@ -55,6 +56,9 @@ public function getOptions(): array
5556
return stream_context_get_options($this->stream);
5657
}
5758

59+
/**
60+
* @throws StreamException on failure
61+
*/
5862
public function setOption(string $wrapper, string $option, mixed $value): self
5963
{
6064
if (!is_resource($this->stream) || !stream_context_set_option($this->stream, $wrapper, $option, $value)) {
@@ -105,6 +109,7 @@ public function setParam(string $param, mixed $value): self
105109
/**
106110
* @param array<string, mixed> $params
107111
* @deprecated Use setOptions and on- callbacks instead.
112+
* @throws StreamException on failure
108113
*/
109114
public function setParams(array $params): self
110115
{

src/SocketClient.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ public function setPersistent(bool $persistent): self
7171
* Set timeout in seconds.
7272
* @param int<0, max>|float|null $timeout
7373
* @return SocketClient
74+
* @throws InvalidArgumentException if invalid timeout
7475
*/
7576
public function setTimeout(int|float|null $timeout): self
7677
{
@@ -87,10 +88,10 @@ public function setTimeout(int|float|null $timeout): self
8788
/**
8889
* Create a connection on remote socket.
8990
* @return SocketStream The stream for opened conenction.
90-
* @throws StreamException if connection could not be created
9191
*/
9292
public function connect(): SocketStream
9393
{
94+
/** @throws StreamException if connection could not be created */
9495
$stream = $this->handler->with(function () {
9596
$error_code = $error_message = '';
9697
return stream_socket_client(

src/SocketServer.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@ class SocketServer extends Stream
2626
/**
2727
* Create new socker server instance
2828
* @param UriInterface $uri The URI to open socket on.
29+
* @throws StreamException if invalid scheme.
30+
* @throws StreamException if unsupported scheme.
2931
* @throws StreamException if unable to create socket.
3032
*/
3133
public function __construct(UriInterface $uri, Context|null $context = null)
@@ -42,6 +44,7 @@ public function __construct(UriInterface $uri, Context|null $context = null)
4244
throw new StreamException(StreamException::SCHEME_HANDLER, ['scheme' => $uri->getScheme()]);
4345
}
4446
$this->context = $context ?? new Context();
47+
/** @throws StreamException on failure */
4548
$this->stream = $this->handler->with(function () {
4649
$error_code = $error_message = '';
4750
return stream_socket_server(
@@ -143,6 +146,7 @@ public function getMetadata(string|null $key = null): mixed
143146
* Accept a connection on a socket.
144147
* @param int<0, max>|float|null $timeout Override the default socket accept timeout.
145148
* @return SocketStream|null The stream for opened conenction.
149+
* @throws InvalidArgumentException if invalid timeout
146150
* @throws StreamException if socket is closed
147151
*/
148152
public function accept(int|float|null $timeout = null): SocketStream|null
@@ -153,6 +157,7 @@ public function accept(int|float|null $timeout = null): SocketStream|null
153157
if (!is_resource($this->stream)) {
154158
throw new StreamException(StreamException::SERVER_CLOSED);
155159
}
160+
/** @throws StreamException */
156161
$stream = $this->handler->with(function () use ($timeout) {
157162
$peer_name = '';
158163
return stream_socket_accept($this->stream, $timeout, $peer_name);

src/SocketStream.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@ public function hasContents(): bool
8080
if (!is_resource($this->stream)) {
8181
return false;
8282
}
83+
/** @throws StreamException */
8384
return $this->handler->with(function () {
8485
$read = [$this->getOpenResource()];
8586
$write = $oob = [];
@@ -92,6 +93,7 @@ public function hasContents(): bool
9293
* @param int<0, max>|float $timeout Seconds to be set.
9394
* @param int|null $microseconds Microseconds to be set - deprecated
9495
* @return bool If operation was succesful.
96+
* @throws InvalidArgumentException if invalid timeout.
9597
* @throws StreamException if stream is closed.
9698
*/
9799
public function setTimeout(int|float $timeout, int|null $microseconds = null): bool
@@ -124,6 +126,7 @@ public function readLine(int $length): string|null
124126
if (!$this->readable) {
125127
throw new StreamException(StreamException::NOT_READABLE);
126128
}
129+
/** @throws StreamException */
127130
return $this->handler->with(function () use ($stream, $length) {
128131
$result = fgets($stream, $length);
129132
return $result === false ? null : $result;

src/Stream.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,7 @@ public function getMetadata(string|null $key = null): mixed
106106
*/
107107
public function tell(): int
108108
{
109+
/** @throws StreamException */
109110
return $this->handler->with(function () {
110111
return ftell($this->getOpenResource());
111112
}, new StreamException(StreamException::FAIL_TELL));
@@ -135,6 +136,7 @@ public function read(int $length): string
135136
if (!$this->readable) {
136137
throw new StreamException(StreamException::NOT_READABLE);
137138
}
139+
/** @throws StreamException */
138140
return $this->handler->with(function () use ($stream, $length) {
139141
return (string)fread($stream, $length);
140142
}, new StreamException(StreamException::FAIL_READ));
@@ -152,6 +154,7 @@ public function write(string $string): int
152154
if (!$this->writable) {
153155
throw new StreamException(StreamException::NOT_WRITABLE);
154156
}
157+
/** @throws StreamException */
155158
return $this->handler->with(function () use ($stream, $string) {
156159
return fwrite($stream, $string);
157160
}, new StreamException(StreamException::FAIL_WRITE));
@@ -237,6 +240,7 @@ public function getContents(): string
237240
if (!$this->readable) {
238241
throw new StreamException(StreamException::NOT_READABLE);
239242
}
243+
/** @throws StreamException */
240244
return $this->handler->with(function () use ($stream) {
241245
return stream_get_contents($stream);
242246
}, new StreamException(StreamException::FAIL_CONTENTS));

src/StreamCollection.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ public function getWritable(): self
104104
* Wait for redable content in stream collection.
105105
* @param int<0, max>|float $timeout Timeout in seconds.
106106
* @return self New collection instance.
107-
* @throws StreamException If fails to select.
107+
* @throws InvalidArgumentException If invalid timeout.
108108
*/
109109
public function waitRead(int|float $timeout = 60): self
110110
{

src/StreamFactory.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,6 @@ public function createStream(string $content = ''): Stream
4848
* Create a stream from an existing file.
4949
* @param string $filename The filename or stream URI to use as basis of stream.
5050
* @param string $mode The mode with which to open the underlying filename/stream.
51-
* @throws RuntimeException If the file cannot be opened.
5251
* @throws InvalidArgumentException If the mode is invalid.
5352
* @return Stream A stream instance.
5453
*/
@@ -124,6 +123,7 @@ public function createStreamCollection(): StreamCollection
124123
*/
125124
private function createResource(string $filename, string $mode)
126125
{
126+
/** @throws RuntimeException */
127127
return $this->handler->with(function () use ($filename, $mode) {
128128
/** @var resource $resource */
129129
$resource = fopen($filename, $mode);

tests/suites/ContextTest.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ public function testContextParams(): void
7676
'test-option-1-2' => 'test-option-1-2-a',
7777
],
7878
],
79-
'notification' => 'test-notification-a',
79+
'notification' => 'trim',
8080
]);
8181
$context->setParams([
8282
'options' => [
@@ -85,17 +85,17 @@ public function testContextParams(): void
8585
'test-option-1-3' => 'test-option-1-3-a',
8686
],
8787
],
88-
'notification' => 'test-notification-b',
88+
'notification' => 'ltrim',
8989
]);
90-
$context->setParam('notification', 'test-notification-c');
90+
$context->setParam('notification', 'rtrim');
9191
$context->setParam('options', [
9292
'test-wrapper-1' => [
9393
'test-option-1-4' => 'test-option-1-4-a',
9494
],
9595
]);
9696
$params = $context->getParams();
9797
$this->assertEquals([
98-
'notification' => 'test-notification-c',
98+
'notification' => 'rtrim',
9999
'options' => [
100100
'test-wrapper-1' => [
101101
'test-option-1-1' => 'test-option-1-1-b',
@@ -105,7 +105,7 @@ public function testContextParams(): void
105105
],
106106
],
107107
], $params);
108-
$this->assertEquals('test-notification-c', $context->getParam('notification'));
108+
$this->assertEquals('rtrim', $context->getParam('notification'));
109109
}
110110

111111
public function testNotifiers(): void

tests/suites/StreamTest.php

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99

1010
namespace Phrity\Net\Test;
1111

12+
use ErrorException;
1213
use InvalidArgumentException;
1314
use PHPUnit\Framework\TestCase;
1415
use Phrity\Net\{
@@ -349,10 +350,11 @@ public function testDirectoryStream(): void
349350

350351
public function testRemoteStream(): void
351352
{
352-
$remote = fopen('https://phrity.sirn.se/', 'r');
353-
if (!$remote) {
354-
$this->markTestSkipped('Could not reach online research.');
355-
}
353+
$remote = (new ErrorHandler())->with(function () {
354+
return fopen('https://phrity.sirn.se/', 'r');
355+
}, function (ErrorException $exception) {
356+
$this->markTestSkipped('Could not reach online resource.');
357+
});
356358
$stream = new Stream($remote);
357359
$this->assertEquals('http', $stream->getMetadata('wrapper_type'));
358360
$this->assertEquals('tcp_socket/ssl', $stream->getMetadata('stream_type'));
@@ -377,10 +379,11 @@ public function testRemoteStream(): void
377379

378380
public function testContext(): void
379381
{
380-
$remote = fopen('https://phrity.sirn.se/', 'r');
381-
if (!$remote) {
382-
$this->markTestSkipped('Could not reach online research.');
383-
}
382+
$remote = (new ErrorHandler())->with(function () {
383+
return fopen('https://phrity.sirn.se/', 'r');
384+
}, function (ErrorException $exception) {
385+
$this->markTestSkipped('Could not reach online resource.');
386+
});
384387
$stream = new Stream($remote);
385388
$context = $stream->getContext();
386389
$this->assertInstanceOf(Context::class, $context);
@@ -390,10 +393,11 @@ public function testContext(): void
390393

391394
public function testSeekOnRemoteError(): void
392395
{
393-
$remote = fopen('https://phrity.sirn.se/', 'r');
394-
if (!$remote) {
395-
$this->markTestSkipped('Could not reach online research.');
396-
}
396+
$remote = (new ErrorHandler())->with(function () {
397+
return fopen('https://phrity.sirn.se/', 'r');
398+
}, function (ErrorException $exception) {
399+
$this->markTestSkipped('Could not reach online resource.');
400+
});
397401
$stream = new Stream($remote);
398402
$this->expectException(StreamException::class);
399403
$this->expectExceptionCode(StreamException::NOT_SEEKABLE);

0 commit comments

Comments
 (0)