Skip to content

phpstan complains about Http\Client\Exception #142

@ramsey

Description

@ramsey
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

  1. Use the composer.json and MyClient.php files provided below.
  2. Run composer install
  3. 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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions