Skip to content
Open
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 @@ -29,7 +29,7 @@
},
"require": {
"php": ">=8.1",
"phpseclib/phpseclib": "^3.0"
"phpseclib/phpseclib": "^3.0 || ^4.0"
},
"require-dev": {
"phpunit/phpunit": "^10.5",
Expand Down
26 changes: 9 additions & 17 deletions src/BCMath.php
Original file line number Diff line number Diff line change
Expand Up @@ -182,10 +182,7 @@
{
$parts = explode('.', $num);

// Ensure both parts exist
if (!isset($parts[1])) {
$parts[1] = '';
}
$parts[1] ??= '';

return [$parts[0], $parts[1]];
}
Expand All @@ -198,20 +195,20 @@
*
* @return array{0: BigInteger, 1: BigInteger, 2: int} Array containing [num1Big, num2Big, maxPad]
*/
protected static function prepareBigIntegerInputs(string $num1, string $num2): array

Check failure on line 198 in src/BCMath.php

View workflow job for this annotation

GitHub Actions / PHPStan

Method bcmath_compat\BCMath::prepareBigIntegerInputs() has invalid return type phpseclib3\Math\BigInteger.

Check failure on line 198 in src/BCMath.php

View workflow job for this annotation

GitHub Actions / PHPStan

Method bcmath_compat\BCMath::prepareBigIntegerInputs() has invalid return type phpseclib3\Math\BigInteger.

Check failure on line 198 in src/BCMath.php

View workflow job for this annotation

GitHub Actions / PHPStan

Method bcmath_compat\BCMath::prepareBigIntegerInputs() has invalid return type phpseclib3\Math\BigInteger.

Check failure on line 198 in src/BCMath.php

View workflow job for this annotation

GitHub Actions / PHPStan

Method bcmath_compat\BCMath::prepareBigIntegerInputs() has invalid return type phpseclib3\Math\BigInteger.
{
// Parse decimal numbers
[$num1Int, $num1Dec] = self::parseDecimalNumber($num1);
[$num2Int, $num2Dec] = self::parseDecimalNumber($num2);

// Pad decimal parts to same length
$maxPad = max(strlen((string) $num1Dec), strlen((string) $num2Dec));
$num1Dec = str_pad((string) $num1Dec, $maxPad, '0');
$num2Dec = str_pad((string) $num2Dec, $maxPad, '0');
$maxPad = max(strlen($num1Dec), strlen($num2Dec));
$num1Dec = str_pad($num1Dec, $maxPad, '0');
$num2Dec = str_pad($num2Dec, $maxPad, '0');

// Convert to BigInteger for calculation
$num1Big = new BigInteger($num1Int.$num1Dec);

Check failure on line 210 in src/BCMath.php

View workflow job for this annotation

GitHub Actions / PHPStan

Instantiated class phpseclib3\Math\BigInteger not found.

Check failure on line 210 in src/BCMath.php

View workflow job for this annotation

GitHub Actions / PHPStan

Instantiated class phpseclib3\Math\BigInteger not found.
$num2Big = new BigInteger($num2Int.$num2Dec);

Check failure on line 211 in src/BCMath.php

View workflow job for this annotation

GitHub Actions / PHPStan

Instantiated class phpseclib3\Math\BigInteger not found.

Check failure on line 211 in src/BCMath.php

View workflow job for this annotation

GitHub Actions / PHPStan

Instantiated class phpseclib3\Math\BigInteger not found.

return [$num1Big, $num2Big, $maxPad];
}
Expand All @@ -222,7 +219,7 @@
* Applies scale formatting and zero normalization. Implements Phase 5
* of the standard processing pattern for most arithmetic operations.
*/
protected static function formatFinalResult(BigInteger $result, int $scale, int $pad = 0): string

Check failure on line 222 in src/BCMath.php

View workflow job for this annotation

GitHub Actions / PHPStan

Parameter $result of method bcmath_compat\BCMath::formatFinalResult() has invalid type phpseclib3\Math\BigInteger.

Check failure on line 222 in src/BCMath.php

View workflow job for this annotation

GitHub Actions / PHPStan

Parameter $result of method bcmath_compat\BCMath::formatFinalResult() has invalid type phpseclib3\Math\BigInteger.
{
$formatted = self::format($result, $scale, $pad);

Expand Down Expand Up @@ -344,23 +341,23 @@
*
* @return BigInteger[] Array containing [num1Big, num2Big]
*/
private static function prepareForComparison(string $num1, string $num2, int $scale): array

Check failure on line 344 in src/BCMath.php

View workflow job for this annotation

GitHub Actions / PHPStan

Method bcmath_compat\BCMath::prepareForComparison() has invalid return type phpseclib3\Math\BigInteger.

Check failure on line 344 in src/BCMath.php

View workflow job for this annotation

GitHub Actions / PHPStan

Method bcmath_compat\BCMath::prepareForComparison() has invalid return type phpseclib3\Math\BigInteger.
{
// Parse decimal numbers
[$num1Int, $num1Dec] = self::parseDecimalNumber($num1);
[$num2Int, $num2Dec] = self::parseDecimalNumber($num2);

// Apply scale truncation and padding
$num1Dec = substr((string) $num1Dec, 0, $scale);
$num2Dec = substr((string) $num2Dec, 0, $scale);
$num1Dec = substr($num1Dec, 0, $scale);
$num2Dec = substr($num2Dec, 0, $scale);

// Pad decimal parts to the same length (scale)
$num1Dec = str_pad($num1Dec, $scale, '0', STR_PAD_RIGHT);
$num2Dec = str_pad($num2Dec, $scale, '0', STR_PAD_RIGHT);

// Convert to BigInteger for comparison
$num1Big = new BigInteger($num1Int.$num1Dec);

Check failure on line 359 in src/BCMath.php

View workflow job for this annotation

GitHub Actions / PHPStan

Instantiated class phpseclib3\Math\BigInteger not found.

Check failure on line 359 in src/BCMath.php

View workflow job for this annotation

GitHub Actions / PHPStan

Instantiated class phpseclib3\Math\BigInteger not found.
$num2Big = new BigInteger($num2Int.$num2Dec);

Check failure on line 360 in src/BCMath.php

View workflow job for this annotation

GitHub Actions / PHPStan

Instantiated class phpseclib3\Math\BigInteger not found.

Check failure on line 360 in src/BCMath.php

View workflow job for this annotation

GitHub Actions / PHPStan

Instantiated class phpseclib3\Math\BigInteger not found.

return [$num1Big, $num2Big];
}
Expand Down Expand Up @@ -415,10 +412,10 @@
*
* Places the decimal place at the appropriate place, adds trailing 0's as appropriate, etc
*/
public static function format(BigInteger $x, ?int $scale = null, int $pad = 0): string

Check failure on line 415 in src/BCMath.php

View workflow job for this annotation

GitHub Actions / PHPStan

Parameter $x of method bcmath_compat\BCMath::format() has invalid type phpseclib3\Math\BigInteger.

Check failure on line 415 in src/BCMath.php

View workflow job for this annotation

GitHub Actions / PHPStan

Parameter $x of method bcmath_compat\BCMath::format() has invalid type phpseclib3\Math\BigInteger.
{
$sign = self::isNegative($x) ? '-' : '';
$x = str_replace('-', '', (string) $x);

Check failure on line 418 in src/BCMath.php

View workflow job for this annotation

GitHub Actions / PHPStan

Parameter #3 $subject of function str_replace expects array<string>|string, mixed given.

Check failure on line 418 in src/BCMath.php

View workflow job for this annotation

GitHub Actions / PHPStan

Parameter #3 $subject of function str_replace expects array<string>|string, mixed given.

if (strlen($x) !== $pad) {
$x = str_pad($x, $pad, '0', STR_PAD_LEFT);
Expand Down Expand Up @@ -695,9 +692,7 @@

// Phase 4: Number processing
$baseParts = explode('.', $base);
if (!isset($baseParts[1])) {
$baseParts[1] = '';
}
$baseParts[1] ??= '';

// Pad decimal parts
$maxPad = strlen($baseParts[1]);
Expand Down Expand Up @@ -768,10 +763,7 @@
self::validateNumberString($exponent, 'bcpowmod', 2, 'exponent');
self::validateNumberString($modulus, 'bcpowmod', 3, 'modulus');

// Phase 2: Scale resolution and validation
if ($scale === null) {
$scale = 0;
}
$scale ??= 0;
self::validateScale($scale, 'bcpowmod', 4);

// Phase 3: Number processing and validation
Expand Down Expand Up @@ -1174,7 +1166,7 @@
$result = self::placeDecimalPoint($roundedInt, $precision);

if ($sign === -1 && !$isZero) {
$result = '-'.$result;
return '-'.$result;
}

return $result;
Expand Down
Loading