diff options
author | Bruce Wells <phpfui@users.noreply.github.com> | 2018-10-25 18:54:54 +0300 |
---|---|---|
committer | GitHub <noreply@github.com> | 2018-10-25 18:54:54 +0300 |
commit | 43f0ff3f28d198fbb4e36346fc5f36fa91cf3e18 (patch) | |
tree | 28a0abef9e0e48df01660985339e93c69a2cd746 /src | |
parent | 4a672cfd94c07e1821227f27fd1edd2217685136 (diff) |
Support for better invalid expression detection and divide by zero (#30)
* Additional validation for bad expressions (*+ for example)
* Removing DivisionByZeroException testing for now
Added more unit tests.
Diffstat (limited to 'src')
-rw-r--r-- | src/NXP/Classes/Calculator.php | 2 | ||||
-rw-r--r-- | src/NXP/Classes/Token/TokenDegree.php | 10 | ||||
-rw-r--r-- | src/NXP/Classes/Token/TokenDivision.php | 12 | ||||
-rw-r--r-- | src/NXP/Classes/Token/TokenMinus.php | 10 | ||||
-rw-r--r-- | src/NXP/Classes/Token/TokenMultiply.php | 10 | ||||
-rw-r--r-- | src/NXP/Classes/Token/TokenPlus.php | 10 | ||||
-rw-r--r-- | src/NXP/Exception/DivisionByZeroException.php | 19 |
7 files changed, 72 insertions, 1 deletions
diff --git a/src/NXP/Classes/Calculator.php b/src/NXP/Classes/Calculator.php index bfa7015..253e70c 100644 --- a/src/NXP/Classes/Calculator.php +++ b/src/NXP/Classes/Calculator.php @@ -54,7 +54,7 @@ class Calculator } } $result = array_pop($stack); - if (!empty($stack)) { + if ($result === null || ! empty($stack)) { throw new IncorrectExpressionException(); } diff --git a/src/NXP/Classes/Token/TokenDegree.php b/src/NXP/Classes/Token/TokenDegree.php index c31b66e..3eec23d 100644 --- a/src/NXP/Classes/Token/TokenDegree.php +++ b/src/NXP/Classes/Token/TokenDegree.php @@ -10,6 +10,8 @@ namespace NXP\Classes\Token; +use NXP\Exception\IncorrectExpressionException; + /** * @author Alexander Kiryukhin <alexander@symdev.org> */ @@ -41,12 +43,20 @@ class TokenDegree extends AbstractOperator /** * @param InterfaceToken[] $stack + * * @return TokenNumber + * + * @throws \NXP\Exception\IncorrectExpressionException */ public function execute(&$stack) { $op2 = array_pop($stack); $op1 = array_pop($stack); + + if ($op1 === null || $op2 === null) { + throw new IncorrectExpressionException("Power operator requires two operators"); + } + $result = $op1->getValue() ** $op2->getValue(); return new TokenNumber($result); diff --git a/src/NXP/Classes/Token/TokenDivision.php b/src/NXP/Classes/Token/TokenDivision.php index f1c35ff..5bbc35e 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,21 @@ 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"); + } + $result = $op2->getValue() != 0 ? $op1->getValue() / $op2->getValue() : 0; return new TokenNumber($result); diff --git a/src/NXP/Classes/Token/TokenMinus.php b/src/NXP/Classes/Token/TokenMinus.php index 0463d4c..566c950 100644 --- a/src/NXP/Classes/Token/TokenMinus.php +++ b/src/NXP/Classes/Token/TokenMinus.php @@ -10,6 +10,8 @@ namespace NXP\Classes\Token; +use NXP\Exception\IncorrectExpressionException; + /** * @author Alexander Kiryukhin <alexander@symdev.org> */ @@ -41,12 +43,20 @@ class TokenMinus extends AbstractOperator /** * @param InterfaceToken[] $stack + * * @return $this + * + * @throws \NXP\Exception\IncorrectExpressionException */ public function execute(&$stack) { $op2 = array_pop($stack); $op1 = array_pop($stack); + + if ($op1 === null || $op2 === null) { + throw new IncorrectExpressionException("Subtraction requires two operators"); + } + $result = $op1->getValue() - $op2->getValue(); return new TokenNumber($result); diff --git a/src/NXP/Classes/Token/TokenMultiply.php b/src/NXP/Classes/Token/TokenMultiply.php index e6fd960..8b173b9 100644 --- a/src/NXP/Classes/Token/TokenMultiply.php +++ b/src/NXP/Classes/Token/TokenMultiply.php @@ -10,6 +10,8 @@ namespace NXP\Classes\Token; +use NXP\Exception\IncorrectExpressionException; + /** * @author Alexander Kiryukhin <alexander@symdev.org> */ @@ -41,12 +43,20 @@ class TokenMultiply extends AbstractOperator /** * @param InterfaceToken[] $stack + * * @return $this + * + * @throws \NXP\Exception\IncorrectExpressionException */ public function execute(&$stack) { $op2 = array_pop($stack); $op1 = array_pop($stack); + + if ($op1 === null || $op2 === null) { + throw new IncorrectExpressionException("Multiplication requires two operators"); + } + $result = $op1->getValue() * $op2->getValue(); return new TokenNumber($result); diff --git a/src/NXP/Classes/Token/TokenPlus.php b/src/NXP/Classes/Token/TokenPlus.php index f9562e7..fe5a1d6 100644 --- a/src/NXP/Classes/Token/TokenPlus.php +++ b/src/NXP/Classes/Token/TokenPlus.php @@ -10,6 +10,8 @@ namespace NXP\Classes\Token; +use NXP\Exception\IncorrectExpressionException; + /** * @author Alexander Kiryukhin <alexander@symdev.org> */ @@ -41,12 +43,20 @@ class TokenPlus extends AbstractOperator /** * @param InterfaceToken[] $stack + * * @return $this + * + * @throws \NXP\Exception\IncorrectExpressionException */ public function execute(&$stack) { $op2 = array_pop($stack); $op1 = array_pop($stack); + + if ($op1 === null || $op2 === null) { + throw new IncorrectExpressionException("Addition requires two operators"); + } + $result = $op1->getValue() + $op2->getValue(); return new TokenNumber($result); diff --git a/src/NXP/Exception/DivisionByZeroException.php b/src/NXP/Exception/DivisionByZeroException.php new file mode 100644 index 0000000..3a9a978 --- /dev/null +++ b/src/NXP/Exception/DivisionByZeroException.php @@ -0,0 +1,19 @@ +<?php + +/** + * This file is part of the MathExecutor package + * + * (c) Alexander Kiryukhin + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code + */ + +namespace NXP\Exception; + +/** + * @author Vitaliy Zhuk <zhuk2205@gmail.com> + */ +class DivisionByZeroException extends MathExecutorException +{ +} |