From f5d324eb4853a309652f28ea88bda7f4bf3ce4c2 Mon Sep 17 00:00:00 2001 From: Yuri Kuznetsov Date: Wed, 3 Nov 2021 11:39:41 +0200 Subject: [PATCH] type fixes --- application/Espo/Services/Record.php | 19 ++++++++- application/Espo/Services/User.php | 39 +++++++++++++------ application/Espo/Services/UserSecurity.php | 3 ++ .../EmailNotification/AssignmentProcessor.php | 5 +++ .../Tools/EmailNotification/Processor.php | 15 +++++-- .../Espo/Tools/EmailTemplate/Processor.php | 16 ++++++-- .../Tools/FieldManager/Hooks/NumberType.php | 10 ++--- 7 files changed, 82 insertions(+), 25 deletions(-) diff --git a/application/Espo/Services/Record.php b/application/Espo/Services/Record.php index f416b1afea..bd1db469d9 100644 --- a/application/Espo/Services/Record.php +++ b/application/Espo/Services/Record.php @@ -135,6 +135,8 @@ class Record extends RecordService implements /** * @deprecated Use `$this->config`. + * + * @return \Espo\Core\Utils\Config */ protected function getConfig() { @@ -143,6 +145,8 @@ class Record extends RecordService implements /** * @deprecated Use `$this->serviceFactory`. + * + * @return \Espo\Core\ServiceFactory */ protected function getServiceFactory() { @@ -159,6 +163,8 @@ class Record extends RecordService implements /** * @deprecated Use `$this->acl`. + * + * @return \Espo\Core\Acl */ protected function getAcl() { @@ -167,14 +173,17 @@ class Record extends RecordService implements /** * @deprecated Use `$this->user`. + * + * @return \Espo\Entities\User */ protected function getUser() { return $this->user; } -/** + /** * @deprecated Use `$this->aclManager`. + * @return \Espo\Core\AclManager */ protected function getAclManager() { @@ -183,6 +192,8 @@ class Record extends RecordService implements /** * @deprecated Use `$this->fileManager`. + * + * @return \Espo\Core\Utils\File\Manager */ protected function getFileManager() { @@ -191,6 +202,8 @@ class Record extends RecordService implements /** * @deprecated Use `$this->metadata`. + * + * @return \Espo\Core\Utils\Metadata */ protected function getMetadata() { @@ -207,6 +220,8 @@ class Record extends RecordService implements /** * @deprecated Use `$this->entityManager`. + * + * @return \Espo\ORM\EntityManager */ protected function getEntityManager() { @@ -243,6 +258,8 @@ class Record extends RecordService implements /** * @deprecated Use `$this->recordServiceContainer->get($name)`. + * + * @return \Espo\Core\Record\Service */ protected function getRecordService($name) { diff --git a/application/Espo/Services/User.php b/application/Espo/Services/User.php index 2d95c10e6a..46586e9a0e 100644 --- a/application/Espo/Services/User.php +++ b/application/Espo/Services/User.php @@ -30,6 +30,7 @@ namespace Espo\Services; use Espo\Entities\User as UserEntity; +use Espo\Entities\Email as EmailEntity; use Espo\Core\{ Exceptions\Forbidden, @@ -101,6 +102,7 @@ class User extends Record implements ?string $currentPassword = null ): void { + /** @var UserEntity $user */ $user = $this->getEntityManager()->getEntity('User', $userId); if (!$user) { @@ -123,9 +125,9 @@ class User extends Record implements $passwordHash = new PasswordHash($this->getConfig()); $u = $this->getEntityManager() - ->getRepository('User') + ->getRDBRepository('User') ->where([ - 'id' => $user->id, + 'id' => $user->getId(), 'password' => $passwordHash->hash($currentPassword), ]) ->findOne(); @@ -290,6 +292,7 @@ class User extends Record implements $data->password = $this->hashPassword($data->password); } + /** @var UserEntity $user */ $user = parent::create($data, $params); if (!is_null($newPassword) && !empty($data->sendAccessInfo)) { @@ -328,6 +331,7 @@ class User extends Record implements unset($data->type); } + /** @var UserEntity $user */ $user = parent::update($id, $data, $params); if (!is_null($newPassword)) { @@ -344,6 +348,8 @@ class User extends Record implements public function prepareEntityForOutput(Entity $entity) { + assert($entity instanceof UserEntity); + parent::prepareEntityForOutput($entity); if ($entity->isApi()) { @@ -368,6 +374,7 @@ class User extends Record implements public function generateNewApiKeyForEntity(string $id): Entity { + /** @var UserEntity $entity */ $entity = $this->getEntity($id); if (!$entity) { @@ -407,6 +414,7 @@ class User extends Record implements } } + /** @var UserEntity $user */ $user = $this->getEntity($id); if (!$user) { @@ -475,7 +483,7 @@ class User extends Record implements protected function getInternalUserCount() { return $this->getEntityManager() - ->getRepository('User') + ->getRDBRepository('User') ->where([ 'isActive' => true, 'type' => ['admin', 'regular'], @@ -487,7 +495,7 @@ class User extends Record implements protected function getPortalUserCount() { return $this->getEntityManager() - ->getRepository('User') + ->getRDBRepository('User') ->where([ 'isActive' => true, 'type' => 'portal', @@ -497,6 +505,8 @@ class User extends Record implements protected function beforeCreateEntity(Entity $entity, $data) { + /** @var UserEntity $entity */ + if ( $this->getConfig()->get('userLimit') && !$this->getUser()->isSuperAdmin() && @@ -548,6 +558,8 @@ class User extends Record implements protected function beforeUpdateEntity(Entity $entity, $data) { + /** @var UserEntity $entity */ + if ($this->getConfig()->get('userLimit') && !$this->getUser()->isSuperAdmin()) { if ( ( @@ -607,7 +619,7 @@ class User extends Record implements } } - protected function sendPassword(Entity $user, $password) + protected function sendPassword(UserEntity $user, $password) { $emailAddress = $user->get('emailAddress'); @@ -615,6 +627,7 @@ class User extends Record implements return; } + /** @var EmailEntity $email */ $email = $this->getEntityManager()->getEntity('Email'); if (!$this->emailSender->hasSystemSmtp() && !$this->getConfig()->get('internalSmtpServer')) { @@ -633,13 +646,13 @@ class User extends Record implements $urlList = []; - $portalList = $this->getEntityManager() - ->getRepository('Portal') + $portalList = $this->entityManager + ->getRDBRepository('Portal') ->distinct() ->join('users') ->where([ 'isActive' => true, - 'users.id' => $user->id, + 'users.id' => $user->getId(), ]) ->find(); @@ -650,12 +663,12 @@ class User extends Record implements else { $url = $siteUrl . 'portal/'; - if ($this->getConfig()->get('defaultPortalId') !== $portal->id) { + if ($this->getConfig()->get('defaultPortalId') !== $portal->getId()) { if ($portal->get('customId')) { $url .= $portal->get('customId'); } else { - $url .= $portal->id; + $url .= $portal->getId(); } } @@ -724,6 +737,8 @@ class User extends Record implements public function afterUpdateEntity(Entity $entity, $data) { + assert($entity instanceof UserEntity); + parent::afterUpdateEntity($entity, $data); if ( @@ -733,7 +748,7 @@ class User extends Record implements property_exists($data, 'portalRolesIds') || property_exists($data, 'portalsIds') ) { - $this->clearRoleCache($entity->id); + $this->clearRoleCache($entity->getId()); } if ( @@ -760,7 +775,7 @@ class User extends Record implements $contact->set('firstName', $data->firstName); } - if (array_key_exists('lastName', $data)) { + if (property_exists('lastName', $data)) { $contact->set('lastName', $data->lastName); } diff --git a/application/Espo/Services/UserSecurity.php b/application/Espo/Services/UserSecurity.php index bdb05f2f11..689f926630 100644 --- a/application/Espo/Services/UserSecurity.php +++ b/application/Espo/Services/UserSecurity.php @@ -87,6 +87,7 @@ class UserSecurity throw new Forbidden(); } + /** @var User $user */ $user = $this->entityManager->getEntity('User', $id); if (!$user) { @@ -113,6 +114,7 @@ class UserSecurity $isReset = $data->reset ?? false; + /** @var User $user */ $user = $this->entityManager->getEntity('User', $id); if (!$user) { @@ -161,6 +163,7 @@ class UserSecurity throw new Forbidden(); } + /** @var User $user */ $user = $this->entityManager->getEntity('User', $id); if (!$user) { diff --git a/application/Espo/Tools/EmailNotification/AssignmentProcessor.php b/application/Espo/Tools/EmailNotification/AssignmentProcessor.php index c08ea592ee..962828b61d 100644 --- a/application/Espo/Tools/EmailNotification/AssignmentProcessor.php +++ b/application/Espo/Tools/EmailNotification/AssignmentProcessor.php @@ -33,6 +33,8 @@ use Espo\Core\ORM\Entity; use Espo\ORM\EntityManager; +use Espo\Entities\User; + use Espo\Core\{ Htmlizer\HtmlizerFactory as HtmlizerFactory, Htmlizer\Htmlizer, @@ -104,6 +106,7 @@ class AssignmentProcessor throw new LogicException(); } + /** @var User $user */ $user = $this->entityManager->getEntity('User', $userId); if (!$user) { @@ -142,6 +145,8 @@ class AssignmentProcessor return; } + assert($entity instanceof Entity); + $this->loadParentNameFields($entity); if (!$entity->hasLinkMultipleField('assignedUsers')) { diff --git a/application/Espo/Tools/EmailNotification/Processor.php b/application/Espo/Tools/EmailNotification/Processor.php index 7c959e0b6f..389f459cfe 100644 --- a/application/Espo/Tools/EmailNotification/Processor.php +++ b/application/Espo/Tools/EmailNotification/Processor.php @@ -32,6 +32,8 @@ namespace Espo\Tools\EmailNotification; use Espo\Core\Notification\EmailNotificationHandler; use Espo\Core\Mail\SenderParams; +use Espo\Repositories\Portal as PortalRepository; + use Espo\{ ORM\Entity, ORM\EntityManager, @@ -187,7 +189,9 @@ class Processor $sql = $this->entityManager->getQueryComposer()->compose($unionQuery); - $notificationList = $this->entityManager->getRepository('Notification')->findBySql($sql); + $notificationList = $this->entityManager + ->getRDBRepository('Notification') + ->findBySql($sql); foreach ($notificationList as $notification) { $notification->set('emailIsProcessed', true); @@ -610,7 +614,7 @@ class Processor if ($portalId) { $portal = $this->entityManager->getEntity('Portal', $portalId); - $this->entityManager->getRepository('Portal')->loadUrlField($portal); + $this->getPortalRepository()->loadUrlField($portal); $this->userIdPortalCacheMap[$user->id] = $portal; } @@ -768,7 +772,7 @@ class Processor return; } - $emailRepository = $this->entityManager->getRepository('Email'); + $emailRepository = $this->entityManager->getRDBRepository('Email'); $eaList = $user->get('emailAddresses'); foreach ($eaList as $ea) { @@ -880,4 +884,9 @@ class Processor return $this->htmlizer; } + + private function getPortalRepository(): PortalRepository + { + return $this->entityManager->getRepository('Portal'); + } } diff --git a/application/Espo/Tools/EmailTemplate/Processor.php b/application/Espo/Tools/EmailTemplate/Processor.php index 2f030d3180..1d082ec809 100644 --- a/application/Espo/Tools/EmailTemplate/Processor.php +++ b/application/Espo/Tools/EmailTemplate/Processor.php @@ -45,6 +45,9 @@ use Espo\Core\Utils\DateTime as DateTimeUtil; use Espo\Entities\EmailTemplate; use Espo\Entities\User; use Espo\Entities\Attachment; +use Espo\Entities\EmailAddress; + +use Espo\Repositories\EmailAddress as EmailAddressRepository; use Exception; use DateTime; @@ -105,9 +108,9 @@ class Processor $foundByAddressEntity = null; if ($data->getEmailAddress()) { - $foundByAddressEntity = $this->entityManager - ->getRepository('EmailAddress') - ->getEntityByAddress($data->getEmailAddress(), + $foundByAddressEntity = $this->getEmailAddressRepository() + ->getEntityByAddress( + $data->getEmailAddress(), null, ['Contact', 'Lead', 'Account', 'User'] ); @@ -334,7 +337,7 @@ class Processor } $relatedEntity = $this->entityManager - ->getRepository($entity->getEntityType()) + ->getRDBRepository($entity->getEntityType()) ->getRelation($entity, $relation) ->findOne(); @@ -412,4 +415,9 @@ class Processor return $this->htmlizerFactory->createForUser($user); } + + private function getEmailAddressRepository(): EmailAddressRepository + { + return $this->entityManager->getRepository(EmailAddress::ENTITY_TYPE); + } } diff --git a/application/Espo/Tools/FieldManager/Hooks/NumberType.php b/application/Espo/Tools/FieldManager/Hooks/NumberType.php index b992a58b06..920d70408d 100644 --- a/application/Espo/Tools/FieldManager/Hooks/NumberType.php +++ b/application/Espo/Tools/FieldManager/Hooks/NumberType.php @@ -38,7 +38,7 @@ class NumberType implements Di\EntityManagerAware public function onRead($scope, $name, &$defs, $options) { $number = $this->entityManager - ->getRepository('NextNumber') + ->getRDBRepository('NextNumber') ->where([ 'entityType' => $scope, 'fieldName' => $name, @@ -68,9 +68,9 @@ class NumberType implements Di\EntityManagerAware return; } - $number = $this - ->entityManager - ->getRepository('NextNumber')->where([ + $number = $this->entityManager + ->getRDBRepository('NextNumber') + ->where([ 'entityType' => $scope, 'fieldName' => $name ]) @@ -91,7 +91,7 @@ class NumberType implements Di\EntityManagerAware public function afterRemove($scope, $name, $defs, $options) { $number = $this->entityManager - ->getRepository('NextNumber') + ->getRDBRepository('NextNumber') ->where([ 'entityType' => $scope, 'fieldName' => $name