Skip to content

Commit

Permalink
Upgrading to PHP 7
Browse files Browse the repository at this point in the history
  • Loading branch information
votemike committed Nov 30, 2017
1 parent d0efa67 commit 927475a
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 68 deletions.
4 changes: 1 addition & 3 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -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:
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
}
],
"require": {
"php": "^5.6|^7",
"php": "^7",
"symfony/intl": "^2.0 || ^3.0 || ^4.0"
},
"require-dev": {
Expand Down
65 changes: 20 additions & 45 deletions src/Money.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand All @@ -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);

Expand All @@ -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');
Expand All @@ -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);

Expand All @@ -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;
Expand All @@ -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;
Expand All @@ -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);

Expand All @@ -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);
Expand All @@ -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);
}
Expand All @@ -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);
}
Expand All @@ -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);
}
Expand All @@ -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) {
Expand Down Expand Up @@ -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);

Expand Down
39 changes: 20 additions & 19 deletions tests/MoneyTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

class MoneyTest extends PHPUnit_Framework_TestCase
{
public function amountDataProvider()
public function amountDataProvider(): array
{
return [
[1000.00, 1000.00],
Expand All @@ -27,7 +27,7 @@ public function testCanRetrieveAmount($amount, $expected)
$this->assertEquals($expected, $money->getAmount());
}

public function currencyCodeDataProvider()
public function currencyCodeDataProvider(): array
{
return [
['USD'],
Expand All @@ -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());
Expand Down Expand Up @@ -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],
Expand Down Expand Up @@ -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'],
Expand All @@ -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'],
Expand All @@ -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'],
Expand All @@ -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'],
Expand All @@ -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],
Expand All @@ -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'],
Expand All @@ -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],
Expand All @@ -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());
Expand Down

0 comments on commit 927475a

Please sign in to comment.