From 5ebd680d64eea3b457bf283a1ad69cf4c796ed78 Mon Sep 17 00:00:00 2001 From: Yuri Kuznetsov Date: Tue, 15 Mar 2022 12:31:02 +0200 Subject: [PATCH] type fixes --- .../Espo/Modules/Crm/Entities/Reminder.php | 15 +++++++ .../Crm/EntryPoints/CampaignTrackOpened.php | 2 +- .../Modules/Crm/EntryPoints/CampaignUrl.php | 12 +++++- .../Crm/EntryPoints/SubscribeAgain.php | 6 ++- .../Modules/Crm/EntryPoints/Unsubscribe.php | 6 ++- .../Modules/Crm/Jobs/SubmitPopupReminders.php | 9 +++-- .../Espo/Modules/Crm/Repositories/CaseObj.php | 7 +++- .../Modules/Crm/Repositories/Opportunity.php | 2 +- .../Espo/Modules/Crm/Services/Activities.php | 40 +++++++++++-------- .../Espo/Modules/Crm/Services/Campaign.php | 2 +- .../Espo/Modules/Crm/Services/Document.php | 2 +- .../Espo/Modules/Crm/Services/Lead.php | 23 ++++++----- .../Espo/Modules/Crm/Services/Meeting.php | 4 +- .../Espo/Modules/Crm/Services/Opportunity.php | 24 +++++------ 14 files changed, 103 insertions(+), 51 deletions(-) diff --git a/application/Espo/Modules/Crm/Entities/Reminder.php b/application/Espo/Modules/Crm/Entities/Reminder.php index ad49dd7bbd..bd9aa9ee59 100644 --- a/application/Espo/Modules/Crm/Entities/Reminder.php +++ b/application/Espo/Modules/Crm/Entities/Reminder.php @@ -32,4 +32,19 @@ namespace Espo\Modules\Crm\Entities; class Reminder extends \Espo\Core\ORM\Entity { public const ENTITY_TYPE = 'Reminder'; + + public function getUserId(): ?string + { + return $this->get('userId'); + } + + public function getTargetEntityId(): ?string + { + return $this->get('entityId'); + } + + public function getTargetEntityType(): ?string + { + return $this->get('entityType'); + } } diff --git a/application/Espo/Modules/Crm/EntryPoints/CampaignTrackOpened.php b/application/Espo/Modules/Crm/EntryPoints/CampaignTrackOpened.php index 3ea65fb7e1..c2b37d203f 100644 --- a/application/Espo/Modules/Crm/EntryPoints/CampaignTrackOpened.php +++ b/application/Espo/Modules/Crm/EntryPoints/CampaignTrackOpened.php @@ -65,7 +65,7 @@ class CampaignTrackOpened implements EntryPoint { $id = $request->getQueryParam('id'); - if (!$id) { + if (!$id || !is_string($id)) { throw new BadRequest(); } diff --git a/application/Espo/Modules/Crm/EntryPoints/CampaignUrl.php b/application/Espo/Modules/Crm/EntryPoints/CampaignUrl.php index fbab3154e4..37fadd6ca5 100644 --- a/application/Espo/Modules/Crm/EntryPoints/CampaignUrl.php +++ b/application/Espo/Modules/Crm/EntryPoints/CampaignUrl.php @@ -109,7 +109,7 @@ class CampaignUrl implements EntryPoint $hash = $request->getQueryParam('hash') ?? null; $uid = $request->getQueryParam('uid') ?? null; - if (!$trackingUrlId) { + if (!$trackingUrlId || !is_string($trackingUrlId)) { throw new BadRequest(); } @@ -120,13 +120,21 @@ class CampaignUrl implements EntryPoint } if ($emailAddress && $hash) { + if (!is_string($emailAddress) || !is_string($hash)) { + throw new BadRequest(); + } + $this->processWithHash($trackingUrl, $emailAddress, $hash); } else if ($uid && $hash) { + if (!is_string($uid) || !is_string($hash)) { + throw new BadRequest(); + } + $this->processWithUniqueId($trackingUrl, $uid, $hash); } else { - if (!$queueItemId) { + if (!$queueItemId || !is_string($queueItemId)) { throw new BadRequest(); } diff --git a/application/Espo/Modules/Crm/EntryPoints/SubscribeAgain.php b/application/Espo/Modules/Crm/EntryPoints/SubscribeAgain.php index c5802c7154..6f6a04fbe5 100644 --- a/application/Espo/Modules/Crm/EntryPoints/SubscribeAgain.php +++ b/application/Espo/Modules/Crm/EntryPoints/SubscribeAgain.php @@ -91,12 +91,16 @@ class SubscribeAgain implements EntryPoint $hash = $request->getQueryParam('hash') ?? null; if ($emailAddress && $hash) { + if (!is_string($emailAddress) || !is_string($hash)) { + throw new BadRequest(); + } + $this->processWithHash($emailAddress, $hash); return; } - if (!$id) { + if (!$id || !is_string($id)) { throw new BadRequest(); } diff --git a/application/Espo/Modules/Crm/EntryPoints/Unsubscribe.php b/application/Espo/Modules/Crm/EntryPoints/Unsubscribe.php index 9ab46b769a..c5b2fc2950 100644 --- a/application/Espo/Modules/Crm/EntryPoints/Unsubscribe.php +++ b/application/Espo/Modules/Crm/EntryPoints/Unsubscribe.php @@ -96,12 +96,16 @@ class Unsubscribe implements EntryPoint $hash = $request->getQueryParam('hash') ?? null; if ($emailAddress && $hash) { + if (!is_string($emailAddress) || !is_string($hash)) { + throw new BadRequest(); + } + $this->processWithHash($emailAddress, $hash); return; } - if (!$id) { + if (!$id || !is_string($id)) { throw new BadRequest(); } diff --git a/application/Espo/Modules/Crm/Jobs/SubmitPopupReminders.php b/application/Espo/Modules/Crm/Jobs/SubmitPopupReminders.php index fa3bd50fc9..094905b0f5 100644 --- a/application/Espo/Modules/Crm/Jobs/SubmitPopupReminders.php +++ b/application/Espo/Modules/Crm/Jobs/SubmitPopupReminders.php @@ -95,9 +95,9 @@ class SubmitPopupReminders implements JobDataLess $submitData = []; foreach ($reminderList as $reminder) { - $userId = $reminder->get('userId'); - $entityType = $reminder->get('entityType'); - $entityId = $reminder->get('entityId'); + $userId = $reminder->getUserId(); + $entityType = $reminder->getTargetEntityId(); + $entityId = $reminder->getTargetEntityId(); if (!$userId || !$entityType || !$entityId) { $this->deleteReminder($reminder); @@ -160,7 +160,8 @@ class SubmitPopupReminders implements JobDataLess $this->webSocketSubmission->submit('popupNotifications.event', $userId, (object) [ 'list' => $list ]); - } catch (Throwable $e) { + } + catch (Throwable $e) { $this->log->error('Job SubmitPopupReminders: [' . $e->getCode() . '] ' .$e->getMessage()); } } diff --git a/application/Espo/Modules/Crm/Repositories/CaseObj.php b/application/Espo/Modules/Crm/Repositories/CaseObj.php index 38cec659f3..a343fdd5ac 100644 --- a/application/Espo/Modules/Crm/Repositories/CaseObj.php +++ b/application/Espo/Modules/Crm/Repositories/CaseObj.php @@ -52,7 +52,12 @@ class CaseObj extends \Espo\Core\Repositories\Database implements protected function getStreamService(): StreamService { - $this->streamService = $this->streamService ?? $this->serviceFactory->create('Stream'); + if (!$this->streamService) { + /** @var StreamService */ + $service = $this->serviceFactory->create('Stream'); + + $this->streamService = $service; + } return $this->streamService; } diff --git a/application/Espo/Modules/Crm/Repositories/Opportunity.php b/application/Espo/Modules/Crm/Repositories/Opportunity.php index 604fb86e7b..398c85cf57 100644 --- a/application/Espo/Modules/Crm/Repositories/Opportunity.php +++ b/application/Espo/Modules/Crm/Repositories/Opportunity.php @@ -119,7 +119,7 @@ class Opportunity extends Database } $probability = $this->metadata - ->get(['entityDefs', 'Opportunity', 'fields', 'stage', 'probabilityMap', $entity->getStage()]) ?? 0; + ->get(['entityDefs', 'Opportunity', 'fields', 'stage', 'probabilityMap', $entity->getStage() ?? '']) ?? 0; if ($probability) { $entity->set('lastStage', $entity->getStage()); diff --git a/application/Espo/Modules/Crm/Services/Activities.php b/application/Espo/Modules/Crm/Services/Activities.php index d2dc4364cf..14595a8eab 100644 --- a/application/Espo/Modules/Crm/Services/Activities.php +++ b/application/Espo/Modules/Crm/Services/Activities.php @@ -728,7 +728,7 @@ class Activities implements $sth = $this->entityManager->getQueryExecutor()->execute($unionQuery); - $rowList = $sth->fetchAll(PDO::FETCH_ASSOC); + $rowList = $sth->fetchAll(PDO::FETCH_ASSOC) ?: []; $boolAttributeList = ['hasAttachment']; @@ -942,20 +942,23 @@ class Activities implements $this->accessCheck($entity); - $fetchAll = empty($params['scope']); + $targetScope = $params['scope'] ?? null; - if (!$fetchAll) { - if (!$this->metadata->get(['scopes', $params['scope'], 'activity'])) { - throw new Error('Entity \'' . $params['scope'] . '\' is not an activity'); + $fetchAll = empty($targetScope); + + if ($targetScope) { + if (!$this->metadata->get(['scopes', $targetScope, 'activity'])) { + throw new Error('Entity \'' . $targetScope . '\' is not an activity'); } } $parts = []; + /** @var string[] */ $entityTypeList = $this->config->get('activitiesEntityList', ['Meeting', 'Call']); foreach ($entityTypeList as $entityType) { - if (!$fetchAll && $params['scope'] !== $entityType) { + if (!$fetchAll && $targetScope !== $entityType) { continue; } @@ -997,19 +1000,23 @@ class Activities implements $this->accessCheck($entity); - $fetchAll = empty($params['scope']); + $targetScope = $params['scope'] ?? null; - if (!$fetchAll) { - if (!$this->metadata->get(['scopes', $params['scope'], 'activity'])) { - throw new Error('Entity \'' . $params['scope'] . '\' is not an activity'); + $fetchAll = empty($targetScope); + + if ($targetScope) { + if (!$this->metadata->get(['scopes', $targetScope, 'activity'])) { + throw new Error('Entity \'' . $targetScope . '\' is not an activity'); } } $parts = []; + + /** @var string[] */ $entityTypeList = $this->config->get('historyEntityList', ['Meeting', 'Call', 'Email']); foreach ($entityTypeList as $entityType) { - if (!$fetchAll && $params['scope'] !== $entityType) { + if (!$fetchAll && $targetScope !== $entityType) { continue; } @@ -1878,7 +1885,7 @@ class Activities implements $sth = $this->entityManager->getQueryExecutor()->execute($unionQuery); - $rowList = $sth->fetchAll(PDO::FETCH_ASSOC); + $rowList = $sth->fetchAll(PDO::FETCH_ASSOC) ?: []; return $rowList; } @@ -2060,7 +2067,8 @@ class Activities implements ]; } - $builder = $this->entityManager->getQueryBuilder() + $builder = $this->entityManager + ->getQueryBuilder() ->union(); foreach ($queryList as $query) { @@ -2081,8 +2089,8 @@ class Activities implements $totalCount = $row['count']; - $offset = intval($params['offset']); - $maxSize = intval($params['maxSize']); + $offset = intval($params['offset'] ?? 0); + $maxSize = intval($params['maxSize'] ?? 0); $unionQuery = $builder ->order('dateStart') @@ -2093,7 +2101,7 @@ class Activities implements $sth = $this->entityManager->getQueryExecutor()->execute($unionQuery); - $rows = $sth->fetchAll(PDO::FETCH_ASSOC); + $rows = $sth->fetchAll(PDO::FETCH_ASSOC) ?: []; $entityDataList = []; diff --git a/application/Espo/Modules/Crm/Services/Campaign.php b/application/Espo/Modules/Crm/Services/Campaign.php index b3a16b5f22..3bc9ba9ee8 100644 --- a/application/Espo/Modules/Crm/Services/Campaign.php +++ b/application/Espo/Modules/Crm/Services/Campaign.php @@ -502,7 +502,7 @@ class Campaign extends Record implements } $filename = $campaign->get('name') . ' - ' . - $this->defaultLanguage->translate($targetEntityType, 'scopeNamesPlural'); + $this->defaultLanguage->translateLabel($targetEntityType, 'scopeNamesPlural'); return $this->getPdfService()->generateMailMerge( $targetEntityType, diff --git a/application/Espo/Modules/Crm/Services/Document.php b/application/Espo/Modules/Crm/Services/Document.php index 8c0776cc65..d5feda2e60 100644 --- a/application/Espo/Modules/Crm/Services/Document.php +++ b/application/Espo/Modules/Crm/Services/Document.php @@ -67,7 +67,7 @@ class Document extends Record $attachment = $this->getAttachmentRepository()->getCopiedAttachment($file, 'Attachment'); - /** @var \Espo\ORM\Collection $attachmentList */ + /** @var \Espo\ORM\EntityCollection $attachmentList */ $attachmentList = $this->entityManager ->getCollectionFactory() ->create('Attachment'); diff --git a/application/Espo/Modules/Crm/Services/Lead.php b/application/Espo/Modules/Crm/Services/Lead.php index 1a79e15ca4..c8ed39d066 100644 --- a/application/Espo/Modules/Crm/Services/Lead.php +++ b/application/Espo/Modules/Crm/Services/Lead.php @@ -32,6 +32,8 @@ namespace Espo\Modules\Crm\Services; use Espo\Core\Exceptions\Forbidden; use Espo\Core\Exceptions\ConflictSilent; +use Espo\Core\Utils\Json; + use Espo\ORM\Entity; use Espo\Modules\Crm\Entities\Lead as LeadEntity; @@ -109,11 +111,13 @@ class Lead extends Record implements $data = []; - $entityList = $this->getMetadata()->get('entityDefs.Lead.convertEntityList', []); + /** @var string[] */ + $entityList = $this->metadata->get('entityDefs.Lead.convertEntityList', []); $ignoreAttributeList = ['createdAt', 'modifiedAt', 'modifiedById', 'modifiedByName', 'createdById', 'createdByName']; - $convertFieldsDefs = $this->getMetadata()->get('entityDefs.Lead.convertFields', []); + /** @var array> */ + $convertFieldsDefs = $this->metadata->get('entityDefs.Lead.convertFields', []); foreach ($entityList as $entityType) { if (!$this->getAcl()->checkScope($entityType, 'edit')) { @@ -121,26 +125,27 @@ class Lead extends Record implements } $attributes = []; - $fieldMap = []; - $fieldList = array_keys($this->getMetadata()->get('entityDefs.Lead.fields', [])); + /** @var string[] */ + $fieldList = array_keys($this->metadata->get('entityDefs.Lead.fields', [])); foreach ($fieldList as $field) { - if (!$this->getMetadata()->get('entityDefs.'.$entityType.'.fields.' . $field)) { + if (!$this->metadata->get('entityDefs.'.$entityType.'.fields.' . $field)) { continue; } if ( - $this->getMetadata()->get(['entityDefs', $entityType, 'fields', $field, 'type']) + $this->metadata->get(['entityDefs', $entityType, 'fields', $field, 'type']) !== - $this->getMetadata()->get(['entityDefs', 'Lead', 'fields', $field, 'type']) + $this->metadata->get(['entityDefs', 'Lead', 'fields', $field, 'type']) ) { continue; } $fieldMap[$field] = $field; } + if (array_key_exists($entityType, $convertFieldsDefs)) { foreach ($convertFieldsDefs[$entityType] as $field => $leadField) { $fieldMap[$field] = $leadField; @@ -148,7 +153,7 @@ class Lead extends Record implements } foreach ($fieldMap as $field => $leadField) { - $type = $this->getMetadata()->get(['entityDefs', 'Lead', 'fields', $field, 'type']); + $type = $this->metadata->get(['entityDefs', 'Lead', 'fields', $field, 'type']); if (in_array($type, ['file', 'image'])) { $attachment = $lead->get($field); @@ -355,7 +360,7 @@ class Lead extends Record implements 'duplicates' => $duplicateList, ]; - throw new ConflictSilent(json_encode($reason)); + throw new ConflictSilent(Json::encode($reason)); } $lead->set('status', 'Converted'); diff --git a/application/Espo/Modules/Crm/Services/Meeting.php b/application/Espo/Modules/Crm/Services/Meeting.php index 545d0c40de..9a7b156450 100644 --- a/application/Espo/Modules/Crm/Services/Meeting.php +++ b/application/Espo/Modules/Crm/Services/Meeting.php @@ -243,7 +243,9 @@ class Meeting extends Record implements { $userId = $userId ?? $this->getUser()->getId(); - $statusList = $this->getMetadata() + assert(is_string($this->entityType)); + + $statusList = $this->metadata ->get(['entityDefs', $this->entityType, 'fields', 'acceptanceStatus', 'options'], []); if (!in_array($status, $statusList)) { diff --git a/application/Espo/Modules/Crm/Services/Opportunity.php b/application/Espo/Modules/Crm/Services/Opportunity.php index badecb5a30..888bd93465 100644 --- a/application/Espo/Modules/Crm/Services/Opportunity.php +++ b/application/Espo/Modules/Crm/Services/Opportunity.php @@ -75,7 +75,7 @@ class Opportunity extends Record $lostStageList = $this->getLostStageList(); - $options = $this->getMetadata()->get('entityDefs.Opportunity.fields.stage.options', []); + $options = $this->metadata->get('entityDefs.Opportunity.fields.stage.options', []); $queryBuilder = $this->selectBuilderFactory ->create() @@ -126,7 +126,7 @@ class Opportunity extends Record ->getQueryExecutor() ->execute($queryBuilder->build()); - $rowList = $sth->fetchAll(PDO::FETCH_ASSOC); + $rowList = $sth->fetchAll(PDO::FETCH_ASSOC) ?: []; $data = []; @@ -138,7 +138,7 @@ class Opportunity extends Record $dataList = []; - $stageList = $this->getMetadata()->get('entityDefs.Opportunity.fields.stage.options', []); + $stageList = $this->metadata->get('entityDefs.Opportunity.fields.stage.options', []); foreach ($stageList as $stage) { if (in_array($stage, $lostStageList)) { @@ -178,7 +178,7 @@ class Opportunity extends Record list($dateFrom, $dateTo) = $this->getDateRangeByFilter($dateFilter); } - $options = $this->getMetadata()->get('entityDefs.Lead.fields.source.options', []); + $options = $this->metadata->get('entityDefs.Lead.fields.source.options', []); $queryBuilder = $this->selectBuilderFactory ->create() @@ -214,7 +214,7 @@ class Opportunity extends Record ->getQueryExecutor() ->execute($queryBuilder->build()); - $rowList = $sth->fetchAll(PDO::FETCH_ASSOC); + $rowList = $sth->fetchAll(PDO::FETCH_ASSOC) ?: []; $result = []; @@ -241,7 +241,7 @@ class Opportunity extends Record list($dateFrom, $dateTo) = $this->getDateRangeByFilter($dateFilter); } - $options = $this->getMetadata()->get('entityDefs.Opportunity.fields.stage.options', []); + $options = $this->metadata->get('entityDefs.Opportunity.fields.stage.options', []); $queryBuilder = $this->selectBuilderFactory ->create() @@ -278,7 +278,7 @@ class Opportunity extends Record ->getQueryExecutor() ->execute($queryBuilder->build()); - $rowList = $sth->fetchAll(PDO::FETCH_ASSOC); + $rowList = $sth->fetchAll(PDO::FETCH_ASSOC) ?: []; $result = []; @@ -355,7 +355,7 @@ class Opportunity extends Record $result = []; - $rowList = $sth->fetchAll(PDO::FETCH_ASSOC); + $rowList = $sth->fetchAll(PDO::FETCH_ASSOC) ?: []; foreach ($rowList as $row) { $month = $row['month']; @@ -536,11 +536,11 @@ class Opportunity extends Record { $lostStageList = []; - $probabilityMap = $this->getMetadata()->get( + $probabilityMap = $this->metadata->get( ['entityDefs', OpportunityEntity::ENTITY_TYPE, 'fields', 'stage', 'probabilityMap'], [] ); - $stageList = $this->getMetadata()->get('entityDefs.Opportunity.fields.stage.options', []); + $stageList = $this->metadata->get('entityDefs.Opportunity.fields.stage.options', []); foreach ($stageList as $stage) { if (empty($probabilityMap[$stage])) { @@ -558,11 +558,11 @@ class Opportunity extends Record { $wonStageList = []; - $probabilityMap = $this->getMetadata()->get( + $probabilityMap = $this->metadata->get( ['entityDefs', OpportunityEntity::ENTITY_TYPE, 'fields', 'stage', 'probabilityMap'], [] ); - $stageList = $this->getMetadata()->get('entityDefs.Opportunity.fields.stage.options', []); + $stageList = $this->metadata->get('entityDefs.Opportunity.fields.stage.options', []); foreach ($stageList as $stage) { if (!empty($probabilityMap[$stage]) && $probabilityMap[$stage] == 100) {