Skip to content

Commit

Permalink
fixed error on evaluate the empty formula
Browse files Browse the repository at this point in the history
  • Loading branch information
optimistex committed Nov 2, 2017
1 parent 112b421 commit 79b601b
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 8 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "optimistex/math-expression",
"version": "2.1.0",
"version": "2.1.1",
"type": "library",
"description": "Taken from http://www.phpclasses.org/browse/file/11680.html, cred to Miles Kaufmann",
"keywords": [
Expand Down
21 changes: 15 additions & 6 deletions expression.php
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ public function evaluate($expr)
{
$this->last_error = null;
$expr = trim($expr);
if ($expr[strlen($expr) - 1] === ';') {
if ($expr && $expr[strlen($expr) - 1] === ';') {
$expr = substr($expr, 0, -1); // strip semicolons at the end
}
//===============
Expand Down Expand Up @@ -199,7 +199,11 @@ public function funcs()

//===================== HERE BE INTERNAL METHODS ====================\\

// Convert infix to postfix notation
/**
* Convert infix to postfix notation
* @param string $expr
* @return array|bool
*/
public function nfx($expr)
{
$index = 0;
Expand Down Expand Up @@ -402,14 +406,19 @@ public function nfx($expr)
return $output;
}

// evaluate postfix notation
public function pfx(array $tokens, array $vars = array())
/**
* evaluate postfix notation
* @param array|bool $tokens
* @param array $vars
* @return bool|mixed|null
*/
public function pfx($tokens, array $vars = array())
{
if (empty($tokens)) {
if ($tokens === false) {
return false;
}
$stack = new ExpressionStack();
foreach ($tokens as $token) { // nice and easy
foreach ((array)$tokens as $token) { // nice and easy
// if the token is a binary operator, pop two values off the stack, do the operation, and push the result back on
if (in_array($token, array('+', '-', '*', '/', '^', '<', '>', '<=', '>=', '==', '&&', '||', '!=', '=~', '%'), true)) {
$op2 = $stack->pop();
Expand Down
9 changes: 8 additions & 1 deletion tests/Expression.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,14 @@ public function testTest() {
echo json_encode($expr->evaluate($expression)) ? "true" : "false";
}
*/

public function testEmptyFormula()
{
$expr = new Expression();
$expr->suppress_errors = true;
$this->assertEquals($expr->evaluate(''), false);
}

// -------------------------------------------------------------------------
public function testIntegers()
{
Expand Down Expand Up @@ -384,5 +392,4 @@ public function testFunctionOrderParameters()
$this->assertEquals($e->evaluate($formula), $result);
}
}

}

0 comments on commit 79b601b

Please sign in to comment.