Skip to content

Commit

Permalink
Use type declarations
Browse files Browse the repository at this point in the history
  • Loading branch information
jeromegamez committed Apr 15, 2021
1 parent a23348e commit c50ed00
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 56 deletions.
33 changes: 7 additions & 26 deletions src/Api/HttpApiClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,30 +14,11 @@

final class HttpApiClient implements ApiClient
{
/**
* @var string
*/
private $apiScheme = 'https';

/**
* @var string
*/
private $apiHost;

/**
* @var string
*/
private $apiKey;

/**
* @var ClientInterface
*/
private $client;

/**
* @var RequestFactoryInterface
*/
private $requestFactory;
private string $apiScheme = 'https';
private string $apiHost;
private string $apiKey;
private ClientInterface $client;
private RequestFactoryInterface $requestFactory;

private function __construct()
{
Expand All @@ -47,7 +28,7 @@ public static function with(string $accountName, string $apiKey, ClientInterface
{
$that = new self();

$that->apiHost = "{$accountName}.mite.yo.lk";
$that->apiHost = "$accountName}.mite.yo.lk";
$that->apiKey = $apiKey;
$that->client = $client;
$that->requestFactory = $requestFactory;
Expand Down Expand Up @@ -84,7 +65,7 @@ public function delete(string $endpoint, array $params = null): ResponseInterfac
* @param array<string, numeric|string>|null $params
* @param array<string, numeric|string>|null $data
*/
private function request(string $method, string $endpoint, array $params = null, array $data = null): ResponseInterface
private function request(string $method, string $endpoint, array $params = null, ?array $data = null): ResponseInterface
{
$url = $this->createUrl($endpoint, $params);

Expand Down
13 changes: 3 additions & 10 deletions src/Exception/ApiClientError.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,8 @@

final class ApiClientError extends RuntimeException implements MiteException
{
/**
* @var RequestInterface
*/
private $request;

/**
* @var ResponseInterface|null
*/
private $response;
private RequestInterface $request;
private ?ResponseInterface $response;

public function __construct(RequestInterface $request, ?ResponseInterface $response, string $message = null, int $code = null, Throwable $previous = null)
{
Expand Down Expand Up @@ -51,7 +44,7 @@ public static function fromRequestAndResponse(RequestInterface $request, Respons
$code = $response->getStatusCode();
try {
$data = JSON::decode((string) $response->getBody(), true);
} catch (\Throwable $e) {
} catch (Throwable $e) {
$data = [];
}
$message = $data['error'] ?? null;
Expand Down
3 changes: 2 additions & 1 deletion src/Exception/InvalidArgument.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,10 @@

namespace Gamez\Mite\Exception;

use InvalidArgumentException;
use Throwable;

final class InvalidArgument extends \InvalidArgumentException implements MiteException
final class InvalidArgument extends InvalidArgumentException implements MiteException
{
public static function because(string $reason, ?Throwable $previous = null): self
{
Expand Down
5 changes: 1 addition & 4 deletions src/SimpleApi.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,7 @@

final class SimpleApi
{
/**
* @var ApiClient
*/
private $client;
private ApiClient $client;

private function __construct()
{
Expand Down
5 changes: 1 addition & 4 deletions src/SimpleTracker.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,7 @@

final class SimpleTracker
{
/**
* @var ApiClient
*/
private $client;
private ApiClient $client;

private function __construct()
{
Expand Down
28 changes: 17 additions & 11 deletions src/Support/JSON.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@
namespace Gamez\Mite\Support;

use Gamez\Mite\Exception\InvalidArgument;
use function json_decode;
use function json_encode;
use JsonException;
use Throwable;

/**
* @internal
Expand All @@ -21,12 +25,11 @@ public static function encode($value, $options = null, $depth = null): string
$options = $options ?? 0;
$depth = $depth ?? 512;

$json = \json_encode($value, $options, $depth);
if (!is_string($json) || JSON_ERROR_NONE !== json_last_error()) {
throw new InvalidArgument('json_encode error: '.json_last_error_msg());
try {
return json_encode($value, JSON_THROW_ON_ERROR | $options, $depth) ?: '';
} catch (JsonException $e) {
throw new InvalidArgument('json decode error:'.$e->getMessage());
}

return $json;
}

/**
Expand All @@ -38,12 +41,15 @@ public static function encode($value, $options = null, $depth = null): string
*/
public static function decode(string $json, $assoc = null, $depth = null, $options = null)
{
$data = \json_decode($json, $assoc ?? false, $depth ?? 512, $options ?? 0);
if (JSON_ERROR_NONE !== json_last_error()) {
throw new InvalidArgument('json_decode error: '.json_last_error_msg());
}
$assoc = $assoc ?? false;
$depth = $depth ?? 512;
$options = $options ?? 0;

return $data;
try {
return json_decode($json, $assoc, $depth, JSON_THROW_ON_ERROR | $options);
} catch (JsonException $e) {
throw new InvalidArgument('json_decode error: '.$e->getMessage());
}
}

/**
Expand All @@ -59,7 +65,7 @@ public static function isValid($value): bool
self::decode($value);

return true;
} catch (\Throwable $e) {
} catch (Throwable $e) {
return false;
}
}
Expand Down

0 comments on commit c50ed00

Please sign in to comment.