Skip to content

Commit

Permalink
Merge branch 'release/1.1.0'
Browse files Browse the repository at this point in the history
  • Loading branch information
remcotolsma committed Jan 10, 2022
2 parents a62fbf8 + d875fea commit 5778321
Show file tree
Hide file tree
Showing 15 changed files with 171 additions and 154 deletions.
7 changes: 6 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,18 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

## [1.1.0] - 2022-01-10
### Changed
- Improved precision support [pronamic/wp-pronamic-pay#281](https://github.com/pronamic/wp-pronamic-pay/issues/281).

## [1.0.1] - 2021-09-30
- Added number parser.

## [1.0.0] - 2021-07-02
### Added
- First release.

[Unreleased]: https://github.com/pronamic/wp-number/compare/1.0.1...HEAD
[Unreleased]: https://github.com/pronamic/wp-number/compare/1.1.0...HEAD
[1.1.0]: https://github.com/pronamic/wp-number/compare/1.0.1...1.1.0
[1.0.1]: https://github.com/pronamic/wp-number/compare/1.0.0...1.0.1
[1.0.0]: https://github.com/pronamic/wp-number/releases/tag/1.0.0
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,8 @@
"phpunit/phpunit": "^4.0 || ^5.0 || ^6.0 || ^7.0",
"pronamic/wp-coding-standards": "^1.0",
"roots/wordpress": "^5.8",
"wp-phpunit/wp-phpunit": "^5.8"
"wp-phpunit/wp-phpunit": "^5.8",
"yoast/phpunit-polyfills": "^1.0"
},
"scripts": {
"ci": [
Expand Down
2 changes: 1 addition & 1 deletion src/Calculator.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* Calculator
*
* @author Pronamic <[email protected]>
* @copyright 2005-2021 Pronamic
* @copyright 2005-2022 Pronamic
* @license GPL-3.0-or-later
* @package Pronamic\WordPress\Number
*/
Expand Down
2 changes: 1 addition & 1 deletion src/Calculator/BcMathCalculator.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* BC Math Calculator
*
* @author Pronamic <[email protected]>
* @copyright 2005-2021 Pronamic
* @copyright 2005-2022 Pronamic
* @license GPL-3.0-or-later
* @package Pronamic\WordPress\Number
*/
Expand Down
2 changes: 1 addition & 1 deletion src/Calculator/PhpCalculator.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* PHP Calculator
*
* @author Pronamic <[email protected]>
* @copyright 2005-2021 Pronamic
* @copyright 2005-2022 Pronamic
* @license GPL-3.0-or-later
* @package Pronamic\WordPress\Number
*/
Expand Down
41 changes: 17 additions & 24 deletions src/Number.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* Number
*
* @author Pronamic <[email protected]>
* @copyright 2005-2021 Pronamic
* @copyright 2005-2022 Pronamic
* @license GPL-3.0-or-later
* @package Pronamic\WordPress\Number
*/
Expand Down Expand Up @@ -377,45 +377,38 @@ private static function parse_float( $value ) {
* @link https://stackoverflow.com/questions/48205572/json-encode-float-precision-in-php7-and-addition-operation
* @link https://bugs.php.net/bug.php?id=75800
*/

/**
* It seems that precision setting was different before PHP 7.1,
* so we're trying to force this precision.
*/
if ( \version_compare( \PHP_VERSION, '7.1', '<' ) ) {
return self::parse_float_with_precision( $value, '14' ); // @codeCoverageIgnore

}

return self::parse_mixed( \wp_json_encode( $value ) );
return self::parse_float_with_precision( $value, -1 );
}

/**
* Parse float precission.
*
* @codeCoverageIgnore
* @param float $value Value.
* @param string $precision Precision.
* @param float $value Value.
* @param int $precision Precision.
* @psalm-return numeric-string
* @return string
*/
private static function parse_float_with_precision( $value, $precision ) {
// phpcs:ignore WordPress.PHP.IniSet.Risky
$ini_precision = \ini_set( 'precision', $precision );
public static function parse_float_with_precision( $value, $precision ) {
$option = 'serialize_precision';

/**
* The `serialize_precision` option was introduced in PHP 7.1.
*
* @link https://wiki.php.net/rfc/precise_float_value
*/
if ( \version_compare( \PHP_VERSION, '7.1', '<' ) ) {
$option = 'precision';
}

// phpcs:ignore WordPress.PHP.IniSet.Risky
$ini_serialize_precision = \ini_set( 'serialize_precision', $precision );
$ini_serialize_precision = \ini_set( $option, (string) $precision );

$result = self::parse_mixed( \wp_json_encode( $value ) );

if ( false !== $ini_precision ) {
// phpcs:ignore WordPress.PHP.IniSet.Risky
\ini_set( 'precision', $ini_precision );
}

if ( false !== $ini_serialize_precision ) {
// phpcs:ignore WordPress.PHP.IniSet.Risky
\ini_set( 'serialize_precision', $ini_serialize_precision );
\ini_set( $option, $ini_serialize_precision );
}

return $result;
Expand Down
2 changes: 1 addition & 1 deletion src/Parser.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* Parser
*
* @author Pronamic <[email protected]>
* @copyright 2005-2021 Pronamic
* @copyright 2005-2022 Pronamic
* @license GPL-3.0-or-later
* @package Pronamic\WordPress\Number
*/
Expand Down
2 changes: 1 addition & 1 deletion tests/bootstrap.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* Bootstrap tests
*
* @author Pronamic <[email protected]>
* @copyright 2005-2021 Pronamic
* @copyright 2005-2022 Pronamic
* @license GPL-3.0-or-later
* @package Pronamic\WordPress\Money
*/
Expand Down
2 changes: 1 addition & 1 deletion tests/src/Calculator/BcMathCalculatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* BC Math Calculator Test
*
* @author Pronamic <[email protected]>
* @copyright 2005-2021 Pronamic
* @copyright 2005-2022 Pronamic
* @license GPL-3.0-or-later
* @package Pronamic\WordPress\Money
*/
Expand Down
2 changes: 1 addition & 1 deletion tests/src/Calculator/CalculatorTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* PHP Calculator Test
*
* @author Pronamic <[email protected]>
* @copyright 2005-2021 Pronamic
* @copyright 2005-2022 Pronamic
* @license GPL-3.0-or-later
* @package Pronamic\WordPress\Money
*/
Expand Down
2 changes: 1 addition & 1 deletion tests/src/Calculator/PhpCalculatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* PHP Calculator Test
*
* @author Pronamic <[email protected]>
* @copyright 2005-2021 Pronamic
* @copyright 2005-2022 Pronamic
* @license GPL-3.0-or-later
* @package Pronamic\WordPress\Money
*/
Expand Down
16 changes: 15 additions & 1 deletion tests/src/NumberTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* Number Test
*
* @author Pronamic <[email protected]>
* @copyright 2005-2021 Pronamic
* @copyright 2005-2022 Pronamic
* @license GPL-3.0-or-later
* @package Pronamic\WordPress\Money
*/
Expand Down Expand Up @@ -529,4 +529,18 @@ public function test_number_from_number() {

$this->assertSame( '5', $number->get_value() );
}

/**
* Test HelpScout ticket #23013.
*
* @link https://github.com/pronamic/wp-pronamic-pay/issues/281
* @group ticket23013
*/
public function test_helpscout_ticket_23013() {
$value = 29.95;

$result = Number::parse_float_with_precision( $value, 17 );

$this->assertSame( '29.949999999999999', $result );
}
}
2 changes: 1 addition & 1 deletion tests/src/ParserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* Parser
*
* @author Pronamic <[email protected]>
* @copyright 2005-2021 Pronamic
* @copyright 2005-2022 Pronamic
* @license GPL-3.0-or-later
* @package Pronamic\WordPress\Number
*/
Expand Down
40 changes: 20 additions & 20 deletions vendor-bin/phpstan/composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 5778321

Please sign in to comment.