Skip to content

Commit 50c2c4c

Browse files
committed
style: 更新代码样式细节
1 parent b93bbec commit 50c2c4c

17 files changed

+155
-181
lines changed

composer.json

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5,30 +5,22 @@
55
"homepage": "https://github.com/graze/guzzle-jsonrpc",
66
"license": "MIT",
77
"type": "library",
8-
"config": {
9-
"platform": {
10-
"ext-simplexml": "1.0",
11-
"ext-dom": "1.0",
12-
"ext-tokenizer": "1.0",
13-
"ext-xmlwriter": "1.0"
14-
}
15-
},
168
"authors": [
179
{
1810
"name": "Graze tech team",
1911
"homepage": "https://github.com/graze/guzzle-jsonrpc/graphs/contributors"
2012
}
2113
],
2214
"autoload": {
23-
"files": ["src/functions_include.php"],
2415
"psr-4": {
2516
"Graze\\GuzzleHttp\\JsonRpc\\": "src"
2617
}
2718
},
2819
"require": {
2920
"php": "^7.4|^8.0",
30-
"guzzlehttp/guzzle": "^6.0",
31-
"guzzlehttp/promises": "^1.0",
32-
"psr/http-message": "^1.0"
21+
"ext-json": "*",
22+
"guzzlehttp/guzzle": "^6.0|^7.0",
23+
"guzzlehttp/promises": "^1.0|^2.0",
24+
"psr/http-message": "^1.0|^2.0"
3325
}
3426
}

src/Client.php

Lines changed: 19 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414

1515
namespace Graze\GuzzleHttp\JsonRpc;
1616

17-
use Graze\GuzzleHttp\JsonRpc;
1817
use Graze\GuzzleHttp\JsonRpc\Message\MessageFactory;
1918
use Graze\GuzzleHttp\JsonRpc\Message\MessageFactoryInterface;
2019
use Graze\GuzzleHttp\JsonRpc\Message\RequestInterface;
@@ -32,12 +31,12 @@ class Client implements ClientInterface
3231
/**
3332
* @var HttpClientInterface
3433
*/
35-
protected $httpClient;
34+
protected HttpClientInterface $httpClient;
3635

3736
/**
3837
* @var MessageFactoryInterface
3938
*/
40-
protected $messageFactory;
39+
protected MessageFactoryInterface $messageFactory;
4140

4241
/**
4342
* @param HttpClientInterface $httpClient
@@ -56,12 +55,12 @@ public function __construct(HttpClientInterface $httpClient, MessageFactoryInter
5655
}
5756

5857
/**
59-
* @param string $uri
58+
* @param string $uri
6059
* @param array $config
6160
*
6261
* @return Client
6362
*/
64-
public static function factory($uri, array $config = [])
63+
public static function factory(string $uri, array $config = []): Client
6564
{
6665
if (isset($config['message_factory'])) {
6766
$factory = $config['message_factory'];
@@ -80,12 +79,12 @@ public static function factory($uri, array $config = [])
8079
*
8180
* @link http://www.jsonrpc.org/specification#notification
8281
*
83-
* @param string $method
82+
* @param string $method
8483
* @param array|null $params
8584
*
8685
* @return RequestInterface
8786
*/
88-
public function notification($method, array $params = null)
87+
public function notification(string $method, array $params = null): RequestInterface
8988
{
9089
return $this->createRequest(RequestInterface::NOTIFICATION, array_filter([
9190
'jsonrpc' => self::SPEC,
@@ -100,12 +99,12 @@ public function notification($method, array $params = null)
10099
* @link http://www.jsonrpc.org/specification#request_object
101100
*
102101
* @param mixed $id
103-
* @param string $method
102+
* @param string $method
104103
* @param array|null $params
105104
*
106105
* @return RequestInterface
107106
*/
108-
public function request($id, $method, array $params = null)
107+
public function request($id, string $method, array $params = null): RequestInterface
109108
{
110109
return $this->createRequest(RequestInterface::REQUEST, array_filter([
111110
'jsonrpc' => self::SPEC,
@@ -122,7 +121,7 @@ public function request($id, $method, array $params = null)
122121
*
123122
* @return ResponseInterface|null
124123
*/
125-
public function send(RequestInterface $request)
124+
public function send(RequestInterface $request): ?ResponseInterface
126125
{
127126
$promise = $this->sendAsync($request);
128127

@@ -136,7 +135,7 @@ public function send(RequestInterface $request)
136135
*
137136
* @return PromiseInterface
138137
*/
139-
public function sendAsync(RequestInterface $request)
138+
public function sendAsync(RequestInterface $request): PromiseInterface
140139
{
141140
return $this->httpClient->sendAsync($request)->then(
142141
function (ResponseInterface $response) use ($request) {
@@ -154,7 +153,7 @@ function (ResponseInterface $response) use ($request) {
154153
*
155154
* @return ResponseInterface[]
156155
*/
157-
public function sendAll(array $requests)
156+
public function sendAll(array $requests): array
158157
{
159158
$promise = $this->sendAllAsync($requests);
160159

@@ -170,7 +169,7 @@ public function sendAll(array $requests)
170169
*
171170
* @return PromiseInterface
172171
*/
173-
public function sendAllAsync(array $requests)
172+
public function sendAllAsync(array $requests): PromiseInterface
174173
{
175174
return $this->httpClient->sendAsync($this->createRequest(
176175
RequestInterface::BATCH,
@@ -181,16 +180,16 @@ public function sendAllAsync(array $requests)
181180
}
182181

183182
/**
184-
* @param string $method
183+
* @param string $method
185184
* @param array $options
186185
*
187186
* @return RequestInterface
188187
*/
189-
protected function createRequest($method, array $options)
188+
protected function createRequest(string $method, array $options): RequestInterface
190189
{
191190
$uri = $this->httpClient->getConfig('base_uri');
192191
$defaults = $this->httpClient->getConfig('defaults');
193-
$headers = isset($defaults['headers']) ? $defaults['headers'] : [];
192+
$headers = $defaults['headers'] ?? [];
194193

195194
return $this->messageFactory->createRequest($method, $uri, $headers, $options);
196195
}
@@ -200,10 +199,10 @@ protected function createRequest($method, array $options)
200199
*
201200
* @return array
202201
*/
203-
protected function getBatchRequestOptions(array $requests)
202+
protected function getBatchRequestOptions(array $requests): array
204203
{
205204
return array_map(function (RequestInterface $request) {
206-
return JsonRpc\json_decode((string) $request->getBody());
205+
return Json::decode((string) $request->getBody());
207206
}, $requests);
208207
}
209208

@@ -212,9 +211,9 @@ protected function getBatchRequestOptions(array $requests)
212211
*
213212
* @return ResponseInterface[]
214213
*/
215-
protected function getBatchResponses(ResponseInterface $response)
214+
protected function getBatchResponses(ResponseInterface $response): array
216215
{
217-
$results = JsonRpc\json_decode((string) $response->getBody(), true);
216+
$results = Json::decode((string) $response->getBody(), true);
218217

219218
return array_map(function (array $result) use ($response) {
220219
return $this->messageFactory->createResponse(

src/ClientInterface.php

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -35,12 +35,12 @@ interface ClientInterface
3535
*
3636
* @link http://www.jsonrpc.org/specification#notification
3737
*
38-
* @param string $method
38+
* @param string $method
3939
* @param array|null $params
4040
*
4141
* @return RequestInterface
4242
*/
43-
public function notification($method, array $params = null);
43+
public function notification(string $method, array $params = null): RequestInterface;
4444

4545
/**
4646
* Build a request object.
@@ -52,12 +52,12 @@ public function notification($method, array $params = null);
5252
* @link http://www.jsonrpc.org/specification#request_object
5353
*
5454
* @param mixed $id
55-
* @param string $method
55+
* @param string $method
5656
* @param array|null $params
5757
*
5858
* @return RequestInterface
5959
*/
60-
public function request($id, $method, array $params = null);
60+
public function request($id, string $method, array $params = null): RequestInterface;
6161

6262
/**
6363
* Send a request.
@@ -69,7 +69,7 @@ public function request($id, $method, array $params = null);
6969
*
7070
* @return ResponseInterface|null
7171
*/
72-
public function send(RequestInterface $request);
72+
public function send(RequestInterface $request): ?ResponseInterface;
7373

7474
/**
7575
* Send a request asynchronously.
@@ -81,7 +81,7 @@ public function send(RequestInterface $request);
8181
*
8282
* @return PromiseInterface
8383
*/
84-
public function sendAsync(RequestInterface $request);
84+
public function sendAsync(RequestInterface $request): PromiseInterface;
8585

8686
/**
8787
* Send a batch of requests.
@@ -96,7 +96,7 @@ public function sendAsync(RequestInterface $request);
9696
*
9797
* @return ResponseInterface[]
9898
*/
99-
public function sendAll(array $requests);
99+
public function sendAll(array $requests): array;
100100

101101
/**
102102
* Send an asynchronous batch of requests.
@@ -111,5 +111,5 @@ public function sendAll(array $requests);
111111
*
112112
* @return PromiseInterface
113113
*/
114-
public function sendAllAsync(array $requests);
114+
public function sendAllAsync(array $requests): PromiseInterface;
115115
}

src/Exception/JsonDecodeException.php

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,22 +14,23 @@
1414

1515
namespace Graze\GuzzleHttp\JsonRpc\Exception;
1616

17+
use InvalidArgumentException;
1718
use Throwable;
1819

19-
class JsonDecodeException extends \InvalidArgumentException
20+
class JsonDecodeException extends InvalidArgumentException
2021
{
2122
/**
2223
* @var string
2324
*/
24-
private $json;
25+
private string $json;
2526

2627
/**
27-
* @param string $message The Exception message to throw
28-
* @param int $code The Exception code
29-
* @param Throwable $previous The previous throwable used for the exception chaining
30-
* @param string $json The JSON data.
28+
* @param string $message The Exception message to throw
29+
* @param int $code The Exception code
30+
* @param Throwable|null $previous The previous throwable used for the exception chaining
31+
* @param string $json The JSON data.
3132
*/
32-
public function __construct($message = "", $code = 0, Throwable $previous = null, $json = "")
33+
public function __construct($message = "", $code = 0, Throwable $previous = null, string $json = "")
3334
{
3435
parent::__construct($message, $code, $previous);
3536
$this->json = $json;
@@ -38,7 +39,7 @@ public function __construct($message = "", $code = 0, Throwable $previous = null
3839
/**
3940
* @return string
4041
*/
41-
public function getJson()
42+
public function getJson(): string
4243
{
4344
return $this->json;
4445
}

src/Exception/RequestException.php

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,28 +17,33 @@
1717
use Exception;
1818
use Graze\GuzzleHttp\JsonRpc\Message\RequestInterface;
1919
use Graze\GuzzleHttp\JsonRpc\Message\ResponseInterface;
20+
use GuzzleHttp\BodySummarizerInterface;
2021
use GuzzleHttp\Exception\RequestException as HttpRequestException;
2122
use Psr\Http\Message\RequestInterface as HttpRequestInterface;
2223
use Psr\Http\Message\ResponseInterface as HttpResponseInterface;
24+
use Throwable;
2325

2426
class RequestException extends HttpRequestException
2527
{
2628
/**
2729
* {@inheritdoc}
2830
*
29-
* @param HttpRequestInterface $request Request
30-
* @param HttpResponseInterface|null $response Response received
31-
* @param \Exception|null $previous Previous exception
32-
* @param array|null $handlerContext Optional handler context.
3331
*
32+
* @param HttpRequestInterface $request
33+
* @param HttpResponseInterface|null $response
34+
* @param Exception|null $previous
35+
* @param array|null $handlerContext
36+
* @param BodySummarizerInterface|null $bodySummarizer
3437
* @return HttpRequestException
3538
*/
3639
public static function create(
3740
HttpRequestInterface $request,
3841
HttpResponseInterface $response = null,
39-
Exception $previous = null,
40-
array $handlerContext = null
41-
) {
42+
Throwable $previous = null,
43+
array $handlerContext = null,
44+
BodySummarizerInterface $bodySummarizer = null
45+
): HttpRequestException
46+
{
4247
if ($request instanceof RequestInterface && $response instanceof ResponseInterface) {
4348
static $clientErrorCodes = [-32600, -32601, -32602, -32700];
4449

0 commit comments

Comments
 (0)