Skip to content

EspoCRM – Open Source CRM Application

formula: key access null coalescing

Yuri Kuznetsovcaf3a729d180
diff --git a/application/Espo/Core/Formula/Exceptions/UndefinedKey.php b/application/Espo/Core/Formula/Exceptions/UndefinedKey.php
new file mode 100644
index 0000000000..c05ef6a8de
--- /dev/null
+++ b/application/Espo/Core/Formula/Exceptions/UndefinedKey.php
@@ -0,0 +1,49 @@
+<?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\Exceptions;
+
+class UndefinedKey extends Error
+{
+ private int $levelsRisen = 0;
+
+ public function getLevelsRisen(): int
+ {
+ return $this->levelsRisen;
+ }
+
+ public static function cloneWithLevelRisen(self $exception): UndefinedKey
+ {
+ $obj = new UndefinedKey($exception->getMessage(), $exception->getCode(), $exception);
+
+ $obj->levelsRisen = $exception->getLevelsRisen() + 1;
+
+ return $obj;
+ }
+}
diff --git a/application/Espo/Core/Formula/Functions/ComparisonGroup/NullCoalescingType.php b/application/Espo/Core/Formula/Functions/ComparisonGroup/NullCoalescingType.php
index 7ce6ced7c9..71c223fab4 100644
--- a/application/Espo/Core/Formula/Functions/ComparisonGroup/NullCoalescingType.php
+++ b/application/Espo/Core/Formula/Functions/ComparisonGroup/NullCoalescingType.php
@@ -29,6 +29,7 @@
namespace Espo\Core\Formula\Functions\ComparisonGroup;
+use Espo\Core\Formula\Exceptions\UndefinedKey;
use Espo\Core\Formula\Functions\BaseFunction;
use Espo\Core\Formula\ArgumentList;
@@ -47,7 +48,16 @@ class NullCoalescingType extends BaseFunction
}
foreach (array_slice($array, 0, -1) as $arg) {
- $value = $this->evaluate($arg);
+ try {
+ $value = $this->evaluate($arg);
+ } catch (UndefinedKey $e) {
+ if ($e->getLevelsRisen() > 1) {
+ throw $e;
+ }
+
+ $value = null;
+ }
+
if ($value !== null) {
return $value;
diff --git a/application/Espo/Core/Formula/Functions/VariableGetValueByKeyType.php b/application/Espo/Core/Formula/Functions/VariableGetValueByKeyType.php
index fb22904949..644b46810c 100644
--- a/application/Espo/Core/Formula/Functions/VariableGetValueByKeyType.php
+++ b/application/Espo/Core/Formula/Functions/VariableGetValueByKeyType.php
@@ -31,6 +31,7 @@ namespace Espo\Core\Formula\Functions;
use Espo\Core\Formula\ArgumentList;
use Espo\Core\Formula\Exceptions\Error;
+use Espo\Core\Formula\Exceptions\UndefinedKey;
use stdClass;
/**
@@ -83,15 +84,15 @@ class VariableGetValueByKeyType extends BaseFunction
}
if ($key < 0) {
- throw new Error("Cannot get array item value by key that is less than zero.");
+ throw new UndefinedKey("Cannot get array item value by key that is less than zero.");
}
if ($key > count($reference) - 1) {
- throw new Error("Cannot get array item value by key that is out of array end.");
+ throw new UndefinedKey("Cannot get array item value by key that is out of array end.");
}
if (!array_key_exists($key, $reference)) {
- throw new Error("Cannot get array item value by non-existent key.");
+ throw new UndefinedKey("Cannot get array item value by non-existent key.");
}
/** @noinspection PhpUnnecessaryLocalVariableInspection */
@@ -109,7 +110,7 @@ class VariableGetValueByKeyType extends BaseFunction
}
if (!property_exists($reference, $key)) {
- throw new Error("Cannot get object item value by non-existent key.");
+ throw new UndefinedKey("Cannot get object item value by non-existent key.");
}
/** @noinspection PhpUnnecessaryLocalVariableInspection */
diff --git a/application/Espo/Core/Formula/Processor.php b/application/Espo/Core/Formula/Processor.php
index ad5b7fa16f..725ecc1cb1 100644
--- a/application/Espo/Core/Formula/Processor.php
+++ b/application/Espo/Core/Formula/Processor.php
@@ -33,6 +33,7 @@ use Espo\Core\Formula\Exceptions\BadArgumentType;
use Espo\Core\Formula\Exceptions\BadArgumentValue;
use Espo\Core\Formula\Exceptions\ExecutionException;
use Espo\Core\Formula\Exceptions\TooFewArguments;
+use Espo\Core\Formula\Exceptions\UndefinedKey;
use Espo\Core\Formula\Functions\Base as DeprecatedBaseFunction;
use Espo\Core\Formula\Functions\BaseFunction;
use Espo\Core\Formula\Parser\Ast\Attribute;
@@ -102,7 +103,11 @@ class Processor
return $function->process(self::dataToStdClass($item->getData()));
}
- return $function->process($item->getArgumentList());
+ try {
+ return $function->process($item->getArgumentList());
+ } catch (UndefinedKey $e) {
+ throw UndefinedKey::cloneWithLevelRisen($e);
+ }
}
/**
diff --git a/tests/unit/Espo/Core/Formula/EvaluatorTest.php b/tests/unit/Espo/Core/Formula/EvaluatorTest.php
index b70dcd7d75..894ff10eb1 100644
--- a/tests/unit/Espo/Core/Formula/EvaluatorTest.php
+++ b/tests/unit/Espo/Core/Formula/EvaluatorTest.php
@@ -31,6 +31,7 @@ namespace tests\unit\Espo\Core\Formula;
use Espo\Core\Formula\Evaluator;
use Espo\Core\Formula\Exceptions\Error;
+use Espo\Core\Formula\Exceptions\UndefinedKey;
use Espo\Core\Formula\Exceptions\UnsafeFunction;
use Espo\Core\InjectableFactory;
use Espo\Core\Formula\Exceptions\SyntaxError;
@@ -2018,4 +2019,83 @@ class EvaluatorTest extends TestCase
$this->assertEquals('1', $result);
}
+
+ public function testGetByKeyObjectCoalescing1(): void
+ {
+ $expression = "
+ \$o = object\\create();
+ \$o['a'] ?? '2';
+ ";
+
+ /** @noinspection PhpUnhandledExceptionInspection */
+ $result = $this->evaluator->process($expression);
+
+ $this->assertEquals('2', $result);
+ }
+
+ public function testGetByKeyObjectCoalescing2(): void
+ {
+ $expression = "
+ \$o = object\\create();
+ \$o['a']['b'] ?? '2';
+ ";
+
+ /** @noinspection PhpUnhandledExceptionInspection */
+ $result = $this->evaluator->process($expression);
+
+ $this->assertEquals('2', $result);
+ }
+
+ public function testGetByKeyArrayCoalescing1(): void
+ {
+ $expression = "
+ \$o = list();
+ \$o[0] ?? '2';
+ ";
+
+ /** @noinspection PhpUnhandledExceptionInspection */
+ $result = $this->evaluator->process($expression);
+
+ $this->assertEquals('2', $result);
+ }
+
+ public function testGetByKeyArrayCoalescing2(): void
+ {
+ $expression = "
+ \$o = list();
+ \$o[0][0] ?? '2';
+ ";
+
+ /** @noinspection PhpUnhandledExceptionInspection */
+ $result = $this->evaluator->process($expression);
+
+ $this->assertEquals('2', $result);
+ }
+
+ public function testGetByKeyArrayCoalescing3(): void
+ {
+ $expression = "
+ \$o = list();
+ \$o[-1] ?? '2';
+ ";
+
+ /** @noinspection PhpUnhandledExceptionInspection */
+ $result = $this->evaluator->process($expression);
+
+ $this->assertEquals('2', $result);
+ }
+
+ public function testGetByKeyArrayCoalescingNestedError(): void
+ {
+ $expression = "
+ \$a = list();
+ \$b = list();
+ \$a[ (\$b[0]) ] ?? '2';
+ ";
+
+ $this->expectException(UndefinedKey::class);
+
+ /** @noinspection PhpUnhandledExceptionInspection */
+ $this->evaluator->process($expression);
+ }
}