Skip to content

Commit

Permalink
Method annotation is not enforcing return type argument (#2497)
Browse files Browse the repository at this point in the history
* Remove annotation method in order to enforce return type

* Wrote tests about BadResponseException

* Added docblocks for BadResponseException

* update baseline and fix codestyle
  • Loading branch information
gmponos authored and Nyholm committed Dec 28, 2019
1 parent c60e592 commit 865ff35
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 2 deletions.
5 changes: 5 additions & 0 deletions phpstan-baseline.neon
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,11 @@ parameters:
count: 1
path: src/Exception/BadResponseException.php

-
message: "#^Method GuzzleHttp\\\\Exception\\\\BadResponseException\\:\\:getResponse\\(\\) should return Psr\\\\Http\\\\Message\\\\ResponseInterface but returns Psr\\\\Http\\\\Message\\\\ResponseInterface\\|null\\.$#"
count: 1
path: src/Exception/BadResponseException.php

-
message: "#^Method GuzzleHttp\\\\Exception\\\\ConnectException\\:\\:__construct\\(\\) has parameter \\$handlerContext with no value type specified in iterable type array\\.$#"
count: 1
Expand Down
13 changes: 11 additions & 2 deletions src/Exception/BadResponseException.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@

/**
* Exception when an HTTP error occurs (4xx or 5xx error)
*
* @method ResponseInterface getResponse()
*/
class BadResponseException extends RequestException
{
Expand All @@ -22,8 +20,19 @@ public function __construct(
parent::__construct($message, $request, $response, $previous, $handlerContext);
}

/**
* Current exception and the ones that extend it will always have a response.
*/
public function hasResponse(): bool
{
return true;
}

/**
* This function narrows the return type from the parent class and does not allow it to be nullable.
*/
public function getResponse(): ResponseInterface
{
return parent::getResponse();
}
}
24 changes: 24 additions & 0 deletions tests/Exception/BadResponseExceptionTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

namespace GuzzleHttp\Tests\Exception;

use GuzzleHttp\Exception\BadResponseException;
use GuzzleHttp\Psr7\Request;
use GuzzleHttp\Psr7\Response;
use PHPUnit\Framework\TestCase;

class BadResponseExceptionTest extends TestCase
{
public function testHasNoResponse()
{
$req = new Request('GET', '/');
$prev = new \Exception();
$response = new Response();
$e = new BadResponseException('foo', $req, $response, $prev);
self::assertSame($req, $e->getRequest());
self::assertSame($response, $e->getResponse());
self::assertTrue($e->hasResponse());
self::assertSame('foo', $e->getMessage());
self::assertSame($prev, $e->getPrevious());
}
}

0 comments on commit 865ff35

Please sign in to comment.