Skip to content

Commit 79b601b

Browse files
committed
fixed error on evaluate the empty formula
1 parent 112b421 commit 79b601b

File tree

3 files changed

+24
-8
lines changed

3 files changed

+24
-8
lines changed

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "optimistex/math-expression",
3-
"version": "2.1.0",
3+
"version": "2.1.1",
44
"type": "library",
55
"description": "Taken from http://www.phpclasses.org/browse/file/11680.html, cred to Miles Kaufmann",
66
"keywords": [

expression.php

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ public function evaluate($expr)
136136
{
137137
$this->last_error = null;
138138
$expr = trim($expr);
139-
if ($expr[strlen($expr) - 1] === ';') {
139+
if ($expr && $expr[strlen($expr) - 1] === ';') {
140140
$expr = substr($expr, 0, -1); // strip semicolons at the end
141141
}
142142
//===============
@@ -199,7 +199,11 @@ public function funcs()
199199

200200
//===================== HERE BE INTERNAL METHODS ====================\\
201201

202-
// Convert infix to postfix notation
202+
/**
203+
* Convert infix to postfix notation
204+
* @param string $expr
205+
* @return array|bool
206+
*/
203207
public function nfx($expr)
204208
{
205209
$index = 0;
@@ -402,14 +406,19 @@ public function nfx($expr)
402406
return $output;
403407
}
404408

405-
// evaluate postfix notation
406-
public function pfx(array $tokens, array $vars = array())
409+
/**
410+
* evaluate postfix notation
411+
* @param array|bool $tokens
412+
* @param array $vars
413+
* @return bool|mixed|null
414+
*/
415+
public function pfx($tokens, array $vars = array())
407416
{
408-
if (empty($tokens)) {
417+
if ($tokens === false) {
409418
return false;
410419
}
411420
$stack = new ExpressionStack();
412-
foreach ($tokens as $token) { // nice and easy
421+
foreach ((array)$tokens as $token) { // nice and easy
413422
// if the token is a binary operator, pop two values off the stack, do the operation, and push the result back on
414423
if (in_array($token, array('+', '-', '*', '/', '^', '<', '>', '<=', '>=', '==', '&&', '||', '!=', '=~', '%'), true)) {
415424
$op2 = $stack->pop();

tests/Expression.php

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,14 @@ public function testTest() {
2020
echo json_encode($expr->evaluate($expression)) ? "true" : "false";
2121
}
2222
*/
23+
24+
public function testEmptyFormula()
25+
{
26+
$expr = new Expression();
27+
$expr->suppress_errors = true;
28+
$this->assertEquals($expr->evaluate(''), false);
29+
}
30+
2331
// -------------------------------------------------------------------------
2432
public function testIntegers()
2533
{
@@ -384,5 +392,4 @@ public function testFunctionOrderParameters()
384392
$this->assertEquals($e->evaluate($formula), $result);
385393
}
386394
}
387-
388395
}

0 commit comments

Comments
 (0)