From be2d936be98f52be5b29fd427bddb78ea55ce60a Mon Sep 17 00:00:00 2001 From: Yuri Kuznetsov Date: Fri, 18 Mar 2022 14:46:07 +0200 Subject: [PATCH] type fixes --- application/Espo/Core/Formula/Argument.php | 4 ++ .../Espo/Core/Formula/FunctionFactory.php | 3 +- .../Functions/DatetimeGroup/ClosestType.php | 58 ++++++++++++++----- .../ExtGroup/EmailGroup/ApplyTemplateType.php | 1 + .../ExtGroup/EmailGroup/SendType.php | 2 + .../Functions/JsonGroup/RetrieveType.php | 1 + .../Functions/NumberGroup/CeilType.php | 2 +- .../Functions/NumberGroup/FloorType.php | 2 +- .../Functions/NumberGroup/RoundType.php | 3 +- .../Functions/RecordGroup/CreateType.php | 2 + .../Functions/RecordGroup/UpdateType.php | 8 +++ 11 files changed, 68 insertions(+), 18 deletions(-) diff --git a/application/Espo/Core/Formula/Argument.php b/application/Espo/Core/Formula/Argument.php index 1fc0b7cc36..eff94d28ae 100644 --- a/application/Espo/Core/Formula/Argument.php +++ b/application/Espo/Core/Formula/Argument.php @@ -58,6 +58,10 @@ class Argument implements Evaluatable return null; } + if (!property_exists($this->data, 'type')) { + return null; + } + return $this->data->type ?? null; } diff --git a/application/Espo/Core/Formula/FunctionFactory.php b/application/Espo/Core/Formula/FunctionFactory.php index 0911ba4a2c..0527ec9ec1 100644 --- a/application/Espo/Core/Formula/FunctionFactory.php +++ b/application/Espo/Core/Formula/FunctionFactory.php @@ -102,8 +102,7 @@ class FunctionFactory ]); if ( - property_exists($className, 'hasAttributeFetcher') || - method_exists($className, 'setAttributeFetcher') + method_exists($object, 'setAttributeFetcher') ) { $object->setAttributeFetcher($this->attributeFetcher); } diff --git a/application/Espo/Core/Formula/Functions/DatetimeGroup/ClosestType.php b/application/Espo/Core/Formula/Functions/DatetimeGroup/ClosestType.php index fd818982b2..45012f88b5 100644 --- a/application/Espo/Core/Formula/Functions/DatetimeGroup/ClosestType.php +++ b/application/Espo/Core/Formula/Functions/DatetimeGroup/ClosestType.php @@ -60,11 +60,13 @@ class ClosestType extends BaseFunction implements Di\ConfigAware } $inPast = false; + if (count($args) > 3) { $inPast = $args[3]; } $timezone = null; + if (count($args) > 4) { $timezone = $args[4]; } @@ -82,6 +84,7 @@ class ClosestType extends BaseFunction implements Di\ConfigAware } $isDate = false; + if (strlen($value) === 10) { $isDate = true; $value .= ' 00:00:00'; @@ -95,20 +98,26 @@ class ClosestType extends BaseFunction implements Di\ConfigAware /** @var DateTime */ $dt = DateTime::createFromFormat($format, $value, new DateTimeZone($timezone)); + $valueTimestamp = $dt->getTimestamp(); if ($type === 'time') { if (!is_string($target)) { $this->throwBadArgumentType(3, 'string'); } + list($hour, $minute) = explode(':', $target); + if (!$hour) { $hour = 0; } + if (!$minute) { $minute = 0; } - $dt->setTime($hour, $minute, 0); + + $dt->setTime((int) $hour, (int) $minute, 0); + if ($valueTimestamp < $dt->getTimestamp()) { if ($inPast) { $dt->modify('-1 day'); @@ -118,32 +127,39 @@ class ClosestType extends BaseFunction implements Di\ConfigAware $dt->modify('+1 day'); } } - } else if ($type === 'hour') { + } + else if ($type === 'hour') { $target = intval($target); $dt->setTime($target, 0, 0); + if ($valueTimestamp < $dt->getTimestamp()) { if ($inPast) { $dt->modify('-1 day'); } - } else if ($valueTimestamp > $dt->getTimestamp()) { + } + else if ($valueTimestamp > $dt->getTimestamp()) { if (!$inPast) { $dt->modify('+1 day'); } } - } else if ($type === 'minute') { + } + else if ($type === 'minute') { $target = intval($target); + $dt->setTime(intval($dt->format('G')), intval($target), 0); if ($valueTimestamp < $dt->getTimestamp()) { if ($inPast) { $dt->modify('-1 hour'); } - } else if ($valueTimestamp > $dt->getTimestamp()) { + } + else if ($valueTimestamp > $dt->getTimestamp()) { if (!$inPast) { $dt->modify('+1 hour'); } } - } else if ($type === 'dayOfWeek') { + } + else if ($type === 'dayOfWeek') { $target = intval($target); $dt->setTime(0, 0, 0); @@ -155,36 +171,45 @@ class ClosestType extends BaseFunction implements Di\ConfigAware if ($inPast) { $dt->modify('-1 week'); } - } else if ($valueTimestamp > $dt->getTimestamp()) { + } + else if ($valueTimestamp > $dt->getTimestamp()) { if (!$inPast) { $dt->modify('+1 week'); } } - } else if ($type === 'date') { + } + else if ($type === 'date') { $target = intval($target); $dt->setTime(0, 0, 0); if ($inPast) { while (true) { $date = intval($dt->format('d')); + if ($date === $target) { break; } + $dt->modify('-1 day'); } - } else { + } + else { if ($valueTimestamp > $dt->getTimestamp()) { $dt->modify('+1 day'); } + while (true) { $date = intval($dt->format('d')); + if ($date === $target) { break; } + $dt->modify('+1 day'); } } - } else if ($type === 'month') { + } + else if ($type === 'month') { $target = intval($target); $dt->setTime(0, 0, 0); @@ -194,20 +219,26 @@ class ClosestType extends BaseFunction implements Di\ConfigAware if ($inPast) { while (true) { $month = intval($dt->format('m')); + if ($month === $target) { break; } + $dt->modify('-1 month'); } - } else { + } + else { if ($valueTimestamp > $dt->getTimestamp()) { $dt->modify('+1 month'); } + while (true) { $month = intval($dt->format('m')); + if ($month === $target) { break; } + $dt->modify('+1 month'); } } @@ -219,9 +250,10 @@ class ClosestType extends BaseFunction implements Di\ConfigAware if (!$isDate) { $dt->setTimezone(new DateTimeZone('UTC')); + return $dt->format('Y-m-d H:i'); - } else { - return $dt->format('Y-m-d'); } + + return $dt->format('Y-m-d'); } } diff --git a/application/Espo/Core/Formula/Functions/ExtGroup/EmailGroup/ApplyTemplateType.php b/application/Espo/Core/Formula/Functions/ExtGroup/EmailGroup/ApplyTemplateType.php index ae1f897e9e..cb45b95934 100644 --- a/application/Espo/Core/Formula/Functions/ExtGroup/EmailGroup/ApplyTemplateType.php +++ b/application/Espo/Core/Formula/Functions/ExtGroup/EmailGroup/ApplyTemplateType.php @@ -99,6 +99,7 @@ class ApplyTemplateType extends BaseFunction implements return false; } + /** @var \Espo\Services\EmailTemplate */ $emailTemplateService = $this->serviceFactory->create('EmailTemplate'); $params = []; diff --git a/application/Espo/Core/Formula/Functions/ExtGroup/EmailGroup/SendType.php b/application/Espo/Core/Formula/Functions/ExtGroup/EmailGroup/SendType.php index 87739fe54a..452629f75f 100644 --- a/application/Espo/Core/Formula/Functions/ExtGroup/EmailGroup/SendType.php +++ b/application/Espo/Core/Formula/Functions/ExtGroup/EmailGroup/SendType.php @@ -75,7 +75,9 @@ class SendType extends BaseFunction implements return false; } + /** @var \Espo\Services\Email */ $service = $this->serviceFactory->create('Email'); + $service->loadAdditionalFields($email); $toSave = false; diff --git a/application/Espo/Core/Formula/Functions/JsonGroup/RetrieveType.php b/application/Espo/Core/Formula/Functions/JsonGroup/RetrieveType.php index 1bf30b6443..c158c938bb 100644 --- a/application/Espo/Core/Formula/Functions/JsonGroup/RetrieveType.php +++ b/application/Espo/Core/Formula/Functions/JsonGroup/RetrieveType.php @@ -73,6 +73,7 @@ class RetrieveType extends BaseFunction */ private function splitPath(string $path): array { + /** @var string[] */ $pathArray = preg_split('/(? $item) { diff --git a/application/Espo/Core/Formula/Functions/NumberGroup/CeilType.php b/application/Espo/Core/Formula/Functions/NumberGroup/CeilType.php index 983c217f0a..1788251925 100644 --- a/application/Espo/Core/Formula/Functions/NumberGroup/CeilType.php +++ b/application/Espo/Core/Formula/Functions/NumberGroup/CeilType.php @@ -48,6 +48,6 @@ class CeilType extends BaseFunction return null; } - return intval(ceil($value)); + return intval(ceil((float) $value)); } } diff --git a/application/Espo/Core/Formula/Functions/NumberGroup/FloorType.php b/application/Espo/Core/Formula/Functions/NumberGroup/FloorType.php index dedecdcfca..357dc0ea5f 100644 --- a/application/Espo/Core/Formula/Functions/NumberGroup/FloorType.php +++ b/application/Espo/Core/Formula/Functions/NumberGroup/FloorType.php @@ -48,6 +48,6 @@ class FloorType extends BaseFunction return null; } - return intval(floor($value)); + return intval(floor((float) $value)); } } diff --git a/application/Espo/Core/Formula/Functions/NumberGroup/RoundType.php b/application/Espo/Core/Formula/Functions/NumberGroup/RoundType.php index 013084864e..f304deefd2 100644 --- a/application/Espo/Core/Formula/Functions/NumberGroup/RoundType.php +++ b/application/Espo/Core/Formula/Functions/NumberGroup/RoundType.php @@ -45,6 +45,7 @@ class RoundType extends BaseFunction $value = $this->evaluate($args[0]); $precision = 0; + if (count($args) > 1) { $precision = $this->evaluate($args[1]); } @@ -53,6 +54,6 @@ class RoundType extends BaseFunction return null; } - return round($value, $precision); + return round((float) $value, $precision); } } diff --git a/application/Espo/Core/Formula/Functions/RecordGroup/CreateType.php b/application/Espo/Core/Formula/Functions/RecordGroup/CreateType.php index c489e150df..cd79ca960a 100644 --- a/application/Espo/Core/Formula/Functions/RecordGroup/CreateType.php +++ b/application/Espo/Core/Formula/Functions/RecordGroup/CreateType.php @@ -66,6 +66,8 @@ class CreateType extends BaseFunction implements $this->throwBadArgumentType($i + 1, 'string'); } + /** @var string $attribute */ + $value = $args[$i + 1]; $data[$attribute] = $value; diff --git a/application/Espo/Core/Formula/Functions/RecordGroup/UpdateType.php b/application/Espo/Core/Formula/Functions/RecordGroup/UpdateType.php index d0cc3e49d9..5cbd26a59a 100644 --- a/application/Espo/Core/Formula/Functions/RecordGroup/UpdateType.php +++ b/application/Espo/Core/Formula/Functions/RecordGroup/UpdateType.php @@ -55,6 +55,7 @@ class UpdateType extends BaseFunction implements if (!is_string($entityType)) { $this->throwBadArgumentType(1, 'string'); } + if (!is_string($id)) { $this->throwBadArgumentType(2, 'string'); } @@ -62,13 +63,20 @@ class UpdateType extends BaseFunction implements $data = []; $i = 2; + while ($i < count($args) - 1) { $attribute = $args[$i]; + if (!is_string($entityType)) { $this->throwBadArgumentType($i + 1, 'string'); } + + /** @var string $attribute */ + $value = $args[$i + 1]; + $data[$attribute] = $value; + $i = $i + 2; }