Skip to content

Commit

Permalink
Fix PHP 8.0 compatibility
Browse files Browse the repository at this point in the history
  • Loading branch information
thekid committed Apr 10, 2020
1 parent 10f3232 commit c619eff
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 42 deletions.
1 change: 1 addition & 0 deletions ChangeLog.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ XP Math changelog

## 9.0.0 / 2020-04-10

* Fixed PHP 8.0 compatibility when dividing by zero - @thekid
* Implemented xp-framework/rfc#334: Drop PHP 5.6:
. **Heads up:** Minimum required PHP version now is PHP 7.0.0
. Rewrote code base, grouping use statements
Expand Down
14 changes: 9 additions & 5 deletions src/main/php/math/BigFloat.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,12 +57,16 @@ public function multiply($other) {
* @return math.BigNum
*/
public function divide($other) {
if (null === ($r= bcdiv($this->num, $other instanceof self ? $other->num : $other))) {
$e= key(\xp::$errors[__FILE__][__LINE__- 1]);
\xp::gc(__FILE__);
throw new IllegalArgumentException($e);
try {
if (null === ($r= bcdiv($this->num, $other instanceof self ? $other->num : $other))) {
$e= key(\xp::$errors[__FILE__][__LINE__- 1]);
\xp::gc(__FILE__);
throw new IllegalArgumentException($e);
}
return new self($r);
} catch (\Error $e) { // PHP 8.0
throw new IllegalArgumentException($e->getMessage());
}
return new self($r);
}

/**
Expand Down
86 changes: 49 additions & 37 deletions src/main/php/math/BigInt.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -81,34 +81,38 @@ public function multiply($other) {
* @return math.BigNum
*/
public function divide($other) {
if ($other instanceof self) {
if (null === ($r= bcdiv($this->num, $other->num, 0))) { // inlined
$e= key(\xp::$errors[__FILE__][__LINE__- 1]);
\xp::gc(__FILE__);
throw new IllegalArgumentException($e);
}
return new self($r);
} else if (is_int($other)) {
if (null === ($r= bcdiv($this->num, $other, 0))) { // inlined
$e= key(\xp::$errors[__FILE__][__LINE__- 1]);
\xp::gc(__FILE__);
throw new IllegalArgumentException($e);
}
return new self($r);
} else if ($other instanceof BigFloat) {
if (null === ($r= bcdiv($this->num, $other->num))) { // inlined
$e= key(\xp::$errors[__FILE__][__LINE__- 1]);
\xp::gc(__FILE__);
throw new IllegalArgumentException($e);
}
return new BigFloat($r);
} else {
if (null === ($r= bcdiv($this->num, $other))) { // inlined
$e= key(\xp::$errors[__FILE__][__LINE__- 1]);
\xp::gc(__FILE__);
throw new IllegalArgumentException($e);
try {
if ($other instanceof self) {
if (null === ($r= bcdiv($this->num, $other->num, 0))) { // inlined
$e= key(\xp::$errors[__FILE__][__LINE__- 1]);
\xp::gc(__FILE__);
throw new IllegalArgumentException($e);
}
return new self($r);
} else if (is_int($other)) {
if (null === ($r= bcdiv($this->num, $other, 0))) { // inlined
$e= key(\xp::$errors[__FILE__][__LINE__- 1]);
\xp::gc(__FILE__);
throw new IllegalArgumentException($e);
}
return new self($r);
} else if ($other instanceof BigFloat) {
if (null === ($r= bcdiv($this->num, $other->num))) { // inlined
$e= key(\xp::$errors[__FILE__][__LINE__- 1]);
\xp::gc(__FILE__);
throw new IllegalArgumentException($e);
}
return new BigFloat($r);
} else {
if (null === ($r= bcdiv($this->num, $other))) { // inlined
$e= key(\xp::$errors[__FILE__][__LINE__- 1]);
\xp::gc(__FILE__);
throw new IllegalArgumentException($e);
}
return new BigFloat($r);
}
return new BigFloat($r);
} catch (\Error $e) { // PHP 8.0
throw new IllegalArgumentException($e->getMessage());
}
}

Expand Down Expand Up @@ -149,12 +153,16 @@ public function multiply0($other) {
* @return math.BigNum
*/
public function divide0($other) {
if (null === ($r= bcdiv($this->num, $other instanceof self ? $other->num : $other, 0))) {
$e= key(\xp::$errors[__FILE__][__LINE__- 1]);
\xp::gc(__FILE__);
throw new IllegalArgumentException($e);
try {
if (null === ($r= bcdiv($this->num, $other instanceof self ? $other->num : $other, 0))) {
$e= key(\xp::$errors[__FILE__][__LINE__- 1]);
\xp::gc(__FILE__);
throw new IllegalArgumentException($e);
}
return new self($r);
} catch (\Error $e) { // PHP 8.0
throw new IllegalArgumentException($e->getMessage());
}
return new self($r);
}

/**
Expand Down Expand Up @@ -189,12 +197,16 @@ public function power($other) {
* @return math.BigNum
*/
public function modulo($other) {
if (null === ($r= bcmod($this->num, $other instanceof self ? $other->num : $other))) {
$e= key(\xp::$errors[__FILE__][__LINE__- 1]);
\xp::gc(__FILE__);
throw new IllegalArgumentException($e);
try {
if (null === ($r= bcmod($this->num, $other instanceof self ? $other->num : $other))) {
$e= key(\xp::$errors[__FILE__][__LINE__- 1]);
\xp::gc(__FILE__);
throw new IllegalArgumentException($e);
}
return new $this($r);
} catch (\Error $e) { // PHP 8.0
throw new IllegalArgumentException($e->getMessage());
}
return new $this($r);
}

/**
Expand Down

0 comments on commit c619eff

Please sign in to comment.