Skip to content

Commit b072af9

Browse files
authored
Refactored error handling to be more robust.
1 parent 9a5b27d commit b072af9

File tree

1 file changed

+23
-9
lines changed

1 file changed

+23
-9
lines changed

src/BC.php

Lines changed: 23 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,12 @@
2727

2828
namespace Phactor;
2929

30+
use Phactor\Exceptions\BCMathException;
31+
32+
use Error;
33+
use Exception;
34+
35+
3036
/**
3137
* Binary Calculator math class used by the main math class.
3238
*
@@ -72,9 +78,14 @@ public function mul($a, $b)
7278
* @param string $a The first number.
7379
* @param string $b The second number.
7480
* @return string
81+
* @throws BCMathException if attempting to divide by zero.
7582
*/
7683
public function div($a, $b)
7784
{
85+
if ($this->bcNormalize($b) === '0') {
86+
throw new BCMathException("Division by zero error in BC::div().");
87+
}
88+
7889
return bcdiv($this->bcNormalize($a), $this->bcNormalize($b));
7990
}
8091

@@ -158,7 +169,7 @@ public function sqrt($a)
158169
* @param string $number The number to inverse modulo.
159170
* @param string $modulus The modulus.
160171
* @return string $a The result.
161-
* @throws \Exception
172+
* @throws BCMathException
162173
*/
163174
public function inv($number, $modulus)
164175
{
@@ -187,9 +198,10 @@ public function inv($number, $modulus)
187198

188199
return $this->addMod($a, $modulus);
189200

190-
} catch (\Exception $e) {
191-
// TODO: Need to do something useful here instead of re-throwing the exception.
192-
throw $e;
201+
} catch (Exception $e) {
202+
throw new BCMathException("Caught the following exception in BC::inv(): " . $e->getMessage(), 0, $e);
203+
} catch (Error $e) {
204+
throw new BCMathException("Fatal error in BC::inv(): " . $e->getMessage(), 0, $e);
193205
}
194206
}
195207

@@ -200,7 +212,7 @@ public function inv($number, $modulus)
200212
* @param string $a First param to check.
201213
* @param string $b Second param to check.
202214
* @return bool Whether the params are cp.
203-
* @throws \Exception
215+
* @throws BCMathException
204216
*/
205217
public function coprime($a, $b)
206218
{
@@ -212,9 +224,10 @@ public function coprime($a, $b)
212224
list($a, $b) = $this->coSwitch($a, $b);
213225
}
214226

215-
} catch (\Exception $e) {
216-
// TODO: Need to do something useful here instead of re-throwing the exception.
217-
throw $e;
227+
} catch (Exception $e) {
228+
throw new BCMathException("Caught the following exception in BC::coprime(): " . $e->getMessage(), 0, $e);
229+
} catch (Error $e) {
230+
throw new BCMathException("Fatal error in BC::coprime(): " . $e->getMessage(), 0, $e);
218231
}
219232

220233
return (bccomp($a, '1') == 0) ? true : false;
@@ -293,6 +306,7 @@ public function convertDecToHex($dec)
293306
* @param string $a First param to check.
294307
* @param string $b Second param to check.
295308
* @return array Array of smaller, larger % smaller.
309+
* @throws BCMathException
296310
*/
297311
public function coSwitch($a, $b)
298312
{
@@ -306,7 +320,7 @@ public function coSwitch($a, $b)
306320
default:
307321
// Should never, ever be here but good practice
308322
// dictates to always have a default clause.
309-
// TODO: Throw exception.
323+
throw new BCMathException("Unexpected comparison result in BC::coSwitch(). Should not be here!");
310324
}
311325
}
312326

0 commit comments

Comments
 (0)