Skip to content

Commit cbc2adc

Browse files
authored
Merge pull request #20 from pottink/add-psl
Use azjezz/psl in favor of thecodingmachine/safe
2 parents 38f1374 + 37ef776 commit cbc2adc

17 files changed

Lines changed: 62 additions & 50 deletions

composer.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,18 +14,18 @@
1414
"php": "^7.4 || ^8.0",
1515
"ext-json": "*",
1616
"amphp/amp": "^2.5",
17+
"azjezz/psl": "^1.6",
1718
"league/uri": "^6.3",
19+
"php-http/client-common": "^2.3",
1820
"php-http/discovery": "^1.12",
1921
"php-http/httplug": "^2.2",
20-
"php-http/client-common": "^2.3",
2122
"php-http/logger-plugin": "^1.2",
2223
"php-http/message": "^1.9",
2324
"psr/http-client-implementation": "^1.0",
2425
"psr/http-factory-implementation": "^1.0",
26+
"psr/http-message": "^1.0.1",
2527
"psr/http-message-implementation": "^1.0",
2628
"psr/log": "^1.1",
27-
"psr/http-message": "^1.0.1",
28-
"thecodingmachine/safe": "^1.2",
2929
"webmozart/assert": "^1.9"
3030
},
3131
"require-dev": {

src/Encoding/Json/JsonDecoder.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@
55
namespace Phpro\HttpTools\Encoding\Json;
66

77
use Phpro\HttpTools\Encoding\DecoderInterface;
8+
use Psl\Json;
89
use Psr\Http\Message\ResponseInterface;
9-
use function Safe\json_decode;
1010

1111
/**
1212
* @implements DecoderInterface<array>
@@ -24,6 +24,6 @@ public function __invoke(ResponseInterface $response): array
2424
return [];
2525
}
2626

27-
return (array) json_decode($responseBody, true);
27+
return (array) Json\decode($responseBody, true);
2828
}
2929
}

src/Encoding/Json/JsonEncoder.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@
66

77
use Http\Discovery\Psr17FactoryDiscovery;
88
use Phpro\HttpTools\Encoding\EncoderInterface;
9+
use Psl\Json;
910
use Psr\Http\Message\RequestInterface;
1011
use Psr\Http\Message\StreamFactoryInterface;
11-
use function Safe\json_encode;
1212

1313
/**
1414
* @implements EncoderInterface<array|null>
@@ -38,7 +38,7 @@ public function __invoke(RequestInterface $request, $data): RequestInterface
3838
->withAddedHeader('Content-Type', 'application/json')
3939
->withAddedHeader('Accept', 'application/json')
4040
->withBody($this->streamFactory->createStream(
41-
null !== $data ? json_encode($data) : ''
41+
null !== $data ? Json\encode($data) : ''
4242
));
4343
}
4444
}

src/Formatter/RemoveSensitiveHeadersFormatter.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@
66

77
use Http\Message\Formatter as HttpFormatter;
88
use function preg_quote;
9+
use Psl\Regex;
910
use Psr\Http\Message\RequestInterface;
1011
use Psr\Http\Message\ResponseInterface;
11-
use function Safe\preg_replace;
1212

1313
final class RemoveSensitiveHeadersFormatter implements HttpFormatter
1414
{
@@ -47,10 +47,10 @@ private function removeCredentials(string $info): string
4747
return array_reduce(
4848
$this->sensitiveHeaders,
4949
/** @psalm-suppress InvalidReturnStatement, InvalidReturnType */
50-
fn (string $sensitiveData, string $header): string => preg_replace(
50+
fn (string $sensitiveData, string $header): string => Regex\replace(
51+
$sensitiveData,
5152
'{^('.preg_quote($header, '{').')\:(.*)}im',
5253
'$1: xxxx',
53-
$sensitiveData
5454
),
5555
$info
5656
);

src/Formatter/RemoveSensitiveJsonKeysFormatter.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@
66

77
use Http\Message\Formatter as HttpFormatter;
88
use function preg_quote;
9+
use Psl\Regex;
910
use Psr\Http\Message\RequestInterface;
1011
use Psr\Http\Message\ResponseInterface;
11-
use function Safe\preg_replace;
1212

1313
final class RemoveSensitiveJsonKeysFormatter implements HttpFormatter
1414
{
@@ -47,10 +47,10 @@ private function removeCredentials(string $info): string
4747
return array_reduce(
4848
$this->sensitiveJsonKeys,
4949
/** @psalm-suppress InvalidReturnStatement, InvalidReturnType */
50-
fn (string $sensitiveData, string $jsonKey): string => preg_replace(
51-
'{"('.preg_quote($jsonKey, '{').')":\s*"([^"]*)"}i',
50+
fn (string $sensitiveData, string $jsonKey): string => Regex\replace(
51+
$sensitiveData,
52+
'{"('.preg_quote($jsonKey, '{').')"\:\s*"([^"]*)"}i',
5253
'"$1": "xxxx"',
53-
$sensitiveData
5454
),
5555
$info
5656
);

tests/Functional/Client/Factory/FactoriesTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@
1313
use Phpro\HttpTools\Test\UseVcrClient;
1414
use Phpro\HttpTools\Tests\Helper\Vcr\FactoryAwareNamingStrategy;
1515
use PHPUnit\Framework\TestCase;
16+
use Psl\Json;
1617
use Psr\Http\Message\RequestInterface;
17-
use function Safe\json_decode;
1818

1919
final class FactoriesTest extends TestCase
2020
{
@@ -38,7 +38,7 @@ public function it_can_use_http_factories(string $factoryName, callable $factory
3838
);
3939

4040
self::assertSame(200, $response->getStatusCode());
41-
self::assertSame(['success' => true], json_decode($response->getBody()->__toString(), true));
41+
self::assertSame(['success' => true], Json\decode($response->getBody()->__toString(), true));
4242
}
4343

4444
public function provideFactories()

tests/Unit/Encoding/Json/JsonEncoderTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
use Phpro\HttpTools\Encoding\Json\JsonEncoder;
88
use Phpro\HttpTools\Test\UseHttpFactories;
99
use PHPUnit\Framework\TestCase;
10-
use function Safe\json_encode;
10+
use Psl\Json;
1111

1212
final class JsonEncoderTest extends TestCase
1313
{
@@ -24,7 +24,7 @@ public function it_can_encode_array_to_json(): void
2424

2525
self::assertSame($request->getMethod(), $actual->getMethod());
2626
self::assertSame($request->getUri(), $actual->getUri());
27-
self::assertSame(json_encode(['hello' => 'world']), (string) $actual->getBody());
27+
self::assertSame(Json\encode(['hello' => 'world']), (string) $actual->getBody());
2828
self::assertSame(['application/json'], $actual->getHeader('Accept'));
2929
self::assertSame(['application/json'], $actual->getHeader('Content-Type'));
3030
}

tests/Unit/Formatter/RemoveSensitiveHeaderKeysFormatterTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,13 +61,13 @@ public function provideJsonExpectations()
6161
yield 'sample1' => [
6262
[
6363
'Hello' => 'World',
64-
'Hello' => 'Toon',
64+
'Hi' => 'Toon',
6565
'X-API-Key' => 'secret',
6666
'X-API-Secret' => 'also-secret',
6767
],
6868
[
6969
'Hello' => 'World',
70-
'Hello' => 'Toon',
70+
'Hi' => 'Toon',
7171
'X-API-Key' => 'xxxx',
7272
'X-API-Secret' => 'xxxx',
7373
],

tests/Unit/Formatter/RemoveSensitiveJsonKeysFormatterTest.php

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,8 @@
88
use Phpro\HttpTools\Test\UseHttpFactories;
99
use Phpro\HttpTools\Tests\Helper\Formatter\CallbackFormatter;
1010
use PHPUnit\Framework\TestCase;
11+
use Psl\Json;
1112
use Psr\Http\Message\MessageInterface;
12-
use function Safe\json_decode;
13-
use function Safe\json_encode;
1413

1514
final class RemoveSensitiveJsonKeysFormatterTest extends TestCase
1615
{
@@ -33,10 +32,13 @@ protected function setUp(): void
3332
public function it_can_remove_sensitive_json_keys_from_request(array $content, array $expected): void
3433
{
3534
$request = $this->createRequest('GET', 'something')
36-
->withBody($this->createStream(json_encode($content)));
35+
->withBody(
36+
$this->createStream(Json\encode($content))
37+
);
38+
3739
$formatted = $this->formatter->formatRequest($request);
3840

39-
self::assertSame($expected, json_decode($formatted, true));
41+
self::assertSame($expected, Json\decode($formatted, true));
4042
}
4143

4244
/**
@@ -46,10 +48,13 @@ public function it_can_remove_sensitive_json_keys_from_request(array $content, a
4648
public function it_can_remove_sensitive_json_keys_from_response(array $content, array $expected): void
4749
{
4850
$response = $this->createResponse(200)
49-
->withBody($this->createStream(json_encode($content)));
51+
->withBody(
52+
$this->createStream(Json\encode($content))
53+
);
54+
5055
$formatted = $this->formatter->formatResponse($response);
5156

52-
self::assertSame($expected, json_decode($formatted, true));
57+
self::assertSame($expected, Json\decode($formatted, true));
5358
}
5459

5560
public function provideJsonExpectations()

tests/Unit/Sdk/Rest/CreateTest.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@
1212
use Phpro\HttpTools\Transport\Presets\JsonPreset;
1313
use Phpro\HttpTools\Uri\RawUriBuilder;
1414
use PHPUnit\Framework\TestCase;
15+
use Psl\Json;
1516
use Psr\Http\Message\RequestInterface;
16-
use function Safe\json_encode;
1717

1818
final class CreateTest extends TestCase
1919
{
@@ -46,11 +46,11 @@ public function it_can_create_a_resource(): void
4646
new CallbackRequestMatcher(
4747
static fn (RequestInterface $request) => 'POST' === $request->getMethod()
4848
&& '/users' === (string) $request->getUri()
49-
&& (string) $request->getBody() === json_encode($requestData)
49+
&& (string) $request->getBody() === Json\encode($requestData)
5050
),
5151
$this->createResponse()
5252
->withBody(
53-
$this->createStream(json_encode($responseData))
53+
$this->createStream(Json\encode($responseData))
5454
)
5555
);
5656

0 commit comments

Comments
 (0)