Skip to content

Commit c619eff

Browse files
committed
Fix PHP 8.0 compatibility
1 parent 10f3232 commit c619eff

File tree

3 files changed

+59
-42
lines changed

3 files changed

+59
-42
lines changed

ChangeLog.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ XP Math changelog
55

66
## 9.0.0 / 2020-04-10
77

8+
* Fixed PHP 8.0 compatibility when dividing by zero - @thekid
89
* Implemented xp-framework/rfc#334: Drop PHP 5.6:
910
. **Heads up:** Minimum required PHP version now is PHP 7.0.0
1011
. Rewrote code base, grouping use statements

src/main/php/math/BigFloat.class.php

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -57,12 +57,16 @@ public function multiply($other) {
5757
* @return math.BigNum
5858
*/
5959
public function divide($other) {
60-
if (null === ($r= bcdiv($this->num, $other instanceof self ? $other->num : $other))) {
61-
$e= key(\xp::$errors[__FILE__][__LINE__- 1]);
62-
\xp::gc(__FILE__);
63-
throw new IllegalArgumentException($e);
60+
try {
61+
if (null === ($r= bcdiv($this->num, $other instanceof self ? $other->num : $other))) {
62+
$e= key(\xp::$errors[__FILE__][__LINE__- 1]);
63+
\xp::gc(__FILE__);
64+
throw new IllegalArgumentException($e);
65+
}
66+
return new self($r);
67+
} catch (\Error $e) { // PHP 8.0
68+
throw new IllegalArgumentException($e->getMessage());
6469
}
65-
return new self($r);
6670
}
6771

6872
/**

src/main/php/math/BigInt.class.php

Lines changed: 49 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -81,34 +81,38 @@ public function multiply($other) {
8181
* @return math.BigNum
8282
*/
8383
public function divide($other) {
84-
if ($other instanceof self) {
85-
if (null === ($r= bcdiv($this->num, $other->num, 0))) { // inlined
86-
$e= key(\xp::$errors[__FILE__][__LINE__- 1]);
87-
\xp::gc(__FILE__);
88-
throw new IllegalArgumentException($e);
89-
}
90-
return new self($r);
91-
} else if (is_int($other)) {
92-
if (null === ($r= bcdiv($this->num, $other, 0))) { // inlined
93-
$e= key(\xp::$errors[__FILE__][__LINE__- 1]);
94-
\xp::gc(__FILE__);
95-
throw new IllegalArgumentException($e);
96-
}
97-
return new self($r);
98-
} else if ($other instanceof BigFloat) {
99-
if (null === ($r= bcdiv($this->num, $other->num))) { // inlined
100-
$e= key(\xp::$errors[__FILE__][__LINE__- 1]);
101-
\xp::gc(__FILE__);
102-
throw new IllegalArgumentException($e);
103-
}
104-
return new BigFloat($r);
105-
} else {
106-
if (null === ($r= bcdiv($this->num, $other))) { // inlined
107-
$e= key(\xp::$errors[__FILE__][__LINE__- 1]);
108-
\xp::gc(__FILE__);
109-
throw new IllegalArgumentException($e);
84+
try {
85+
if ($other instanceof self) {
86+
if (null === ($r= bcdiv($this->num, $other->num, 0))) { // inlined
87+
$e= key(\xp::$errors[__FILE__][__LINE__- 1]);
88+
\xp::gc(__FILE__);
89+
throw new IllegalArgumentException($e);
90+
}
91+
return new self($r);
92+
} else if (is_int($other)) {
93+
if (null === ($r= bcdiv($this->num, $other, 0))) { // inlined
94+
$e= key(\xp::$errors[__FILE__][__LINE__- 1]);
95+
\xp::gc(__FILE__);
96+
throw new IllegalArgumentException($e);
97+
}
98+
return new self($r);
99+
} else if ($other instanceof BigFloat) {
100+
if (null === ($r= bcdiv($this->num, $other->num))) { // inlined
101+
$e= key(\xp::$errors[__FILE__][__LINE__- 1]);
102+
\xp::gc(__FILE__);
103+
throw new IllegalArgumentException($e);
104+
}
105+
return new BigFloat($r);
106+
} else {
107+
if (null === ($r= bcdiv($this->num, $other))) { // inlined
108+
$e= key(\xp::$errors[__FILE__][__LINE__- 1]);
109+
\xp::gc(__FILE__);
110+
throw new IllegalArgumentException($e);
111+
}
112+
return new BigFloat($r);
110113
}
111-
return new BigFloat($r);
114+
} catch (\Error $e) { // PHP 8.0
115+
throw new IllegalArgumentException($e->getMessage());
112116
}
113117
}
114118

@@ -149,12 +153,16 @@ public function multiply0($other) {
149153
* @return math.BigNum
150154
*/
151155
public function divide0($other) {
152-
if (null === ($r= bcdiv($this->num, $other instanceof self ? $other->num : $other, 0))) {
153-
$e= key(\xp::$errors[__FILE__][__LINE__- 1]);
154-
\xp::gc(__FILE__);
155-
throw new IllegalArgumentException($e);
156+
try {
157+
if (null === ($r= bcdiv($this->num, $other instanceof self ? $other->num : $other, 0))) {
158+
$e= key(\xp::$errors[__FILE__][__LINE__- 1]);
159+
\xp::gc(__FILE__);
160+
throw new IllegalArgumentException($e);
161+
}
162+
return new self($r);
163+
} catch (\Error $e) { // PHP 8.0
164+
throw new IllegalArgumentException($e->getMessage());
156165
}
157-
return new self($r);
158166
}
159167

160168
/**
@@ -189,12 +197,16 @@ public function power($other) {
189197
* @return math.BigNum
190198
*/
191199
public function modulo($other) {
192-
if (null === ($r= bcmod($this->num, $other instanceof self ? $other->num : $other))) {
193-
$e= key(\xp::$errors[__FILE__][__LINE__- 1]);
194-
\xp::gc(__FILE__);
195-
throw new IllegalArgumentException($e);
200+
try {
201+
if (null === ($r= bcmod($this->num, $other instanceof self ? $other->num : $other))) {
202+
$e= key(\xp::$errors[__FILE__][__LINE__- 1]);
203+
\xp::gc(__FILE__);
204+
throw new IllegalArgumentException($e);
205+
}
206+
return new $this($r);
207+
} catch (\Error $e) { // PHP 8.0
208+
throw new IllegalArgumentException($e->getMessage());
196209
}
197-
return new $this($r);
198210
}
199211

200212
/**

0 commit comments

Comments
 (0)