Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix psalm errors #170

Merged
merged 1 commit into from
Feb 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ public function testReverseFormValueIsNotArray(): void

public function testReverseThrowExceptionIfCurrencyCodeNotValid(): void
{
$this->expectException(TransformationFailedException::class);
$this->expectException(UnexpectedTypeException::class);
$value = ['tbbc_name' => 123];
$transformer = new CurrencyToArrayTransformer();
$transformer->reverseTransform($value);
Expand Down
2 changes: 1 addition & 1 deletion Tests/Pair/RatioProvider/ECBRatioProviderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ public function testNotCorrectReferenceCode(): void
public function testUnknownCurrency(): void
{
$this->expectException(MoneyException::class);
$this->expectExceptionMessage('The currency code "" does not exist');
$this->expectExceptionMessage('The currency code is an empty string');
$this->ratio->fetchRatio('EUR', '');
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,7 @@ protected function getRatioProvider(): RatioProviderInterface
public function testInvalidCurrencyCode(): void
{
$this->expectException(MoneyException::class);
$this->expectException(InvalidArgumentException::class);
$this->expectExceptionMessage('The currency pair must be in the form "EUR/USD".');
//$this->expectExceptionMessage('The currency code "" does not exist');
$this->expectExceptionMessage('The currency code is an empty string');

$ratiosSetup['EUR/123'] = $this->randomRatio(1, 3, 1);
$service = new PhpArray($ratiosSetup);
Expand Down
6 changes: 3 additions & 3 deletions src/Command/RatioListCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ protected function configure(): void
}

/**
* @param array<string, float> $ratioList
* @param array<string, null|float> $ratioList
*/
protected function displayTxt(array $ratioList, OutputInterface $output, SymfonyStyle $io): int
{
Expand All @@ -49,7 +49,7 @@ protected function displayTxt(array $ratioList, OutputInterface $output, Symfony
}

/**
* @param array<string, float> $ratioList
* @param array<string, null|float> $ratioList
*/
protected function displayTable(array $ratioList, OutputInterface $output, SymfonyStyle $io): int
{
Expand All @@ -67,7 +67,7 @@ protected function displayTable(array $ratioList, OutputInterface $output, Symfo
}

/**
* @param array<string, float> $ratioList
* @param array<string, null|float> $ratioList
* @throws \JsonException
*/
protected function displayJson(array $ratioList, OutputInterface $output): int
Expand Down
23 changes: 18 additions & 5 deletions src/Form/DataTransformer/CurrencyToArrayTransformer.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,18 @@
use TypeError;

/**
* Transforms between a Currency and a string.
* Transforms between a Currency and an array.
*
* @implements DataTransformerInterface<Currency, array>
*/
class CurrencyToArrayTransformer implements DataTransformerInterface
{
/**
* {@inheritdoc}
*
* @psalm-param Currency|null $value
*/
public function transform($value): ?array
public function transform(mixed $value): ?array
{
if (null === $value) {
return null;
Expand All @@ -33,15 +37,16 @@ public function transform($value): ?array

/**
* {@inheritdoc}
*
* @psalm-suppress MixedArgument
*
* @psalm-param array|null $value
*/
public function reverseTransform($value): ?Currency
public function reverseTransform(mixed $value): ?Currency
{
if (null === $value) {
return null;
}

/** @psalm-suppress DocblockTypeContradiction */
if (!is_array($value)) {
throw new UnexpectedTypeException($value, 'array');
}
Expand All @@ -50,6 +55,14 @@ public function reverseTransform($value): ?Currency
return null;
}

if (!is_string($value['tbbc_name'])) {
throw new UnexpectedTypeException($value, 'string');
}

if ('' === $value['tbbc_name']) {
throw new TransformationFailedException('name can not be an empty string');
}

try {
return new Currency($value['tbbc_name']);
} catch (InvalidArgumentException|TypeError $e) {
Expand Down
24 changes: 19 additions & 5 deletions src/Form/DataTransformer/MoneyToArrayTransformer.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,14 @@
use Money\Currency;
use Money\Money;
use Symfony\Component\Form\DataTransformerInterface;
use Symfony\Component\Form\Exception\TransformationFailedException;
use Symfony\Component\Form\Exception\UnexpectedTypeException;
use Symfony\Component\Form\Extension\Core\DataTransformer\MoneyToLocalizedStringTransformer;

/**
* Transforms between a Money instance and an array.
* Transforms between a Money and an array.
*
* @implements DataTransformerInterface<Money, array>
*/
class MoneyToArrayTransformer implements DataTransformerInterface
{
Expand All @@ -24,10 +27,12 @@ public function __construct(protected int $decimals = 2)

/**
* {@inheritdoc}
*
* @psalm-return array{tbbc_amount: string, tbbc_currency: Currency}|null
*
* @psalm-param Money|null $value
*
* @psalm-return array{tbbc_amount: string, tbbc_currency: Currency}|array{tbbc_amount: string}|null
*/
public function transform($value): ?array
public function transform(mixed $value): ?array
{
if (null === $value) {
return null;
Expand All @@ -47,16 +52,20 @@ public function transform($value): ?array

/**
* {@inheritdoc}
*
* @psalm-param array|null $value
*/
public function reverseTransform($value): ?Money
public function reverseTransform(mixed $value): ?Money
{
if (null === $value) {
return null;
}

/** @psalm-suppress DocblockTypeContradiction */
if (!is_array($value)) {
throw new UnexpectedTypeException($value, 'array');
}

if (!isset($value['tbbc_amount']) || !isset($value['tbbc_currency'])) {
return null;
}
Expand All @@ -69,6 +78,11 @@ public function reverseTransform($value): ?Money

/** @var string|Currency $currency */
$currency = $value['tbbc_currency'];

if ('' === $currency) {
throw new TransformationFailedException('currency can not be an empty string');
}

if (!$currency instanceof Currency) {
$currency = new Currency($currency);
}
Expand Down
14 changes: 10 additions & 4 deletions src/Form/DataTransformer/SimpleMoneyToArrayTransformer.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,21 +7,25 @@
use Money\Money;

/**
* Transforms between a Money instance and an array.
* Transforms between a Money and an array.
*/
class SimpleMoneyToArrayTransformer extends MoneyToArrayTransformer
{
protected string $currency = '';

public function __construct(int $decimals)
public function __construct(protected int $decimals)
{
parent::__construct($decimals);
}

/**
* {@inheritdoc}
*
* @psalm-param Money|null $value
*
* @psalm-return array{tbbc_amount: string}|null
*/
public function transform($value): ?array
public function transform(mixed $value): ?array
{
if (!$tab = parent::transform($value)) {
return null;
Expand All @@ -34,8 +38,10 @@ public function transform($value): ?array

/**
* {@inheritdoc}
*
* @psalm-param array|null $value
*/
public function reverseTransform($value): ?Money
public function reverseTransform(mixed $value): ?Money
{
if (is_array($value)) {
$value['tbbc_currency'] = $this->currency;
Expand Down
11 changes: 10 additions & 1 deletion src/Money/MoneyManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

use Money\Currency;
use Money\Money;
use Tbbc\MoneyBundle\MoneyException;

/**
* @author levan
Expand All @@ -14,16 +15,24 @@ class MoneyManager implements MoneyManagerInterface
{
public function __construct(protected string $referenceCurrencyCode, protected int $decimals = 2)
{
if ('' === $referenceCurrencyCode) {
throw new MoneyException('reference currency can not be an empty string');
}
}

/**
* {@inheritdoc}
*/
public function createMoneyFromFloat(float $floatAmount, ?string $currencyCode = null): Money
{
if (is_null($currencyCode)) {
if (null === $currencyCode) {
$currencyCode = $this->referenceCurrencyCode;
}

if ('' === $currencyCode) {
throw new MoneyException('currency can not be an empty string');
}

$currency = new Currency($currencyCode);
$amountAsInt = $floatAmount * 10 ** $this->decimals;
$amountAsInt = round($amountAsInt);
Expand Down
17 changes: 17 additions & 0 deletions src/Pair/PairManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ class PairManager implements PairManagerInterface, Exchange

public function __construct(
protected StorageInterface $storage,
/** @var string[] */
protected array $currencyCodeList,
protected string $referenceCurrencyCode,
protected EventDispatcherInterface $dispatcher
Expand All @@ -38,6 +39,10 @@ public function __construct(
*/
public function convert(Money $amount, string $currencyCode): Money
{
if ('' === $currencyCode) {
throw new MoneyException('currency can not be an empty string');
}

$converter = new Converter($this->currencies, $this);

return $converter->convert($amount, new Currency($currencyCode));
Expand All @@ -58,6 +63,10 @@ public function quote(Currency $baseCurrency, Currency $counterCurrency): Curren
*/
public function saveRatio(string $currencyCode, float $ratio): void
{
if ('' === $currencyCode) {
throw new MoneyException('currency can not be an empty string');
}

$currency = new Currency($currencyCode);

if ($ratio <= 0) {
Expand Down Expand Up @@ -85,6 +94,14 @@ public function saveRatio(string $currencyCode, float $ratio): void
*/
public function getRelativeRatio(string $referenceCurrencyCode, string $currencyCode): float
{
if ('' === $referenceCurrencyCode) {
throw new MoneyException('reference currency can not be an empty string');
}

if ('' === $currencyCode) {
throw new MoneyException('currency can not be an empty string');
}

$currency = new Currency($currencyCode);
$referenceCurrency = new Currency($referenceCurrencyCode);
if ($currencyCode === $referenceCurrencyCode) {
Expand Down
2 changes: 1 addition & 1 deletion src/Pair/PairManagerInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public function getReferenceCurrencyCode(): string;
* return ratio list for currencies in comparison to reference currency
* array of type array("EUR" => 1, "USD" => 1.25);.
*
* @return array<string, float>
* @return array<string, null|float>
*/
public function getRatioList(): array;

Expand Down
5 changes: 3 additions & 2 deletions src/Pair/RatioProvider/ECBRatioProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ public function fetchRatio(string $referenceCurrencyCode, string $currencyCode):
}

if ('' === $currencyCode) {
throw new MoneyException(sprintf('The currency code "%s" does not exist', $currencyCode));
throw new MoneyException('The currency code is an empty string');
}

try {
Expand All @@ -60,7 +60,8 @@ public function fetchRatio(string $referenceCurrencyCode, string $currencyCode):
throw new MoneyException(sprintf('The currency code "%s" does not exists', $currencyCode));
}

if (!$xml = $this->getXML()) {
$xml = $this->getXML();
if (null === $xml || '' === $xml) {
throw new MoneyException('Could not fetch XML from ECB');
}

Expand Down
4 changes: 4 additions & 0 deletions src/Pair/RatioProvider/ExchangerAdapterRatioProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,10 @@ private function getCurrencyPair(string $referenceCurrencyCode, string $currency

private function ensureValidCurrency(string $currencyCode): void
{
if ('' === $currencyCode) {
throw new MoneyException('The currency code is an empty string');
}

try {
new Currency($currencyCode);
} catch (UnknownCurrencyException|InvalidArgumentException) {
Expand Down
4 changes: 4 additions & 0 deletions src/Pair/Storage/CsvStorage.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
*/
class CsvStorage implements StorageInterface
{
/** @psalm-var array<string, null|float> */
protected array $ratioList = [];

public function __construct(protected string $ratioFileName, protected string $referenceCurrencyCode)
Expand Down Expand Up @@ -52,6 +53,9 @@ public function loadRatioList(bool $force = false): array
[$currencyCode, $ratio] = $data;

// validate that currency exist in currency code list
if (null === $currencyCode || '' === $currencyCode) {
throw new MoneyException('error in ratioFileName '.$this->ratioFileName.' on line '.$row.', currency is an empty string or is null');
}
// @codeCoverageIgnoreStart
try {
// hack to throw an exception if currency doesn't exist
Expand Down
Loading