diff --git a/examples/before_send_retry.php b/examples/before_send_retry.php index b6703a8..9be2c6e 100644 --- a/examples/before_send_retry.php +++ b/examples/before_send_retry.php @@ -1,4 +1,5 @@ headers['Content-Type']) && + if ( + isset($this->headers['Content-Type']) && preg_match($this->jsonPattern, $this->headers['Content-Type']) && ( is_array($data) || @@ -151,7 +154,8 @@ class Curl extends BaseCurl interface_exists('JsonSerializable', false) && $data instanceof \JsonSerializable ) - )) { + ) + ) { $data = \Curl\Encoder::encodeJson($data); } elseif (is_array($data)) { // Manually build a single-dimensional array from a multi-dimensional array as using curl_setopt($ch, @@ -179,12 +183,14 @@ class Curl extends BaseCurl } } - if (!$binary_data && + if ( + !$binary_data && (is_array($data) || is_object($data)) && ( !isset($this->headers['Content-Type']) || !preg_match('/^multipart\/form-data/', $this->headers['Content-Type']) - )) { + ) + ) { // Avoid using http_build_query() as keys with null values are // unexpectedly excluded from the resulting string. // @@ -1310,10 +1316,12 @@ class Curl extends BaseCurl echo 'Request contained ' . ($request_body_empty ? 'no body' : 'a body') . '.' . "\n"; - if ($request_headers_count === 0 && ( + if ( + $request_headers_count === 0 && ( $this->getOpt(CURLOPT_VERBOSE) || !$this->getOpt(CURLINFO_HEADER_OUT) - )) { + ) + ) { echo 'Warning: Request headers (Curl::requestHeaders) are expected to be empty ' . '(CURLOPT_VERBOSE was enabled or CURLINFO_HEADER_OUT was disabled).' . "\n"; @@ -1378,8 +1386,10 @@ class Curl extends BaseCurl echo 'Response content length (calculated): ' . $response_calculated_length . "\n"; } - if (isset($this->responseHeaders['Content-Type']) && - preg_match($this->jsonPattern, $this->responseHeaders['Content-Type'])) { + if ( + isset($this->responseHeaders['Content-Type']) && + preg_match($this->jsonPattern, $this->responseHeaders['Content-Type']) + ) { $parsed_response = json_decode($this->rawResponse, true); if ($parsed_response !== null) { $messages = []; @@ -1617,8 +1627,10 @@ class Curl extends BaseCurl public function __get($name) { $return = null; - if (in_array($name, self::$deferredProperties, true) && - is_callable([$this, $getter = 'get' . ucfirst($name)])) { + if ( + in_array($name, self::$deferredProperties, true) && + is_callable([$this, $getter = 'get' . ucfirst($name)]) + ) { $return = $this->$name = $this->$getter(); } return $return; @@ -1915,8 +1927,10 @@ class Curl extends BaseCurl } } - if (isset($response_headers['Content-Encoding']) && $response_headers['Content-Encoding'] === 'gzip' && - is_string($response)) { + if ( + isset($response_headers['Content-Encoding']) && $response_headers['Content-Encoding'] === 'gzip' && + is_string($response) + ) { // Use @ to suppress message "Warning gzdecode(): data error". $decoded_response = @gzdecode($response); if ($decoded_response !== false) { diff --git a/src/Curl/Decoder.php b/src/Curl/Decoder.php index d52d44c..5f98219 100644 --- a/src/Curl/Decoder.php +++ b/src/Curl/Decoder.php @@ -1,4 +1,6 @@ -queuedCurls as $curl_id => $curl) { - if (!isset($this->instanceSpecificOptions[$curl_id][$option]) || - $this->instanceSpecificOptions[$curl_id][$option] === null) { + if ( + !isset($this->instanceSpecificOptions[$curl_id][$option]) || + $this->instanceSpecificOptions[$curl_id][$option] === null + ) { $this->instanceSpecificOptions[$curl_id][$option] = $value; } } @@ -644,7 +648,8 @@ class MultiCurl extends BaseCurl $this->currentRequestCount = 0; do { - while (count($this->queuedCurls) && + while ( + count($this->queuedCurls) && count($this->activeCurls) < $this->concurrency && (!$this->rateLimitEnabled || $this->hasRequestQuota()) ) { @@ -690,8 +695,10 @@ class MultiCurl extends BaseCurl } } - while ((is_resource($this->multiCurl) || $this->multiCurl instanceof \CurlMultiHandle) && - (($info_array = curl_multi_info_read($this->multiCurl)) !== false)) { + while ( + (is_resource($this->multiCurl) || $this->multiCurl instanceof \CurlMultiHandle) && + (($info_array = curl_multi_info_read($this->multiCurl)) !== false) + ) { if ($info_array['msg'] === CURLMSG_DONE) { foreach ($this->activeCurls as $key => $curl) { if ($curl->curl === $info_array['handle']) { diff --git a/src/Curl/StringUtil.php b/src/Curl/StringUtil.php index a2cf6d8..cc7c68b 100644 --- a/src/Curl/StringUtil.php +++ b/src/Curl/StringUtil.php @@ -1,4 +1,6 @@ -relativeUrl = $relative_url; } - public function __toString() : string + public function __toString(): string { return $this->absolutizeUrl(); } diff --git a/tests/ContentRangeServer.php b/tests/ContentRangeServer.php index c691d73..5be219a 100644 --- a/tests/ContentRangeServer.php +++ b/tests/ContentRangeServer.php @@ -1,4 +1,6 @@ - 'OK', 'POST' => 'OK', 'PUT' => 'OK', @@ -1120,7 +1125,8 @@ class PHPCurlClassTest extends \PHPUnit\Framework\TestCase 'DELETE' => 'OK', 'HEAD' => '', 'OPTIONS' => 'OK', - ] as $request_method => $expected_response) { + ] as $request_method => $expected_response + ) { $curl = new Curl(); $curl->setHeader('X-DEBUG-TEST', 'response_body'); $this->assertEquals($expected_response, $curl->$request_method(Test::TEST_URL)); @@ -1489,7 +1495,8 @@ class PHPCurlClassTest extends \PHPUnit\Framework\TestCase public function testJsonRequest() { - foreach ([ + foreach ( + [ [ [ 'key' => 'value', @@ -1507,24 +1514,29 @@ class PHPCurlClassTest extends \PHPUnit\Framework\TestCase ], '{"key":"value","strings":["a","b","c"]}', ], - ] as $test) { + ] as $test + ) { list($data, $expected_response) = $test; $test = new Test(); $this->assertEquals($expected_response, $test->server('post_json', 'POST', json_encode($data))); - foreach ([ + foreach ( + [ 'Content-Type', 'content-type', - 'CONTENT-TYPE'] as $key) { - foreach ([ + 'CONTENT-TYPE'] as $key + ) { + foreach ( + [ 'APPLICATION/JSON', 'APPLICATION/JSON; CHARSET=UTF-8', 'APPLICATION/JSON;CHARSET=UTF-8', 'application/json', 'application/json; charset=utf-8', 'application/json;charset=UTF-8', - ] as $value) { + ] as $value + ) { $test = new Test(); $test->curl->setHeader($key, $value); $this->assertEquals($expected_response, $test->server('post_json', 'POST', json_encode($data))); @@ -1539,18 +1551,22 @@ class PHPCurlClassTest extends \PHPUnit\Framework\TestCase public function testJsonResponse() { - foreach ([ + foreach ( + [ 'Content-Type', 'content-type', - 'CONTENT-TYPE'] as $key) { - foreach ([ + 'CONTENT-TYPE'] as $key + ) { + foreach ( + [ 'APPLICATION/JSON', 'APPLICATION/JSON; CHARSET=UTF-8', 'APPLICATION/JSON;CHARSET=UTF-8', 'application/json', 'application/json; charset=utf-8', 'application/json;charset=UTF-8', - ] as $value) { + ] as $value + ) { $test = new Test(); $test->server('json_response', 'POST', [ 'key' => $key, @@ -2850,11 +2866,14 @@ class PHPCurlClassTest extends \PHPUnit\Framework\TestCase public function testXmlResponse() { - foreach ([ + foreach ( + [ 'Content-Type', 'content-type', - 'CONTENT-TYPE'] as $key) { - foreach ([ + 'CONTENT-TYPE'] as $key + ) { + foreach ( + [ 'application/atom+xml; charset=UTF-8', 'application/atom+xml;charset=UTF-8', 'application/rss+xml', @@ -2869,7 +2888,8 @@ class PHPCurlClassTest extends \PHPUnit\Framework\TestCase 'text/xml', 'text/xml; charset=utf-8', 'text/xml;charset=utf-8', - ] as $value) { + ] as $value + ) { $test = new Test(); $test->server('xml_response', 'POST', [ 'key' => $key, @@ -4225,7 +4245,8 @@ class PHPCurlClassTest extends \PHPUnit\Framework\TestCase $test_3_output = ob_get_contents(); ob_end_clean(); - foreach ([ + foreach ( + [ '--- Begin PHP Curl Class diagnostic output ---', 'PHP Curl Class version: ' . Curl::VERSION, 'PHP version: ' . PHP_VERSION, @@ -4238,7 +4259,8 @@ class PHPCurlClassTest extends \PHPUnit\Framework\TestCase 'Received an HTTP 401 error response with message "HTTP/1.1 401 Unauthorized".', 'Received an empty response body (response="").', '--- End PHP Curl Class diagnostic output ---', - ] as $expected_string) { + ] as $expected_string + ) { $this->assertStringContainsString($expected_string, $test_1_output); $this->assertStringContainsString($expected_string, $test_2_output); $this->assertStringContainsString($expected_string, $test_3_output); @@ -4251,7 +4273,8 @@ class PHPCurlClassTest extends \PHPUnit\Framework\TestCase $test->server('error_message', 'POST'); $test_output = $test->curl->diagnose(true); - foreach ([ + foreach ( + [ '--- Begin PHP Curl Class diagnostic output ---', 'PHP Curl Class version: ' . Curl::VERSION, 'PHP version: ' . PHP_VERSION, @@ -4264,7 +4287,8 @@ class PHPCurlClassTest extends \PHPUnit\Framework\TestCase 'Received an HTTP 401 error response with message "HTTP/1.1 401 Unauthorized".', 'Received an empty response body (response="").', '--- End PHP Curl Class diagnostic output ---', - ] as $expected_string) { + ] as $expected_string + ) { $this->assertStringContainsString($expected_string, $test_output); } } diff --git a/tests/PHPCurlClass/PHPMultiCurlClassTest.php b/tests/PHPCurlClass/PHPMultiCurlClassTest.php index dfbf391..0ec41de 100644 --- a/tests/PHPCurlClass/PHPMultiCurlClassTest.php +++ b/tests/PHPCurlClass/PHPMultiCurlClassTest.php @@ -1,4 +1,6 @@ - 'value', @@ -2709,7 +2712,8 @@ class PHPMultiCurlClassTest extends \PHPUnit\Framework\TestCase ], '{"key":"value","strings":["a","b","c"]}', ], - ] as $test) { + ] as $test + ) { list($data, $expected_response) = $test; $multi_curl = new MultiCurl(); @@ -2720,18 +2724,22 @@ class PHPMultiCurlClassTest extends \PHPUnit\Framework\TestCase $multi_curl->addPost(Test::TEST_URL, json_encode($data)); $multi_curl->start(); - foreach ([ + foreach ( + [ 'Content-Type', 'content-type', - 'CONTENT-TYPE'] as $key) { - foreach ([ + 'CONTENT-TYPE'] as $key + ) { + foreach ( + [ 'APPLICATION/JSON', 'APPLICATION/JSON; CHARSET=UTF-8', 'APPLICATION/JSON;CHARSET=UTF-8', 'application/json', 'application/json; charset=utf-8', 'application/json;charset=UTF-8', - ] as $value) { + ] as $value + ) { $multi_curl = new MultiCurl(); $multi_curl->setHeader('X-DEBUG-TEST', 'post_json'); $multi_curl->setHeader($key, $value); @@ -3034,7 +3042,8 @@ class PHPMultiCurlClassTest extends \PHPUnit\Framework\TestCase $filesize = filesize($filename); - foreach ([ + foreach ( + [ false, 0, 1, @@ -3065,7 +3074,8 @@ class PHPMultiCurlClassTest extends \PHPUnit\Framework\TestCase $filesize + 2, $filesize + 3, - ] as $length) { + ] as $length + ) { $source = Test::TEST_URL; $destination = \Helper\get_tmp_file_path(); @@ -3971,7 +3981,8 @@ class PHPMultiCurlClassTest extends \PHPUnit\Framework\TestCase public function testSetRateLimitUnits() { - foreach ([ + foreach ( + [ [ 'rate_limit' => '1/s', 'expected' => [ @@ -4102,7 +4113,8 @@ class PHPMultiCurlClassTest extends \PHPUnit\Framework\TestCase 'interval_seconds' => '86400', ], ], - ] as $test) { + ] as $test + ) { $multi_curl = new MultiCurl(); $multi_curl->setRateLimit($test['rate_limit']); diff --git a/tests/PHPCurlClass/UrlTest.php b/tests/PHPCurlClass/UrlTest.php index 154f065..1d445f7 100644 --- a/tests/PHPCurlClass/UrlTest.php +++ b/tests/PHPCurlClass/UrlTest.php @@ -1,4 +1,6 @@ - - + @@ -7,12 +7,15 @@ - - + + + + + diff --git a/tests/server.php b/tests/server.php index c42b868..49c7572 100644 --- a/tests/server.php +++ b/tests/server.php @@ -1,9 +1,14 @@ -get('https://www.example.com/');