From 287048b731150ba243d45582e1715b57f3b7b66a Mon Sep 17 00:00:00 2001 From: Simon Podlipsky Date: Mon, 11 Nov 2024 09:52:42 +0100 Subject: [PATCH] feat: remove Safe --- composer.json | 6 ++---- src/Extractor/Extractor.php | 6 ++++-- src/HttpJsonRpcRequestFactory.php | 8 ++++++-- tests/HttpJsonRpcRequestFactoryTest.php | 2 +- 4 files changed, 13 insertions(+), 9 deletions(-) diff --git a/composer.json b/composer.json index 7155e09..9e41e06 100644 --- a/composer.json +++ b/composer.json @@ -19,8 +19,7 @@ "php": "^8.2", "ext-json": "*", "psr/http-factory": "^1.0", - "psr/http-message": "^1.0 || ^2.0", - "thecodingmachine/safe": "^2" + "psr/http-message": "^1.0 || ^2.0" }, "require-dev": { "cdn77/coding-standard": "^7.0", @@ -30,8 +29,7 @@ "phpstan/phpstan-phpunit": "^1.0.0", "phpstan/phpstan-strict-rules": "^1.0.0", "phpunit/phpunit": "^11.0", - "psr-mock/http": "^1.0", - "thecodingmachine/phpstan-safe-rule": "^1.0" + "psr-mock/http": "^1.0" }, "config": { "allow-plugins": { diff --git a/src/Extractor/Extractor.php b/src/Extractor/Extractor.php index 1530f71..3a6c9ae 100644 --- a/src/Extractor/Extractor.php +++ b/src/Extractor/Extractor.php @@ -6,7 +6,9 @@ use Psr\Http\Message\MessageInterface; -use function Safe\json_decode; +use function json_decode; + +use const JSON_THROW_ON_ERROR; abstract class Extractor { @@ -16,7 +18,7 @@ abstract class Extractor public function __construct(MessageInterface $message) { /** @var array{id: string, jsonrpc: string, error?: array{code: int, message: string, data?: mixed}, method: string, params?: array, result?: mixed} $contents */ - $contents = json_decode((string) $message->getBody(), true); + $contents = json_decode((string) $message->getBody(), true, flags: JSON_THROW_ON_ERROR); $this->messageContents = $contents; } } diff --git a/src/HttpJsonRpcRequestFactory.php b/src/HttpJsonRpcRequestFactory.php index f9de6a9..fa9b21c 100644 --- a/src/HttpJsonRpcRequestFactory.php +++ b/src/HttpJsonRpcRequestFactory.php @@ -8,7 +8,9 @@ use Psr\Http\Message\RequestInterface; use Psr\Http\Message\StreamFactoryInterface; -use function Safe\json_encode; +use function json_encode; + +use const JSON_THROW_ON_ERROR; final class HttpJsonRpcRequestFactory implements JsonRpcRequestFactory { @@ -49,8 +51,10 @@ public function request(string|int|null $id, string $method, array|null $params /** @param mixed[] $body */ private function createRequest(array $body = []): RequestInterface { + $encodedBody = json_encode($body, JSON_THROW_ON_ERROR); + return $this->messageFactory->createRequest('POST', '') ->withHeader('Content-Type', 'application/json') - ->withBody($this->streamFactory->createStream(json_encode($body))); + ->withBody($this->streamFactory->createStream($encodedBody)); } } diff --git a/tests/HttpJsonRpcRequestFactoryTest.php b/tests/HttpJsonRpcRequestFactoryTest.php index 0a14968..ed91c66 100644 --- a/tests/HttpJsonRpcRequestFactoryTest.php +++ b/tests/HttpJsonRpcRequestFactoryTest.php @@ -9,7 +9,7 @@ use PsrMock\Psr17\RequestFactory; use PsrMock\Psr17\StreamFactory; -use function Safe\preg_replace; +use function preg_replace; #[CoversClass(HttpJsonRpcRequestFactory::class)] final class HttpJsonRpcRequestFactoryTest extends TestCase