Skip to content

cleanups found with phpstan #216

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Nov 26, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@
"require": {
"php": "^7.1 || ^8.0",
"php-http/httplug": "^2.0",
"php-http/message-factory": "^1.0",
"php-http/message": "^1.6",
"php-http/message-factory": "^1.0",
"psr/http-client": "^1.0",
"psr/http-factory": "^1.0",
"psr/http-message": "^1.0",
Expand Down
31 changes: 26 additions & 5 deletions phpstan.neon.dist
Original file line number Diff line number Diff line change
Expand Up @@ -5,22 +5,43 @@ parameters:
paths:
- src
ignoreErrors:
# Exception still thrown in PHP 8, not sure why phpstan complains
-
message: "#^Cannot call method createStream\\(\\) on Psr\\\\Http\\\\Message\\\\StreamFactoryInterface\\|null\\.$#"
count: 1
path: src/HttpMethodsClient.php
message: "#^Dead catch - UnexpectedValueException is never thrown in the try block\\.$#"
count: 2
path: src/BatchResult.php

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

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

-
message: "#^Call to an undefined method Http\\\\Client\\\\HttpAsyncClient\\:\\:sendRequest\\(\\)\\.$#"
count: 1
path: src/PluginClient.php

-
message: "#^Method Http\\\\Client\\\\Common\\\\EmulatedHttpClient\\:\\:sendRequest\\(\\) should return Psr\\\\Http\\\\Message\\\\ResponseInterface but returns mixed\\.$#"
count: 1
path: src/EmulatedHttpClient.php

-
message: "#^Anonymous function should return Psr\\\\Http\\\\Message\\\\ResponseInterface but returns mixed\\.$#"
count: 1
path: src/Plugin/RedirectPlugin.php

-
message: "#^Method Http\\\\Client\\\\Common\\\\Plugin\\\\RetryPlugin\\:\\:retry\\(\\) should return Psr\\\\Http\\\\Message\\\\ResponseInterface but returns mixed\\.$#"
count: 1
path: src/Plugin/RetryPlugin.php

-
message: "#^Method Http\\\\Client\\\\Common\\\\PluginClient\\:\\:sendRequest\\(\\) should return Psr\\\\Http\\\\Message\\\\ResponseInterface but returns mixed\\.$#"
count: 2
path: src/PluginClient.php
8 changes: 4 additions & 4 deletions src/HttpMethodsClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -124,17 +124,17 @@ private function createRequest(string $method, $uri, array $headers = [], $body
);
}

if (is_string($body) && '' !== $body && null === $this->streamFactory) {
throw new \RuntimeException('Cannot create request: A stream factory is required to create a request with a non-empty string body.');
}

$request = $this->requestFactory->createRequest($method, $uri);

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

if (null !== $body && '' !== $body) {
if (null === $this->streamFactory) {
throw new \RuntimeException('Cannot create request: A stream factory is required to create a request with a non-empty string body.');
}

$request = $request->withBody(
is_string($body) ? $this->streamFactory->createStream($body) : $body
);
Expand Down
6 changes: 3 additions & 3 deletions src/Plugin/AddHostPlugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,10 @@ final class AddHostPlugin implements Plugin
private $replace;

/**
* @param array $config {
* @param array{'replace'?: bool} $config
*
* @var bool $replace True will replace all hosts, false will only add host when none is specified.
* }
* Configuration options:
* - replace: True will replace all hosts, false will only add host when none is specified.
*/
public function __construct(UriInterface $host, array $config = [])
{
Expand Down
8 changes: 4 additions & 4 deletions src/Plugin/ContentTypePlugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,11 @@ final class ContentTypePlugin implements Plugin
private $sizeLimit;

/**
* @param array $config {
* @param array{'skip_detection'?: bool, 'size_limit'?: int} $config
*
* @var bool $skip_detection true skip detection if stream size is bigger than $size_limit
* @var int $size_limit size stream limit for which the detection as to be skipped.
* }
* Configuration options:
* - skip_detection: true skip detection if stream size is bigger than $size_limit
* - size_limit: size stream limit for which the detection as to be skipped.
*/
public function __construct(array $config = [])
{
Expand Down
2 changes: 1 addition & 1 deletion src/Plugin/CookiePlugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ private function createCookie(RequestInterface $request, string $setCookieHeader
{
$parts = array_map('trim', explode(';', $setCookieHeader));

if (empty($parts) || !strpos($parts[0], '=')) {
if ('' === $parts[0] || false === strpos($parts[0], '=')) {
return null;
}

Expand Down
6 changes: 3 additions & 3 deletions src/Plugin/DecoderPlugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,10 @@ final class DecoderPlugin implements Plugin
private $useContentEncoding;

/**
* @param array $config {
* @param array{'use_content_encoding'?: bool} $config
*
* @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).
* }
* Configuration options:
* - use_content_encoding: Whether this plugin should look at the Content-Encoding header first or only at the Transfer-Encoding (defaults to true).
*/
public function __construct(array $config = [])
{
Expand Down
6 changes: 3 additions & 3 deletions src/Plugin/ErrorPlugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,10 @@ final class ErrorPlugin implements Plugin
private $onlyServerException;

/**
* @param array $config {
* @param array{'only_server_exception'?: bool} $config
*
* @var bool only_server_exception Whether this plugin should only throw 5XX Exceptions (default to false).
* }
* Configuration options:
* - only_server_exception: Whether this plugin should only throw 5XX Exceptions (default to false).
*/
public function __construct(array $config = [])
{
Expand Down
14 changes: 6 additions & 8 deletions src/Plugin/RedirectPlugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -102,12 +102,12 @@ final class RedirectPlugin implements Plugin
private $circularDetection = [];

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

// Call redirect request synchronously
$redirectPromise = $first($redirectRequest);

return $redirectPromise->wait();
return $first($redirectRequest)->wait();
});
}

Expand Down
16 changes: 9 additions & 7 deletions src/Plugin/RetryPlugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,14 +56,14 @@ final class RetryPlugin implements Plugin
private $retryStorage = [];

/**
* @param array $config {
* @param array{'retries'?: int, 'error_response_decider'?: callable, 'exception_decider'?: callable, 'error_response_delay'?: callable, 'exception_delay'?: callable} $config
*
* @var int $retries Number of retries to attempt if an exception occurs before letting the exception bubble up
* @var callable $error_response_decider A callback that gets a request and response to decide whether the request should be retried
* @var callable $exception_decider A callback that gets a request and an exception to decide after a failure whether the request should be retried
* @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
* @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
* }
* Configuration options:
* - retries: Number of retries to attempt if an exception occurs before letting the exception bubble up
* - error_response_decider: A callback that gets a request and response to decide whether the request should be retried
* - exception_decider: A callback that gets a request and an exception to decide after a failure whether the request should be retried
* - 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
* - 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
*/
public function __construct(array $config = [])
{
Expand Down Expand Up @@ -115,6 +115,7 @@ public function handleRequest(RequestInterface $request, callable $next, callabl
}

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

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

return $this->retry($request, $next, $first, $chainIdentifier, $time);
Expand Down
8 changes: 4 additions & 4 deletions src/Plugin/SeekableBodyPlugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,11 @@ abstract class SeekableBodyPlugin implements Plugin
protected $memoryBufferSize;

/**
* @param array $config {
* @param array{'use_file_buffer'?: bool, 'memory_boffer_size'?: int} $config
*
* @var bool $use_file_buffer Whether this plugin should use a file as a buffer if the stream is too big, defaults to true
* @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)
* }
* Configuration options:
* - use_file_buffer: Whether this plugin should use a file as a buffer if the stream is too big, defaults to true
* - memory_buffer_size: Max memory size in bytes to use for the buffer before it use a file, defaults to 2097152 (2 mb)
*/
public function __construct(array $config = [])
{
Expand Down
5 changes: 1 addition & 4 deletions src/PluginChain.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,7 @@ final class PluginChain
/**
* @param Plugin[] $plugins A plugin chain
* @param callable(RequestInterface): Promise $clientCallable Callable making the HTTP call
* @param array $options {
*
* @var int $max_restarts
* }
* @param array{'max_restarts'?: int} $options
*/
public function __construct(array $plugins, callable $clientCallable, array $options = [])
{
Expand Down
4 changes: 1 addition & 3 deletions src/PluginClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,7 @@ final class PluginClient implements HttpClient, HttpAsyncClient
/**
* @param ClientInterface|HttpAsyncClient $client An HTTP async client
* @param Plugin[] $plugins A plugin chain
* @param array $options {
* @var int $max_restarts
* }
* @param array{'max_restarts'?: int} $options
*/
public function __construct($client, array $plugins = [], array $options = [])
{
Expand Down
8 changes: 4 additions & 4 deletions src/PluginClientFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,11 @@ public static function setFactory(callable $factory): void
/**
* @param ClientInterface|HttpAsyncClient $client
* @param Plugin[] $plugins
* @param array $options {
* @param array{'client_name'?: string} $options
*
* @var string $client_name to give client a name which may be used when displaying client information like in
* the HTTPlugBundle profiler.
* }
* Configuration options:
* - client_name: to give client a name which may be used when displaying client information
* like in the HTTPlugBundle profiler.
*
* @see PluginClient constructor for PluginClient specific $options.
*/
Expand Down