From 526b8ac6c9ea5e9dcd9b9e7559ea666538cdce2c Mon Sep 17 00:00:00 2001 From: Roman Grishin Date: Wed, 17 May 2023 08:24:03 +0300 Subject: [PATCH 1/7] Update .gitignore --- .gitignore | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/.gitignore b/.gitignore index 77aae3d..df26bab 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,10 @@ /.idea /composer.lock /vendor +/build +/test-reports +!.editorconfig +!.gitattributes +!.gitignore +!.gitkeep +!.php-cs-fixer.dist.php From 676295e038c4bfcafc71c14194dd3a59f38440d0 Mon Sep 17 00:00:00 2001 From: Roman Grishin Date: Wed, 17 May 2023 08:39:00 +0300 Subject: [PATCH 2/7] Code style fix --- src/Lamia/HttpClient/HttpClient.php | 20 ++++++++++---------- src/Lamia/HttpClient/HttpClientInterface.php | 12 ++++++------ 2 files changed, 16 insertions(+), 16 deletions(-) diff --git a/src/Lamia/HttpClient/HttpClient.php b/src/Lamia/HttpClient/HttpClient.php index 4d4db51..476c6eb 100644 --- a/src/Lamia/HttpClient/HttpClient.php +++ b/src/Lamia/HttpClient/HttpClient.php @@ -15,7 +15,7 @@ class HttpClient implements HttpClientInterface */ protected $client; - protected $headers = array(); + protected $headers = []; private $lastResponse; private $lastRequest; @@ -29,7 +29,7 @@ class HttpClient implements HttpClientInterface * @param array $options * @param ClientInterface $client */ - public function __construct($baseUrl, array $options = array(), ClientInterface $client = null) + public function __construct($baseUrl, array $options = [], ClientInterface $client = null) { $this->options = array_merge($this->options, $options); $this->options['base_uri'] = $baseUrl; @@ -74,7 +74,7 @@ public function clearHeaders() /** * {@inheritDoc} */ - public function get($path, array $parameters = array(), array $headers = array()) + public function get($path, array $parameters = [], array $headers = []) { $path .= '?'. http_build_query($parameters); return $this->request($path, null, 'GET', $headers); @@ -83,7 +83,7 @@ public function get($path, array $parameters = array(), array $headers = array() /** * {@inheritDoc} */ - public function post($path, $body = null, array $headers = array()) + public function post($path, $body = null, array $headers = []) { if (!isset($headers['Content-Type'])) { $headers['Content-Type'] = 'application/x-www-form-urlencoded'; @@ -95,7 +95,7 @@ public function post($path, $body = null, array $headers = array()) /** * {@inheritDoc} */ - public function patch($path, $body = null, array $headers = array()) + public function patch($path, $body = null, array $headers = []) { if (!isset($headers['Content-Type'])) { $headers['Content-Type'] = 'application/x-www-form-urlencoded'; @@ -107,7 +107,7 @@ public function patch($path, $body = null, array $headers = array()) /** * {@inheritDoc} */ - public function delete($path, $body = null, array $headers = array()) + public function delete($path, $body = null, array $headers = []) { return $this->request($path, $body, 'DELETE', $headers); } @@ -115,7 +115,7 @@ public function delete($path, $body = null, array $headers = array()) /** * {@inheritDoc} */ - public function put($path, $body, array $headers = array()) + public function put($path, $body, array $headers = []) { if (!isset($headers['Content-Type'])) { $headers['Content-Type'] = 'application/x-www-form-urlencoded'; @@ -130,8 +130,8 @@ public function request( $path, $body = null, $httpMethod = 'GET', - array $headers = array(), - array $options = array() + array $headers = [], + array $options = [] ) { $request = $this->createRequest($httpMethod, $path, $body, $headers); try { @@ -172,7 +172,7 @@ protected function createRequest( $httpMethod, $path, $body = null, - array $headers = array() + array $headers = [] ) { return new Request( $httpMethod, diff --git a/src/Lamia/HttpClient/HttpClientInterface.php b/src/Lamia/HttpClient/HttpClientInterface.php index 97cc18d..7dee8ee 100644 --- a/src/Lamia/HttpClient/HttpClientInterface.php +++ b/src/Lamia/HttpClient/HttpClientInterface.php @@ -16,7 +16,7 @@ interface HttpClientInterface * * @return Response */ - public function get($path, array $parameters = array(), array $headers = array()); + public function get($path, array $parameters = [], array $headers = []); /** * Send a POST request * @@ -26,7 +26,7 @@ public function get($path, array $parameters = array(), array $headers = array() * * @return Response */ - public function post($path, $body = null, array $headers = array()); + public function post($path, $body = null, array $headers = []); /** * Send a PATCH request * @@ -37,7 +37,7 @@ public function post($path, $body = null, array $headers = array()); * @internal param array $parameters Request body * @return Response */ - public function patch($path, $body = null, array $headers = array()); + public function patch($path, $body = null, array $headers = []); /** * Send a PUT request * @@ -47,7 +47,7 @@ public function patch($path, $body = null, array $headers = array()); * * @return Response */ - public function put($path, $body, array $headers = array()); + public function put($path, $body, array $headers = []); /** * Send a DELETE request * @@ -57,7 +57,7 @@ public function put($path, $body, array $headers = array()); * * @return Response */ - public function delete($path, $body = null, array $headers = array()); + public function delete($path, $body = null, array $headers = []); /** * Send a request to the server, receive a response, * decode the response and returns an associative array @@ -69,7 +69,7 @@ public function delete($path, $body = null, array $headers = array()); * * @return Response */ - public function request($path, $body, $httpMethod = 'GET', array $headers = array()); + public function request($path, $body, $httpMethod = 'GET', array $headers = []); /** * Change an option value. * From 248cd21a56bb0d615d2fc83528183dd0c337a276 Mon Sep 17 00:00:00 2001 From: Roman Grishin Date: Wed, 17 May 2023 08:39:44 +0300 Subject: [PATCH 3/7] Fix function return type --- src/Lamia/HttpClient/HttpClient.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Lamia/HttpClient/HttpClient.php b/src/Lamia/HttpClient/HttpClient.php index 476c6eb..350da9a 100644 --- a/src/Lamia/HttpClient/HttpClient.php +++ b/src/Lamia/HttpClient/HttpClient.php @@ -166,7 +166,7 @@ public function getLastResponse() * @param string $httpMethod * @param string $path * - * @return mixed|\Psr\Http\Message\ResponseInterface + * @return Request */ protected function createRequest( $httpMethod, From bebc32c42e1ee625bad550240d7712b2e31516c3 Mon Sep 17 00:00:00 2001 From: Roman Grishin Date: Wed, 17 May 2023 08:40:22 +0300 Subject: [PATCH 4/7] allow guzzlehttp 7.0 --- composer.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/composer.json b/composer.json index 3ccfb79..08feea0 100644 --- a/composer.json +++ b/composer.json @@ -12,7 +12,7 @@ } ], "require": { - "guzzlehttp/guzzle": "6.*" + "guzzlehttp/guzzle": "^6.0 || ^7.0" }, "require-dev": { "phpunit/phpunit": "~5.7" From 445dd86582ba8d9b72bca44114a23ad5d3a0bf4e Mon Sep 17 00:00:00 2001 From: Roman Grishin Date: Wed, 17 May 2023 08:41:27 +0300 Subject: [PATCH 5/7] Remove bootstrap test --- tests/bootstrap.php | 21 --------------------- 1 file changed, 21 deletions(-) delete mode 100644 tests/bootstrap.php diff --git a/tests/bootstrap.php b/tests/bootstrap.php deleted file mode 100644 index ec0a61f..0000000 --- a/tests/bootstrap.php +++ /dev/null @@ -1,21 +0,0 @@ -add('Lamia\HttpClient', __DIR__); - -return $loader; From e145a54e49c94de6295e573b22d9248c9ea8e29c Mon Sep 17 00:00:00 2001 From: Roman Grishin Date: Wed, 17 May 2023 08:45:03 +0300 Subject: [PATCH 6/7] Apply array short syntax --- tests/Lamia/HttpClient/HttpClientTest.php | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/tests/Lamia/HttpClient/HttpClientTest.php b/tests/Lamia/HttpClient/HttpClientTest.php index 28cf124..ad2edb0 100644 --- a/tests/Lamia/HttpClient/HttpClientTest.php +++ b/tests/Lamia/HttpClient/HttpClientTest.php @@ -1,4 +1,5 @@ get('posts', array('userId' => 2))->getBody()->getContents()); + $result = json_decode($newInstance->get('posts', ['userId' => 2])->getBody()->getContents()); $this->assertEquals(2, $result[1]->userId); } public function testDummyPostRequest() { $newInstance = new HttpClient(self::TEST_URL); - $result = json_decode($newInstance->post('posts', json_encode(array('title' => 'test', 'body' => 'blabla', 'userId' => 1)))->getBody()->getContents()); + $result = json_decode($newInstance->post('posts', json_encode(['title' => 'test', 'body' => 'blabla', 'userId' => 1]))->getBody()->getContents()); $this->assertNotEmpty($result->id); } @@ -38,7 +39,7 @@ public function testDummyPostRequest() public function testDummyPutRequest() { $newInstance = new HttpClient(self::TEST_URL); - $result = json_decode($newInstance->put('posts/1', json_encode(array('id' => 1, 'title' => 'test', 'body' => 'blabla', 'userId' => 1)))->getBody()->getContents()); + $result = json_decode($newInstance->put('posts/1', json_encode(['id' => 1, 'title' => 'test', 'body' => 'blabla', 'userId' => 1]))->getBody()->getContents()); $this->assertEquals(1, $result->id); } From 4b2df14f8a59968fde269f22736a65427248e911 Mon Sep 17 00:00:00 2001 From: Roman Grishin Date: Wed, 17 May 2023 08:52:14 +0300 Subject: [PATCH 7/7] Specify exceptions thrown; fix code style --- src/Lamia/HttpClient/HttpClient.php | 2 +- src/Lamia/HttpClient/HttpClientInterface.php | 28 +++++++++++++------- 2 files changed, 19 insertions(+), 11 deletions(-) diff --git a/src/Lamia/HttpClient/HttpClient.php b/src/Lamia/HttpClient/HttpClient.php index 350da9a..7a309ce 100644 --- a/src/Lamia/HttpClient/HttpClient.php +++ b/src/Lamia/HttpClient/HttpClient.php @@ -66,7 +66,7 @@ public function setHeaders(array $headers) public function clearHeaders() { $this->headers = array( - 'User-Agent' => sprintf('%s', $this->options['user_agent']), + 'User-Agent' => \sprintf('%s', $this->options['user_agent']), ); } diff --git a/src/Lamia/HttpClient/HttpClientInterface.php b/src/Lamia/HttpClient/HttpClientInterface.php index 7dee8ee..aab7f46 100644 --- a/src/Lamia/HttpClient/HttpClientInterface.php +++ b/src/Lamia/HttpClient/HttpClientInterface.php @@ -2,8 +2,11 @@ namespace Lamia\HttpClient; -use GuzzleHttp\Psr7\Response; +use GuzzleHttp\Exception\GuzzleException; +use Lamia\HttpClient\Exception\ErrorException; use Lamia\HttpClient\Exception\InvalidArgumentException; +use Lamia\HttpClient\Exception\RuntimeException; +use Psr\Http\Message\ResponseInterface; interface HttpClientInterface { @@ -14,7 +17,7 @@ interface HttpClientInterface * @param array $parameters GET Parameters * @param array $headers Reconfigure the request headers for this call only * - * @return Response + * @return ResponseInterface */ public function get($path, array $parameters = [], array $headers = []); /** @@ -24,7 +27,7 @@ public function get($path, array $parameters = [], array $headers = []); * @param mixed $body Request body * @param array $headers Reconfigure the request headers for this call only * - * @return Response + * @return ResponseInterface */ public function post($path, $body = null, array $headers = []); /** @@ -35,7 +38,7 @@ public function post($path, $body = null, array $headers = []); * @param array $headers Reconfigure the request headers for this call only * * @internal param array $parameters Request body - * @return Response + * @return ResponseInterface */ public function patch($path, $body = null, array $headers = []); /** @@ -45,7 +48,7 @@ public function patch($path, $body = null, array $headers = []); * @param mixed $body Request body * @param array $headers Reconfigure the request headers for this call only * - * @return Response + * @return ResponseInterface */ public function put($path, $body, array $headers = []); /** @@ -55,7 +58,7 @@ public function put($path, $body, array $headers = []); * @param mixed $body Request body * @param array $headers Reconfigure the request headers for this call only * - * @return Response + * @return ResponseInterface */ public function delete($path, $body = null, array $headers = []); /** @@ -67,23 +70,28 @@ public function delete($path, $body = null, array $headers = []); * @param string $httpMethod HTTP method to use * @param array $headers Request headers * - * @return Response + * @return ResponseInterface + * + * @throws ErrorException + * @throws RuntimeException + * @throws GuzzleException */ public function request($path, $body, $httpMethod = 'GET', array $headers = []); /** * Change an option value. * - * @param string $name The option name - * @param mixed $value The value + * @param string $name The option name + * @param mixed $value The value * * @throws InvalidArgumentException + * * @return void */ public function setOption($name, $value); /** * Set HTTP headers * - * @param array $headers + * @param array $headers * @return void */ public function setHeaders(array $headers);