Skip to content

Commit

Permalink
Enforce strict typing
Browse files Browse the repository at this point in the history
  • Loading branch information
jimmerioles committed Dec 20, 2024
1 parent e8f4b20 commit 2273201
Show file tree
Hide file tree
Showing 34 changed files with 124 additions and 86 deletions.
3 changes: 3 additions & 0 deletions rector.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
declare(strict_types=1);

use Rector\Config\RectorConfig;
use Rector\TypeDeclaration\Rector\StmtsAwareInterface\DeclareStrictTypesRector;

return RectorConfig::configure()
->withPaths([
Expand All @@ -12,8 +13,10 @@
->withPreparedSets(
deadCode: true,
codeQuality: true,
codingStyle: true,
typeDeclarations: true,
privatization: true,
earlyReturn: true
)
->withRules([DeclareStrictTypesRector::class])
->withPhpSets(php80: true);
2 changes: 2 additions & 0 deletions src/Contracts/ExceptionInterface.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

declare(strict_types=1);

namespace Jimmerioles\BitcoinCurrencyConverter\Contracts;

interface ExceptionInterface
Expand Down
7 changes: 3 additions & 4 deletions src/Contracts/ProviderInterface.php
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
<?php

declare(strict_types=1);

namespace Jimmerioles\BitcoinCurrencyConverter\Contracts;

interface ProviderInterface
{
/**
* Get rate of currency code.
*
* @param string $currencyCode
* @return float
*/
public function getRate($currencyCode);
public function getRate(string $currencyCode): float;
}
12 changes: 4 additions & 8 deletions src/Converter.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

declare(strict_types=1);

namespace Jimmerioles\BitcoinCurrencyConverter;

use Jimmerioles\BitcoinCurrencyConverter\Provider\CoinbaseProvider;
Expand Down Expand Up @@ -34,11 +36,8 @@ public function toCurrency(string $currencyCode, float $btcAmount): float

/**
* Get rate of currency.
*
* @param string $currencyCode
* @return float
*/
protected function getRate($currencyCode)
protected function getRate(string $currencyCode): float
{
return $this->provider->getRate($currencyCode);
}
Expand All @@ -53,11 +52,8 @@ protected function computeCurrencyValue(int|float $btcAmount, int|float $rate):

/**
* Format value based on currency.
*
* @param string $currencyCode
* @param float $value
*/
protected function formatToCurrency($currencyCode, $value): float
protected function formatToCurrency(string $currencyCode, float $value): float
{
return format_to_currency($currencyCode, $value);
}
Expand Down
2 changes: 2 additions & 0 deletions src/Exception/InvalidArgumentException.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

declare(strict_types=1);

namespace Jimmerioles\BitcoinCurrencyConverter\Exception;

use InvalidArgumentException as GlobalInvalidArgumentException;
Expand Down
2 changes: 2 additions & 0 deletions src/Exception/UnexpectedValueException.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

declare(strict_types=1);

namespace Jimmerioles\BitcoinCurrencyConverter\Exception;

use Jimmerioles\BitcoinCurrencyConverter\Contracts\ExceptionInterface;
Expand Down
44 changes: 18 additions & 26 deletions src/Provider/AbstractProvider.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

declare(strict_types=1);

namespace Jimmerioles\BitcoinCurrencyConverter\Provider;

use GuzzleHttp\Client;
Expand Down Expand Up @@ -28,28 +30,22 @@ abstract class AbstractProvider implements ProviderInterface
*
* @var array<string, int|float>
*/
protected $exchangeRates = [];
protected array $exchangeRates = [];

/**
* Cache key string.
*
* @var string
*/
protected $cacheKey;
protected string $cacheKey;

/**
* Provider's exchange rates API endpoint, with 1 BTC as base.
*
* @var string
*/
protected static $apiEndpoint;
protected static string $apiEndpoint;

/**
* Create provider instance.
*
* @param integer $cacheTTL
*/
public function __construct(Client $client = null, CacheInterface $cache = null, protected $cacheTTL = 60)
public function __construct(Client $client = null, CacheInterface $cache = null, protected int $cacheTTL = 60)
{
$this->client = $client ?? new Client();
$this->cache = $cache ?? new Repository(new FileStore(new Filesystem(), project_root_path('cache')));
Expand All @@ -58,31 +54,27 @@ public function __construct(Client $client = null, CacheInterface $cache = null,
/**
* Get rate of currency.
*
* @param string $currencyCode
* @return float
* @throws InvalidArgumentException
*/
public function getRate($currencyCode)
public function getRate(string $currencyCode): float
{
if (! is_currency_code($currencyCode)) {
throw new InvalidArgumentException('Argument passed not a valid currency code, \'' . $currencyCode . '\' given.');
throw new InvalidArgumentException("Argument passed not a valid currency code, '" . $currencyCode . "' given.");
}

$exchangeRates = $this->getExchangeRates();

if (! $this->isSupportedByProvider($currencyCode)) {
throw new InvalidArgumentException('Argument $currencyCode \'' . $currencyCode . '\' not supported by provider.');
throw new InvalidArgumentException('Argument $currencyCode \'' . $currencyCode . "' not supported by provider.");
}

return $exchangeRates[strtoupper($currencyCode)];
return (float) $exchangeRates[strtoupper($currencyCode)];
}

/**
* Check if currency code supported by provider.
*
* @param string $currencyCode
* @return boolean
*/
protected function isSupportedByProvider($currencyCode)
protected function isSupportedByProvider(string $currencyCode): bool
{
return in_array(strtoupper($currencyCode), array_keys($this->exchangeRates), true);
}
Expand All @@ -92,7 +84,7 @@ protected function isSupportedByProvider($currencyCode)
*
* @return array<string, int|float>
*/
protected function getExchangeRates()
protected function getExchangeRates(): array
{
if ($this->exchangeRates === []) {
$this->setExchangeRates($this->retrieveExchangeRates());
Expand All @@ -106,7 +98,7 @@ protected function getExchangeRates()
*
* @param array<string, int|float> $exchangeRatesArray
*/
protected function setExchangeRates($exchangeRatesArray): void
protected function setExchangeRates(array $exchangeRatesArray): void
{
$this->exchangeRates = $exchangeRatesArray;
}
Expand Down Expand Up @@ -135,24 +127,24 @@ protected function retrieveExchangeRates(): array

/**
* Fetch exchange rates json data from API endpoint.
*
* @throws UnexpectedValueException
*/
protected function fetchExchangeRates(): string
{
$response = $this->client->request('GET', self::getApiEndpoint());

if ($response->getStatusCode() != 200) {
throw new UnexpectedValueException("Not OK response received from API endpoint."); //TODO: add @throw in phpdoc
throw new UnexpectedValueException("Not OK response received from API endpoint.");
}

return (string) $response->getBody();
}

/**
* Get the API endpoint.
*
* @return string
*/
public static function getApiEndpoint()
public static function getApiEndpoint(): string
{
return static::$apiEndpoint;
}
Expand Down
15 changes: 7 additions & 8 deletions src/Provider/BitpayProvider.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

declare(strict_types=1);

namespace Jimmerioles\BitcoinCurrencyConverter\Provider;

use Jimmerioles\BitcoinCurrencyConverter\Exception\InvalidArgumentException;
Expand All @@ -8,25 +10,22 @@ class BitpayProvider extends AbstractProvider
{
/**
* Provider's exchange rates API endpoint, with 1 BTC as base.
*
* @var string
*/
protected static $apiEndpoint = 'https://bitpay.com/api/rates';
protected static string $apiEndpoint = 'https://bitpay.com/api/rates';

/**
* Cache key to use when storing and retrieving from cache.
*
* @var string
*/
protected $cacheKey = 'bitpay-cache-key';
protected string $cacheKey = 'bitpay-cache-key';

/**
* Parse retrieved JSON data to exchange rates associative array.
* i.e. ['BTC' => 1, 'USD' => 4000.00, ...]
*
* @param string $rawJsonData
* @return array<string, int|float>
* @throws InvalidArgumentException
*/
protected function parseToExchangeRatesArray($rawJsonData): array
protected function parseToExchangeRatesArray(string $rawJsonData): array
{
$arrayData = json_decode($rawJsonData, true);

Expand Down
14 changes: 6 additions & 8 deletions src/Provider/CoinbaseProvider.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

declare(strict_types=1);

namespace Jimmerioles\BitcoinCurrencyConverter\Provider;

use Jimmerioles\BitcoinCurrencyConverter\Exception\InvalidArgumentException;
Expand All @@ -8,26 +10,22 @@ class CoinbaseProvider extends AbstractProvider
{
/**
* Provider's exchange rates API endpoint, with 1 BTC as base.
*
* @var string
*/
protected static $apiEndpoint = 'https://api.coinbase.com/v2/exchange-rates?currency=BTC';
protected static string $apiEndpoint = 'https://api.coinbase.com/v2/exchange-rates?currency=BTC';

/**
* Cache key to use when storing and retrieving from cache.
*
* @var string
*/
protected $cacheKey = 'coinbase-cache-key';
protected string $cacheKey = 'coinbase-cache-key';

/**
* Parse retrieved JSON data to exchange rates associative array.
* i.e. ['BTC' => 1, 'USD' => 4000.00, ...]
*
* @param string $rawJsonData
* @return array<string, int|float>
* @throws InvalidArgumentException
*/
protected function parseToExchangeRatesArray($rawJsonData): array
protected function parseToExchangeRatesArray(string $rawJsonData): array
{
$arrayData = json_decode($rawJsonData, true);

Expand Down
15 changes: 7 additions & 8 deletions src/Provider/CoindeskProvider.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

declare(strict_types=1);

namespace Jimmerioles\BitcoinCurrencyConverter\Provider;

use Jimmerioles\BitcoinCurrencyConverter\Exception\InvalidArgumentException;
Expand All @@ -8,25 +10,22 @@ class CoindeskProvider extends AbstractProvider
{
/**
* Provider's exchange rates API endpoint, with 1 BTC as base.
*
* @var string
*/
protected static $apiEndpoint = 'https://api.coindesk.com/v1/bpi/currentprice.json';
protected static string $apiEndpoint = 'https://api.coindesk.com/v1/bpi/currentprice.json';

/**
* Cache key to use when storing and retrieving from cache.
*
* @var string
*/
protected $cacheKey = 'coindesk-cache-key';
protected string $cacheKey = 'coindesk-cache-key';

/**
* Parse retrieved JSON data to exchange rates associative array.
* i.e. ['BTC' => 1, 'USD' => 4000.00, ...]
*
* @param string $rawJsonData
* @return array<string, int|float>
* @throws InvalidArgumentException
*/
protected function parseToExchangeRatesArray($rawJsonData): array
protected function parseToExchangeRatesArray(string $rawJsonData): array
{
$arrayData = json_decode($rawJsonData, true);

Expand Down
Loading

0 comments on commit 2273201

Please sign in to comment.