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:
pull_request:
paths:
- 'composer.lock'
- "composer.lock"
jobs:
composer-diff:
name: Composer Diff
@@ -11,17 +11,20 @@ jobs:
- name: Checkout
uses: actions/checkout@v3
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
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: marocchino/sticky-pull-request-comment@v2
# An empty diff result will break this action.
if: ${{ steps.composer_diff.outputs.composer_diff_exit_code != 0 }}
with:
header: composer-diff # Creates a collapsed comment with the report
# Creates a collapsed comment with the report
header: composer-diff
message: |
<details>
<summary>Composer package changes</summary>

View File

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

View File

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

View File

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