Skip to content

Commit 93c99d8

Browse files
committed
use phpstan
1 parent ee4c555 commit 93c99d8

10 files changed

+203
-120
lines changed

.github/workflows/ci.yml

+9
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,9 @@ jobs:
3939
- name: Install dependencies
4040
run: composer update --prefer-dist --no-interaction --no-progress
4141

42+
- name: generate ssl
43+
run: cd ./tests/server/ssl && ./generate.sh && pwd && ls -la && cd ../../../
44+
4245
- name: boot test server
4346
run: vendor/bin/http_test_server > /dev/null 2>&1 &
4447

@@ -68,6 +71,9 @@ jobs:
6871
- name: Install dependencies
6972
run: composer update --prefer-dist --prefer-stable --prefer-lowest --no-interaction --no-progress
7073

74+
- name: generate ssl
75+
run: cd ./tests/server/ssl && ./generate.sh && pwd && ls -la && cd ../../../
76+
7177
- name: boot test server
7278
run: vendor/bin/http_test_server > /dev/null 2>&1 &
7379

@@ -95,5 +101,8 @@ jobs:
95101
- name: generate ssl
96102
run: cd ./tests/server/ssl && ./generate.sh && pwd && ls -la && cd ../../../
97103

104+
- name: boot test server
105+
run: vendor/bin/http_test_server > /dev/null 2>&1 &
106+
98107
- name: Execute tests
99108
run: composer test-ci

composer.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
}
1010
],
1111
"require": {
12-
"php": "^7.1 | ^8.0",
12+
"php": "^7.2 || ^8.0",
1313
"nyholm/psr7": "^1.3",
1414
"php-http/httplug": "^2.0",
1515
"psr/http-client": "^1.0",
@@ -20,7 +20,7 @@
2020
"php-http/client-integration-tests": "^3.0",
2121
"php-http/message": "^1.9",
2222
"php-http/client-common": "^2.3",
23-
"phpunit/phpunit": "^8.5.8 || ~9.5"
23+
"phpunit/phpunit": "^8.5.23 || ~9.5"
2424
},
2525
"provide": {
2626
"php-http/client-implementation": "1.0",

phpstan.neon.dist

+35-2
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,38 @@ parameters:
33
paths:
44
- src
55
ignoreErrors:
6-
- message: '#^Method Http\\Adapter\\React\\Client::sendRequest\(\) should return Psr\\Http\\Message\\ResponseInterface but returns mixed.$#'
7-
path: src/Client.php
6+
# phpstan seems confused by passing a variable by reference to stream_select
7+
-
8+
message: '#^Negated boolean expression is always false.$#'
9+
count: 1
10+
path: src/RequestWriter.php
11+
12+
-
13+
message: "#^Constructor of class Http\\\\Client\\\\Socket\\\\Client has an unused parameter \\$config2\\.$#"
14+
count: 1
15+
path: src/Client.php
16+
17+
-
18+
message: "#^Method Http\\\\Client\\\\Socket\\\\Client\\:\\:__construct\\(\\) has parameter \\$config1 with no type specified\\.$#"
19+
count: 1
20+
path: src/Client.php
21+
22+
-
23+
message: "#^Method Http\\\\Client\\\\Socket\\\\Client\\:\\:__construct\\(\\) has parameter \\$config2 with no type specified\\.$#"
24+
count: 1
25+
path: src/Client.php
26+
27+
-
28+
message: "#^Method Http\\\\Client\\\\Socket\\\\Client\\:\\:configure\\(\\) should return array\\{remote_socket\\: string\\|null, timeout\\: int, stream_context\\: resource, stream_context_options\\: array\\<string, mixed\\>, stream_context_param\\: array\\<string, mixed\\>, ssl\\: bool\\|null, write_buffer_size\\: int, ssl_method\\: int\\} but returns array\\.$#"
29+
count: 1
30+
path: src/Client.php
31+
32+
-
33+
message: "#^Parameter \\#1 \\$options of function stream_context_create expects array\\|null, mixed given\\.$#"
34+
count: 1
35+
path: src/Client.php
36+
37+
-
38+
message: "#^Parameter \\#2 \\$params of function stream_context_create expects array\\|null, mixed given\\.$#"
39+
count: 1
40+
path: src/Client.php

src/Client.php

+29-25
Original file line numberDiff line numberDiff line change
@@ -24,29 +24,24 @@ class Client implements HttpClient
2424
use RequestWriter;
2525
use ResponseReader;
2626

27-
private $config = [
28-
'remote_socket' => null,
29-
'timeout' => null,
30-
'stream_context_options' => [],
31-
'stream_context_param' => [],
32-
'ssl' => null,
33-
'write_buffer_size' => 8192,
34-
'ssl_method' => STREAM_CRYPTO_METHOD_TLSv1_2_CLIENT,
35-
];
27+
/**
28+
* @var array{remote_socket: string|null, timeout: int, stream_context: resource, stream_context_options: array<string, mixed>, stream_context_param: array<string, mixed>, ssl: ?boolean, write_buffer_size: int, ssl_method: int}
29+
*/
30+
private $config;
3631

3732
/**
3833
* Constructor.
3934
*
40-
* @param array $config {
35+
* @param array{remote_socket?: string|null, timeout?: int, stream_context?: resource, stream_context_options?: array<string, mixed>, stream_context_param?: array<string, mixed>, ssl?: ?boolean, write_buffer_size?: int, ssl_method?: int} $config
4136
*
42-
* @var string $remote_socket Remote entrypoint (can be a tcp or unix domain address)
43-
* @var int $timeout Timeout before canceling request
44-
* @var array $stream_context_options Context options as defined in the PHP documentation
45-
* @var array $stream_context_param Context params as defined in the PHP documentation
46-
* @var bool $ssl Use ssl, default to scheme from request, false if not present
47-
* @var int $write_buffer_size Buffer when writing the request body, defaults to 8192
48-
* @var int $ssl_method Crypto method for ssl/tls, see PHP doc, defaults to STREAM_CRYPTO_METHOD_TLSv1_2_CLIENT
49-
* }
37+
* string|null remote_socket Remote entrypoint (can be a tcp or unix domain address)
38+
* int timeout Timeout before canceling request
39+
* stream resource The initialized stream context, if not set the context is created from the options and param.
40+
* array<string, mixed> stream_context_options Context options as defined in the PHP documentation
41+
* array<string, mixed> stream_context_param Context params as defined in the PHP documentation
42+
* boolean ssl Use ssl, default to scheme from request, false if not present
43+
* int write_buffer_size Buffer when writing the request body, defaults to 8192
44+
* int ssl_method Crypto method for ssl/tls, see PHP doc, defaults to STREAM_CRYPTO_METHOD_TLSv1_2_CLIENT
5045
*/
5146
public function __construct($config1 = [], $config2 = null, array $config = [])
5247
{
@@ -120,10 +115,10 @@ protected function createSocket(RequestInterface $request, string $remote, bool
120115
throw new ConnectionException($errMsg, $request);
121116
}
122117

123-
stream_set_timeout($socket, floor($this->config['timeout'] / 1000), $this->config['timeout'] % 1000);
118+
stream_set_timeout($socket, (int) floor($this->config['timeout'] / 1000), $this->config['timeout'] % 1000);
124119

125120
if ($useSsl && false === @stream_socket_enable_crypto($socket, true, $this->config['ssl_method'])) {
126-
throw new SSLConnectionException(sprintf('Cannot enable tls: %s', error_get_last()['message']), $request);
121+
throw new SSLConnectionException(sprintf('Cannot enable tls: %s', error_get_last()['message'] ?? 'no error reported'), $request);
127122
}
128123

129124
return $socket;
@@ -133,6 +128,8 @@ protected function createSocket(RequestInterface $request, string $remote, bool
133128
* Close the socket, used when having an error.
134129
*
135130
* @param resource $socket
131+
*
132+
* @return void
136133
*/
137134
protected function closeSocket($socket)
138135
{
@@ -142,19 +139,26 @@ protected function closeSocket($socket)
142139
/**
143140
* Return configuration for the socket client.
144141
*
145-
* @param array $config Configuration from user
142+
* @param array{remote_socket?: string|null, timeout?: int, stream_context?: resource, stream_context_options?: array<string, mixed>, stream_context_param?: array<string, mixed>, ssl?: ?boolean, write_buffer_size?: int, ssl_method?: int} $config
146143
*
147-
* @return array Configuration resolved
144+
* @return array{remote_socket: string|null, timeout: int, stream_context: resource, stream_context_options: array<string, mixed>, stream_context_param: array<string, mixed>, ssl: ?boolean, write_buffer_size: int, ssl_method: int}
148145
*/
149146
protected function configure(array $config = [])
150147
{
151148
$resolver = new OptionsResolver();
152-
$resolver->setDefaults($this->config);
149+
$resolver->setDefaults([
150+
'remote_socket' => null,
151+
'timeout' => null,
152+
'stream_context_options' => [],
153+
'stream_context_param' => [],
154+
'ssl' => null,
155+
'write_buffer_size' => 8192,
156+
'ssl_method' => STREAM_CRYPTO_METHOD_TLSv1_2_CLIENT,
157+
]);
153158
$resolver->setDefault('stream_context', function (Options $options) {
154159
return stream_context_create($options['stream_context_options'], $options['stream_context_param']);
155160
});
156-
157-
$resolver->setDefault('timeout', ini_get('default_socket_timeout') * 1000);
161+
$resolver->setDefault('timeout', ((int) ini_get('default_socket_timeout')) * 1000);
158162

159163
$resolver->setAllowedTypes('stream_context_options', 'array');
160164
$resolver->setAllowedTypes('stream_context_param', 'array');

src/Exception/NetworkException.php

+3
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@
77

88
class NetworkException extends \RuntimeException implements NetworkExceptionInterface
99
{
10+
/**
11+
* @var RequestInterface
12+
*/
1013
private $request;
1114

1215
public function __construct(string $message, RequestInterface $request, \Exception $previous = null)

src/RequestWriter.php

+4
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ trait RequestWriter
1919
*
2020
* @param resource $socket
2121
*
22+
* @return void
23+
*
2224
* @throws BrokenPipeException
2325
*/
2426
protected function writeRequest($socket, RequestInterface $request, int $bufferSize = 8192)
@@ -37,6 +39,8 @@ protected function writeRequest($socket, RequestInterface $request, int $bufferS
3739
*
3840
* @param resource $socket
3941
*
42+
* @return void
43+
*
4044
* @throws BrokenPipeException
4145
*/
4246
protected function writeBody($socket, RequestInterface $request, int $bufferSize = 8192)

src/ResponseReader.php

+6-3
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,8 @@ protected function readResponse(RequestInterface $request, $socket): ResponseInt
4848
if (array_key_exists('timed_out', $metadatas) && true === $metadatas['timed_out']) {
4949
throw new TimeoutException('Error while reading response, stream timed out', $request, null);
5050
}
51-
52-
$parts = explode(' ', array_shift($headers), 3);
51+
$header = array_shift($headers);
52+
$parts = null !== $header ? explode(' ', $header, 3) : [];
5353

5454
if (count($parts) <= 1) {
5555
throw new BrokenPipeException('Cannot read the response', $request);
@@ -77,7 +77,7 @@ protected function readResponse(RequestInterface $request, $socket): ResponseInt
7777
: '';
7878
}
7979

80-
$response = new Response($status, $responseHeaders, null, $protocol, $reason);
80+
$response = new Response((int) $status, $responseHeaders, null, $protocol, $reason);
8181
$stream = $this->createStream($socket, $request, $response);
8282

8383
return $response->withBody($stream);
@@ -95,6 +95,9 @@ protected function createStream($socket, RequestInterface $request, ResponseInte
9595
if ($response->hasHeader('Content-Length')) {
9696
$size = (int) $response->getHeaderLine('Content-Length');
9797
}
98+
if ($size < 0) {
99+
$size = null;
100+
}
98101

99102
return new Stream($request, $socket, $size);
100103
}

0 commit comments

Comments
 (0)