diff --git a/composer.json b/composer.json index 178019f4..a420e5e6 100644 --- a/composer.json +++ b/composer.json @@ -10,7 +10,7 @@ ], "require": { "php": "^8.0", - "brick/math": "^0.8.16 || ^0.9 || ^0.10 || ^0.11 || ^0.12 || ^0.13 || ^0.14", + "brick/math": "^0.8.16 || ^0.9 || ^0.10 || ^0.11 || ^0.12 || ^0.13 || ^0.14 || ^0.15 || ^0.16", "ramsey/collection": "^1.2 || ^2.0" }, "require-dev": { diff --git a/composer.lock b/composer.lock index 5ce3d12c..d275937e 100644 --- a/composer.lock +++ b/composer.lock @@ -4,27 +4,26 @@ "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", "This file is @generated automatically" ], - "content-hash": "bb087ab286c5dc749e589e7bb6eb96c1", + "content-hash": "dbec92684751a2779a8432fcb664f7fb", "packages": [ { "name": "brick/math", - "version": "0.14.1", + "version": "0.16.1", "source": { "type": "git", "url": "https://github.com/brick/math.git", - "reference": "f05858549e5f9d7bb45875a75583240a38a281d0" + "reference": "9b8bd30c534479b1cd279eec37a7b0aed30a323c" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/brick/math/zipball/f05858549e5f9d7bb45875a75583240a38a281d0", - "reference": "f05858549e5f9d7bb45875a75583240a38a281d0", + "url": "https://api.github.com/repos/brick/math/zipball/9b8bd30c534479b1cd279eec37a7b0aed30a323c", + "reference": "9b8bd30c534479b1cd279eec37a7b0aed30a323c", "shasum": "" }, "require": { "php": "^8.2" }, "require-dev": { - "php-coveralls/php-coveralls": "^2.2", "phpstan/phpstan": "2.1.22", "phpunit/phpunit": "^11.5" }, @@ -56,7 +55,7 @@ ], "support": { "issues": "https://github.com/brick/math/issues", - "source": "https://github.com/brick/math/tree/0.14.1" + "source": "https://github.com/brick/math/tree/0.16.1" }, "funding": [ { @@ -64,7 +63,7 @@ "type": "github" } ], - "time": "2025-11-24T14:40:29+00:00" + "time": "2026-03-09T13:41:19+00:00" }, { "name": "ramsey/collection", @@ -5930,5 +5929,5 @@ "php": "^8.0" }, "platform-dev": {}, - "plugin-api-version": "2.6.0" + "plugin-api-version": "2.9.0" } diff --git a/phpstan.neon.dist b/phpstan.neon.dist index 87969bfc..6f265fb1 100644 --- a/phpstan.neon.dist +++ b/phpstan.neon.dist @@ -18,3 +18,9 @@ parameters: - identifier: staticMethod.resultUnused path: tests/* + # Workaround to deal with the BC layer for brick math. + - + message: "#^Access to undefined constant Brick\\\\Math#" + - + message: "#of (static )?method Brick\\\\Math#" + diff --git a/src/Math/BrickMathCalculator.php b/src/Math/BrickMathCalculator.php index 649f5803..eae038a0 100644 --- a/src/Math/BrickMathCalculator.php +++ b/src/Math/BrickMathCalculator.php @@ -31,18 +31,11 @@ */ final class BrickMathCalculator implements CalculatorInterface { - private const ROUNDING_MODE_MAP = [ - RoundingMode::UNNECESSARY => BrickMathRounding::UNNECESSARY, - RoundingMode::UP => BrickMathRounding::UP, - RoundingMode::DOWN => BrickMathRounding::DOWN, - RoundingMode::CEILING => BrickMathRounding::CEILING, - RoundingMode::FLOOR => BrickMathRounding::FLOOR, - RoundingMode::HALF_UP => BrickMathRounding::HALF_UP, - RoundingMode::HALF_DOWN => BrickMathRounding::HALF_DOWN, - RoundingMode::HALF_CEILING => BrickMathRounding::HALF_CEILING, - RoundingMode::HALF_FLOOR => BrickMathRounding::HALF_FLOOR, - RoundingMode::HALF_EVEN => BrickMathRounding::HALF_EVEN, - ]; + /** + * @var array|null $roundingModeMap + * @phpstan-ignore-next-line + */ + private static ?array $roundingModeMap = null; public function add(NumberInterface $augend, NumberInterface ...$addends): NumberInterface { @@ -111,7 +104,9 @@ public function fromBase(string $value, int $base): IntegerObject return new IntegerObject((string) BigInteger::fromBase($value, $base)); } catch (MathException | \InvalidArgumentException $exception) { throw new InvalidArgumentException( + /** @phpstan-ignore possiblyImpure.methodCall */ $exception->getMessage(), + /** @phpstan-ignore possiblyImpure.methodCall */ (int) $exception->getCode(), $exception ); @@ -124,7 +119,9 @@ public function toBase(IntegerObject $value, int $base): string return BigInteger::of($value->toString())->toBase($base); } catch (MathException | \InvalidArgumentException $exception) { throw new InvalidArgumentException( + /** @phpstan-ignore possiblyImpure.methodCall */ $exception->getMessage(), + /** @phpstan-ignore possiblyImpure.methodCall */ (int) $exception->getCode(), $exception ); @@ -149,6 +146,46 @@ public function toInteger(Hexadecimal $value): IntegerObject */ private function getBrickRoundingMode(int $roundingMode) { - return self::ROUNDING_MODE_MAP[$roundingMode] ?? BrickMathRounding::UNNECESSARY; + return self::getRoundingMap()[$roundingMode] ?? self::getRoundingMap()[0]; + } + + /** + * @return array + */ + private static function getRoundingMap(): array + { + if (self::$roundingModeMap === null) { + if (defined(BrickMathRounding::class . '::UNNECESSARY')) { + /** @phpstan-ignore-next-line */ + self::$roundingModeMap = [ + RoundingMode::UNNECESSARY => BrickMathRounding::UNNECESSARY, + RoundingMode::UP => BrickMathRounding::UP, + RoundingMode::DOWN => BrickMathRounding::DOWN, + RoundingMode::CEILING => BrickMathRounding::CEILING, + RoundingMode::FLOOR => BrickMathRounding::FLOOR, + RoundingMode::HALF_UP => BrickMathRounding::HALF_UP, + RoundingMode::HALF_DOWN => BrickMathRounding::HALF_DOWN, + RoundingMode::HALF_CEILING => BrickMathRounding::HALF_CEILING, + RoundingMode::HALF_FLOOR => BrickMathRounding::HALF_FLOOR, + RoundingMode::HALF_EVEN => BrickMathRounding::HALF_EVEN, + ]; + } else { + self::$roundingModeMap = [ + RoundingMode::UNNECESSARY => BrickMathRounding::Unnecessary, + RoundingMode::UP => BrickMathRounding::Up, + RoundingMode::DOWN => BrickMathRounding::Down, + RoundingMode::CEILING => BrickMathRounding::Ceiling, + RoundingMode::FLOOR => BrickMathRounding::Floor, + RoundingMode::HALF_UP => BrickMathRounding::HalfUp, + RoundingMode::HALF_DOWN => BrickMathRounding::HalfDown, + RoundingMode::HALF_CEILING => BrickMathRounding::HalfCeiling, + RoundingMode::HALF_FLOOR => BrickMathRounding::HalfFloor, + RoundingMode::HALF_EVEN => BrickMathRounding::HalfEven, + ]; + } + } + + /** @phpstan-ignore-next-line */ + return self::$roundingModeMap; } } diff --git a/tests/Converter/Number/BigNumberConverterTest.php b/tests/Converter/Number/BigNumberConverterTest.php index 83bcff23..8d32b364 100644 --- a/tests/Converter/Number/BigNumberConverterTest.php +++ b/tests/Converter/Number/BigNumberConverterTest.php @@ -15,7 +15,7 @@ public function testFromHexThrowsExceptionWhenStringDoesNotContainOnlyHexadecima $converter = new BigNumberConverter(); $this->expectException(InvalidArgumentException::class); - $this->expectExceptionMessage('"." is not a valid character in base 16'); + $this->expectExceptionMessageMatches('/"\." is not (a )?valid (character )?in base 16/'); $converter->fromHex('123.34'); } diff --git a/tests/Math/BrickMathCalculatorTest.php b/tests/Math/BrickMathCalculatorTest.php index 8cb5d906..41b3a380 100644 --- a/tests/Math/BrickMathCalculatorTest.php +++ b/tests/Math/BrickMathCalculatorTest.php @@ -96,7 +96,7 @@ public function testFromBaseThrowsException(): void $calculator = new BrickMathCalculator(); $this->expectException(InvalidArgumentException::class); - $this->expectExceptionMessage('"o" is not a valid character in base 16'); + $this->expectExceptionMessageMatches('/"o" is not (a )?valid (character )?in base 16/'); $calculator->fromBase('foobar', 16); } diff --git a/tests/UuidTest.php b/tests/UuidTest.php index 415b0c3a..d2e2cbc0 100644 --- a/tests/UuidTest.php +++ b/tests/UuidTest.php @@ -1213,9 +1213,19 @@ public function test32BitMatch64BitForOneHourPeriod(): void . "; 32-bit: {$uuid32->toString()}, 64-bit: {$uuid64->toString()}" ); + if (defined(RoundingMode::class . '::HALF_UP')) { + $halfUp = RoundingMode::HALF_UP; + $down = RoundingMode::DOWN; + } else { + $halfUp = RoundingMode::HalfUp; + $down = RoundingMode::Down; + } + // Assert that the time matches - $usecAdd = BigDecimal::of($usec)->dividedBy('1000000', 14, RoundingMode::HALF_UP); - $testTime = BigDecimal::of($currentTime)->plus($usecAdd)->toScale(0, RoundingMode::DOWN); + // @phpstan-ignore-next-line + $usecAdd = BigDecimal::of($usec)->dividedBy('1000000', 14, $halfUp); + // @phpstan-ignore-next-line + $testTime = BigDecimal::of($currentTime)->plus($usecAdd)->toScale(0, $down); $this->assertSame((string) $testTime, (string) $uuid64->getDateTime()->getTimestamp()); $this->assertSame((string) $testTime, (string) $uuid32->getDateTime()->getTimestamp()); }