diff --git a/application/Espo/Core/Formula/Parser.php b/application/Espo/Core/Formula/Parser.php index a16f55547a..f25223040a 100644 --- a/application/Espo/Core/Formula/Parser.php +++ b/application/Espo/Core/Formula/Parser.php @@ -176,6 +176,8 @@ class Parser $this->processStrings($expression, $modifiedExpression, $splitterIndexList, true); + $expressionOutOfBraceList = []; + for ($i = 0; $i < strlen($modifiedExpression); $i++) { if ($modifiedExpression[$i] === '(') { $braceCounter++; @@ -186,6 +188,11 @@ class Parser if ($braceCounter === 0 && $i < strlen($modifiedExpression) - 1) { $hasExcessBraces = false; } + if ($braceCounter === 0) { + $expressionOutOfBraceList[] = true; + } else { + $expressionOutOfBraceList[] = false; + } } if ($braceCounter !== 0) { throw new Error('Incorrect round brackets in expression ' . $expression . '.'); @@ -226,7 +233,13 @@ class Parser foreach ($this->priorityList as $operationList) { foreach ($operationList as $operator) { - $index = strpos($expression, $operator, 1); + $startFrom = 1; + while (true) { + $index = strpos($expression, $operator, $startFrom); + if ($index === false) break; + if ($expressionOutOfBraceList[$index]) break; + $startFrom = $index + 1; + } if ($index !== false) { $possibleRightOperator = null; if (strlen($operator) === 1) { diff --git a/tests/unit/Espo/Core/Formula/EvaluatorTest.php b/tests/unit/Espo/Core/Formula/EvaluatorTest.php index d5ce8f6606..fb5b71aa49 100644 --- a/tests/unit/Espo/Core/Formula/EvaluatorTest.php +++ b/tests/unit/Espo/Core/Formula/EvaluatorTest.php @@ -77,4 +77,29 @@ class EvaluatorTest extends \PHPUnit\Framework\TestCase $actual = $this->evaluator->process($expression); $this->assertTrue($actual); } + + function testSummationOfMultipleIfThenElse() + { + $expression = " + ifThenElse( + true, + (1 + 0 + 1) - 1 * 0.5, + 0 + ) + + + ifThenElse( + true, + (1 - 0) * 0.5, + 0 + ) + + + ifThenElse( + true, + (1 - 0) * 0.5, + 0 + ) + "; + $actual = $this->evaluator->process($expression); + $this->assertEquals(2.5, $actual); + } } \ No newline at end of file diff --git a/tests/unit/Espo/Core/Formula/ParserTest.php b/tests/unit/Espo/Core/Formula/ParserTest.php index 35b687d885..3795df01c3 100644 --- a/tests/unit/Espo/Core/Formula/ParserTest.php +++ b/tests/unit/Espo/Core/Formula/ParserTest.php @@ -305,6 +305,7 @@ class ParserTest extends \PHPUnit\Framework\TestCase ] ] ]; + $this->assertEquals($expected, $actual); $expression = "!value * 10";