Skip to content

Commit

Permalink
PHP 8 only
Browse files Browse the repository at this point in the history
  • Loading branch information
RobinDev committed Feb 23, 2022
1 parent de7bd7d commit 7e31c30
Show file tree
Hide file tree
Showing 7 changed files with 21 additions and 35 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/psalm.yml
Expand Up @@ -9,7 +9,7 @@ jobs:
fail-fast: false
matrix:
os: [ubuntu-latest]
php: [7.4, 8.0]
php: [8.0]
stability: [prefer-stable] #prefer-lowest,

name: PHPStan - P${{ matrix.php }} - ${{ matrix.stability }} - ${{ matrix.os }}
Expand Down Expand Up @@ -44,7 +44,7 @@ jobs:
- name: Setup PHP
uses: shivammathur/setup-php@v2
with:
php-version: "7.4"
php-version: "8.0"
extensions: dom, curl, libxml, mbstring, zip, pcntl, pdo, sqlite, pdo_sqlite, bcmath, soap, intl, gd, exif, iconv, imagick
coverage: none

Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/run-tests.yml
Expand Up @@ -9,7 +9,7 @@ jobs:
fail-fast: true
matrix:
os: [ubuntu-latest, windows-latest]
php: [7.4]
php: [8.0]
stability: [prefer-lowest, prefer-stable]

name: P${{ matrix.php }} - ${{ matrix.stability }} - ${{ matrix.os }}
Expand Down
2 changes: 1 addition & 1 deletion .phpunit.result.cache
@@ -1 +1 @@
{"version":1,"defects":{"PiedWeb\\Curl\\Test\\RequestTest::testDownloadIfHtml":4,"PiedWeb\\Curl\\Test\\RequestTest::testMultipleCheckInHeaders":4,"PiedWeb\\Curl\\Test\\RequestTest::testProxy":3},"times":{"PiedWeb\\Curl\\Test\\HelperTest::testSchemeFromProxy":0.002,"PiedWeb\\Curl\\Test\\HelperTest::testCheckContentType":0,"PiedWeb\\Curl\\Test\\HelperTest::testCheckStatusCode":0,"PiedWeb\\Curl\\Test\\HelperTest::testHeaderParsing":0.312,"PiedWeb\\Curl\\Test\\RequestTest::testDownloadIfHtml":0.329,"PiedWeb\\Curl\\Test\\RequestTest::testNotDownload":0.164,"PiedWeb\\Curl\\Test\\RequestTest::testEffectiveUrl":10.436,"PiedWeb\\Curl\\Test\\RequestTest::testCurlError":0.053,"PiedWeb\\Curl\\Test\\RequestTest::test404":0.231,"PiedWeb\\Curl\\Test\\RequestTest::testAllMethods":0.355,"PiedWeb\\Curl\\Test\\RequestTest::testMultipleCheckInHeaders":0.199,"PiedWeb\\Curl\\Test\\RequestTest::testProxy":1.001,"PiedWeb\\Curl\\Test\\RequestTest::testAbortIfTooBig":0.265,"PiedWeb\\Curl\\Test\\RequestTest::testDownloadOnlyFirstBytes":0.218,"PiedWeb\\Curl\\Test\\RequestTest::testResponseFromCache":0.001,"PiedWeb\\Curl\\Test\\StaticWrapperTest::testStaticGet":0.241}}
{"version":1,"defects":{"PiedWeb\\Curl\\Test\\RequestTest::testDownloadIfHtml":4,"PiedWeb\\Curl\\Test\\RequestTest::testMultipleCheckInHeaders":4,"PiedWeb\\Curl\\Test\\RequestTest::testProxy":3,"PiedWeb\\Curl\\Test\\RequestTest::testNotDownload":3},"times":{"PiedWeb\\Curl\\Test\\HelperTest::testSchemeFromProxy":0.002,"PiedWeb\\Curl\\Test\\HelperTest::testCheckContentType":0,"PiedWeb\\Curl\\Test\\HelperTest::testCheckStatusCode":0,"PiedWeb\\Curl\\Test\\HelperTest::testHeaderParsing":0.271,"PiedWeb\\Curl\\Test\\RequestTest::testDownloadIfHtml":0.344,"PiedWeb\\Curl\\Test\\RequestTest::testNotDownload":0.166,"PiedWeb\\Curl\\Test\\RequestTest::testEffectiveUrl":10.34,"PiedWeb\\Curl\\Test\\RequestTest::testCurlError":0.052,"PiedWeb\\Curl\\Test\\RequestTest::test404":0.228,"PiedWeb\\Curl\\Test\\RequestTest::testAllMethods":0.346,"PiedWeb\\Curl\\Test\\RequestTest::testMultipleCheckInHeaders":0.202,"PiedWeb\\Curl\\Test\\RequestTest::testProxy":1.002,"PiedWeb\\Curl\\Test\\RequestTest::testAbortIfTooBig":0.277,"PiedWeb\\Curl\\Test\\RequestTest::testDownloadOnlyFirstBytes":0.247,"PiedWeb\\Curl\\Test\\RequestTest::testResponseFromCache":0,"PiedWeb\\Curl\\Test\\StaticWrapperTest::testStaticGet":0.246}}
6 changes: 3 additions & 3 deletions composer.json
Expand Up @@ -16,16 +16,16 @@
}
],
"require": {
"php": "^7.1|^8.0",
"thecodingmachine/safe": "^1.3 || ^2.1"
"php": "^8.0",
"thecodingmachine/safe": "^2.1"
},
"require-dev": {
"friendsofphp/php-cs-fixer": "^3.2",
"phpstan/phpstan": "^1.0",
"phpunit/phpunit": "^9.5",
"symfony/debug": "^4.4",
"vimeo/psalm": "^4",
"symfony/var-dumper": "^5.4 || ^6.0"
"symfony/var-dumper": "^6.0"
},
"scripts": {
"psalm": "vendor/bin/psalm --no-suggestions --no-cache",
Expand Down
28 changes: 11 additions & 17 deletions src/Request.php
Expand Up @@ -13,10 +13,7 @@ class Request

public const RETURN_HEADER = 1;

/**
* @var resource // When dropping 7.4 set CurlHandle
*/
private $handle;
private CurlHandle $handle;

/** @var string contains targeted URL */
private string $url;
Expand All @@ -43,10 +40,7 @@ public function __construct(?string $url = null)
}
}

/**
* @return resource // When dropping 7.4 set CurlHandle
*/
public function getHandle()
public function getHandle(): CurlHandle
{
return $this->handle;
}
Expand Down Expand Up @@ -145,7 +139,7 @@ public function setReturnHeader(bool $only = false): self
$this->setOpt(\CURLOPT_HEADER, 1);
$this->returnHeaders = $only ? self::RETURN_HEADER_ONLY : self::RETURN_HEADER;

if ($only) {
if ($only === true) {
$this->setOpt(\CURLOPT_RETURNTRANSFER, 0);
$this->setOpt(\CURLOPT_NOBODY, 1);
}
Expand Down Expand Up @@ -279,10 +273,7 @@ public function setDownloadOnly(string $range = '0-500'): self
return $this;
}

/**
* @param resource $handle
*/
public function checkHeader($handle, string $line): int
public function checkHeader(CurlHandle $handle, string $line): int
{
if (\call_user_func($this->filter, $line)) {
$this->optChangeDuringRequest = true;
Expand All @@ -292,6 +283,10 @@ public function checkHeader($handle, string $line): int
return \strlen($line);
}

public function getOptChangeDuringRequest(): bool
{
return $this->optChangeDuringRequest;
}
/**
* Execute the request.
*
Expand All @@ -304,7 +299,6 @@ public function exec(bool $optChange = false)
// Permits to transform HEAD request in GET request
if ($this->optChangeDuringRequest && false === $optChange) {
$this->optChangeDuringRequest = true;

return $this->exec(true);
}

Expand Down Expand Up @@ -351,15 +345,15 @@ public function getError(): string
/**
* Get information regarding the request.
*
* @param int $opt This may be one of the following constants:
* @param ?int $opt This may be one of the following constants:
* http://php.net/manual/en/function.curl-getinfo.php
*
* @return string|array<string, string> If opt is given, returns its value as a string. Otherwise, returns an associative array with the following elements (which correspond to opt): "url" "content_type" "http_code" "header_size" "request_size" "filetime" "ssl_verify_result" "redirect_count" "total_time" "namelookup_time" "connect_time" "pretransfer_time" "size_upload" "size_download" "speed_download" "speed_upload" "download_content_length" "upload_content_length" "starttransfer_time" "redirect_time"
* @psalm-suppress InvalidArgument (for $handle)
*/
public function getInfo(?int $opt = null)
{
return curl_getinfo($this->handle, $opt); // @phpstan-ignore-line
return curl_getinfo($this->getHandle(), $opt); // @phpstan-ignore-line
}

/**
Expand All @@ -368,7 +362,7 @@ public function getInfo(?int $opt = null)
*/
public function getRequestInfo(int $opt)
{
return curl_getinfo($this->handle, $opt); // @phpstan-ignore-line
return curl_getinfo($this->getHandle(), $opt); // @phpstan-ignore-line
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/Response.php
Expand Up @@ -32,7 +32,7 @@ public static function get(Request $request)
}

if (true === $content) {
throw new Exception('CURLOPT_RETURNTRANSFER and CURLOPT_HEADER was set to 0.');
return 0; //throw new Exception('CURLOPT_RETURNTRANSFER and CURLOPT_HEADER was set to 0.');
}

$self = new self($request);
Expand Down
12 changes: 2 additions & 10 deletions tests/RequestTest.php
Expand Up @@ -154,16 +154,8 @@ public function testMultipleCheckInHeaders()

$result = $request->exec();

if (\is_int($result)) {
dump($result);
} else {
$this->assertTrue(\is_array($result->getHeaders()));
$this->assertNull($result->getCookies());
$this->assertSame(404, $result->getInfo('http_code'));
$this->assertSame(0, $result->getRequest()->hasError());
$this->assertSame(404, $result->getRequest()->getInfo(\CURLINFO_HTTP_CODE));
$result->getRequest()->close();
}
$this->assertSame(0, $result);
$this->assertSame(404, $request->getRequestInfos()['http_code']);
}

public function testProxy()
Expand Down

0 comments on commit 7e31c30

Please sign in to comment.