diff --git a/.travis.yml b/.travis.yml index 7896b6d..e196514 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,16 +1,14 @@ language: php php: -- '5.6' - '7.0' - '7.1' +- '7.2' env: - INTL_VERSION="2.3.*" - INTL_VERSION="3.0.*" - INTL_VERSION="4.0.*" matrix: exclude: - - php: '5.6' - env: INTL_VERSION="4.0.*" - php: '7.0' env: INTL_VERSION="4.0.*" before_script: diff --git a/composer.json b/composer.json index 1cff74b..8a92f14 100644 --- a/composer.json +++ b/composer.json @@ -13,7 +13,7 @@ } ], "require": { - "php": "^5.6|^7", + "php": "^7", "symfony/intl": "^2.0 || ^3.0 || ^4.0" }, "require-dev": { diff --git a/src/Money.php b/src/Money.php index 97c483a..348f0ce 100644 --- a/src/Money.php +++ b/src/Money.php @@ -17,10 +17,10 @@ class Money private $currency; /** - * @param float $amount + * @param float|int|string $amount * @param string $currency */ - public function __construct($amount, $currency) + public function __construct($amount, string $currency) { if (!is_numeric($amount)) { throw new InvalidArgumentException('Money only accepts numeric amounts'); @@ -33,29 +33,20 @@ public function __construct($amount, $currency) $this->currency = $currency; } - /** - * @return string - */ - public function __toString() + public function __toString(): string { return $this->format(); } /** * Returns a positive clone of Money object - * - * @return static */ - public function abs() + public function abs(): Money { return new static(abs($this->amount), $this->currency); } - /** - * @param Money $money - * @return static - */ - public function add(Money $money) + public function add(Money $money): Money { $this->assertCurrencyMatches($money); @@ -66,7 +57,7 @@ public function add(Money $money) * @param float $operator * @return static */ - public function divide($operator) + public function divide($operator): Money { if ($operator == 0) { throw new InvalidArgumentException('Cannot divide by zero'); @@ -80,7 +71,7 @@ public function divide($operator) * @param bool $displayCountryForUS Set to true if you would like 'US$' instead of just '$' * @return string */ - public function format($displayCountryForUS = false) + public function format(bool $displayCountryForUS = false): string { $formatter = new NumberFormatter('en', NumberFormatter::CURRENCY); @@ -96,10 +87,8 @@ public function format($displayCountryForUS = false) /** * Returns a rounded number without currency * If the number is negative, the currency is within parentheses - * - * @return string */ - public function formatForAccounting() + public function formatForAccounting(): string { $amount = $this->getRoundedAmount(); $negative = 0 > $amount; @@ -113,10 +102,8 @@ public function formatForAccounting() /** * Returns a string consisting of the currency symbol, a rounded int and a suffix * e.g. $33k instead of $3321.12 - * - * @return string */ - public function formatShorthand() + public function formatShorthand(): string { $amount = $this->amount; $negative = 0 > $amount; @@ -135,7 +122,7 @@ public function formatShorthand() * @param bool $displayCountryForUS Set to true if you would like 'US$' instead of just '$' * @return string */ - public function formatWithSign($displayCountryForUS = false) + public function formatWithSign(bool $displayCountryForUS = false): string { $string = $this->format($displayCountryForUS); @@ -146,28 +133,20 @@ public function formatWithSign($displayCountryForUS = false) return '+' . $string; } - /** - * @return float - */ - public function getAmount() + public function getAmount(): float { return $this->amount; } - /** - * @return string - */ - public function getCurrency() + public function getCurrency(): string { return $this->currency; } /** * Returns the amount rounded to the correct number of decimal places for that currency - * - * @return float */ - public function getRoundedAmount() + public function getRoundedAmount(): float { $fractionDigits = Intl::getCurrencyBundle()->getFractionDigits($this->currency); $roundingIncrement = Intl::getCurrencyBundle()->getRoundingIncrement($this->currency); @@ -185,10 +164,8 @@ public function getRoundedAmount() /** * Invert the amount - * - * @return static */ - public function inv() + public function inv(): Money { return new static(-$this->amount, $this->currency); } @@ -197,7 +174,7 @@ public function inv() * @param float $operator * @return static */ - public function multiply($operator) + public function multiply($operator): Money { return new static($this->amount * $operator, $this->currency); } @@ -208,17 +185,15 @@ public function multiply($operator) * @param float $percentage * @return static */ - public function percentage($percentage) + public function percentage($percentage): Money { return new static(($this->amount * $percentage) / 100, $this->currency); } /** * Returns rounded clone of Money object, rounded to the correct number of decimal places for that currency - * - * @return static */ - public function round() + public function round(): Money { return new static($this->getRoundedAmount(), $this->currency); } @@ -232,9 +207,9 @@ public function round() * * @param float[] $percentages An array of percentages that must total 100 or less * @param bool $round - * @return array + * @return Money[] */ - public function split(array $percentages, $round = true) + public function split(array $percentages, bool $round = true): array { $totalPercentage = array_sum($percentages); if ($totalPercentage > 100) { @@ -278,7 +253,7 @@ public function split(array $percentages, $round = true) * @param Money $money * @return static */ - public function sub(Money $money) + public function sub(Money $money): Money { $this->assertCurrencyMatches($money); diff --git a/tests/MoneyTest.php b/tests/MoneyTest.php index 221cc9b..985aa07 100644 --- a/tests/MoneyTest.php +++ b/tests/MoneyTest.php @@ -7,7 +7,7 @@ class MoneyTest extends PHPUnit_Framework_TestCase { - public function amountDataProvider() + public function amountDataProvider(): array { return [ [1000.00, 1000.00], @@ -27,7 +27,7 @@ public function testCanRetrieveAmount($amount, $expected) $this->assertEquals($expected, $money->getAmount()); } - public function currencyCodeDataProvider() + public function currencyCodeDataProvider(): array { return [ ['USD'], @@ -41,7 +41,7 @@ public function currencyCodeDataProvider() * @dataProvider currencyCodeDataProvider * @param string $currencyCode */ - public function testCanRetrieveCurrency($currencyCode) + public function testCanRetrieveCurrency(string $currencyCode) { $money = new Money(1000, $currencyCode); $this->assertEquals($currencyCode, $money->getCurrency()); @@ -85,7 +85,7 @@ public function testAbsoluteMoney($amount, $expected) $this->assertEquals($expected, $money->abs()->getAmount()); } - public function invertedAmountDataProvider() + public function invertedAmountDataProvider(): array { return [ ['-9.9999', 9.9999], @@ -156,7 +156,7 @@ public function testMultiplying() $this->assertEquals(30, $money->multiply(3)->getAmount()); } - public function formattingDataProvider() + public function formattingDataProvider(): array { return [ ['10.000', 'USD', '$10.00'], @@ -176,13 +176,13 @@ public function formattingDataProvider() * @param string $currencyCode * @param string $expected */ - public function testFormatting($amount, $currencyCode, $expected) + public function testFormatting($amount, string $currencyCode, $expected) { $money = new Money($amount, $currencyCode); $this->assertEquals($expected, (string)$money); } - public function manualFormattingDataProvider() + public function manualFormattingDataProvider(): array { return [ [-10, 'USD', false, '-$10.00'], @@ -196,15 +196,16 @@ public function manualFormattingDataProvider() * @dataProvider manualFormattingDataProvider * @param mixed $amount * @param string $currencyCode + * @param bool $displayCountryForUS * @param string $expected */ - public function testManualFormatting($amount, $currencyCode, $displayCountryForUS, $expected) + public function testManualFormatting($amount, string $currencyCode, bool $displayCountryForUS, string $expected) { $money = new Money($amount, $currencyCode); $this->assertEquals($expected, $money->format($displayCountryForUS)); } - public function forcedPlusFormattingDataProvider() + public function forcedPlusFormattingDataProvider(): array { return [ [0, 'USD', false, '$0.00'], @@ -223,13 +224,13 @@ public function forcedPlusFormattingDataProvider() * @param bool $displayCountryForUS * @param string $expected */ - public function testForcePlusFormatting($amount, $currencyCode, $displayCountryForUS, $expected) + public function testForcePlusFormatting($amount, string $currencyCode, bool $displayCountryForUS, string $expected) { $money = new Money($amount, $currencyCode); $this->assertEquals($expected, $money->formatWithSign($displayCountryForUS)); } - public function accountingFormattingDataProvider() + public function accountingFormattingDataProvider(): array { return [ [0, 'USD', '0.00'], @@ -246,13 +247,13 @@ public function accountingFormattingDataProvider() * @param string $currencyCode * @param string $expected */ - public function testAccountingFormatting($amount, $currencyCode, $expected) + public function testAccountingFormatting($amount, string $currencyCode, string $expected) { $money = new Money($amount, $currencyCode); $this->assertSame($expected, $money->formatForAccounting()); } - public function roundingDataProvider() + public function roundingDataProvider(): array { return [ ['10.000', 'USD', 10.00], @@ -272,14 +273,14 @@ public function roundingDataProvider() * @param string $currencyCode * @param string $expected */ - public function testRoundedAmount($amount, $currencyCode, $expected) + public function testRoundedAmount($amount, string $currencyCode, $expected) { $money = new Money($amount, $currencyCode); $this->assertEquals($expected, $money->getRoundedAmount()); $this->assertEquals($expected, $money->round()->getAmount()); } - public function invalidCurrencyDataProvider() + public function invalidCurrencyDataProvider(): array { return [ ['ZZZ'], @@ -291,13 +292,13 @@ public function invalidCurrencyDataProvider() * @dataProvider invalidCurrencyDataProvider * @param string $currencyCode */ - public function testValidCurrency($currencyCode) + public function testValidCurrency(string $currencyCode) { $this->expectException(InvalidArgumentException::class); new Money(10.000000, $currencyCode); } - public function percentageDataProvider() + public function percentageDataProvider(): array { return [ ['2.222222', 50, 1.111111], @@ -310,9 +311,9 @@ public function percentageDataProvider() * @dataProvider percentageDataProvider * @param mixed $amount * @param int $percentage - * @param string $expected + * @param float $expected */ - public function testGettingPercentage($amount, $percentage, $expected) + public function testGettingPercentage($amount, int $percentage, float $expected) { $money = new Money($amount, 'USD'); $this->assertEquals($expected, $money->percentage($percentage)->getAmount());