Skip to content

Commit fe073fb

Browse files
Update source code to PHP 8.0
1 parent 3791e5a commit fe073fb

20 files changed

+220
-399
lines changed

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
"homepage": "https://github.com/procurios/JsonRpc",
1313
"license": "MIT",
1414
"require": {
15-
"php": ">=5.6",
15+
"php": ">=8.0",
1616
"psr/http-message": "^1.0"
1717
},
1818
"require-dev": {

src/Request/BatchRequest.php

Lines changed: 7 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,23 @@
11
<?php
2+
declare(strict_types=1);
23
/**
34
* © 2015 Procurios - License MIT
45
*/
56
namespace Procurios\Json\JsonRpc\Request;
67

78
use InvalidArgumentException;
89

9-
/**
10-
*/
1110
class BatchRequest
1211
{
1312
/** @var Request[] */
14-
private $requests = [];
13+
private array $requests;
1514

16-
/**
17-
* @param Request[] $requests
18-
*/
19-
public function __construct(array $requests = [])
15+
public function __construct(Request ...$requests)
2016
{
21-
foreach ($requests as $Request) {
22-
if (!$Request instanceof Request) {
23-
throw new InvalidArgumentException();
24-
}
25-
26-
$this->requests[] = $Request;
27-
}
17+
$this->requests = $requests;
2818
}
2919

30-
/**
31-
* @param array $batch
32-
* @return BatchRequest
33-
*/
34-
public static function fromArray(array $batch)
20+
public static function fromArray(array $batch): self
3521
{
3622
$requests = [];
3723
foreach ($batch as $requestArray) {
@@ -42,13 +28,13 @@ public static function fromArray(array $batch)
4228
$requests[] = Request::fromArray($requestArray);
4329
}
4430

45-
return new self($requests);
31+
return new self(...$requests);
4632
}
4733

4834
/**
4935
* @return Request[]
5036
*/
51-
public function getRequests()
37+
public function getRequests(): array
5238
{
5339
return $this->requests;
5440
}

src/Request/Request.php

Lines changed: 33 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -1,47 +1,38 @@
11
<?php
2+
declare(strict_types=1);
23
/**
34
* © 2015 Procurios - License MIT
45
*/
56
namespace Procurios\Json\JsonRpc\Request;
67

78
use InvalidArgumentException;
9+
use JsonException;
810
use Procurios\Json\JsonRpc\exception\CouldNotParse;
911
use Psr\Http\Message\ServerRequestInterface;
1012

11-
/**
12-
*/
1313
class Request
1414
{
15-
/** @var string */
16-
private $method;
17-
/** @var array */
18-
private $params = [];
19-
/** @var string|int|null */
20-
private $id;
15+
private array $params = [];
16+
private string|int|null $id = null;
2117

22-
/**
23-
* @param string $method
24-
*/
25-
public function __construct($method)
18+
public function __construct(private string $method)
2619
{
27-
if (!is_string($method)) {
28-
throw new InvalidArgumentException('Method member should be a string');
29-
}
30-
$this->method = $method;
3120
}
3221

3322
/**
34-
* @param ServerRequestInterface $ServerRequest
35-
* @return BatchRequest|Request
3623
* @throws CouldNotParse
3724
*/
38-
public static function fromHttpRequest(ServerRequestInterface $ServerRequest)
25+
public static function fromHttpRequest(ServerRequestInterface $serverRequest): self|BatchRequest
3926
{
40-
$Body = $ServerRequest->getBody();
41-
$jsonString = $Body->__toString();
42-
$data = json_decode($jsonString, true);
27+
$body = $serverRequest->getBody();
28+
$jsonString = $body->__toString();
29+
try {
30+
$data = json_decode($jsonString, true, flags: JSON_THROW_ON_ERROR);
31+
} catch (JsonException $e) {
32+
throw new CouldNotParse(previous: $e);
33+
}
4334

44-
if (is_null($data) && strtolower($jsonString) != 'null') {
35+
if (is_null($data) && strtolower($jsonString) !== 'null') {
4536
throw new CouldNotParse();
4637
}
4738

@@ -57,108 +48,74 @@ public static function fromHttpRequest(ServerRequestInterface $ServerRequest)
5748
return self::fromArray($data);
5849
}
5950

60-
/**
61-
* @param array $data
62-
* @return Request
63-
*/
64-
public static function fromArray(array $data)
51+
public static function fromArray(array $data): self
6552
{
6653
self::validateData($data);
6754

68-
$Request = new self($data['method']);
55+
$request = new self($data['method']);
6956
if (array_key_exists('params', $data)) {
70-
$Request->setParams($data['params']);
57+
$request->setParams($data['params']);
7158
}
7259
if (array_key_exists('id', $data)) {
73-
$Request->setId($data['id']);
60+
$request->setId($data['id']);
7461
}
7562

76-
return $Request;
63+
return $request;
7764
}
7865

79-
/**
80-
* @param array $data
81-
*/
82-
private static function validateData(array $data)
66+
private static function validateData(array $data): void
8367
{
8468
if (!array_key_exists('jsonrpc', $data)) {
8569
throw new InvalidArgumentException('Missing jsonrpc member');
8670
}
8771
if ($data['jsonrpc'] !== '2.0') {
8872
throw new InvalidArgumentException('Member jsonrpc must be exactly "2.0"');
8973
}
90-
9174
if (!array_key_exists('method', $data)) {
9275
throw new InvalidArgumentException('Missing method member');
9376
}
9477
}
9578

96-
/**
97-
* @return string
98-
*/
99-
public function getMethod()
79+
public function getMethod(): string
10080
{
10181
return $this->method;
10282
}
10383

104-
/**
105-
* @param array $params
106-
* @return Request
107-
*/
108-
public function withParams($params)
84+
public function withParams(array $params): self
10985
{
110-
$Clone = clone $this;
111-
$Clone->setParams($params);
86+
$clone = clone $this;
87+
$clone->setParams($params);
11288

113-
return $Clone;
89+
return $clone;
11490
}
11591

116-
/**
117-
* @param array $params
118-
*/
119-
private function setParams($params)
92+
private function setParams(array $params): void
12093
{
121-
if (!is_array($params)) {
122-
throw new InvalidArgumentException('Member params should be either an object or an array');
123-
}
12494
$this->params = $params;
12595
}
12696

127-
/**
128-
* @return array
129-
*/
130-
public function getParams()
97+
public function getParams(): array
13198
{
13299
return $this->params;
133100
}
134101

135-
/**
136-
* @param string|int|null $id
137-
* @return Request
138-
*/
139-
public function withId($id)
102+
public function withId(string|int|null $id): self
140103
{
141-
$Clone = clone $this;
142-
$Clone->setId($id);
104+
$clone = clone $this;
105+
$clone->setId($id);
143106

144-
return $Clone;
107+
return $clone;
145108
}
146109

147110
/**
148111
* @param string|int|null $id
149112
*/
150-
private function setId($id)
113+
private function setId(string|int|null $id): void
151114
{
152-
if (!is_scalar($id) && !is_null($id)) {
153-
throw new InvalidArgumentException('Member id should be either a string, number or Null');
154-
}
155115
$this->id = $id;
156116
}
157117

158-
/**
159-
* @return string|int|null
160-
*/
161-
public function getId()
118+
public function getId(): int|string|null
162119
{
163120
return $this->id;
164121
}

src/Response/BatchResponse.php

Lines changed: 13 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,56 +1,43 @@
11
<?php
2+
declare(strict_types=1);
23
/**
34
* © 2015 Procurios - License MIT
45
*/
56
namespace Procurios\Json\JsonRpc\Response;
67

78
use InvalidArgumentException;
89

9-
/**
10-
*
11-
*/
1210
class BatchResponse implements Response
1311
{
1412
/** @var Response[] */
15-
private $responses;
13+
private array $responses;
1614

17-
/**
18-
* @param Response[] $responses
19-
*/
20-
public function __construct(array $responses)
15+
public function __construct(Response ...$responses)
2116
{
22-
foreach ($responses as $Response) {
23-
if (!$Response instanceof Response) {
24-
throw new InvalidArgumentException();
17+
$this->responses = [];
18+
foreach ($responses as $response) {
19+
if ($response instanceof EmptyResponse) {
20+
continue;
2521
}
2622

27-
if ($Response instanceof BatchResponse) {
23+
if ($response instanceof self) {
2824
throw new InvalidArgumentException();
2925
}
3026

31-
if ($Response instanceof EmptyResponse) {
32-
continue;
33-
}
34-
35-
$this->responses[] = $Response;
27+
$this->responses[] = $response;
3628
}
3729
}
3830

39-
/**
40-
* @return string
41-
*/
42-
public function asString()
31+
public function asString(): string
4332
{
44-
if (count($this->responses) == 0) {
33+
if ($this->responses === []) {
4534
return '';
4635
}
4736

4837
return '[' .
4938
implode(',',
50-
array_map(function (Response $Response)
51-
{
52-
return $Response->asString();
53-
},
39+
array_map(
40+
static fn (Response $response) => $response->asString(),
5441
$this->responses
5542
)
5643
) .

src/Response/EmptyResponse.php

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,13 @@
11
<?php
2+
declare(strict_types=1);
23
/**
34
* © 2015 Procurios - License MIT
45
*/
56
namespace Procurios\Json\JsonRpc\Response;
67

7-
/**
8-
*
9-
*/
108
class EmptyResponse implements Response
119
{
12-
/**
13-
* @return string
14-
*/
15-
public function asString()
10+
public function asString(): string
1611
{
1712
return '';
1813
}

0 commit comments

Comments
 (0)