Skip to content
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

phpstan analysis error when using GuzzleException #1971

Closed
babaorum opened this issue Dec 6, 2017 · 7 comments
Closed

phpstan analysis error when using GuzzleException #1971

babaorum opened this issue Dec 6, 2017 · 7 comments

Comments

@babaorum
Copy link

babaorum commented Dec 6, 2017

Q A
Bug? no
New Feature? no
Version actual

Actual Behavior

Experiencing error with phpstan like those with level analysis >= 5

$exception of class My\Namespace\Exception\MyCustomException constructor expects Exception, GuzzleHttp\Exception\GuzzleException given.

Expected Behavior

No error found.

Steps to Reproduce

I am catching GuzzleException like this.

try {
    // use guzzle for an http call
} catch (GuzzleException $exception) {
    throw new MyCustomException($exception);
}

with a custom exception like this :

class MyCustomException extends \Exception
{
    public function __construct(\Exception $exception)
    {
    }
}

Possible Solutions

The problem seems to be that GuzzleException does not extends \Exception directly. And there is no guarantee that every class that will implement GuzzleException will extend \Exception.

For me, the problem could be solved by changing GuzzleException into an abstract class which will extends \RuntimeException since it is the actual extended exception.

@ondrejmirtes
Copy link

Hi, author of PHPStan here. You should:

  1. Require \Throwable and not \Exception in your constructor.
  2. Use the latest version of PHPStan (0.9) that understands that even interfaces caught in a catch block can be accepted as Throwable.

@ondrejmirtes
Copy link

Maintainers of Guzzle can feel free to close this issue 😊

@babaorum
Copy link
Author

Thank you @ondrejmirtes ! I saw there was some stuff done for Guzzle, but did not saw this problematic.
Sorry everyone for the time lost. Love your projects :)

I am closing the issue.

@rvanlaak
Copy link
Contributor

rvanlaak commented Jun 26, 2018

@ondrejmirtes just as a follow-up on this. Since 0.10 was released (🎉) the phpstan error now thrown is:

PHPDoc tag @throws with type GuzzleHttp\Exception\GuzzleException is not subtype of Throwable

Should the GuzzleException interface extend \Throwable? It does solve above error, and the @method class annotations could be removed.

@babaorum
Copy link
Author

@rvanlaak Throwable interface can not be extended by userland interfaces. neither can it be implemented by userland classes.

@ondrejmirtes
Copy link

@babaorum This definitely isn’t true, you can define interface X extends \Throwable.

@babaorum
Copy link
Author

this won't work.

But it is true that we won't see any error because this works.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants