phpstan: Variable probably undefined in second catch block (Guzzle request + JSON decoding it's response)
Bug report
PHPStan version 1.5.4
./vendor/bin/phpstan -V
PHPStan - PHP Static Analysis Tool 1.5.4
I make a call to an API via Guzzle and would like to decode the response with the following code:
// .. make request via Guzzle here
json_decode(
$response->getBody()->getContents(),
true,
flags: JSON_THROW_ON_ERROR,
);
I have two catch blocks: catching GuzzleException first, and after that I catch the JsonException. Meaning that $response should exist when I’m in the JsonException-catch-block, but I get the following error in PhpStorm:
phpstan: Variable $response might not be defined.
Code snippet that reproduces the problem
Please see the Playground code here: https://phpstan.org/r/65c2bc4a-58e8-4a59-b740-02b4801fb8bd
Expected output
I would like to think that $response is defined since the Guzzle request was successful: this is verified because the catch-block for the GuzzleException-error is “skipped”.
Did PHPStan help you today? Did it make you happy in any way?
I very much enjoy using PHPStan and it helps me write better quality code. Thank you very much for all your hard work!
Thank you for your time!
About this issue
- Original URL
- State: closed
- Created 2 years ago
- Comments: 18 (6 by maintainers)
You don’t need to. You just need to replicate the called methods with the same PHPDocs.