From 17c2204dd32f03baa39f67ce2ea43979dd4a6448 Mon Sep 17 00:00:00 2001 From: Yuri Kuznetsov Date: Tue, 8 Mar 2022 21:17:23 +0200 Subject: [PATCH] type fixes --- .../Core/Utils/Database/Orm/Converter.php | 3 +- application/Espo/Core/Utils/Metadata.php | 77 +++++++++++-------- .../Espo/Core/Utils/Metadata/Helper.php | 49 ++++++------ .../Core/Utils/Metadata/OrmMetadataData.php | 27 +++++-- application/Espo/Core/Utils/Util.php | 3 +- 5 files changed, 91 insertions(+), 68 deletions(-) diff --git a/application/Espo/Core/Utils/Database/Orm/Converter.php b/application/Espo/Core/Utils/Database/Orm/Converter.php index a2da88c35f..6ffc763dbe 100644 --- a/application/Espo/Core/Utils/Database/Orm/Converter.php +++ b/application/Espo/Core/Utils/Database/Orm/Converter.php @@ -400,8 +400,7 @@ class Converter if (isset($fieldTypeMetadata['linkDefs'])) { $linkDefs = $this->getMetadataHelper()->getLinkDefsInFieldMeta( $entityType, - $attributeParams, - $fieldTypeMetadata['linkDefs'] + $attributeParams ); if (isset($linkDefs)) { diff --git a/application/Espo/Core/Utils/Metadata.php b/application/Espo/Core/Utils/Metadata.php index 9da0bd391a..9f761bc1ab 100644 --- a/application/Espo/Core/Utils/Metadata.php +++ b/application/Espo/Core/Utils/Metadata.php @@ -46,31 +46,40 @@ use stdClass; */ class Metadata { - private $data = null; + /** + * @var ?array + */ + private ?array $data = null; - private $objData = null; + private ?stdClass $objData = null; - private $useCache; + private bool $useCache; - private $cacheKey = 'metadata'; + private string $cacheKey = 'metadata'; - private $objCacheKey = 'objMetadata'; + private string $objCacheKey = 'objMetadata'; - private $customPath = 'custom/Espo/Custom/Resources/metadata'; + private string $customPath = 'custom/Espo/Custom/Resources/metadata'; + /** + * @var array> + */ private $deletedData = []; + /** + * @var array> + */ private $changedData = []; - private $metadataHelper; + private Helper $metadataHelper; - private $module; + private Module $module; - private $fileManager; + private FileManager $fileManager; - private $dataCache; + private DataCache $dataCache; - private $resourceReader; + private ResourceReader $resourceReader; public function __construct( FileManager $fileManager, @@ -125,7 +134,7 @@ class Metadata /** * Get metadata array. * - * @return array + * @return array */ private function getData(): array { @@ -139,9 +148,8 @@ class Metadata /** * Get metadata by key. * - * @param string|array $key + * @param string|string[] $key * @param mixed $default - * * @return mixed */ public function get($key = null, $default = null) @@ -156,8 +164,7 @@ class Metadata * * @param bool $isJSON * @param bool $reload - * - * @return array|string + * @return array|string */ public function getAll(bool $isJSON = false, bool $reload = false) { @@ -205,9 +212,8 @@ class Metadata /** * Get metadata with stdClass items. * - * @param string|array $key + * @param string|string[] $key * @param mixed $default - * * @return mixed */ public function getObjects($key = null, $default = null) @@ -217,7 +223,10 @@ class Metadata return Util::getValueByKey($objData, $key, $default); } - public function getAllObjects($isJSON = false, $reload = false) + /** + * @return stdClass|string + */ + public function getAllObjects(bool $isJSON = false, bool $reload = false) { $objData = $this->getObjData($reload); @@ -241,6 +250,11 @@ class Metadata return $data; } + /** + * + * @param string[] $row + * @param stdClass $data + */ private function removeDataByPath($row, &$data): void { $p = &$data; @@ -254,7 +268,8 @@ class Metadata if ($item === '__ANY__') { foreach (get_object_vars($p) as &$v) { $this->removeDataByPath( - array_slice($row, $i + 1), $v + array_slice($row, $i + 1), + $v ); } @@ -355,13 +370,9 @@ class Metadata /** * Get metadata definition in custom directory. * - * @param string $key1 - * @param string $key2 * @param mixed $default - * - * @return stdClass */ - public function getCustom($key1, $key2, $default = null) + public function getCustom(string $key1, string $key2, $default = null): stdClass { $filePath = $this->customPath . "/{$key1}/{$key2}.json"; @@ -378,15 +389,12 @@ class Metadata * Set and save metadata in custom directory. * The data is not merging with existing data. Use getCustom() to get existing data. * - * @param string $key1 - * @param string $key2 - * @param array|stdClass $data + * @param array|stdClass $data */ public function saveCustom(string $key1, string $key2, $data): void { if (is_object($data)) { - /** @phpstan-ignore-next-line */ - foreach ($data as $key => $item) { + foreach (get_object_vars($data) as $key => $item) { if ($item == new stdClass()) { unset($data->$key); } @@ -404,6 +412,8 @@ class Metadata /** * Set Metadata data. + * + * @param array|scalar|null $data */ public function set(string $key1, string $key2, $data): void { @@ -430,7 +440,7 @@ class Metadata /** * Unset some fields and other stuff in metadata. * - * @param array|string $unsets Example: `fields.name`. + * @param string[]|string $unsets Example: `fields.name`. */ public function delete(string $key1, string $key2, $unsets = null): void { @@ -489,7 +499,10 @@ class Metadata $this->data = Util::unsetInArray($this->getData(), $metadataUnsetData, true); } - private function undelete($key1, $key2, $data): void + /** + * @param stdClass|array $data + */ + private function undelete(string $key1, string $key2, $data): void { if (isset($this->deletedData[$key1][$key2])) { foreach ($this->deletedData[$key1][$key2] as $unsetIndex => $unsetItem) { diff --git a/application/Espo/Core/Utils/Metadata/Helper.php b/application/Espo/Core/Utils/Metadata/Helper.php index fa2cc07eaa..7144f051ce 100644 --- a/application/Espo/Core/Utils/Metadata/Helper.php +++ b/application/Espo/Core/Utils/Metadata/Helper.php @@ -30,17 +30,20 @@ namespace Espo\Core\Utils\Metadata; use Espo\Core\Utils\Util; +use Espo\Core\Utils\Metadata; class Helper { - private $metadata; + private Metadata $metadata; - protected $defaultNaming = 'postfix'; + protected string $defaultNaming = 'postfix'; /** * List of copied params for metadata -> 'fields' from parent items. + * + * @var string[] */ - protected $copiedDefParams = array( + protected $copiedDefParams = [ 'readOnly', 'disabled', 'notStorable', @@ -53,37 +56,36 @@ class Helper 'customizationDisabled', 'importDisabled', 'exportDisabled', - ); + ]; - public function __construct(\Espo\Core\Utils\Metadata $metadata) + public function __construct(Metadata $metadata) { $this->metadata = $metadata; } - protected function getMetadata() - { - return $this->metadata; - } - /** * Get field definition by type in metadata, "fields" key. * - * @param array|string $fieldDef - It can be a string or field definition from entityDefs - * @return array|null + * @param array|string $fieldDef It can be a string or field definition from entityDefs. + * @return ?array */ public function getFieldDefsByType($fieldDef) { if (is_string($fieldDef)) { - $fieldDef = array('type' => $fieldDef); + $fieldDef = ['type' => $fieldDef]; } if (isset($fieldDef['type'])) { - return $this->getMetadata()->get('fields.'.$fieldDef['type']); + return $this->metadata->get('fields.' . $fieldDef['type']); } return null; } + /** + * @param array|string $fieldDef + * @return ?array + */ public function getFieldDefsInFieldMeta($fieldDef) { $fieldDefsByType = $this->getFieldDefsByType($fieldDef); @@ -101,11 +103,10 @@ class Helper * Variables should be defined into fieldDefs (in 'entityDefs' metadata). * * @param string $entityName - * @param array $fieldDef - * @param array $linkFieldDefsByType - * @return array|null + * @param array|string $fieldDef + * @return ?array */ - public function getLinkDefsInFieldMeta($entityName, $fieldDef, array $linkFieldDefsByType = null) + public function getLinkDefsInFieldMeta($entityName, $fieldDef) { $fieldDefsByType = $this->getFieldDefsByType($fieldDef); @@ -115,11 +116,12 @@ class Helper $linkFieldDefsByType = $fieldDefsByType['linkDefs']; - foreach ($linkFieldDefsByType as $paramName => &$paramValue) { + foreach ($linkFieldDefsByType as &$paramValue) { if (preg_match('/{(.*?)}/', $paramValue, $matches)) { if (in_array($matches[1], array_keys($fieldDef))) { $value = $fieldDef[$matches[1]]; - } else if (strtolower($matches[1]) == 'entity') { + } + else if (strtolower($matches[1]) == 'entity') { $value = $entityName; } @@ -136,10 +138,9 @@ class Helper * Get additional field list based on field definition in metadata 'fields'. * * @param string $fieldName - * @param array $fieldParams - * @param array $definitionList - * - * @return array|null + * @param array $fieldParams + * @param array $definitionList + * @return ?array */ public function getAdditionalFieldList($fieldName, array $fieldParams, array $definitionList) { diff --git a/application/Espo/Core/Utils/Metadata/OrmMetadataData.php b/application/Espo/Core/Utils/Metadata/OrmMetadataData.php index 981f31fcd8..bf743c8aa6 100644 --- a/application/Espo/Core/Utils/Metadata/OrmMetadataData.php +++ b/application/Espo/Core/Utils/Metadata/OrmMetadataData.php @@ -40,21 +40,24 @@ use Espo\Core\{ class OrmMetadataData { + /** + * @var ?array> + */ protected $data = null; - protected $cacheKey = 'ormMetadata'; + protected string $cacheKey = 'ormMetadata'; - protected $useCache; + protected bool $useCache; - protected $metadata; + protected Metadata $metadata; - protected $fileManager; + protected FileManager $fileManager; - protected $dataCache; + protected DataCache $dataCache; - protected $config; + protected Config $config; - private $converter; + private ?Converter $converter = null; public function __construct( Metadata $metadata, @@ -67,7 +70,7 @@ class OrmMetadataData $this->dataCache = $dataCache; $this->config = $config; - $this->useCache = $this->config->get('useCache', false); + $this->useCache = (bool) $this->config->get('useCache', false); } protected function getConverter(): Converter @@ -79,6 +82,9 @@ class OrmMetadataData return $this->converter; } + /** + * @return array> + */ public function getData(bool $reload = false): array { if (isset($this->data) && !$reload) { @@ -100,6 +106,11 @@ class OrmMetadataData return $this->data; } + /** + * @param string|string[]|null $key + * @param mixed $default + * @return mixed + */ public function get($key = null, $default = null) { return Util::getValueByKey($this->getData(), $key, $default); diff --git a/application/Espo/Core/Utils/Util.php b/application/Espo/Core/Utils/Util.php index 1ccd2b67aa..0b96191938 100644 --- a/application/Espo/Core/Utils/Util.php +++ b/application/Espo/Core/Utils/Util.php @@ -498,7 +498,7 @@ class Util /** * Return values of defined $key. * - * @param mixed $data + * @param \stdClass|array $data * @param string[]|string $key Ex. of key is "entityDefs", "entityDefs.User". * @param mixed $default * @return mixed @@ -535,7 +535,6 @@ class Util return $default; } } - } return $item;