Skip to content

Commit

Permalink
Merge pull request #536 from thelovekesh/fix/phpstan-errors
Browse files Browse the repository at this point in the history
Fix PHPStan errors
  • Loading branch information
westonruter authored Oct 20, 2022
2 parents 325bc7b + e00195e commit bd739dd
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 2 deletions.
10 changes: 8 additions & 2 deletions src/RemoteRequest/CurlRemoteGetRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use AmpProject\Exception\FailedToGetFromRemoteUrl;
use AmpProject\RemoteGetRequest;
use AmpProject\Response;
use AmpProject\Exception\FailedToParseUrl;

/**
* Remote request transport using cURL.
Expand Down Expand Up @@ -97,13 +98,18 @@ public function __construct($sslVerify = true, $timeout = self::DEFAULT_TIMEOUT,
public function get($url, $headers = [])
{
$retriesLeft = $this->retries;

if (! is_string($url) || empty($url)) {
throw FailedToGetFromRemoteUrl::withException($url, FailedToParseUrl::forUrl($url));
}

do {
$curlHandle = curl_init();

curl_setopt($curlHandle, CURLOPT_URL, $url);
curl_setopt($curlHandle, CURLOPT_HEADER, 0);
curl_setopt($curlHandle, CURLOPT_HEADER, false);
curl_setopt($curlHandle, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($curlHandle, CURLOPT_SSL_VERIFYPEER, $this->sslVerify ? 1 : 0);
curl_setopt($curlHandle, CURLOPT_SSL_VERIFYPEER, $this->sslVerify);
curl_setopt($curlHandle, CURLOPT_SSL_VERIFYHOST, $this->sslVerify ? 2 : 0);
curl_setopt($curlHandle, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curlHandle, CURLOPT_FAILONERROR, true);
Expand Down
17 changes: 17 additions & 0 deletions tests/RemoteRequest/CurlRemoteGetRequestTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace AmpProject\RemoteRequest;

use AmpProject\RemoteGetRequest;
use AmpProject\Exception\FailedToGetFromRemoteUrl;
use AmpProject\Tests\TestCase;

/**
Expand All @@ -19,4 +20,20 @@ public function testInstantiation()
$this->assertInstanceOf(RemoteGetRequest::class, $curlRequest);
$this->assertInstanceOf(CurlRemoteGetRequest::class, $curlRequest);
}

/**
* @covers ::get()
*/
public function testGet()
{
$curlRequest = new CurlRemoteGetRequest(false);

// When passed url is not string, it should throw exception.
$this->expectException(FailedToGetFromRemoteUrl::class);
$curlRequest->get(123);

// When passed url is empty string, it should throw exception.
$this->expectException(FailedToGetFromRemoteUrl::class);
$curlRequest->get('');
}
}

0 comments on commit bd739dd

Please sign in to comment.