Skip to content

Commit

Permalink
Merge pull request #89 from commerceguys/php8
Browse files Browse the repository at this point in the history
Drop PHP7 compatibility.
  • Loading branch information
jsacksick authored Mar 2, 2023
2 parents 6dad224 + 4c90d45 commit b49228e
Show file tree
Hide file tree
Showing 24 changed files with 201 additions and 198 deletions.
24 changes: 18 additions & 6 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,26 +9,38 @@ jobs:
- uses: actions/checkout@v3
- uses: php-actions/composer@v6

- name: PHPUnit Tests PHP 7.3
- name: PHPUnit Tests PHP 8.0
uses: php-actions/phpunit@master
with:
bootstrap: vendor/autoload.php
configuration: phpunit.xml
args: --coverage-text
php_extensions: xdebug bcmath
php_version: 7.3
version: 9
php_version: 8.0
version: 10
env:
XDEBUG_MODE: coverage

- name: PHPUnit Tests PHP 8.0
- name: PHPUnit Tests PHP 8.1
uses: php-actions/phpunit@master
with:
bootstrap: vendor/autoload.php
configuration: phpunit.xml
args: --coverage-text
php_extensions: xdebug bcmath
php_version: 8.0
version: 9
php_version: 8.1
version: 10
env:
XDEBUG_MODE: coverage

- name: PHPUnit Tests PHP 8.2
uses: php-actions/phpunit@master
with:
bootstrap: vendor/autoload.php
configuration: phpunit.xml
args: --coverage-text
php_extensions: xdebug bcmath
php_version: 8.2
version: 10
env:
XDEBUG_MODE: coverage
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ intl

[![Build Status](https://github.com/commerceguys/intl/actions/workflows/build.yml/badge.svg)](https://github.com/commerceguys/intl/actions/workflows/build.yml)

A PHP 7.3+ internationalization library, powered by CLDR data.
A PHP 8.0+ internationalization library, powered by CLDR data.

Features:
- NumberFormatter and CurrencyFormatter, inspired by [intl](http://php.net/manual/en/class.numberformatter.php).
Expand Down
6 changes: 3 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@
"description": "Internationalization library powered by CLDR data.",
"license": "MIT",
"require": {
"php": ">=7.3"
"php": ">=8.0"
},
"require-dev": {
"phpunit/phpunit": "^9.5",
"phpunit/phpunit": "^10",
"mikey179/vfsstream": "1.*"
},
"autoload": {
Expand All @@ -30,7 +30,7 @@
],
"extra": {
"branch-alias": {
"dev-master": "1.x-dev"
"dev-master": "2.x-dev"
}
}
}
2 changes: 1 addition & 1 deletion phpunit.xml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" backupGlobals="false" backupStaticAttributes="false" colors="true" convertErrorsToExceptions="true" convertNoticesToExceptions="true" convertWarningsToExceptions="true" stopOnFailure="false" bootstrap="vendor/autoload.php" xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/9.3/phpunit.xsd">
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" backupGlobals="false" colors="true" stopOnFailure="false" bootstrap="vendor/autoload.php" xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/10.0/phpunit.xsd" cacheDirectory=".phpunit.cache" backupStaticProperties="false">
<coverage>
<include>
<directory suffix=".php">./src/</directory>
Expand Down
51 changes: 24 additions & 27 deletions src/Calculator.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ final class Calculator
*
* @return string The result.
*/
public static function add($first_number, $second_number, $scale = 6)
public static function add(string $first_number, string $second_number, int $scale = 6): string
{
self::assertNumberFormat($first_number);
self::assertNumberFormat($second_number);
Expand All @@ -44,7 +44,7 @@ public static function add($first_number, $second_number, $scale = 6)
*
* @return string The result.
*/
public static function subtract($first_number, $second_number, $scale = 6)
public static function subtract(string $first_number, string $second_number, int $scale = 6): string
{
self::assertNumberFormat($first_number);
self::assertNumberFormat($second_number);
Expand All @@ -64,7 +64,7 @@ public static function subtract($first_number, $second_number, $scale = 6)
*
* @return string The result.
*/
public static function multiply($first_number, $second_number, $scale = 6)
public static function multiply(string $first_number, string $second_number, int $scale = 6): string
{
self::assertNumberFormat($first_number);
self::assertNumberFormat($second_number);
Expand All @@ -84,7 +84,7 @@ public static function multiply($first_number, $second_number, $scale = 6)
*
* @return string The result.
*/
public static function divide($first_number, $second_number, $scale = 6)
public static function divide(string $first_number, string $second_number, int $scale = 6): string
{
self::assertNumberFormat($first_number);
self::assertNumberFormat($second_number);
Expand All @@ -100,7 +100,7 @@ public static function divide($first_number, $second_number, $scale = 6)
*
* @return string The result.
*/
public static function ceil($number)
public static function ceil(string $number): string
{
if (self::compare($number, 0) == 1) {
$result = bcadd($number, '1', 0);
Expand All @@ -118,7 +118,7 @@ public static function ceil($number)
*
* @return string The result.
*/
public static function floor($number)
public static function floor(string $number): string
{
if (self::compare($number, 0) == 1) {
$result = bcadd($number, '0', 0);
Expand All @@ -145,7 +145,7 @@ public static function floor($number)
*
* @throws \InvalidArgumentException
*/
public static function round($number, $precision = 0, $mode = PHP_ROUND_HALF_UP)
public static function round(string $number, int $precision = 0, int $mode = PHP_ROUND_HALF_UP): string
{
self::assertNumberFormat($number);
if (!is_numeric($precision) || $precision < 0) {
Expand All @@ -163,7 +163,7 @@ public static function round($number, $precision = 0, $mode = PHP_ROUND_HALF_UP)
// The rounding direction is based on the first decimal after $precision.
$number_parts = explode('.', $number);
$decimals = !empty($number_parts[1]) ? $number_parts[1] : '0';
$relevant_decimal = isset($decimals[$precision]) ? $decimals[$precision] : 0;
$relevant_decimal = $decimals[$precision] ?? 0;
if ($relevant_decimal < 5) {
$number = $rounded_down;
} elseif ($relevant_decimal == 5) {
Expand Down Expand Up @@ -197,11 +197,10 @@ public static function round($number, $precision = 0, $mode = PHP_ROUND_HALF_UP)
* @return int 0 if both numbers are equal, 1 if the first one is greater,
* -1 otherwise.
*/
public static function compare($first_number, $second_number, $scale = 6)
public static function compare(string $first_number, string $second_number, int $scale = 6): int
{
self::assertNumberFormat($first_number);
self::assertNumberFormat($second_number);

return bccomp($first_number, $second_number, $scale);
}

Expand All @@ -216,7 +215,7 @@ public static function compare($first_number, $second_number, $scale = 6)
*
* @return string The trimmed number.
*/
public static function trim($number)
public static function trim(string $number): string
{
if (strpos($number, '.') != false) {
// The number is decimal, strip trailing zeroes.
Expand All @@ -228,20 +227,18 @@ public static function trim($number)
return $number;
}

/**
* Assert that the given number is a numeric string value.
*
* @param string $number The number to check.
*
* @throws \InvalidArgumentException
*/
public static function assertNumberFormat($number)
{
if (is_float($number)) {
throw new \InvalidArgumentException(sprintf('The provided value "%s" must be a string, not a float.', $number));
}
if (!is_numeric($number)) {
throw new \InvalidArgumentException(sprintf('The provided value "%s" is not a numeric value.', $number));
}
}
/**
* Assert that the given number is a numeric string value.
*
* @param string $number The number to check.
*
* @throws \InvalidArgumentException
*/
public static function assertNumberFormat(string $number)
{
if (!is_numeric($number)) {
throw new \InvalidArgumentException(sprintf('The provided value "%s" is not a numeric value.', $number));
}
}

}
26 changes: 13 additions & 13 deletions src/Currency/Currency.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,42 +12,42 @@ final class Currency
*
* @var string
*/
protected $currencyCode;
protected string $currencyCode;

/**
* The currency name.
*
* @var string
*/
protected $name;
protected string $name;

/**
* The numeric currency code.
*
* @var string
*/
protected $numericCode;
protected string $numericCode;

/**
* The currency symbol.
*
* @var string
*/
protected $symbol;
protected string $symbol;

/**
* The number of fraction digits.
*
* @var int
*/
protected $fractionDigits = 2;
protected int $fractionDigits = 2;

/**
* The locale (i.e. "en_US").
*
* @var string
*/
protected $locale;
protected string $locale;

/**
* Creates a new Currency instance.
Expand Down Expand Up @@ -80,7 +80,7 @@ public function __construct(array $definition)
*
* @return string
*/
public function __toString()
public function __toString(): string
{
return $this->currencyCode;
}
Expand All @@ -90,7 +90,7 @@ public function __toString()
*
* @return string
*/
public function getCurrencyCode()
public function getCurrencyCode(): string
{
return $this->currencyCode;
}
Expand All @@ -102,7 +102,7 @@ public function getCurrencyCode()
*
* @return string
*/
public function getName()
public function getName(): string
{
return $this->name;
}
Expand All @@ -115,7 +115,7 @@ public function getName()
*
* @return string
*/
public function getNumericCode()
public function getNumericCode(): string
{
return $this->numericCode;
}
Expand All @@ -127,7 +127,7 @@ public function getNumericCode()
*
* @return string
*/
public function getSymbol()
public function getSymbol(): string
{
return $this->symbol;
}
Expand All @@ -140,7 +140,7 @@ public function getSymbol()
*
* @return int
*/
public function getFractionDigits()
public function getFractionDigits(): string
{
return $this->fractionDigits;
}
Expand All @@ -152,7 +152,7 @@ public function getFractionDigits()
*
* @return string
*/
public function getLocale()
public function getLocale(): string
{
return $this->locale;
}
Expand Down
Loading

0 comments on commit b49228e

Please sign in to comment.