-
Notifications
You must be signed in to change notification settings - Fork 39
Description
Q | A |
---|---|
Bug? | maybe |
New Feature? | no |
Version | 1.1.0 |
Actual Behavior
When building an HTTP client that implements Http\Client\HttpClient
, I am implementing the sendRequest()
method, as required by the interface. I have added the @throws
docblock annotation with Http\Client\Exception
, since that is what the interface defines. When I run phpstan
, I receive the following error because phpstan
detects that the Exception
interface does not inherit from Throwable
.
$ composer run phpstan
> phpstan analyse MyClient.php --level max --no-progress
------ ----------------------------------------------------------------------
Line MyClient.php
------ ----------------------------------------------------------------------
32 PHPDoc tag @throws with type Http\Client\Exception is not subtype of
Throwable
------ ----------------------------------------------------------------------
[ERROR] Found 1 error
Expected Behavior
I expect static analysis tools to pass without error when defining @throws \Http\Client\Exception
on a method docblock.
Steps to Reproduce
- Use the
composer.json
andMyClient.php
files provided below. - Run
composer install
- Run
composer run phpstan
composer.json
{
"require": {
"php-http/httplug": "^1.1",
"phpstan/phpstan": "^0.10.3"
},
"autoload": {
"psr-4": {
"Ramsey\\Example\\": "."
}
},
"scripts": {
"phpstan": "phpstan analyse MyClient.php --level max --no-progress"
}
}
MyClient.php
<?php
declare(strict_types=1);
namespace Ramsey\Example;
use Http\Client\Exception;
use Http\Client\HttpClient;
use Psr\Http\Message\RequestInterface;
use Psr\Http\Message\ResponseInterface;
class MyClient implements HttpClient
{
/**
* @var HttpClient
*/
private $client;
/**
* @param HttpClient $client
*/
public function __construct(HttpClient $client)
{
$this->client = $client;
}
/**
* @param RequestInterface $request
* @return ResponseInterface
* @throws Exception
*/
public function sendRequest(RequestInterface $request): ResponseInterface
{
return $this->client->sendRequest($request);
}
}
Possible Solutions
The Http\Client\Exception
interface should extend Throwable
, which would cause phpstan
and other static analysis tools to run without errors.
I understand this may not be possible, since this library supports versions of PHP earlier than 7.0. This is also why I put "maybe" as an answer to whether this is a bug.