Skip to content

Commit 461b711

Browse files
authored
Merge pull request #216 from php-http/phpstan
cleanups found with phpstan
2 parents cb83bfe + 16d90d1 commit 461b711

14 files changed

+70
-54
lines changed

composer.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@
1313
"require": {
1414
"php": "^7.1 || ^8.0",
1515
"php-http/httplug": "^2.0",
16-
"php-http/message-factory": "^1.0",
1716
"php-http/message": "^1.6",
17+
"php-http/message-factory": "^1.0",
1818
"psr/http-client": "^1.0",
1919
"psr/http-factory": "^1.0",
2020
"psr/http-message": "^1.0",

phpstan.neon.dist

+26-5
Original file line numberDiff line numberDiff line change
@@ -5,22 +5,43 @@ parameters:
55
paths:
66
- src
77
ignoreErrors:
8+
# Exception still thrown in PHP 8, not sure why phpstan complains
89
-
9-
message: "#^Cannot call method createStream\\(\\) on Psr\\\\Http\\\\Message\\\\StreamFactoryInterface\\|null\\.$#"
10-
count: 1
11-
path: src/HttpMethodsClient.php
10+
message: "#^Dead catch - UnexpectedValueException is never thrown in the try block\\.$#"
11+
count: 2
12+
path: src/BatchResult.php
1213

1314
-
14-
message: "#^Method Http\\\\Client\\\\Common\\\\Plugin\\\\Journal\\:\\:addSuccess\\(\\) has no return typehint specified\\.$#"
15+
message: "#^Method Http\\\\Client\\\\Common\\\\Plugin\\\\Journal\\:\\:addSuccess\\(\\) has no return type specified\\.$#"
1516
count: 1
1617
path: src/Plugin/Journal.php
1718

1819
-
19-
message: "#^Method Http\\\\Client\\\\Common\\\\Plugin\\\\Journal\\:\\:addFailure\\(\\) has no return typehint specified\\.$#"
20+
message: "#^Method Http\\\\Client\\\\Common\\\\Plugin\\\\Journal\\:\\:addFailure\\(\\) has no return type specified\\.$#"
2021
count: 1
2122
path: src/Plugin/Journal.php
2223

2324
-
2425
message: "#^Call to an undefined method Http\\\\Client\\\\HttpAsyncClient\\:\\:sendRequest\\(\\)\\.$#"
2526
count: 1
2627
path: src/PluginClient.php
28+
29+
-
30+
message: "#^Method Http\\\\Client\\\\Common\\\\EmulatedHttpClient\\:\\:sendRequest\\(\\) should return Psr\\\\Http\\\\Message\\\\ResponseInterface but returns mixed\\.$#"
31+
count: 1
32+
path: src/EmulatedHttpClient.php
33+
34+
-
35+
message: "#^Anonymous function should return Psr\\\\Http\\\\Message\\\\ResponseInterface but returns mixed\\.$#"
36+
count: 1
37+
path: src/Plugin/RedirectPlugin.php
38+
39+
-
40+
message: "#^Method Http\\\\Client\\\\Common\\\\Plugin\\\\RetryPlugin\\:\\:retry\\(\\) should return Psr\\\\Http\\\\Message\\\\ResponseInterface but returns mixed\\.$#"
41+
count: 1
42+
path: src/Plugin/RetryPlugin.php
43+
44+
-
45+
message: "#^Method Http\\\\Client\\\\Common\\\\PluginClient\\:\\:sendRequest\\(\\) should return Psr\\\\Http\\\\Message\\\\ResponseInterface but returns mixed\\.$#"
46+
count: 2
47+
path: src/PluginClient.php

src/HttpMethodsClient.php

+4-4
Original file line numberDiff line numberDiff line change
@@ -124,17 +124,17 @@ private function createRequest(string $method, $uri, array $headers = [], $body
124124
);
125125
}
126126

127-
if (is_string($body) && '' !== $body && null === $this->streamFactory) {
128-
throw new \RuntimeException('Cannot create request: A stream factory is required to create a request with a non-empty string body.');
129-
}
130-
131127
$request = $this->requestFactory->createRequest($method, $uri);
132128

133129
foreach ($headers as $key => $value) {
134130
$request = $request->withHeader($key, $value);
135131
}
136132

137133
if (null !== $body && '' !== $body) {
134+
if (null === $this->streamFactory) {
135+
throw new \RuntimeException('Cannot create request: A stream factory is required to create a request with a non-empty string body.');
136+
}
137+
138138
$request = $request->withBody(
139139
is_string($body) ? $this->streamFactory->createStream($body) : $body
140140
);

src/Plugin/AddHostPlugin.php

+3-3
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,10 @@ final class AddHostPlugin implements Plugin
2828
private $replace;
2929

3030
/**
31-
* @param array $config {
31+
* @param array{'replace'?: bool} $config
3232
*
33-
* @var bool $replace True will replace all hosts, false will only add host when none is specified.
34-
* }
33+
* Configuration options:
34+
* - replace: True will replace all hosts, false will only add host when none is specified.
3535
*/
3636
public function __construct(UriInterface $host, array $config = [])
3737
{

src/Plugin/ContentTypePlugin.php

+4-4
Original file line numberDiff line numberDiff line change
@@ -35,11 +35,11 @@ final class ContentTypePlugin implements Plugin
3535
private $sizeLimit;
3636

3737
/**
38-
* @param array $config {
38+
* @param array{'skip_detection'?: bool, 'size_limit'?: int} $config
3939
*
40-
* @var bool $skip_detection true skip detection if stream size is bigger than $size_limit
41-
* @var int $size_limit size stream limit for which the detection as to be skipped.
42-
* }
40+
* Configuration options:
41+
* - skip_detection: true skip detection if stream size is bigger than $size_limit
42+
* - size_limit: size stream limit for which the detection as to be skipped.
4343
*/
4444
public function __construct(array $config = [])
4545
{

src/Plugin/CookiePlugin.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ private function createCookie(RequestInterface $request, string $setCookieHeader
9797
{
9898
$parts = array_map('trim', explode(';', $setCookieHeader));
9999

100-
if (empty($parts) || !strpos($parts[0], '=')) {
100+
if ('' === $parts[0] || false === strpos($parts[0], '=')) {
101101
return null;
102102
}
103103

src/Plugin/DecoderPlugin.php

+3-3
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,10 @@ final class DecoderPlugin implements Plugin
3131
private $useContentEncoding;
3232

3333
/**
34-
* @param array $config {
34+
* @param array{'use_content_encoding'?: bool} $config
3535
*
36-
* @var bool $use_content_encoding Whether this plugin should look at the Content-Encoding header first or only at the Transfer-Encoding (defaults to true).
37-
* }
36+
* Configuration options:
37+
* - use_content_encoding: Whether this plugin should look at the Content-Encoding header first or only at the Transfer-Encoding (defaults to true).
3838
*/
3939
public function __construct(array $config = [])
4040
{

src/Plugin/ErrorPlugin.php

+3-3
Original file line numberDiff line numberDiff line change
@@ -37,10 +37,10 @@ final class ErrorPlugin implements Plugin
3737
private $onlyServerException;
3838

3939
/**
40-
* @param array $config {
40+
* @param array{'only_server_exception'?: bool} $config
4141
*
42-
* @var bool only_server_exception Whether this plugin should only throw 5XX Exceptions (default to false).
43-
* }
42+
* Configuration options:
43+
* - only_server_exception: Whether this plugin should only throw 5XX Exceptions (default to false).
4444
*/
4545
public function __construct(array $config = [])
4646
{

src/Plugin/RedirectPlugin.php

+6-8
Original file line numberDiff line numberDiff line change
@@ -102,12 +102,12 @@ final class RedirectPlugin implements Plugin
102102
private $circularDetection = [];
103103

104104
/**
105-
* @param array $config {
105+
* @param array{'preserve_header'?: bool|string[], 'use_default_for_multiple'?: bool, 'strict'?: bool} $config
106106
*
107-
* @var bool|string[] $preserve_header True keeps all headers, false remove all of them, an array is interpreted as a list of header names to keep
108-
* @var bool $use_default_for_multiple Whether the location header must be directly used for a multiple redirection status code (300)
109-
* @var bool $strict When true, redirect codes 300, 301, 302 will not modify request method and body.
110-
* }
107+
* Configuration options:
108+
* - preserve_header: True keeps all headers, false remove all of them, an array is interpreted as a list of header names to keep
109+
* - use_default_for_multiple: Whether the location header must be directly used for a multiple redirection status code (300)
110+
* - strict: When true, redirect codes 300, 301, 302 will not modify request method and body.
111111
*/
112112
public function __construct(array $config = [])
113113
{
@@ -182,9 +182,7 @@ public function handleRequest(RequestInterface $request, callable $next, callabl
182182
}
183183

184184
// Call redirect request synchronously
185-
$redirectPromise = $first($redirectRequest);
186-
187-
return $redirectPromise->wait();
185+
return $first($redirectRequest)->wait();
188186
});
189187
}
190188

src/Plugin/RetryPlugin.php

+9-7
Original file line numberDiff line numberDiff line change
@@ -56,14 +56,14 @@ final class RetryPlugin implements Plugin
5656
private $retryStorage = [];
5757

5858
/**
59-
* @param array $config {
59+
* @param array{'retries'?: int, 'error_response_decider'?: callable, 'exception_decider'?: callable, 'error_response_delay'?: callable, 'exception_delay'?: callable} $config
6060
*
61-
* @var int $retries Number of retries to attempt if an exception occurs before letting the exception bubble up
62-
* @var callable $error_response_decider A callback that gets a request and response to decide whether the request should be retried
63-
* @var callable $exception_decider A callback that gets a request and an exception to decide after a failure whether the request should be retried
64-
* @var callable $error_response_delay A callback that gets a request and response and the current number of retries and returns how many microseconds we should wait before trying again
65-
* @var callable $exception_delay A callback that gets a request, an exception and the current number of retries and returns how many microseconds we should wait before trying again
66-
* }
61+
* Configuration options:
62+
* - retries: Number of retries to attempt if an exception occurs before letting the exception bubble up
63+
* - error_response_decider: A callback that gets a request and response to decide whether the request should be retried
64+
* - exception_decider: A callback that gets a request and an exception to decide after a failure whether the request should be retried
65+
* - error_response_delay: A callback that gets a request and response and the current number of retries and returns how many microseconds we should wait before trying again
66+
* - exception_delay: A callback that gets a request, an exception and the current number of retries and returns how many microseconds we should wait before trying again
6767
*/
6868
public function __construct(array $config = [])
6969
{
@@ -115,6 +115,7 @@ public function handleRequest(RequestInterface $request, callable $next, callabl
115115
}
116116

117117
if (call_user_func($this->errorResponseDecider, $request, $response)) {
118+
/** @var int $time */
118119
$time = call_user_func($this->errorResponseDelay, $request, $response, $this->retryStorage[$chainIdentifier]);
119120
$response = $this->retry($request, $next, $first, $chainIdentifier, $time);
120121
}
@@ -139,6 +140,7 @@ public function handleRequest(RequestInterface $request, callable $next, callabl
139140
throw $exception;
140141
}
141142

143+
/** @var int $time */
142144
$time = call_user_func($this->exceptionDelay, $request, $exception, $this->retryStorage[$chainIdentifier]);
143145

144146
return $this->retry($request, $next, $first, $chainIdentifier, $time);

src/Plugin/SeekableBodyPlugin.php

+4-4
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,11 @@ abstract class SeekableBodyPlugin implements Plugin
2323
protected $memoryBufferSize;
2424

2525
/**
26-
* @param array $config {
26+
* @param array{'use_file_buffer'?: bool, 'memory_boffer_size'?: int} $config
2727
*
28-
* @var bool $use_file_buffer Whether this plugin should use a file as a buffer if the stream is too big, defaults to true
29-
* @var int $memory_buffer_size Max memory size in bytes to use for the buffer before it use a file, defaults to 2097152 (2 mb)
30-
* }
28+
* Configuration options:
29+
* - use_file_buffer: Whether this plugin should use a file as a buffer if the stream is too big, defaults to true
30+
* - memory_buffer_size: Max memory size in bytes to use for the buffer before it use a file, defaults to 2097152 (2 mb)
3131
*/
3232
public function __construct(array $config = [])
3333
{

src/PluginChain.php

+1-4
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,7 @@ final class PluginChain
2626
/**
2727
* @param Plugin[] $plugins A plugin chain
2828
* @param callable(RequestInterface): Promise $clientCallable Callable making the HTTP call
29-
* @param array $options {
30-
*
31-
* @var int $max_restarts
32-
* }
29+
* @param array{'max_restarts'?: int} $options
3330
*/
3431
public function __construct(array $plugins, callable $clientCallable, array $options = [])
3532
{

src/PluginClient.php

+1-3
Original file line numberDiff line numberDiff line change
@@ -46,9 +46,7 @@ final class PluginClient implements HttpClient, HttpAsyncClient
4646
/**
4747
* @param ClientInterface|HttpAsyncClient $client An HTTP async client
4848
* @param Plugin[] $plugins A plugin chain
49-
* @param array $options {
50-
* @var int $max_restarts
51-
* }
49+
* @param array{'max_restarts'?: int} $options
5250
*/
5351
public function __construct($client, array $plugins = [], array $options = [])
5452
{

src/PluginClientFactory.php

+4-4
Original file line numberDiff line numberDiff line change
@@ -39,11 +39,11 @@ public static function setFactory(callable $factory): void
3939
/**
4040
* @param ClientInterface|HttpAsyncClient $client
4141
* @param Plugin[] $plugins
42-
* @param array $options {
42+
* @param array{'client_name'?: string} $options
4343
*
44-
* @var string $client_name to give client a name which may be used when displaying client information like in
45-
* the HTTPlugBundle profiler.
46-
* }
44+
* Configuration options:
45+
* - client_name: to give client a name which may be used when displaying client information
46+
* like in the HTTPlugBundle profiler.
4747
*
4848
* @see PluginClient constructor for PluginClient specific $options.
4949
*/

0 commit comments

Comments
 (0)