Skip to content

Commit da34e36

Browse files
Bump highest supported brick/math version to ^0.17 (#2343)
Co-authored-by: Norbert Orzechowicz <1921950+norberttech@users.noreply.github.com>
1 parent afe6b29 commit da34e36

3 files changed

Lines changed: 19 additions & 9 deletions

File tree

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
"ext-zlib": "*",
2323
"composer-runtime-api": "^2.2",
2424
"async-aws/s3": "^2.6 || ^3.0",
25-
"brick/math": "^0.12 || ^0.13 || ^0.14",
25+
"brick/math": "^0.12 || ^0.13 || ^0.14 || ^0.15 || ^0.16 || ^0.17",
2626
"coduo/php-humanizer": "^5.0",
2727
"cuyz/valinor": "^2.4",
2828
"doctrine/dbal": "^3.6 || ^4.0",

src/core/etl/composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
"php": "~8.3.0 || ~8.4.0 || ~8.5.0",
1313
"ext-json": "*",
1414
"psr/clock": "^1.0",
15-
"brick/math": "^0.12 || ^0.13 || ^0.14",
15+
"brick/math": "^0.12 || ^0.13 || ^0.14 || ^0.15 || ^0.16 || ^0.17",
1616
"composer-runtime-api": "^2.2",
1717
"flow-php/telemetry": "self.version",
1818
"flow-php/types": "self.version",

src/core/etl/src/Flow/Calculator/Calculator.php

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ public function add(int|float|string $a, int|float|string $b) : int|float
2121
{
2222
$result = BigDecimal::of((string) $a)->plus(BigDecimal::of((string) $b));
2323

24-
if (!$result->hasNonZeroFractionalPart()) {
24+
if (!self::hasNonZeroFractionalPart($result)) {
2525
return $result->toInt();
2626
}
2727

@@ -39,9 +39,10 @@ public function divide(int|float|string $a, int|float|string $b, ?int $scale = n
3939
{
4040
try {
4141
if ($scale === null && $rounding === null) {
42-
$result = BigDecimal::of($a)->dividedBy(BigDecimal::of($b));
42+
$aDecimal = BigDecimal::of((string) $a);
43+
$result = $aDecimal->dividedBy(BigDecimal::of((string) $b), $aDecimal->getScale());
4344

44-
if (!$result->hasNonZeroFractionalPart()) {
45+
if (!self::hasNonZeroFractionalPart($result)) {
4546
return $result->toInt();
4647
}
4748
}
@@ -63,7 +64,7 @@ public function divide(int|float|string $a, int|float|string $b, ?int $scale = n
6364

6465
$result = BigDecimal::of((string) $a)->dividedBy(BigDecimal::of((string) $b), $scale, $brickMode);
6566

66-
if (!$result->hasNonZeroFractionalPart()) {
67+
if (!self::hasNonZeroFractionalPart($result)) {
6768
return $result->toInt();
6869
}
6970

@@ -92,7 +93,7 @@ public function multiply(int|float|string $a, int|float|string $b) : int|float
9293
{
9394
$result = BigDecimal::of((string) $a)->multipliedBy(BigDecimal::of((string) $b));
9495

95-
if (!$result->hasNonZeroFractionalPart()) {
96+
if (!self::hasNonZeroFractionalPart($result)) {
9697
return $result->toInt();
9798
}
9899

@@ -107,7 +108,7 @@ public function power(int|float|string $a, int|string $b) : int|float
107108
{
108109
$result = BigDecimal::of((string) $a)->power(BigInteger::of((string) $b)->toInt());
109110

110-
if (!$result->hasNonZeroFractionalPart()) {
111+
if (!self::hasNonZeroFractionalPart($result)) {
111112
return $result->toInt();
112113
}
113114

@@ -122,10 +123,19 @@ public function subtract(int|float|string $a, int|float|string $b) : int|float
122123
{
123124
$result = BigDecimal::of((string) $a)->minus(BigDecimal::of((string) $b));
124125

125-
if (!$result->hasNonZeroFractionalPart()) {
126+
if (!self::hasNonZeroFractionalPart($result)) {
126127
return $result->toInt();
127128
}
128129

129130
return $result->toFloat();
130131
}
132+
133+
private static function hasNonZeroFractionalPart(BigDecimal $result) : bool
134+
{
135+
if (method_exists($result, 'hasNonZeroFractionalPart')) { // @phpstan-ignore function.alreadyNarrowedType
136+
return $result->hasNonZeroFractionalPart();
137+
}
138+
139+
return !$result->getFractionalPart()->isZero(); // @phpstan-ignore method.nonObject
140+
}
131141
}

0 commit comments

Comments
 (0)