Skip to content

EspoCRM – Open Source CRM Application

formula: increment decrement

Yuri Kuznetsoveafd465ba224
diff --git a/application/Espo/Core/Formula/Functions/VariableDecrementType.php b/application/Espo/Core/Formula/Functions/VariableDecrementType.php
new file mode 100644
index 0000000000..c69513d96e
--- /dev/null
+++ b/application/Espo/Core/Formula/Functions/VariableDecrementType.php
@@ -0,0 +1,64 @@
+<?php
+/************************************************************************
+ * This file is part of EspoCRM.
+ *
+ * EspoCRM – Open Source CRM application.
+ * Copyright (C) 2014-2025 Yurii Kuznietsov, Taras Machyshyn, Oleksii Avramenko
+ * Website: https://www.espocrm.com
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Affero General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU Affero General Public License for more details.
+ *
+ * You should have received a copy of the GNU Affero General Public License
+ * along with this program. If not, see <https://www.gnu.org/licenses/>.
+ *
+ * The interactive user interfaces in modified source and object code versions
+ * of this program must display Appropriate Legal Notices, as required under
+ * Section 5 of the GNU Affero General Public License version 3.
+ *
+ * In accordance with Section 7(b) of the GNU Affero General Public License version 3,
+ * these Appropriate Legal Notices must retain the display of the "EspoCRM" word.
+ ************************************************************************/
+
+namespace Espo\Core\Formula\Functions;
+
+use Espo\Core\Formula\ArgumentList;
+use Espo\Core\Formula\Exceptions\Error;
+
+/**
+ * @noinspection PhpUnused
+ */
+class VariableDecrementType extends BaseFunction
+{
+ public function process(ArgumentList $args)
+ {
+ if (count($args) < 1) {
+ $this->throwTooFewArguments();
+ }
+
+ $name = $this->evaluate($args[0]);
+
+ if (!is_string($name)) {
+ $this->throwBadArgumentValue(1, 'string');
+ }
+
+ if (!property_exists($this->getVariables(), $name)) {
+ throw new Error("Cannot decrement non-existing variable.");
+ }
+
+ $reference =& $this->getVariables()->$name;
+
+ if (!is_float($reference) && !is_int($reference)) {
+ throw new Error("Cannot decrement non-number variable.");
+ }
+
+ $reference--;
+ }
+}
diff --git a/application/Espo/Core/Formula/Functions/VariableIncrementType.php b/application/Espo/Core/Formula/Functions/VariableIncrementType.php
new file mode 100644
index 0000000000..2f577e7de9
--- /dev/null
+++ b/application/Espo/Core/Formula/Functions/VariableIncrementType.php
@@ -0,0 +1,64 @@
+<?php
+/************************************************************************
+ * This file is part of EspoCRM.
+ *
+ * EspoCRM – Open Source CRM application.
+ * Copyright (C) 2014-2025 Yurii Kuznietsov, Taras Machyshyn, Oleksii Avramenko
+ * Website: https://www.espocrm.com
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Affero General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU Affero General Public License for more details.
+ *
+ * You should have received a copy of the GNU Affero General Public License
+ * along with this program. If not, see <https://www.gnu.org/licenses/>.
+ *
+ * The interactive user interfaces in modified source and object code versions
+ * of this program must display Appropriate Legal Notices, as required under
+ * Section 5 of the GNU Affero General Public License version 3.
+ *
+ * In accordance with Section 7(b) of the GNU Affero General Public License version 3,
+ * these Appropriate Legal Notices must retain the display of the "EspoCRM" word.
+ ************************************************************************/
+
+namespace Espo\Core\Formula\Functions;
+
+use Espo\Core\Formula\ArgumentList;
+use Espo\Core\Formula\Exceptions\Error;
+
+/**
+ * @noinspection PhpUnused
+ */
+class VariableIncrementType extends BaseFunction
+{
+ public function process(ArgumentList $args)
+ {
+ if (count($args) < 1) {
+ $this->throwTooFewArguments();
+ }
+
+ $name = $this->evaluate($args[0]);
+
+ if (!is_string($name)) {
+ $this->throwBadArgumentValue(1, 'string');
+ }
+
+ if (!property_exists($this->getVariables(), $name)) {
+ throw new Error("Cannot increment non-existing variable.");
+ }
+
+ $reference =& $this->getVariables()->$name;
+
+ if (!is_float($reference) && !is_int($reference)) {
+ throw new Error("Cannot increment non-number variable.");
+ }
+
+ $reference++;
+ }
+}
diff --git a/application/Espo/Core/Formula/Parser.php b/application/Espo/Core/Formula/Parser.php
index 484e2f9f9d..4daa4157d6 100644
--- a/application/Espo/Core/Formula/Parser.php
+++ b/application/Espo/Core/Formula/Parser.php
@@ -987,10 +987,37 @@ class Parser
if ($expression[0] === "$") {
$value = substr($expression, 1);
+ $isIncrement = false;
+ $isDecrement = false;
+
+ if (str_ends_with($expression, '++')) {
+ $isIncrement = true;
+
+ $value = rtrim(substr($value, 0, -2));
+ }
+
+ if (str_ends_with($expression, '--')) {
+ $isDecrement = true;
+
+ $value = rtrim(substr($value, 0, -2));
+ }
+
if ($value === '' || !preg_match($this->variableNameRegExp, $value)) {
throw new SyntaxError("Bad variable name `$value`.");
}
+ if ($isIncrement) {
+ return new Node('variableIncrement', [
+ new Value($value),
+ ]);
+ }
+
+ if ($isDecrement) {
+ return new Node('variableDecrement', [
+ new Value($value),
+ ]);
+ }
+
return new Variable($value);
}
diff --git a/tests/unit/Espo/Core/Formula/EvaluatorTest.php b/tests/unit/Espo/Core/Formula/EvaluatorTest.php
index eec7b03832..0b5bdf3114 100644
--- a/tests/unit/Espo/Core/Formula/EvaluatorTest.php
+++ b/tests/unit/Espo/Core/Formula/EvaluatorTest.php
@@ -1731,4 +1731,114 @@ class EvaluatorTest extends TestCase
/** @noinspection PhpUnhandledExceptionInspection */
$this->evaluator->process($expression);
}
+
+ public function testIncrementInt(): void
+ {
+ $expression = "
+ \$v = 0;
+ \$v ++;
+ \$v++;
+ \$v;
+ ";
+
+ /** @noinspection PhpUnhandledExceptionInspection */
+ $result = $this->evaluator->process($expression);
+
+ $this->assertEquals(2, $result);
+ }
+
+ public function testIncrementFloat(): void
+ {
+ $expression = "
+ \$v = 0.0;
+ \$v ++;
+ \$v++;
+ \$v;
+ ";
+
+ /** @noinspection PhpUnhandledExceptionInspection */
+ $result = $this->evaluator->process($expression);
+
+ $this->assertEquals(2.0, $result);
+ }
+
+ public function testDecrementInt(): void
+ {
+ $expression = "
+ \$v = 2;
+ \$v --;
+ \$v--;
+ \$v;
+ ";
+
+ /** @noinspection PhpUnhandledExceptionInspection */
+ $result = $this->evaluator->process($expression);
+
+ $this->assertEquals(0, $result);
+ }
+
+ public function testDecrementFloat(): void
+ {
+ $expression = "
+ \$v = 2.0;
+ \$v --;
+ \$v--;
+ \$v;
+ ";
+
+ /** @noinspection PhpUnhandledExceptionInspection */
+ $result = $this->evaluator->process($expression);
+
+ $this->assertEquals(0.0, $result);
+ }
+
+ public function testIncrementError1(): void
+ {
+ $expression = "
+ \$v++;
+ ";
+
+ $this->expectException(Error::class);
+
+ /** @noinspection PhpUnhandledExceptionInspection */
+ $this->evaluator->process($expression);
+ }
+
+ public function testIncrementError2(): void
+ {
+ $expression = "
+ \$v = '1';
+ \$v++;
+ ";
+
+ $this->expectException(Error::class);
+
+ /** @noinspection PhpUnhandledExceptionInspection */
+ $this->evaluator->process($expression);
+ }
+
+ public function testDecrementError1(): void
+ {
+ $expression = "
+ \$v--;
+ ";
+
+ $this->expectException(Error::class);
+
+ /** @noinspection PhpUnhandledExceptionInspection */
+ $this->evaluator->process($expression);
+ }
+
+ public function testDecrementError2(): void
+ {
+ $expression = "
+ \$v = '1';
+ \$v--;
+ ";
+
+ $this->expectException(Error::class);
+
+ /** @noinspection PhpUnhandledExceptionInspection */
+ $this->evaluator->process($expression);
+ }
}