From 227320177bc3ed936306b8d6f96b2dce57af808c Mon Sep 17 00:00:00 2001 From: Jim Merioles Date: Fri, 20 Dec 2024 11:05:29 +0000 Subject: [PATCH] Enforce strict typing --- rector.php | 3 ++ src/Contracts/ExceptionInterface.php | 2 + src/Contracts/ProviderInterface.php | 7 ++- src/Converter.php | 12 ++--- src/Exception/InvalidArgumentException.php | 2 + src/Exception/UnexpectedValueException.php | 2 + src/Provider/AbstractProvider.php | 44 ++++++++----------- src/Provider/BitpayProvider.php | 15 +++---- src/Provider/CoinbaseProvider.php | 14 +++--- src/Provider/CoindeskProvider.php | 15 +++---- src/Util/CurrencyCodeChecker.php | 19 ++++---- src/Util/converter_helper.php | 12 ++--- src/Util/currency_code_checker_helper.php | 2 + src/Util/currency_formatter_helper.php | 6 ++- src/Util/path_helper.php | 2 + .../ConvertUsingBitpayExchangeRatesTest.php | 2 + .../ConvertUsingCoinbaseExchangeRatesTest.php | 2 + .../ConvertUsingCoindeskExchangeRatesTest.php | 2 + ...UsingHelperWithBitpayExchangeRatesTest.php | 2 + ...ingHelperWithCoinbaseExchangeRatesTest.php | 2 + ...ingHelperWithCoindeskExchangeRatesTest.php | 2 + tests/Integration/BitpayIntegrationTest.php | 4 +- tests/Integration/CoinbaseIntegrationTest.php | 4 +- tests/Integration/CoindeskIntegrationTest.php | 4 +- tests/TestCase.php | 4 +- tests/Unit/ConverterTest.php | 3 ++ tests/Unit/Provider/BitpayProviderTest.php | 4 ++ tests/Unit/Provider/CoinbaseProviderTest.php | 4 ++ tests/Unit/Provider/CoindeskProviderTest.php | 4 ++ tests/Unit/Provider/ProviderTest.php | 2 + tests/Unit/Util/ConverterHelperTest.php | 2 + .../Util/CurrencyCodeCheckerHelperTest.php | 2 + tests/Unit/Util/CurrencyCodeCheckerTest.php | 2 + .../Unit/Util/CurrencyFormatterHelperTest.php | 2 + 34 files changed, 124 insertions(+), 86 deletions(-) diff --git a/rector.php b/rector.php index 62a467f..0278973 100644 --- a/rector.php +++ b/rector.php @@ -3,6 +3,7 @@ declare(strict_types=1); use Rector\Config\RectorConfig; +use Rector\TypeDeclaration\Rector\StmtsAwareInterface\DeclareStrictTypesRector; return RectorConfig::configure() ->withPaths([ @@ -12,8 +13,10 @@ ->withPreparedSets( deadCode: true, codeQuality: true, + codingStyle: true, typeDeclarations: true, privatization: true, earlyReturn: true ) + ->withRules([DeclareStrictTypesRector::class]) ->withPhpSets(php80: true); diff --git a/src/Contracts/ExceptionInterface.php b/src/Contracts/ExceptionInterface.php index a8f0e53..cfa0117 100644 --- a/src/Contracts/ExceptionInterface.php +++ b/src/Contracts/ExceptionInterface.php @@ -1,5 +1,7 @@ provider->getRate($currencyCode); } @@ -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); } diff --git a/src/Exception/InvalidArgumentException.php b/src/Exception/InvalidArgumentException.php index ba64970..4e37bfd 100644 --- a/src/Exception/InvalidArgumentException.php +++ b/src/Exception/InvalidArgumentException.php @@ -1,5 +1,7 @@ */ - 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'))); @@ -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); } @@ -92,7 +84,7 @@ protected function isSupportedByProvider($currencyCode) * * @return array */ - protected function getExchangeRates() + protected function getExchangeRates(): array { if ($this->exchangeRates === []) { $this->setExchangeRates($this->retrieveExchangeRates()); @@ -106,7 +98,7 @@ protected function getExchangeRates() * * @param array $exchangeRatesArray */ - protected function setExchangeRates($exchangeRatesArray): void + protected function setExchangeRates(array $exchangeRatesArray): void { $this->exchangeRates = $exchangeRatesArray; } @@ -135,13 +127,15 @@ 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(); @@ -149,10 +143,8 @@ protected function fetchExchangeRates(): string /** * Get the API endpoint. - * - * @return string */ - public static function getApiEndpoint() + public static function getApiEndpoint(): string { return static::$apiEndpoint; } diff --git a/src/Provider/BitpayProvider.php b/src/Provider/BitpayProvider.php index 99d78e6..d656b5c 100644 --- a/src/Provider/BitpayProvider.php +++ b/src/Provider/BitpayProvider.php @@ -1,5 +1,7 @@ 1, 'USD' => 4000.00, ...] * - * @param string $rawJsonData + * @return array + * @throws InvalidArgumentException */ - protected function parseToExchangeRatesArray($rawJsonData): array + protected function parseToExchangeRatesArray(string $rawJsonData): array { $arrayData = json_decode($rawJsonData, true); diff --git a/src/Provider/CoinbaseProvider.php b/src/Provider/CoinbaseProvider.php index 5cb5366..f48be0d 100644 --- a/src/Provider/CoinbaseProvider.php +++ b/src/Provider/CoinbaseProvider.php @@ -1,5 +1,7 @@ 1, 'USD' => 4000.00, ...] * - * @param string $rawJsonData * @return array + * @throws InvalidArgumentException */ - protected function parseToExchangeRatesArray($rawJsonData): array + protected function parseToExchangeRatesArray(string $rawJsonData): array { $arrayData = json_decode($rawJsonData, true); diff --git a/src/Provider/CoindeskProvider.php b/src/Provider/CoindeskProvider.php index 5bde3de..f2af606 100644 --- a/src/Provider/CoindeskProvider.php +++ b/src/Provider/CoindeskProvider.php @@ -1,5 +1,7 @@ 1, 'USD' => 4000.00, ...] * - * @param string $rawJsonData + * @return array + * @throws InvalidArgumentException */ - protected function parseToExchangeRatesArray($rawJsonData): array + protected function parseToExchangeRatesArray(string $rawJsonData): array { $arrayData = json_decode($rawJsonData, true); diff --git a/src/Util/CurrencyCodeChecker.php b/src/Util/CurrencyCodeChecker.php index 6fb02f2..de451d5 100644 --- a/src/Util/CurrencyCodeChecker.php +++ b/src/Util/CurrencyCodeChecker.php @@ -1,5 +1,7 @@ */ - protected $fiatCurrencyCodes = [ + protected array $fiatCurrencyCodes = [ 'AED', 'AFN', 'ALL', @@ -189,7 +191,7 @@ class CurrencyCodeChecker * * @var array */ - protected $cryptoCurrencyCodes = [ + protected array $cryptoCurrencyCodes = [ 'AUR', 'BIS', 'BTC', @@ -313,34 +315,29 @@ class CurrencyCodeChecker /** * Check if crypto currency. - * - * @param string $currencyCode */ - public function isCryptoCurrency($currencyCode): bool + public function isCryptoCurrency(string $currencyCode): bool { return in_array(strtoupper($currencyCode), $this->cryptoCurrencyCodes, true); } /** * Check if fiat currency. - * - * @param string $currencyCode */ - public function isFiatCurrency($currencyCode): bool + public function isFiatCurrency(string $currencyCode): bool { return in_array(strtoupper($currencyCode), $this->fiatCurrencyCodes, true); } /** * Check if currency code. - * - * @param string $currencyCode */ - public function isCurrencyCode($currencyCode): bool + public function isCurrencyCode(string $currencyCode): bool { if ($this->isFiatCurrency($currencyCode)) { return true; } + return $this->isCryptoCurrency($currencyCode); } } diff --git a/src/Util/converter_helper.php b/src/Util/converter_helper.php index ac4f349..6f74ca9 100644 --- a/src/Util/converter_helper.php +++ b/src/Util/converter_helper.php @@ -1,16 +1,15 @@ toCurrency($currencyCode, $btcAmount); @@ -23,11 +22,8 @@ function to_currency($currencyCode, $btcAmount, ?ProviderInterface $provider): f if (! function_exists('to_btc')) { /** * Convert currency amount to Bitcoin. - * - * @param float $amount - * @param string $currencyCode */ - function to_btc($amount, $currencyCode, ?ProviderInterface $provider = null): float + function to_btc(float $amount, string $currencyCode, ?ProviderInterface $provider = null): float { if ($provider instanceof ProviderInterface) { return (new Converter($provider))->toBtc($amount, $currencyCode); diff --git a/src/Util/currency_code_checker_helper.php b/src/Util/currency_code_checker_helper.php index bcfb73f..592946e 100644 --- a/src/Util/currency_code_checker_helper.php +++ b/src/Util/currency_code_checker_helper.php @@ -1,5 +1,7 @@ getBody(); - $responseArray = json_decode($body, true); + $responseArray = json_decode((string) $body, true); $this->assertEquals(200, $response->getStatusCode()); $this->assertNotEmpty($body); diff --git a/tests/Integration/CoinbaseIntegrationTest.php b/tests/Integration/CoinbaseIntegrationTest.php index 7c595ca..f0e62e2 100644 --- a/tests/Integration/CoinbaseIntegrationTest.php +++ b/tests/Integration/CoinbaseIntegrationTest.php @@ -1,5 +1,7 @@ getBody(); - $responseArray = json_decode($body, true); + $responseArray = json_decode((string) $body, true); $this->assertEquals(200, $response->getStatusCode()); $this->assertNotEmpty($body); diff --git a/tests/Integration/CoindeskIntegrationTest.php b/tests/Integration/CoindeskIntegrationTest.php index f0eb37f..665e80a 100644 --- a/tests/Integration/CoindeskIntegrationTest.php +++ b/tests/Integration/CoindeskIntegrationTest.php @@ -1,5 +1,7 @@ getBody(); - $responseArray = json_decode($body, true); + $responseArray = json_decode((string) $body, true); $this->assertEquals(200, $response->getStatusCode()); $this->assertNotEmpty($body); diff --git a/tests/TestCase.php b/tests/TestCase.php index e2f6078..778f26c 100644 --- a/tests/TestCase.php +++ b/tests/TestCase.php @@ -1,5 +1,7 @@ getProperty('provider'); $property->setAccessible(true); + $provider = $property->getValue($convert); $this->assertInstanceOf(CoinbaseProvider::class, $provider); diff --git a/tests/Unit/Provider/BitpayProviderTest.php b/tests/Unit/Provider/BitpayProviderTest.php index fcfcc4d..7807a32 100644 --- a/tests/Unit/Provider/BitpayProviderTest.php +++ b/tests/Unit/Provider/BitpayProviderTest.php @@ -1,5 +1,7 @@ getProperty('client'); $property->setAccessible(true); + $client = $property->getValue($provider); $this->assertInstanceOf(Client::class, $client); @@ -133,6 +136,7 @@ public function test_uses_illuminateCache_as_default_cache_implementation(): voi $reflection = new ReflectionClass($provider); $property = $reflection->getProperty('cache'); $property->setAccessible(true); + $cache = $property->getValue($provider); $this->assertInstanceOf(Repository::class, $cache); diff --git a/tests/Unit/Provider/CoinbaseProviderTest.php b/tests/Unit/Provider/CoinbaseProviderTest.php index 8a22d41..cdc5aea 100644 --- a/tests/Unit/Provider/CoinbaseProviderTest.php +++ b/tests/Unit/Provider/CoinbaseProviderTest.php @@ -1,5 +1,7 @@ getProperty('client'); $property->setAccessible(true); + $client = $property->getValue($provider); $this->assertInstanceOf(Client::class, $client); @@ -133,6 +136,7 @@ public function test_uses_illuminateCache_as_default_cache_implementation(): voi $reflection = new ReflectionClass($provider); $property = $reflection->getProperty('cache'); $property->setAccessible(true); + $cache = $property->getValue($provider); $this->assertInstanceOf(Repository::class, $cache); diff --git a/tests/Unit/Provider/CoindeskProviderTest.php b/tests/Unit/Provider/CoindeskProviderTest.php index b30718b..b3fe526 100644 --- a/tests/Unit/Provider/CoindeskProviderTest.php +++ b/tests/Unit/Provider/CoindeskProviderTest.php @@ -1,5 +1,7 @@ getProperty('client'); $property->setAccessible(true); + $client = $property->getValue($provider); $this->assertInstanceOf(Client::class, $client); @@ -129,6 +132,7 @@ public function test_uses_illuminateCache_as_default_cache_implementation(): voi $reflection = new ReflectionClass($provider); $property = $reflection->getProperty('cache'); $property->setAccessible(true); + $cache = $property->getValue($provider); $this->assertInstanceOf(Repository::class, $cache); diff --git a/tests/Unit/Provider/ProviderTest.php b/tests/Unit/Provider/ProviderTest.php index 8181b77..b2d8ae7 100644 --- a/tests/Unit/Provider/ProviderTest.php +++ b/tests/Unit/Provider/ProviderTest.php @@ -1,5 +1,7 @@