diff --git a/.php_cs b/.php_cs index 8cbe9c4..d438a45 100644 --- a/.php_cs +++ b/.php_cs @@ -1,28 +1,49 @@ - -This source file is subject to the MIT license that is bundled -with this source code in the file LICENSE. -EOF; return PhpCsFixer\Config::create() - ->setRiskyAllowed(true) - ->setRules(array( - '@Symfony' => true, - 'header_comment' => array('header' => $header), - 'array_syntax' => array('syntax' => 'short'), - 'ordered_imports' => true, - 'no_useless_else' => true, - 'no_useless_return' => true, - 'php_unit_construct' => true, - 'php_unit_strict' => true, - )) + ->setRules([ + '@PSR2' => true, + 'binary_operator_spaces' => true, + 'blank_line_after_opening_tag' => true, + 'compact_nullable_typehint' => true, + 'declare_equal_normalize' => true, + 'lowercase_cast' => true, + 'lowercase_static_reference' => true, + 'new_with_braces' => true, + 'no_unused_imports' => true, + 'no_blank_lines_after_class_opening' => true, + 'no_leading_import_slash' => true, + 'no_whitespace_in_blank_line' => true, + 'ordered_class_elements' => [ + 'order' => [ + 'use_trait', + ], + ], + 'ordered_imports' => [ + 'imports_order' => [ + 'class', + 'function', + 'const', + ], + 'sort_algorithm' => 'none', + ], + 'return_type_declaration' => true, + 'short_scalar_cast' => true, + 'single_blank_line_before_namespace' => true, + 'single_trait_insert_per_statement' => true, + 'ternary_operator_spaces' => true, + 'unary_operator_spaces' => true, + 'visibility_required' => [ + 'elements' => [ + 'const', + 'method', + 'property', + ], + ], + ]) ->setFinder( PhpCsFixer\Finder::create() ->exclude('vendor') - ->in(__DIR__) + ->in([__DIR__.'/src/', __DIR__.'/tests/']) ) -; \ No newline at end of file +; diff --git a/composer.json b/composer.json index 87d28b9..3805c61 100644 --- a/composer.json +++ b/composer.json @@ -11,9 +11,11 @@ "ext-libxml": "*" }, "require-dev": { - "phpunit/phpunit": "^6.5 || ^8.0", + "phpunit/phpunit": "^6.5 || ^8.5", "mockery/mockery": "^1.0", - "overtrue/phplint": "^1.1 || ^2.0" + "overtrue/phplint": "^1.1 || ^2.0", + "brainmaestro/composer-git-hooks": "^2.7", + "friendsofphp/php-cs-fixer": "^2.15" }, "autoload": { "psr-4": { @@ -26,5 +28,24 @@ "name": "overtrue", "email": "anzhengchao@gmail.com" } - ] + ], + "scripts": { + "post-update-cmd": [ + "cghooks update" + ], + "post-merge": "composer install", + "post-install-cmd": [ + "cghooks add --ignore-lock", + "cghooks update" + ], + "cghooks": "vendor/bin/cghooks", + "check-style": "php-cs-fixer fix --using-cache=no --diff --config=.php_cs --dry-run --ansi", + "fix-style": "php-cs-fixer fix --using-cache=no --config=.php_cs --ansi", + "test": "vendor/bin/phpunit --colors=always" + }, + "scripts-descriptions": { + "test": "Run all tests.", + "check-style": "Run style checks (only dry run - no fixing!).", + "fix-style": "Run style checks and fix violations." + } } diff --git a/src/Client.php b/src/Client.php index c28e183..fa6538e 100644 --- a/src/Client.php +++ b/src/Client.php @@ -264,14 +264,17 @@ public function uploadAsync(string $uri, array $files = [], array $form = [], ar * @param array $options * @param bool $async * - * @return \Psr\Http\Message\ResponseInterface|\Overtrue\Http\Support\Collection|array|object|string + * @return \Psr\Http\Message\ResponseInterface|\GuzzleHttp\Promise\PromiseInterface|\Overtrue\Http\Support\Collection|array|object|string */ public function request(string $uri, string $method = 'GET', array $options = [], bool $async = false) { - return $this->castResponseToType( - $this->requestRaw($uri, $method, $options, $async), - $this->config->getOption('response_type') - ); + $result = $this->requestRaw($uri, $method, $options, $async); + + $transformer = function ($response) { + return $this->castResponseToType($response, $this->config->getOption('response_type')); + }; + + return $async ? $result->then($transformer) : $transformer($result); } /** @@ -280,7 +283,7 @@ public function request(string $uri, string $method = 'GET', array $options = [] * @param array $options * @param bool $async * - * @return array|object|\Overtrue\Http\Support\Collection|\Psr\Http\Message\ResponseInterface|string + * @return \Psr\Http\Message\ResponseInterface|\GuzzleHttp\Promise\PromiseInterface|\Overtrue\Http\Support\Collection|array|object|string */ public function requestRaw(string $uri, string $method = 'GET', array $options = [], bool $async = false) { diff --git a/src/Config.php b/src/Config.php index 30f6003..cbd40a2 100644 --- a/src/Config.php +++ b/src/Config.php @@ -24,10 +24,10 @@ class Config * @var array */ protected $options = [ - 'base_uri' => null, - 'timeout' => 3000, + 'base_uri' => null, + 'timeout' => 3000, 'connect_timeout' => 3000, - 'proxy' => [], + 'proxy' => [], ]; /** diff --git a/src/Traits/HasHttpRequests.php b/src/Traits/HasHttpRequests.php index 015c8c9..6b28c08 100644 --- a/src/Traits/HasHttpRequests.php +++ b/src/Traits/HasHttpRequests.php @@ -13,7 +13,6 @@ use GuzzleHttp\Client; use GuzzleHttp\ClientInterface; -use Psr\Http\Message\ResponseInterface; /** * Trait HasHttpRequests.