Formatting, phpcs.xml

This commit is contained in:
Ismo Vuorinen
2023-03-09 14:21:23 +02:00
parent 69e385b4ad
commit 12350f6c0a
8 changed files with 330 additions and 238 deletions

View File

@@ -2,7 +2,7 @@ name: Composer Diff
on: on:
pull_request: pull_request:
paths: paths:
- 'composer.lock' - "composer.lock"
jobs: jobs:
composer-diff: composer-diff:
name: Composer Diff name: Composer Diff
@@ -11,17 +11,20 @@ jobs:
- name: Checkout - name: Checkout
uses: actions/checkout@v3 uses: actions/checkout@v3
with: with:
fetch-depth: 0 # Required to make it possible to compare with PR base branch # Required to make it possible to compare with PR base branch
fetch-depth: 0
- name: Generate composer diff - name: Generate composer diff
id: composer_diff # To reference the output in comment # To reference the output in comment
id: composer_diff
uses: IonBazan/composer-diff-action@v1 uses: IonBazan/composer-diff-action@v1
- uses: marocchino/sticky-pull-request-comment@v2 - uses: marocchino/sticky-pull-request-comment@v2
# An empty diff result will break this action. # An empty diff result will break this action.
if: ${{ steps.composer_diff.outputs.composer_diff_exit_code != 0 }} if: ${{ steps.composer_diff.outputs.composer_diff_exit_code != 0 }}
with: with:
header: composer-diff # Creates a collapsed comment with the report # Creates a collapsed comment with the report
header: composer-diff
message: | message: |
<details> <details>
<summary>Composer package changes</summary> <summary>Composer package changes</summary>

View File

@@ -2,9 +2,9 @@ name: PHP Composer
on: on:
push: push:
branches: [ master ] branches: [master]
pull_request: pull_request:
branches: [ master ] branches: [master]
jobs: jobs:
build: build:

82
CHANGELOG.md Normal file
View File

@@ -0,0 +1,82 @@
# Changelog
## v0.1.0 (05/12/2022)
- [**closed**] Update dependency phpstan/phpstan to v1.9.2 [#87](https://github.com/ivuorinen/curly/pull/87)
- [**closed**] Update dependency phpstan/phpstan to v1.9.1 [#86](https://github.com/ivuorinen/curly/pull/86)
- [**closed**] Update dependency phpstan/phpstan to v1.9.0 [#85](https://github.com/ivuorinen/curly/pull/85)
- [**closed**] Update dependency phpunit/phpunit to v9.5.26 [#84](https://github.com/ivuorinen/curly/pull/84)
- [**closed**] Update dependency phpstan/phpstan to v1.8.11 [#83](https://github.com/ivuorinen/curly/pull/83)
- [**closed**] Update dependency phpstan/phpstan to v1.8.10 [#82](https://github.com/ivuorinen/curly/pull/82)
- [**closed**] Update dependency phpstan/phpstan to v1.8.9 [#81](https://github.com/ivuorinen/curly/pull/81)
- [**closed**] Update dependency phpstan/phpstan to v1.8.8 [#80](https://github.com/ivuorinen/curly/pull/80)
- [**closed**] Update dependency phpstan/phpstan to v1.8.7 [#79](https://github.com/ivuorinen/curly/pull/79)
- [**closed**] Update dependency phpunit/phpunit to v9.5.25 [#78](https://github.com/ivuorinen/curly/pull/78)
- [**closed**] Update dependency phpstan/phpstan to v1.8.6 [#77](https://github.com/ivuorinen/curly/pull/77)
- [**closed**] Update dependency phpstan/phpstan to v1.8.5 [#75](https://github.com/ivuorinen/curly/pull/75)
- [**closed**] Update dependency phpstan/phpstan to v1.8.4 [#74](https://github.com/ivuorinen/curly/pull/74)
- [**closed**] Update dependency phpstan/phpstan to v1.8.3 [#73](https://github.com/ivuorinen/curly/pull/73)
- [**closed**] Update dependency phpunit/phpunit to v9.5.24 [#72](https://github.com/ivuorinen/curly/pull/72)
- [**closed**] Update dependency phpunit/phpunit to v9.5.23 [#71](https://github.com/ivuorinen/curly/pull/71)
- [**closed**] Update dependency phpunit/phpunit to v9.5.22 [#70](https://github.com/ivuorinen/curly/pull/70)
- [**closed**] Update dependency phpstan/phpstan to v1.8.2 [#69](https://github.com/ivuorinen/curly/pull/69)
- [**closed**] Update dependency phpstan/phpstan to v1.8.1 [#68](https://github.com/ivuorinen/curly/pull/68)
- [**closed**] Update dependency ergebnis/composer-normalize to v2.28.3 [#67](https://github.com/ivuorinen/curly/pull/67)
- [**closed**] Update dependency ergebnis/composer-normalize to v2.28.2 [#66](https://github.com/ivuorinen/curly/pull/66)
- [**closed**] Update dependency league/uri to v6.7.1 [#64](https://github.com/ivuorinen/curly/pull/64)
- [**closed**] Update dependency phpstan/phpstan to v1.8.0 [#65](https://github.com/ivuorinen/curly/pull/65)
- [**closed**] Update dependency ergebnis/composer-normalize to v2.28.1 [#63](https://github.com/ivuorinen/curly/pull/63)
- [**closed**] Update dependency phpstan/phpstan to v1.7.15 [#62](https://github.com/ivuorinen/curly/pull/62)
- [**closed**] Update dependency phpunit/phpunit to v9.5.21 [#61](https://github.com/ivuorinen/curly/pull/61)
- [**closed**] Update dependency phpstan/phpstan to v1.7.14 [#60](https://github.com/ivuorinen/curly/pull/60)
- [**closed**] Update dependency phpstan/phpstan to v1.7.13 [#59](https://github.com/ivuorinen/curly/pull/59)
- [**closed**] Update dependency phpstan/phpstan to v1.7.12 [#58](https://github.com/ivuorinen/curly/pull/58)
- [**closed**] Update dependency phpstan/phpstan to v1.7.11 [#57](https://github.com/ivuorinen/curly/pull/57)
- [**closed**] Update dependency phpstan/phpstan to v1.7.10 [#56](https://github.com/ivuorinen/curly/pull/56)
- [**closed**] Update dependency phpstan/phpstan to v1.7.9 [#55](https://github.com/ivuorinen/curly/pull/55)
- [**closed**] Update dependency ergebnis/composer-normalize to v2.28.0 [#54](https://github.com/ivuorinen/curly/pull/54)
- [**closed**] Update dependency phpstan/phpstan to v1.7.8 [#53](https://github.com/ivuorinen/curly/pull/53)
- [**closed**] Update dependency phpstan/phpstan to v1.7.7 [#52](https://github.com/ivuorinen/curly/pull/52)
- [**closed**] Update dependency phpstan/phpstan to v1.7.6 [#51](https://github.com/ivuorinen/curly/pull/51)
- [**closed**] Update dependency phpstan/phpstan to v1.7.5 [#50](https://github.com/ivuorinen/curly/pull/50)
- [**closed**] Update dependency phpstan/phpstan to v1.7.4 [#49](https://github.com/ivuorinen/curly/pull/49)
- [**closed**] Update dependency phpstan/phpstan to v1.7.3 [#48](https://github.com/ivuorinen/curly/pull/48)
- [**closed**] Update dependency league/uri to v6.6.0 [#47](https://github.com/ivuorinen/curly/pull/47)
- [**closed**] Update dependency phpstan/phpstan to v1.7.2 [#46](https://github.com/ivuorinen/curly/pull/46)
- [**closed**] Update dependency ergebnis/composer-normalize to v2.27.0 [#45](https://github.com/ivuorinen/curly/pull/45)
- [**closed**] Update dependency phpstan/phpstan to v1.7.1 [#44](https://github.com/ivuorinen/curly/pull/44)
- [**closed**] Update dependency phpstan/phpstan to v1.7.0 [#43](https://github.com/ivuorinen/curly/pull/43)
- [**closed**] Update dependency phpstan/phpstan to v1.6.9 [#42](https://github.com/ivuorinen/curly/pull/42)
- [**closed**] Update dependency phpstan/phpstan to v1.6.8 [#41](https://github.com/ivuorinen/curly/pull/41)
- [**closed**] Update dependency phpstan/phpstan to v1.6.7 [#40](https://github.com/ivuorinen/curly/pull/40)
- [**closed**] Update dependency phpstan/phpstan to v1.6.5 [#39](https://github.com/ivuorinen/curly/pull/39)
- [**closed**] Update dependency phpstan/phpstan to v1.6.4 [#38](https://github.com/ivuorinen/curly/pull/38)
- [**closed**] Update dependency ergebnis/composer-normalize to v2.26.0 [#37](https://github.com/ivuorinen/curly/pull/37)
- [**closed**] Update dependency phpstan/phpstan to v1.6.3 [#36](https://github.com/ivuorinen/curly/pull/36)
- [**closed**] Update dependency phpstan/phpstan to v1.6.2 [#35](https://github.com/ivuorinen/curly/pull/35)
- [**closed**] Update dependency phpstan/phpstan to v1.6.1 [#34](https://github.com/ivuorinen/curly/pull/34)
- [**closed**] Update dependency phpstan/phpstan to v1.6.0 [#33](https://github.com/ivuorinen/curly/pull/33)
- [**closed**] Update dependency phpstan/phpstan to v1.5.7 [#32](https://github.com/ivuorinen/curly/pull/32)
- [**closed**] Update dependency ergebnis/composer-normalize to v2.25.2 [#31](https://github.com/ivuorinen/curly/pull/31)
- [**closed**] Update dependency phpstan/phpstan to v1.5.6 [#30](https://github.com/ivuorinen/curly/pull/30)
- [**closed**] Update dependency phpstan/phpstan to v1.5.5 [#29](https://github.com/ivuorinen/curly/pull/29)
- [**closed**] Update dependency phpunit/phpunit to v9.5.20 [#27](https://github.com/ivuorinen/curly/pull/27)
- [**closed**] Update dependency phpstan/phpstan to v1.5.4 [#28](https://github.com/ivuorinen/curly/pull/28)
- [**closed**] Update dependency phpstan/phpstan to v1.5.3 [#26](https://github.com/ivuorinen/curly/pull/26)
- [**closed**] Update dependency ergebnis/composer-normalize to v2.25.1 [#25](https://github.com/ivuorinen/curly/pull/25)
- [**closed**] Update dependency phpstan/phpstan to v1.5.2 [#24](https://github.com/ivuorinen/curly/pull/24)
- [**closed**] Update dependency phpstan/phpstan to v1.5.1 [#23](https://github.com/ivuorinen/curly/pull/23)
- [**closed**] Update dependency phpstan/phpstan to v1.5.0 [#22](https://github.com/ivuorinen/curly/pull/22)
- [**closed**] Update actions/cache action to v3 [#21](https://github.com/ivuorinen/curly/pull/21)
- [**closed**] Update dependency ergebnis/composer-normalize to v2.24.1 [#20](https://github.com/ivuorinen/curly/pull/20)
- [**closed**] Update dependency phpstan/phpstan to v1.4.10 [#18](https://github.com/ivuorinen/curly/pull/18)
- [**closed**] Update dependency phpunit/phpunit to v9.5.19 [#19](https://github.com/ivuorinen/curly/pull/19)
- [**closed**] Update dependency phpstan/phpstan to v1.4.9 [#17](https://github.com/ivuorinen/curly/pull/17)
- [**closed**] Update dependency ergebnis/composer-normalize to v2.24.0 [#16](https://github.com/ivuorinen/curly/pull/16)
- [**closed**] Update dependency phpunit/phpunit to v9.5.18 [#15](https://github.com/ivuorinen/curly/pull/15)
- [**closed**] Update dependency phpunit/phpunit to v9.5.17 [#14](https://github.com/ivuorinen/curly/pull/14)
- [**closed**] Update dependency phpstan/phpstan to v1.4.8 [#13](https://github.com/ivuorinen/curly/pull/13)
- [**closed**] Update dependency phpstan/phpstan to v1.4.7 [#12](https://github.com/ivuorinen/curly/pull/12)
- [**closed**] Update actions/checkout action to v3 [#11](https://github.com/ivuorinen/curly/pull/11)
- [**closed**] Update dependency phpunit/phpunit to v9.5.16 [#10](https://github.com/ivuorinen/curly/pull/10)
- [**closed**] Update dependency phpunit/phpunit to v9.5.15 [#9](https://github.com/ivuorinen/curly/pull/9)
- [**closed**] Update dependency phpunit/phpunit to v9.5.14 [#8](https://github.com/ivuorinen/curly/pull/8)

View File

@@ -48,7 +48,7 @@
"post-update-cmd": [ "post-update-cmd": [
"@composer normalize || true" "@composer normalize || true"
], ],
"check": "vendor/bin/phpstan analyse src tests --level=7", "check": "vendor/bin/phpstan analyse src tests --level=5 --xdebug",
"test": "vendor/bin/phpunit" "test": "vendor/bin/phpunit --colors --verbose"
} }
} }

16
phpcs.xml Normal file
View File

@@ -0,0 +1,16 @@
<?xml version="1.0"?>
<ruleset name="Basic Project Coding Standards">
<rule ref="PSR2"/>
<rule ref="Generic.CodeAnalysis.EmptyStatement"/>
<rule ref="Generic.Classes.DuplicateClassName"/>
<rule ref="Generic.CodeAnalysis.EmptyStatement"/>
<rule ref="Generic.CodeAnalysis.UnconditionalIfStatement"/>
<rule ref="Generic.CodeAnalysis.UnusedFunctionParameter"/>
<rule ref="Generic.CodeAnalysis.UselessOverridingMethod"/>
<rule ref="Generic.ControlStructures.InlineControlStructure"/>
<rule ref="Squiz.PHP.NonExecutableCode"/>
<file>./src</file>
<file>./tests</file>
</ruleset>

View File

@@ -1,11 +1,12 @@
<?php namespace ivuorinen\Curly; <?php
namespace ivuorinen\Curly;
use CurlHandle;
use League\Uri\Uri; use League\Uri\Uri;
/** /**
* Class Curly * Class Curly
*
* @package ivuorinen\Curly
*/ */
class Curly class Curly
{ {
@@ -22,10 +23,10 @@ class Curly
private int $timeout = 30; private int $timeout = 30;
/** @var string HTTP Version (1.0/1.1) */ /** @var string HTTP Version (1.0/1.1) */
private string $httpVersion = "1.0"; private string $httpVersion = '1.1';
/** /**
* Auth method to use. It currently support only: * Auth method to use. It currently supports only:
* - BASIC * - BASIC
* - NTLM * - NTLM
* *
@@ -45,56 +46,53 @@ class Curly
/** @var string Content type */ /** @var string Content type */
private string $contentType = 'application/x-www-form-urlencoded'; private string $contentType = 'application/x-www-form-urlencoded';
/** @var array Array of headers to send */ /** @var array|string[] Array of headers to send */
private array $headers = [ private array $headers = [
'Accept' => 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8', 'Accept' => 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
'Accept-Language' => 'en-us,en;q=0.5', 'Accept-Language' => 'en-us,en;q=0.5',
'Accept-Encoding' => 'gzip,deflate', 'Accept-Encoding' => 'gzip,deflate',
'Accept-Charset' => 'UTF-8;q=0.7,*;q=0.7' 'Accept-Charset' => 'UTF-8;q=0.7,*;q=0.7',
]; ];
/** @var string|null Should we use a proxy? */ /** @var string|null Should we use a proxy? */
private ?string $proxy = null; private ?string $proxy = null;
/** @var string|null */ private ?string $proxyAuth = null;
private ?string $proxy_auth = null;
/** @var array Allowed HTTP methods */ /** @var array|string[] Allowed HTTP methods */
private array $supported_auth_methods = ["BASIC", "DIGEST", "SPNEGO", "NTLM"]; private array $supportedAuthMethods = ["BASIC", "DIGEST", "SPNEGO", "NTLM"];
/** @var array Allowed HTTP authentication */ /** @var array|string[] Allowed HTTP authentication */
private array $supported_http_methods = ["GET", "POST", "PUT", "DELETE"]; private array $supportedHttpMethods = ["GET", "POST", "PUT", "DELETE"];
/** @var boolean Are we using curl? */ /** @var boolean Are we using curl? */
private bool $curl = true; private bool $curl = true;
/** @var array Received headers */ /** @var array|string[] Received headers */
private array $receivedHeaders = []; private array $receivedHeaders = [];
/** @var int|null Received http status code */ /** @var int|null Received http status code */
private ?int $receivedHttpStatus = null; private ?int $receivedHttpStatus = null;
/** @var resource|null Transfer channel */ private CurlHandle|false $ch = false;
private $ch = null;
/** @var string|null */ private ?string $streamGetData = null;
private ?string $stream_get_data = null;
/** @var boolean Output debug messages? */ /** @var boolean Output debug messages? */
private bool $verbose; private bool $verbose = false;
/** @var boolean Should we blindly trust host? */ /** @var boolean Should we blindly trust host? */
private bool $skipSSLVerify; private bool $skipSSLVerify = false;
/** /**
* Class constructor * Class constructor
* *
* @param bool|string $address Remote host address * @param string $address Remote host address
* @param bool $curl Use curl (true) or stream (false) * @param bool $curl Use curl (true) or stream (false)
* *
* @throws Exceptions\HTTPException * @throws Exceptions\HTTPException
*/ */
public function __construct(bool|string $address = false, bool $curl = true) public function __construct(string $address = '', bool $curl = true)
{ {
$this->reset(); $this->reset();
@@ -110,30 +108,30 @@ class Curly
*/ */
public function reset(): void public function reset(): void
{ {
$this->address = ''; $this->address = null;
$this->port = 80; $this->port = 80;
$this->method = "GET"; $this->method = "GET";
$this->timeout = 30; $this->timeout = 30;
$this->httpVersion = "1.0"; $this->httpVersion = "1.0";
$this->authenticationMethod = null; $this->authenticationMethod = null;
$this->user = null; $this->user = null;
$this->pass = null; $this->pass = null;
$this->userAgent = "ivuorinen-curly"; $this->userAgent = "ivuorinen-curly";
$this->contentType = "application/x-www-form-urlencoded"; $this->contentType = "application/x-www-form-urlencoded";
$this->headers = [ $this->headers = [
"Accept" => "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8", "Accept" => "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8",
"Accept-Language" => "en-us,en;q=0.5", "Accept-Language" => "en-us,en;q=0.5",
"Accept-Encoding" => "deflate", "Accept-Encoding" => "deflate",
"Accept-Charset" => "UTF-8;q=0.7,*;q=0.7" "Accept-Charset" => "UTF-8;q=0.7,*;q=0.7",
]; ];
$this->proxy = null; $this->proxy = null;
$this->proxy_auth = null; $this->proxyAuth = null;
$this->receivedHeaders = array(); $this->receivedHeaders = [];
$this->receivedHttpStatus = null; $this->receivedHttpStatus = null;
$this->stream_get_data = null; $this->streamGetData = null;
$this->verbose = false; $this->verbose = false;
$this->skipSSLVerify = false; $this->skipSSLVerify = false;
if ($this->ch !== null) { if ($this->ch) {
$this->closeTransport(); $this->closeTransport();
} }
} }
@@ -143,9 +141,9 @@ class Curly
*/ */
private function closeTransport(): void private function closeTransport(): void
{ {
if ($this->curl && !is_null($this->ch)) { if ($this->curl && $this->ch !== false) {
curl_close($this->ch); curl_close($this->ch);
$this->ch = null; $this->ch = false;
} }
} }
@@ -170,9 +168,6 @@ class Curly
return $this; return $this;
} }
/**
* @return string|null
*/
public function getHost(): ?string public function getHost(): ?string
{ {
return $this->address; return $this->address;
@@ -195,7 +190,7 @@ class Curly
} }
/** /**
* Class destructor * @return void
*/ */
public function __destruct() public function __destruct()
{ {
@@ -204,11 +199,6 @@ class Curly
} }
} }
/**
* Is Curl verbose?
*
* @return bool
*/
public function getVerbose(): bool public function getVerbose(): bool
{ {
return $this->verbose; return $this->verbose;
@@ -256,9 +246,9 @@ class Curly
/** /**
* Set http authentication * Set http authentication
* *
* @param string $method Auth method (BASIC or NTLM) * @param string $method Auth method (BASIC or NTLM)
* @param string $user Username to use * @param string $user Username to use
* @param string|null $pass User password * @param string|null $pass User password
* *
* @return self * @return self
* @throws Exceptions\HTTPException * @throws Exceptions\HTTPException
@@ -267,7 +257,7 @@ class Curly
{ {
$method = strtoupper($method); $method = strtoupper($method);
if (!in_array($method, $this->supported_auth_methods)) { if (!in_array($method, $this->supportedAuthMethods, true)) {
throw new Exceptions\HTTPException("Unsupported authentication method"); throw new Exceptions\HTTPException("Unsupported authentication method");
} }
@@ -314,33 +304,20 @@ class Curly
return $this; return $this;
} }
/** /**
* Get received headers * @return array|string[]
*
* @return array
*/ */
public function getReceivedHeaders(): array public function getReceivedHeaders(): array
{ {
return $this->receivedHeaders; return $this->receivedHeaders;
} }
/**
* Get received headers
*
* @return integer|null
*/
public function getHttpStatusCode(): ?int public function getHttpStatusCode(): ?int
{ {
return $this->receivedHttpStatus; return $this->receivedHttpStatus;
} }
/** public function getChannel(): CurlHandle|false
* Get transport channel (curl channel or stream context)
*
* @return resource|null
*/
public function getChannel()
{ {
return $this->ch; return $this->ch;
} }
@@ -348,7 +325,7 @@ class Curly
/** /**
* Init transport and send data to the remote host. * Init transport and send data to the remote host.
* *
* @param object|array|string|null $data * @param object|array|string[]|string|null $data
* *
* @return string * @return string
* @throws Exceptions\HTTPException * @throws Exceptions\HTTPException
@@ -358,20 +335,18 @@ class Curly
if ($this->curl) { if ($this->curl) {
$this->initCurl($data); $this->initCurl($data);
$received = $this->sendUsingCurl(); return $this->sendUsingCurl();
} else {
$this->initStream($data);
$received = $this->sendUsingStream();
} }
return $received; $this->initStream($data);
return $this->sendUsingStream();
} }
/** /**
* Init the CURL channel * Init the CURL channel
* *
* @param object|array|string|null $data * @param object|array|string[]|string|null $data
* *
* @throws Exceptions\HTTPException * @throws Exceptions\HTTPException
*/ */
@@ -392,7 +367,7 @@ class Curly
$this->initCurlMethod($data); $this->initCurlMethod($data);
$headers = []; $headers = [];
if (sizeof($this->headers) != 0) { if (count($this->headers) !== 0) {
foreach ($this->getHeaders() as $header => $value) { foreach ($this->getHeaders() as $header => $value) {
if (is_null($value)) { if (is_null($value)) {
$headers[] = $header; $headers[] = $header;
@@ -420,7 +395,9 @@ class Curly
} }
if ($this->skipSSLVerify) { if ($this->skipSSLVerify) {
/** @noinspection CurlSslServerSpoofingInspection */
curl_setopt($this->ch, CURLOPT_SSL_VERIFYHOST, 0); curl_setopt($this->ch, CURLOPT_SSL_VERIFYHOST, 0);
/** @noinspection CurlSslServerSpoofingInspection */
curl_setopt($this->ch, CURLOPT_SSL_VERIFYPEER, 0); curl_setopt($this->ch, CURLOPT_SSL_VERIFYPEER, 0);
} }
} }
@@ -452,7 +429,7 @@ class Curly
private function initCurlAuthenticationMethod(): void private function initCurlAuthenticationMethod(): void
{ {
if ($this->ch == null) { if (!$this->ch) {
return; return;
} }
@@ -513,45 +490,37 @@ class Curly
private function initCurlProxy(): void private function initCurlProxy(): void
{ {
if (!is_null($this->proxy) && !is_null($this->ch)) { if (!is_null($this->proxy) && $this->ch) {
curl_setopt($this->ch, CURLOPT_PROXY, $this->proxy); curl_setopt($this->ch, CURLOPT_PROXY, $this->proxy);
if (!is_null($this->proxy_auth)) { if (!is_null($this->proxyAuth)) {
curl_setopt( curl_setopt(
$this->ch, $this->ch,
CURLOPT_PROXYUSERPWD, CURLOPT_PROXYUSERPWD,
$this->proxy_auth $this->proxyAuth
); );
} }
} }
} }
/**
* @param object|array|string|null $data
*/
private function initCurlMethod(object|array|string|null $data): void private function initCurlMethod(object|array|string|null $data): void
{ {
if ($this->ch == null) { if (!$this->ch) {
return; return;
} }
switch ($this->method) { switch ($this->method) {
case 'GET': case 'GET':
if (empty($data)) { $payload = empty($data)
curl_setopt( ? $this->address
$this->ch, : $this->address . "?" . $this->parseData($data);
CURLOPT_URL,
$this->address curl_setopt(
); $this->ch,
} else { CURLOPT_URL,
$payload = $this->address $payload
. "?" . $this->parseData($data); );
curl_setopt(
$this->ch,
CURLOPT_URL,
$payload
);
}
return; return;
case 'PUT': case 'PUT':
@@ -579,6 +548,7 @@ class Curly
CURLOPT_URL, CURLOPT_URL,
$this->address $this->address
); );
return; return;
case 'POST': case 'POST':
@@ -605,6 +575,7 @@ class Curly
CURLOPT_URL, CURLOPT_URL,
$this->address $this->address
); );
return; return;
case 'DELETE': case 'DELETE':
@@ -632,6 +603,7 @@ class Curly
CURLOPT_URL, CURLOPT_URL,
$this->address $this->address
); );
return; return;
} }
} }
@@ -645,7 +617,7 @@ class Curly
*/ */
public function parseData(object|array|string $data = ''): string public function parseData(object|array|string $data = ''): string
{ {
return (is_array($data) or is_object($data)) return (is_array($data) || is_object($data))
? http_build_query($data) ? http_build_query($data)
: $data; : $data;
} }
@@ -653,14 +625,15 @@ class Curly
/** /**
* Set header component * Set header component
* *
* @param string $header Header name * @param string $header Header name
* @param string|null $value Header content (optional) * @param string|null $value Header content (optional)
* *
* @return self * @return self
*/ */
public function setHeader(string $header, string $value = null): self public function setHeader(string $header, string $value = null): self
{ {
$this->headers[$header] = $value; $this->headers[$header] = $value;
return $this; return $this;
} }
@@ -682,7 +655,7 @@ class Curly
*/ */
private function sendUsingCurl(): string private function sendUsingCurl(): string
{ {
if ($this->ch == null) { if (!$this->ch) {
throw new Exceptions\HTTPException( throw new Exceptions\HTTPException(
"No CURL available" "No CURL available"
); );
@@ -702,7 +675,7 @@ class Curly
CURLINFO_HTTP_CODE CURLINFO_HTTP_CODE
); );
$header_size = curl_getinfo( $headerSize = curl_getinfo(
$this->ch, $this->ch,
CURLINFO_HEADER_SIZE CURLINFO_HEADER_SIZE
); );
@@ -710,12 +683,12 @@ class Curly
$headers = substr( $headers = substr(
$request, $request,
0, 0,
$header_size $headerSize
); );
$body = substr( $body = substr(
$request, $request,
$header_size $headerSize
); );
$this->receivedHeaders = self::tokenizeHeaders($headers); $this->receivedHeaders = self::tokenizeHeaders($headers);
@@ -734,19 +707,19 @@ class Curly
{ {
$return = []; $return = [];
$headers_array = explode("\r\n", $headers); $headersArray = explode("\r\n", $headers);
foreach ($headers_array as $header) { foreach ($headersArray as $header) {
if (empty($header)) { if (empty($header)) {
continue; continue;
} }
$header_components = explode(":", $header); $headerComponents = explode(":", $header);
if (!isset($header_components[1]) or @empty($header_components[1])) { if (!isset($headerComponents[1]) || @empty($headerComponents[1])) {
$return[] = $header_components[0]; $return[] = $headerComponents[0];
} else { } else {
$return[$header_components[0]] = $header_components[1]; $return[$headerComponents[0]] = $headerComponents[1];
} }
} }
@@ -771,56 +744,55 @@ class Curly
); );
} }
$stream_options = [ $streamOptions = [
"http" => [ "http" => [
"method" => $this->method, "method" => $this->method,
"protocol_version" => $this->httpVersion == "NONE" "protocol_version" => $this->httpVersion === "NONE"
? "1.0" ? "1.0"
: $this->httpVersion, : $this->httpVersion,
"user_agent" => $this->userAgent, "user_agent" => $this->userAgent,
"timeout" => $this->timeout, "timeout" => $this->timeout,
"header" => [ "header" => [
"Connection: close" "Connection: close",
] ],
] ],
]; ];
if (!is_null($this->proxy)) { if (!is_null($this->proxy)) {
$stream_options['http']['proxy'] = $this->proxy; $streamOptions['http']['proxy'] = $this->proxy;
if (!is_null($this->proxy_auth)) { if (!is_null($this->proxyAuth)) {
$stream_options['http']['header'][] = 'Proxy-Authorization: Basic ' $streamOptions['http']['header'][] = 'Proxy-Authorization: Basic '
. base64_encode($this->proxy_auth); . base64_encode($this->proxyAuth);
} }
} }
if ($this->authenticationMethod == "BASIC") { if ($this->authenticationMethod === "BASIC") {
$stream_options["http"]["header"][] = "Authorization: Basic " $streamOptions["http"]["header"][] = "Authorization: Basic "
. base64_encode($this->user . ":" . $this->pass); . base64_encode($this->user . ":" . $this->pass);
} }
foreach ($this->getHeaders() as $header => $value) { foreach ($this->getHeaders() as $header => $value) {
if (is_null($value)) { if (is_null($value)) {
$stream_options["http"]["header"][] = $header; $streamOptions["http"]["header"][] = $header;
} else { } else {
$stream_options["http"]["header"][] = $header . ': ' . $value; $streamOptions["http"]["header"][] = $header . ': ' . $value;
} }
} }
if (!empty($data)) { if (!empty($data)) {
$data_query = $this->parseData($data); $dataQuery = $this->parseData($data);
if ($this->method == "GET") { if ($this->method === "GET") {
$this->stream_get_data = $data_query; $this->streamGetData = $dataQuery;
} else { } else {
$stream_options['http']['header'][] = "Content-Type: " . $this->contentType; $streamOptions['http']['header'][] = "Content-Type: " . $this->contentType;
$stream_options['http']['header'][] = "Content-Length: " . strlen($data_query); $streamOptions['http']['header'][] = "Content-Length: " . strlen($dataQuery);
$streamOptions['http']['content'] = $dataQuery;
$stream_options['http']['content'] = $data_query;
} }
} }
$this->ch = stream_context_create($stream_options); $this->ch = stream_context_create($streamOptions);
if (!$this->ch) { if (!$this->ch) {
throw new Exceptions\HTTPException("Cannot init data channel"); throw new Exceptions\HTTPException("Cannot init data channel");
@@ -830,6 +802,7 @@ class Curly
/** /**
* Send data via STREAM * Send data via STREAM
* *
* @uses \League\Uri\Uri
* @return string * @return string
* *
* @throws Exceptions\HTTPException * @throws Exceptions\HTTPException
@@ -837,7 +810,7 @@ class Curly
*/ */
private function sendUsingStream(): string private function sendUsingStream(): string
{ {
if (is_null($this->address) || mb_strlen($this->address) < 1) { if (empty($this->address)) {
throw new Exceptions\HTTPException( throw new Exceptions\HTTPException(
"No address to use with stream" "No address to use with stream"
); );
@@ -846,20 +819,20 @@ class Curly
$parts = parse_url($this->address); $parts = parse_url($this->address);
$url = Uri::createFromComponents([ $url = Uri::createFromComponents([
'scheme' => $parts['scheme'] ?? null, 'scheme' => $parts['scheme'] ?? null,
'user' => $parts['user'] ?? $this->user, 'user' => $parts['user'] ?? $this->user,
'pass' => $parts['pass'] ?? $this->pass, 'pass' => $parts['pass'] ?? $this->pass,
'host' => $parts['host'] ?? null, 'host' => $parts['host'] ?? null,
'port' => $this->port != 80 ? $this->port : 80, 'port' => $parts['port'] ?? $this->port,
'path' => $parts['path'] ?? null, 'path' => $parts['path'] ?? null,
'query' => !is_null($this->stream_get_data) 'query' => !is_null($this->streamGetData)
? $this->stream_get_data ? $this->streamGetData
: null, : null,
'fragment' => $parts['fragment'] ?? null 'fragment' => $parts['fragment'] ?? null,
]); ]);
set_error_handler( set_error_handler(
function ($severity, $message, $file, $line) { static function ($severity, $message, $file, $line) {
unset($severity, $file, $line); unset($severity, $file, $line);
throw new Exceptions\HTTPException($message); throw new Exceptions\HTTPException($message);
} }
@@ -892,17 +865,19 @@ class Curly
implode("\r\n", $http_response_header) implode("\r\n", $http_response_header)
); );
$content_encoding = array_key_exists( $contentEncoding = array_key_exists(
"Content-Encoding", "Content-Encoding",
$this->receivedHeaders $this->receivedHeaders
); );
list($version, $this->receivedHttpStatus, $msg) = explode( [$version, $receivedHttpStatus, $msg] = explode(
' ', ' ',
$this->receivedHeaders[0], $this->receivedHeaders[0],
3 3
); );
$this->receivedHttpStatus = (int) $receivedHttpStatus;
unset($version, $msg); unset($version, $msg);
$gzipped = strpos( $gzipped = strpos(
@@ -910,8 +885,8 @@ class Curly
"gzip" "gzip"
); );
$response = ($content_encoding === true && $gzipped !== false $response = ($contentEncoding === true && $gzipped !== false
? gzinflate(substr($received, 10, -8)) ?? '' ? gzinflate(substr($received, 10, -8))
: $received : $received
); );
@@ -920,7 +895,7 @@ class Curly
: $response; : $response;
} }
public function getHeader($header) public function getHeader(string $header): string|false
{ {
return $this->headers[$header] ?? false; return $this->headers[$header] ?? false;
} }
@@ -947,9 +922,6 @@ class Curly
return $received; return $received;
} }
/**
* @return int
*/
public function getPort(): int public function getPort(): int
{ {
return $this->port; return $this->port;
@@ -971,8 +943,8 @@ class Curly
"options" => [ "options" => [
"min_range" => 1, "min_range" => 1,
"max_range" => 65535, "max_range" => 65535,
"default" => 80 "default" => 80,
] ],
] ]
); );
@@ -996,11 +968,11 @@ class Curly
{ {
$method = strtoupper($method); $method = strtoupper($method);
if (!in_array($method, $this->supported_http_methods)) { if (!in_array($method, $this->supportedHttpMethods, true)) {
throw new Exceptions\HTTPException( throw new Exceptions\HTTPException(
"Unsupported HTTP method: " . $method "Unsupported HTTP method: " . $method
. '. Should be one of: ' . '. Should be one of: '
. implode(', ', $this->supported_http_methods) . implode(', ', $this->supportedHttpMethods)
); );
} }
@@ -1043,9 +1015,9 @@ class Curly
/** /**
* Set HTTP method to use * Set HTTP method to use
* *
* @param string $address Proxy URL or IP address * @param string $address Proxy URL or IP address
* @param string|null $user (optional) Username for proxy auth * @param string|null $user (optional) User name for proxy auth
* @param string|null $pass (optional) User password for proxy auth * @param string|null $pass (optional) User password for proxy auth
* *
* @return self * @return self
* @throws Exceptions\HTTPException * @throws Exceptions\HTTPException
@@ -1054,7 +1026,7 @@ class Curly
{ {
$proxy = filter_var($address, FILTER_VALIDATE_URL); $proxy = filter_var($address, FILTER_VALIDATE_URL);
if (!$proxy) { if ($proxy === false) {
throw new Exceptions\HTTPException( throw new Exceptions\HTTPException(
"Invalid proxy address or URL" "Invalid proxy address or URL"
); );
@@ -1062,11 +1034,9 @@ class Curly
$this->proxy = $proxy; $this->proxy = $proxy;
$this->proxy_auth = ( $this->proxyAuth = !is_null($user) && !is_null($pass)
!is_null($user) && !is_null($pass)
? $user . ':' . $pass ? $user . ':' . $pass
: (!is_null($user) ? $user : null) : $user;
);
return $this; return $this;
} }
@@ -1111,7 +1081,7 @@ class Curly
public function setContentType(string $type): self public function setContentType(string $type): self
{ {
if (empty($type)) { if (empty($type)) {
throw new Exceptions\HTTPException("Content Type cannot be null"); throw new Exceptions\HTTPException("Content Type cannot be empty");
} }
$this->contentType = $type; $this->contentType = $type;
@@ -1119,7 +1089,7 @@ class Curly
return $this; return $this;
} }
public function getHttpVersion(): ?string public function getHttpVersion(): string
{ {
return $this->httpVersion; return $this->httpVersion;
} }

View File

@@ -15,10 +15,10 @@ class CurlyBasicsTest extends TestCase
* *
* @throws \ivuorinen\Curly\Exceptions\HTTPException * @throws \ivuorinen\Curly\Exceptions\HTTPException
*/ */
public function testIsThereAnySyntaxError() public function testIsThereAnySyntaxError(): void
{ {
$var = new \ivuorinen\Curly\Curly; $var = new \ivuorinen\Curly\Curly();
$this->assertTrue(is_object($var)); $this->assertIsObject($var);
unset($var); unset($var);
} }
@@ -31,14 +31,14 @@ class CurlyBasicsTest extends TestCase
* *
* @throws \ivuorinen\Curly\Exceptions\HTTPException * @throws \ivuorinen\Curly\Exceptions\HTTPException
*/ */
public function testParseData() public function testParseData(): void
{ {
$var = new \ivuorinen\Curly\Curly; $var = new \ivuorinen\Curly\Curly();
$expected = "foo=bar&baz=buzz"; $expected = "foo=bar&baz=buzz";
// String // String
$this->assertTrue($var->parseData($expected) == $expected); $this->assertEquals($expected, $var->parseData($expected));
// Array // Array
$this->assertEquals( $this->assertEquals(
@@ -47,7 +47,7 @@ class CurlyBasicsTest extends TestCase
); );
// Object // Object
$actual = new \stdClass(); $actual = new \stdClass();
$actual->foo = "bar"; $actual->foo = "bar";
$actual->baz = "buzz"; $actual->baz = "buzz";

View File

@@ -7,32 +7,36 @@ use PHPUnit\Framework\TestCase;
class CurlyMethodsTest extends TestCase class CurlyMethodsTest extends TestCase
{ {
protected $curly = false; protected Curly $curly;
public function testPort() public function testPort(): void
{ {
$this->assertEquals(80, $this->curly->getPort()); $this->assertEquals(80, $this->curly->getPort());
$this->curly->setPort(1234); $this->curly->setPort(1234);
$this->assertEquals(1234, $this->curly->getPort()); $this->assertEquals(1234, $this->curly->getPort());
} }
public function testTimeout() public function testTimeout(): void
{ {
$this->assertEquals(30, $this->curly->getTimeout()); $this->assertEquals(30, $this->curly->getTimeout());
$this->curly->setTimeout(10); $this->curly->setTimeout(10);
$this->assertEquals(10, $this->curly->getTimeout()); $this->assertEquals(10, $this->curly->getTimeout());
} }
public function testSkipSSLVerify() public function testSkipSSLVerify(): void
{ {
$this->assertTrue(method_exists( $this->assertTrue(
$this->curly, method_exists(
'getSkipSSLVerify' $this->curly,
)); 'getSkipSSLVerify'
$this->assertTrue(method_exists( )
$this->curly, );
'setSkipSSLVerify' $this->assertTrue(
)); method_exists(
$this->curly,
'setSkipSSLVerify'
)
);
$this->assertFalse($this->curly->getSkipSSLVerify()); $this->assertFalse($this->curly->getSkipSSLVerify());
$this->curly->setSkipSSLVerify(true); $this->curly->setSkipSSLVerify(true);
@@ -41,7 +45,7 @@ class CurlyMethodsTest extends TestCase
$this->assertFalse($this->curly->getSkipSSLVerify()); $this->assertFalse($this->curly->getSkipSSLVerify());
} }
public function testHttpMethod() public function testHttpMethod(): void
{ {
$this->assertTrue(method_exists($this->curly, 'getMethod')); $this->assertTrue(method_exists($this->curly, 'getMethod'));
$this->assertTrue(method_exists($this->curly, 'setMethod')); $this->assertTrue(method_exists($this->curly, 'setMethod'));
@@ -55,7 +59,7 @@ class CurlyMethodsTest extends TestCase
); );
} }
public function testVerbose() public function testVerbose(): void
{ {
$this->assertTrue(method_exists($this->curly, 'getVerbose')); $this->assertTrue(method_exists($this->curly, 'getVerbose'));
$this->assertTrue(method_exists($this->curly, 'setVerbose')); $this->assertTrue(method_exists($this->curly, 'setVerbose'));
@@ -65,7 +69,7 @@ class CurlyMethodsTest extends TestCase
$this->assertTrue($this->curly->getVerbose()); $this->assertTrue($this->curly->getVerbose());
} }
public function testProxy() public function testProxy(): void
{ {
$this->assertTrue(method_exists($this->curly, 'getProxy')); $this->assertTrue(method_exists($this->curly, 'getProxy'));
$this->assertTrue(method_exists($this->curly, 'setProxy')); $this->assertTrue(method_exists($this->curly, 'setProxy'));
@@ -77,7 +81,7 @@ class CurlyMethodsTest extends TestCase
$this->assertEquals($proxy, $this->curly->getProxy()); $this->assertEquals($proxy, $this->curly->getProxy());
} }
public function testUserAgent() public function testUserAgent(): void
{ {
$this->assertTrue(method_exists($this->curly, 'getUserAgent')); $this->assertTrue(method_exists($this->curly, 'getUserAgent'));
$this->assertTrue(method_exists($this->curly, 'setUserAgent')); $this->assertTrue(method_exists($this->curly, 'setUserAgent'));
@@ -94,20 +98,32 @@ class CurlyMethodsTest extends TestCase
); );
} }
public function testHeaders() public function testHeaders(): void
{ {
$this->assertTrue(method_exists( $this->assertTrue(
$this->curly, 'getHeaders' method_exists(
)); $this->curly,
$this->assertTrue(method_exists( 'getHeaders'
$this->curly, 'setHeader' )
)); );
$this->assertTrue(method_exists( $this->assertTrue(
$this->curly, 'getHeader' method_exists(
)); $this->curly,
$this->assertTrue(method_exists( 'setHeader'
$this->curly, 'unsetHeader' )
)); );
$this->assertTrue(
method_exists(
$this->curly,
'getHeader'
)
);
$this->assertTrue(
method_exists(
$this->curly,
'unsetHeader'
)
);
$headers = $this->curly->getHeaders(); $headers = $this->curly->getHeaders();
@@ -115,11 +131,13 @@ class CurlyMethodsTest extends TestCase
'Accept', 'Accept',
'Accept-Language', 'Accept-Language',
'Accept-Encoding', 'Accept-Encoding',
'Accept-Charset' 'Accept-Charset',
]; ];
foreach ($defaultHeaders as $defaultHeader) { foreach ($defaultHeaders as $defaultHeader) {
$this->assertArrayHasKey( $this->assertArrayHasKey(
$defaultHeader, $headers, $defaultHeader $defaultHeader,
$headers,
$defaultHeader
); );
$this->curly->setHeader($defaultHeader, 'testing'); $this->curly->setHeader($defaultHeader, 'testing');
@@ -134,14 +152,20 @@ class CurlyMethodsTest extends TestCase
} }
} }
public function testContentType() public function testContentType(): void
{ {
$this->assertTrue(method_exists( $this->assertTrue(
$this->curly, 'getContentType' method_exists(
)); $this->curly,
$this->assertTrue(method_exists( 'getContentType'
$this->curly, 'setContentType' )
)); );
$this->assertTrue(
method_exists(
$this->curly,
'setContentType'
)
);
$default = 'application/x-www-form-urlencoded'; $default = 'application/x-www-form-urlencoded';
$this->assertEquals($default, $this->curly->getContentType()); $this->assertEquals($default, $this->curly->getContentType());
@@ -153,7 +177,7 @@ class CurlyMethodsTest extends TestCase
); );
} }
public function testAuth() public function testAuth(): void
{ {
$this->curly->setAuth( $this->curly->setAuth(
'BASIC', 'BASIC',
@@ -169,7 +193,7 @@ class CurlyMethodsTest extends TestCase
$this->assertEquals('pass', $this->curly->getPass()); $this->assertEquals('pass', $this->curly->getPass());
} }
public function testHttpVersion() public function testHttpVersion(): void
{ {
$this->assertEquals( $this->assertEquals(
'1.0', '1.0',
@@ -189,17 +213,14 @@ class CurlyMethodsTest extends TestCase
); );
} }
public function testHost() public function testHost(): void
{ {
$test = 'http://user:password@example.com/xyz'; $test = 'http://user:password@example.com/xyz';
$this->curly->setHost($test); $this->curly->setHost($test);
$this->assertEquals($test, $this->curly->getHost()); $this->assertEquals($test, $this->curly->getHost());
} }
/** protected function setUp(): void
* @throws \ivuorinen\Curly\Exceptions\HTTPException
*/
protected function setUp() : void
{ {
parent::setUp(); parent::setUp();
$this->curly = new Curly; $this->curly = new Curly;