diff options
author | Bruce Wells <bruce.wells@simparel.com> | 2018-10-24 21:16:11 +0300 |
---|---|---|
committer | Bruce Wells <bruce.wells@simparel.com> | 2018-10-24 23:01:01 +0300 |
commit | b9f72a001bca4772a003248595ad20b13a5b2277 (patch) | |
tree | bf38dac0f68746efbf5adf31e3a7e94b56bc7db0 /src/NXP/Classes/Token/TokenDivision.php | |
parent | 12d41b160bbf8c26601819fcc1f7628c48bc7a00 (diff) |
Additional validation for bad expressions (*+ for example)
Diffstat (limited to 'src/NXP/Classes/Token/TokenDivision.php')
-rw-r--r-- | src/NXP/Classes/Token/TokenDivision.php | 16 |
1 files changed, 16 insertions, 0 deletions
diff --git a/src/NXP/Classes/Token/TokenDivision.php b/src/NXP/Classes/Token/TokenDivision.php index f1c35ff..a85bec5 100644 --- a/src/NXP/Classes/Token/TokenDivision.php +++ b/src/NXP/Classes/Token/TokenDivision.php @@ -10,6 +10,9 @@ namespace NXP\Classes\Token; +use NXP\Exception\IncorrectExpressionException; +use NXP\Exception\DivisionByZeroException; + /** * @author Alexander Kiryukhin <alexander@symdev.org> */ @@ -41,12 +44,25 @@ class TokenDivision extends AbstractOperator /** * @param InterfaceToken[] $stack + * * @return $this + * + * @throws \NXP\Exception\IncorrectExpressionException + * @throws \NXP\Exception\DivisionByZeroException */ public function execute(&$stack) { $op2 = array_pop($stack); $op1 = array_pop($stack); + + if ($op1 === null || $op2 === null) { + throw new IncorrectExpressionException("Division requires two operators"); + } + + if ($op2->getValue() == 0){ + throw new DivisionByZeroException(); + } + $result = $op2->getValue() != 0 ? $op1->getValue() / $op2->getValue() : 0; return new TokenNumber($result); |