Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand Down
17 changes: 8 additions & 9 deletions composer.lock

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

6 changes: 6 additions & 0 deletions phpstan.neon.dist
Original file line number Diff line number Diff line change
Expand Up @@ -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#"

63 changes: 50 additions & 13 deletions src/Math/BrickMathCalculator.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,18 +31,11 @@
*/
final class BrickMathCalculator implements CalculatorInterface
{
private const ROUNDING_MODE_MAP = [
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

    private const ROUNDING_MODE_MAP = [
        RoundingMode::UNNECESSARY => BrickMathRounding::Unnecessary ?? BrickMathRounding::UNNECESSARY,
        ...

Alternatively and a matter of taste, but this could also work instead of the static $roundingModeMap.
With this approach, you don't need defined(). The whole compat layer would be these 10 lines.

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<int, BrickMathRounding::*>|null $roundingModeMap
* @phpstan-ignore-next-line
*/
private static ?array $roundingModeMap = null;

public function add(NumberInterface $augend, NumberInterface ...$addends): NumberInterface
{
Expand Down Expand Up @@ -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
);
Expand All @@ -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
);
Expand All @@ -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<int, BrickMathRounding::*>
*/
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;
}
}
2 changes: 1 addition & 1 deletion tests/Converter/Number/BigNumberConverterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');
}
Expand Down
2 changes: 1 addition & 1 deletion tests/Math/BrickMathCalculatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
14 changes: 12 additions & 2 deletions tests/UuidTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}
Expand Down
Loading