Skip to content

Commit

Permalink
Add PHP 8 support (#15)
Browse files Browse the repository at this point in the history
* Add PHP 8 support

* Psalm fixes

* Type casting fix

* Refactor psalm types

* Revert composer.lock changes

* Fix code

* Clean up type annotations

* Fix code style

* Cover creation money from stringable object

* Add mbstring extension to requirements

* Remove replace section from composer.json

* Update deps for support PHP 8.0

Co-authored-by: Veaceslav Medvedev <[email protected]>
  • Loading branch information
akozhevnikov-rebilly and Veaceslav Medvedev authored Oct 8, 2021
1 parent c3293fa commit 476ec17
Show file tree
Hide file tree
Showing 8 changed files with 428 additions and 214 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/workflow.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ jobs:
strategy:
max-parallel: 1
matrix:
php-version: ['7.3', '7.4']
php-version: ['7.3', '7.4', '8.0']

env:
EXECUTE_COVERAGE: ${{ matrix.php-version == '7.3' }}
Expand Down
5 changes: 3 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,9 @@
},

"require": {
"php": "^7.3",
"ext-json": "*"
"php": "^7.3 || ^8.0",
"ext-json": "*",
"ext-mbstring": "*"
},

"require-dev": {
Expand Down
582 changes: 387 additions & 195 deletions composer.lock

Large diffs are not rendered by default.

15 changes: 12 additions & 3 deletions src/Currency.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,15 @@
*
* @see http://www.github.com/sebastianbergmann/money
* @see http://docs.oracle.com/javase/7/docs/api/java/util/Currency.html
*
* @psalm-type CurrencyAlpha2Code=string
* @psalm-type CurrencyMetadata=array{display_name: string, numeric_code: int, default_fraction_digits: int, sub_unit: int, sign: string, deprecated: boolean}
* @psalm-type CurrencyRegistry=array<CurrencyAlpha2Code, CurrencyMetadata>
*/
final class Currency implements JsonSerializable
{
/**
* @var array
* @var CurrencyRegistry
*/
private static $currencies = [
'AED' => [
Expand Down Expand Up @@ -1518,7 +1522,7 @@ final class Currency implements JsonSerializable
];

/**
* @var string
* @var CurrencyAlpha2Code
*/
private $currencyCode;

Expand Down Expand Up @@ -1571,25 +1575,30 @@ public static function addCurrency(
'numeric_code' => $numericCode,
'default_fraction_digits' => $defaultFractionDigits,
'sub_unit' => $subUnit,
'sign' => '',
'deprecated' => $deprecated,
];
}

/**
* Returns only active currencies.
*
* @return CurrencyRegistry
*/
public static function getCurrencies(): array
{
return array_filter(
self::$currencies,
function (array $currency): bool {
static function (array $currency): bool {
return !$currency['deprecated'];
}
);
}

/**
* Returns all currencies: active and deprecated.
*
* @return CurrencyRegistry
*/
public static function getCurrenciesIncludingDeprecated(): array
{
Expand Down
7 changes: 5 additions & 2 deletions src/Exchange/InMemoryRateProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,13 @@

use DateTimeInterface;

/**
* @psalm-type RateRegistry=array<string, float>
*/
final class InMemoryRateProvider implements RateProvider
{
/**
* @var Rate[]
* @var RateRegistry
*/
private $rates;

Expand All @@ -28,7 +31,7 @@ final class InMemoryRateProvider implements RateProvider
* ]
* ```
*
* @param array $rates Array of rates indexed by currency pair string.
* @param RateRegistry $rates Array of rates indexed by currency pair string.
* @param DateTimeInterface $date The datetime at the time the rates were fetched.
*/
public function __construct(array $rates, DateTimeInterface $date)
Expand Down
4 changes: 2 additions & 2 deletions src/Money.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ public function __construct(int $amount, Currency $currency)
* number of fractional digits then the value will be rounded to the
* currency's number of fractional digits.
*
* @param float|int|string $value
* @param float|int|object|string $value
* @param Currency|string $currency
*
* @throws InvalidArgumentException
Expand Down Expand Up @@ -299,7 +299,7 @@ public function allocateToTargets(int $n): array
* Allocate the monetary value represented by this Money object
* using a list of ratios.
*
* @param array $ratios
* @param array<float|int> $ratios
*
* @return self[]
*/
Expand Down
11 changes: 2 additions & 9 deletions tests/CurrencyTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

namespace Money;

use Generator;
use InvalidArgumentException;

class CurrencyTest extends TestCase
Expand Down Expand Up @@ -71,7 +70,6 @@ public function testRegisteredCurrenciesCanBeAccessed(): void
$currencies = Currency::getCurrencies();

self::assertArrayHasKey('EUR', $currencies);
self::assertIsArray($currencies['EUR']);
self::assertArrayHasKey('display_name', $currencies['EUR']);
self::assertArrayHasKey('numeric_code', $currencies['EUR']);
self::assertArrayHasKey('default_fraction_digits', $currencies['EUR']);
Expand Down Expand Up @@ -118,14 +116,9 @@ public function testExceptionIsRaisedForInvalidNumCode(): void
Currency::fromNumericCode(0);
}

/**
* @return Generator
*/
public function provideCurrenciesData(): Generator
public function provideCurrenciesData(): iterable
{
$data = Currency::getCurrencies();

foreach ($data as $isoAlphaCode => $item) {
foreach (Currency::getCurrencies() as $isoAlphaCode => $item) {
yield [$isoAlphaCode, $item];
}
}
Expand Down
16 changes: 16 additions & 0 deletions tests/MoneyTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,22 @@ public function testObjectCanBeConstructedFromEmptyValueAndCurrencyString(): voi
);
}

public function testObjectCanBeConstructedFromStringableObject(): void
{
$amount = new class() {
public function __toString()
{
return '10.0';
}
};

self::assertTrue(
(new Money(1000, new Currency('EUR')))->equals(
Money::fromString($amount, 'EUR')
)
);
}

/**
* @depends testObjectCanBeConstructedFromIntegerValueAndCurrencyObject
*
Expand Down

0 comments on commit 476ec17

Please sign in to comment.