From f8faf3fa8d21ebd6e6f8c73f022eb64e407dc6e1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Javier=20Mar=C3=ADn?= Date: Tue, 15 Sep 2020 02:47:26 +0200 Subject: [PATCH] Improved support for null variables (#72) * Added handler to define not found variables Added support for string variables Fixed strings and ints comparison error * Check if variables have scalar types (int, float, string and bool) Better $onVarNotFound logic * Better support for null variables * Better support for null variables * Better support for null variables --- src/NXP/Classes/Calculator.php | 4 +--- src/NXP/MathExecutor.php | 2 +- tests/MathTest.php | 42 +++++++++++++++++----------------- 3 files changed, 23 insertions(+), 25 deletions(-) diff --git a/src/NXP/Classes/Calculator.php b/src/NXP/Classes/Calculator.php index 49142df..4785a8f 100644 --- a/src/NXP/Classes/Calculator.php +++ b/src/NXP/Classes/Calculator.php @@ -64,9 +64,7 @@ public function calculate(array $tokens, array $variables, callable $onVarNotFou $value = $variables[$variable]; } elseif ($onVarNotFound) { $value = call_user_func($onVarNotFound, $variable); - } - - if (!isset($value)) { + } else { throw new UnknownVariableException($variable); } diff --git a/src/NXP/MathExecutor.php b/src/NXP/MathExecutor.php index 434246a..f45335c 100644 --- a/src/NXP/MathExecutor.php +++ b/src/NXP/MathExecutor.php @@ -416,7 +416,7 @@ public function getVars() : array */ public function getVar(string $variable) { - if (!isset($this->variables[$variable])) { + if (!array_key_exists($variable, $this->variables)) { throw new UnknownVariableException("Variable ({$variable}) not set"); } return $this->variables[$variable]; diff --git a/tests/MathTest.php b/tests/MathTest.php index 8f5665b..c519c5c 100644 --- a/tests/MathTest.php +++ b/tests/MathTest.php @@ -237,8 +237,8 @@ public function testZeroDivisionException() $calculator = new MathExecutor(); $this->expectException(DivisionByZeroException::class); $calculator->execute('10 / 0'); - $calculator->setVar('one', 1)->setVar('zero', 0); - $this->assertEquals(0.0, $calculator->execute('$one / $zero')); + $calculator->setVar('one', 1)->setVar('zero', 0); + $this->assertEquals(0.0, $calculator->execute('$one / $zero')); } public function testVariableIncorrectExpressionException() @@ -337,19 +337,19 @@ public function testVariables() $this->assertEquals(2.71828182846, $calculator->execute('$e')); $this->assertEquals(2.71828182846, $calculator->execute('e')); $calculator->setVars([ - 'trx_amount' => 100000.01, - 'ten' => 10, - 'nine' => 9, - 'eight' => 8, - 'seven' => 7, - 'six' => 6, - 'five' => 5, - 'four' => 4, - 'three' => 3, - 'two' => 2, - 'one' => 1, - 'zero' => 0, - ]); + 'trx_amount' => 100000.01, + 'ten' => 10, + 'nine' => 9, + 'eight' => 8, + 'seven' => 7, + 'six' => 6, + 'five' => 5, + 'four' => 4, + 'three' => 3, + 'two' => 2, + 'one' => 1, + 'zero' => 0, + ]); $this->assertEquals(100000.01, $calculator->execute('$trx_amount')); $this->assertEquals(10 - 9, $calculator->execute('$ten - $nine')); $this->assertEquals(9 - 10, $calculator->execute('$nine - $ten')); @@ -374,8 +374,8 @@ public function testEvaluateFunctionParameters() $calculator->addFunction( 'round', function ($value, $decimals) { - return round($value, $decimals); - } + return round($value, $decimals); + } ); $expression = 'round(100 * 1.111111, 2)'; $phpResult = 0; @@ -403,9 +403,9 @@ public function testQuotes() $calculator->addFunction( 'test', function ($arg) use ($testString) { - $this->assertEquals($testString, $arg); - return 0; - } + $this->assertEquals($testString, $arg); + return 0; + } ); $calculator->execute('test("' . $testString . '")'); // single quotes $calculator->execute("test('" . $testString . "')"); // double quotes @@ -526,4 +526,4 @@ public function testSetVarsDoesNotAcceptResource() $this->expectException(MathExecutorException::class); $calculator->setVar('resource', tmpfile()); } -} +} \ No newline at end of file