← All changes
|
includes/sdk/google/ramsey/uuid/src/Math/BrickMathCalculator.php
+31
-21
1.2.8
→
1.4.2
View file →
| @@ -9,53 +9,57 @@ | ||
| 9 | 9 | * @copyright Copyright (c) Ben Ramsey <[email protected]> |
| 10 | 10 | * @license http://opensource.org/licenses/MIT MIT |
| 11 | 11 | */ |
| 12 | 12 | declare (strict_types=1); |
| 13 | -namespace Dudlewebs\WPMCS\Ramsey\Uuid\Math; | |
| 13 | +namespace Dudlewebs\WPMCS\GCP\Ramsey\Uuid\Math; | |
| 14 | 14 | |
| 15 | -use Dudlewebs\WPMCS\Brick\Math\BigDecimal; | |
| 16 | -use Dudlewebs\WPMCS\Brick\Math\BigInteger; | |
| 17 | -use Dudlewebs\WPMCS\Brick\Math\Exception\MathException; | |
| 18 | -use Dudlewebs\WPMCS\Brick\Math\RoundingMode as BrickMathRounding; | |
| 19 | -use Dudlewebs\WPMCS\Ramsey\Uuid\Exception\InvalidArgumentException; | |
| 20 | -use Dudlewebs\WPMCS\Ramsey\Uuid\Type\Decimal; | |
| 21 | -use Dudlewebs\WPMCS\Ramsey\Uuid\Type\Hexadecimal; | |
| 22 | -use Dudlewebs\WPMCS\Ramsey\Uuid\Type\Integer as IntegerObject; | |
| 23 | -use Dudlewebs\WPMCS\Ramsey\Uuid\Type\NumberInterface; | |
| 15 | +use Dudlewebs\WPMCS\GCP\Brick\Math\BigDecimal; | |
| 16 | +use Dudlewebs\WPMCS\GCP\Brick\Math\BigInteger; | |
| 17 | +use Dudlewebs\WPMCS\GCP\Brick\Math\Exception\MathException; | |
| 18 | +use Dudlewebs\WPMCS\GCP\Brick\Math\RoundingMode as BrickMathRounding; | |
| 19 | +use Dudlewebs\WPMCS\GCP\Ramsey\Uuid\Exception\InvalidArgumentException; | |
| 20 | +use Dudlewebs\WPMCS\GCP\Ramsey\Uuid\Type\Decimal; | |
| 21 | +use Dudlewebs\WPMCS\GCP\Ramsey\Uuid\Type\Hexadecimal; | |
| 22 | +use Dudlewebs\WPMCS\GCP\Ramsey\Uuid\Type\Integer as IntegerObject; | |
| 23 | +use Dudlewebs\WPMCS\GCP\Ramsey\Uuid\Type\NumberInterface; | |
| 24 | 24 | /** |
| 25 | 25 | * A calculator using the brick/math library for arbitrary-precision arithmetic |
| 26 | 26 | * |
| 27 | - * @psalm-immutable | |
| 27 | + * @immutable | |
| 28 | 28 | */ |
| 29 | 29 | final class BrickMathCalculator implements CalculatorInterface |
| 30 | 30 | { |
| 31 | 31 | 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]; |
| 32 | - public function add(NumberInterface $augend, NumberInterface ...$addends): NumberInterface | |
| 32 | + public function add(NumberInterface $augend, NumberInterface ...$addends) : NumberInterface | |
| 33 | 33 | { |
| 34 | 34 | $sum = BigInteger::of($augend->toString()); |
| 35 | 35 | foreach ($addends as $addend) { |
| 36 | 36 | $sum = $sum->plus($addend->toString()); |
| 37 | 37 | } |
| 38 | + /** @phpstan-ignore possiblyImpure.new */ | |
| 38 | 39 | return new IntegerObject((string) $sum); |
| 39 | 40 | } |
| 40 | - public function subtract(NumberInterface $minuend, NumberInterface ...$subtrahends): NumberInterface | |
| 41 | + public function subtract(NumberInterface $minuend, NumberInterface ...$subtrahends) : NumberInterface | |
| 41 | 42 | { |
| 42 | 43 | $difference = BigInteger::of($minuend->toString()); |
| 43 | 44 | foreach ($subtrahends as $subtrahend) { |
| 44 | 45 | $difference = $difference->minus($subtrahend->toString()); |
| 45 | 46 | } |
| 47 | + /** @phpstan-ignore possiblyImpure.new */ | |
| 46 | 48 | return new IntegerObject((string) $difference); |
| 47 | 49 | } |
| 48 | - public function multiply(NumberInterface $multiplicand, NumberInterface ...$multipliers): NumberInterface | |
| 50 | + public function multiply(NumberInterface $multiplicand, NumberInterface ...$multipliers) : NumberInterface | |
| 49 | 51 | { |
| 50 | 52 | $product = BigInteger::of($multiplicand->toString()); |
| 51 | 53 | foreach ($multipliers as $multiplier) { |
| 52 | 54 | $product = $product->multipliedBy($multiplier->toString()); |
| 53 | 55 | } |
| 56 | + /** @phpstan-ignore possiblyImpure.new */ | |
| 54 | 57 | return new IntegerObject((string) $product); |
| 55 | 58 | } |
| 56 | - public function divide(int $roundingMode, int $scale, NumberInterface $dividend, NumberInterface ...$divisors): NumberInterface | |
| 59 | + public function divide(int $roundingMode, int $scale, NumberInterface $dividend, NumberInterface ...$divisors) : NumberInterface | |
| 57 | 60 | { |
| 61 | + /** @phpstan-ignore possiblyImpure.methodCall */ | |
| 58 | 62 | $brickRounding = $this->getBrickRoundingMode($roundingMode); |
| 59 | 63 | $quotient = BigDecimal::of($dividend->toString()); |
| 60 | 64 | foreach ($divisors as $divisor) { |
| 61 | 65 | $quotient = $quotient->dividedBy($divisor->toString(), $scale, $brickRounding); |
| @@ -60,21 +64,24 @@ | ||
| 60 | 64 | foreach ($divisors as $divisor) { |
| 61 | 65 | $quotient = $quotient->dividedBy($divisor->toString(), $scale, $brickRounding); |
| 62 | 66 | } |
| 63 | 67 | if ($scale === 0) { |
| 68 | + /** @phpstan-ignore possiblyImpure.new */ | |
| 64 | 69 | return new IntegerObject((string) $quotient->toBigInteger()); |
| 65 | 70 | } |
| 71 | + /** @phpstan-ignore possiblyImpure.new */ | |
| 66 | 72 | return new Decimal((string) $quotient); |
| 67 | 73 | } |
| 68 | - public function fromBase(string $value, int $base): IntegerObject | |
| 74 | + public function fromBase(string $value, int $base) : IntegerObject | |
| 69 | 75 | { |
| 70 | 76 | try { |
| 77 | + /** @phpstan-ignore possiblyImpure.new */ | |
| 71 | 78 | return new IntegerObject((string) BigInteger::fromBase($value, $base)); |
| 72 | 79 | } catch (MathException|\InvalidArgumentException $exception) { |
| 73 | 80 | throw new InvalidArgumentException($exception->getMessage(), (int) $exception->getCode(), $exception); |
| 74 | 81 | } |
| 75 | 82 | } |
| 76 | - public function toBase(IntegerObject $value, int $base): string | |
| 83 | + public function toBase(IntegerObject $value, int $base) : string | |
| 77 | 84 | { |
| 78 | 85 | try { |
| 79 | 86 | return BigInteger::of($value->toString())->toBase($base); |
| 80 | 87 | } catch (MathException|\InvalidArgumentException $exception) { |
| @@ -80,20 +87,23 @@ | ||
| 80 | 87 | } catch (MathException|\InvalidArgumentException $exception) { |
| 81 | 88 | throw new InvalidArgumentException($exception->getMessage(), (int) $exception->getCode(), $exception); |
| 82 | 89 | } |
| 83 | 90 | } |
| 84 | - public function toHexadecimal(IntegerObject $value): Hexadecimal | |
| 91 | + public function toHexadecimal(IntegerObject $value) : Hexadecimal | |
| 85 | 92 | { |
| 93 | + /** @phpstan-ignore possiblyImpure.new */ | |
| 86 | 94 | return new Hexadecimal($this->toBase($value, 16)); |
| 87 | 95 | } |
| 88 | - public function toInteger(Hexadecimal $value): IntegerObject | |
| 96 | + public function toInteger(Hexadecimal $value) : IntegerObject | |
| 89 | 97 | { |
| 90 | 98 | return $this->fromBase($value->toString(), 16); |
| 91 | 99 | } |
| 92 | 100 | /** |
| 93 | 101 | * Maps ramsey/uuid rounding modes to those used by brick/math |
| 102 | + * | |
| 103 | + * @return BrickMathRounding::* | |
| 94 | 104 | */ |
| 95 | - private function getBrickRoundingMode(int $roundingMode): int | |
| 105 | + private function getBrickRoundingMode(int $roundingMode) | |
| 96 | 106 | { |
| 97 | - return self::ROUNDING_MODE_MAP[$roundingMode] ?? 0; | |
| 107 | + return self::ROUNDING_MODE_MAP[$roundingMode] ?? BrickMathRounding::UNNECESSARY; | |
| 98 | 108 | } |
| 99 | 109 | } |