From f347dd5140d2864a1d62f0b9eea2fccf51b77ee2 Mon Sep 17 00:00:00 2001 From: Jens Schuppe Date: Wed, 4 Dec 2024 15:59:18 +0100 Subject: [PATCH] WIP: Fix PHPStan issues --- Civi/Api4/Hiorg.php | 14 ++++---- .../Hiorg/Api4/Action/AbstractHiorgAction.php | 12 +++---- .../Api4/Action/AbstractHiorgApiAction.php | 34 +++++++++++-------- .../Api4/Action/AbstractSynchronizeAction.php | 5 ++- hiorg.php | 2 +- 5 files changed, 38 insertions(+), 29 deletions(-) diff --git a/Civi/Api4/Hiorg.php b/Civi/Api4/Hiorg.php index 5685497..e55c9d1 100644 --- a/Civi/Api4/Hiorg.php +++ b/Civi/Api4/Hiorg.php @@ -30,7 +30,7 @@ class Hiorg extends Generic\AbstractEntity { /** * @inheritDoc */ - public static function getFields($checkPermissions = TRUE): BasicGetFieldsAction { + public static function getFields(bool $checkPermissions = TRUE): BasicGetFieldsAction { return (new BasicGetFieldsAction( __CLASS__, __FUNCTION__, @@ -42,32 +42,32 @@ function(BasicGetFieldsAction $self) { ->setLoadOptions(TRUE); } - public static function getPersonal($checkPermissions = TRUE): GetPersonalApiAction { + public static function getPersonal(bool $checkPermissions = TRUE): GetPersonalApiAction { return (new GetPersonalApiAction(__CLASS__, __FUNCTION__)) ->setCheckPermissions($checkPermissions); } - public static function getAusbildungen($checkPermissions = TRUE): GetAusbildungenApiAction { + public static function getAusbildungen(bool $checkPermissions = TRUE): GetAusbildungenApiAction { return (new GetAusbildungenApiAction(__CLASS__, __FUNCTION__)) ->setCheckPermissions($checkPermissions); } - public static function getHelferstunden($checkPermissions = TRUE): GetHelferstundenApiAction { + public static function getHelferstunden(bool $checkPermissions = TRUE): GetHelferstundenApiAction { return (new GetHelferstundenApiAction(__CLASS__, __FUNCTION__)) ->setCheckPermissions($checkPermissions); } - public static function getUeberpruefungen($checkPermissions = TRUE): GetUeberpruefungenApiAction { + public static function getUeberpruefungen(bool $checkPermissions = TRUE): GetUeberpruefungenApiAction { return (new GetUeberpruefungenApiAction(__CLASS__, __FUNCTION__)) ->setCheckPermissions($checkPermissions); } - public static function synchronizeContacts($checkPermissions = TRUE): SynchronizeContactsAction { + public static function synchronizeContacts(bool $checkPermissions = TRUE): SynchronizeContactsAction { return (new SynchronizeContactsAction(__CLASS__, __FUNCTION__)) ->setCheckPermissions($checkPermissions); } - public static function synchronizeVolunteerHours($checkPermissions = TRUE): SynchronizeVolunteerHoursAction { + public static function synchronizeVolunteerHours(bool $checkPermissions = TRUE): SynchronizeVolunteerHoursAction { return (new SynchronizeVolunteerHoursAction(__CLASS__, __FUNCTION__)) ->setCheckPermissions($checkPermissions); } diff --git a/Civi/Hiorg/Api4/Action/AbstractHiorgAction.php b/Civi/Hiorg/Api4/Action/AbstractHiorgAction.php index 43952d3..a462bf7 100644 --- a/Civi/Hiorg/Api4/Action/AbstractHiorgAction.php +++ b/Civi/Hiorg/Api4/Action/AbstractHiorgAction.php @@ -22,7 +22,7 @@ use Civi\Hiorg\ConfigProfiles\ConfigProfile; /** - * @method int getConfigProfileId + * @method int getConfigProfileId() */ abstract class AbstractHiorgAction extends AbstractAction { @@ -46,28 +46,28 @@ abstract class AbstractHiorgAction extends AbstractAction { * @return static * @throws \Exception */ - public function setConfigProfileId($configProfileId) { + public function setConfigProfileId(int $configProfileId) { // parent::setConfigProfileId($configProfileId); is magic via // parent::__call() and can't be documented. parent::__call(__FUNCTION__, func_get_args()); // Load the profile. - $this->_configProfile = ConfigProfile::getById($this->configProfileId); + $this->_configProfile = ConfigProfile::getById($configProfileId); return $this; } - public function getConfigProfile() { + public function getConfigProfile(): ?ConfigProfile { return $this->_configProfile; } /** * @inheritDoc */ - abstract public function _run(Result $result); + abstract public function _run(Result $result): void; /** * Defines parameters for this API action. * - * @return array[] + * @phpstan-return list> */ public static function fields(): array { return [ diff --git a/Civi/Hiorg/Api4/Action/AbstractHiorgApiAction.php b/Civi/Hiorg/Api4/Action/AbstractHiorgApiAction.php index c5359cf..9d685db 100644 --- a/Civi/Hiorg/Api4/Action/AbstractHiorgApiAction.php +++ b/Civi/Hiorg/Api4/Action/AbstractHiorgApiAction.php @@ -25,22 +25,26 @@ abstract class AbstractHiorgApiAction extends AbstractHiorgAction { /** * The JSON-decoded HiOrg-Server API result. * - * @var object + * @var object|null */ - protected object $_response; + protected ?object $_response = NULL; /** * The HiOrg-Server API client. * - * @var \Civi\Hiorg\HiorgApi\HiorgClient + * @var \Civi\Hiorg\HiorgApi\HiorgClient|null */ - protected HiorgClient $_hiorgClient; + protected ?HiorgClient $_hiorgClient = NULL; /** * @inheritDoc */ public function _run(Result $result): void { - $this->_hiorgClient = new HiorgClient($this->getConfigProfile()); + $configProfile = $this->getConfigProfile(); + if (NULL === $configProfile) { + throw new \RuntimeException('Configuration Profile not set.'); + } + $this->_hiorgClient = new HiorgClient($configProfile); $this->doRun(); $this->formatResult($result); } @@ -69,21 +73,23 @@ protected function formatResult(Result $result): void { elseif (isset($this->_response->data)) { // Wrap single result in array for CiviCRM API to count correctly. $data = is_object($this->_response->data) ? [$this->_response->data] : $this->_response->data; - } - // Add data from included records to relationships. - if (!empty($this->_response->included)) { - foreach ($data as &$record) { - foreach ($record->relationships as &$relationship) { - self::addRelationshipIncludeData($relationship, $this->_response->included); + // Add data from included records to relationships. + if (!empty($this->_response->included)) { + foreach ($data as &$record) { + foreach ($record->relationships as &$relationship) { + self::addRelationshipIncludeData($relationship, $this->_response->included); + } } } + $result->exchangeArray($data); } - - $result->exchangeArray($data); } - protected static function addRelationshipIncludeData(\stdClass $relationship, array $included) { + /** + * @phpstan-param array}> $included + */ + protected static function addRelationshipIncludeData(\stdClass $relationship, array $included): void { foreach ($included as $include) { if ( $include->type == $relationship->data->type diff --git a/Civi/Hiorg/Api4/Action/AbstractSynchronizeAction.php b/Civi/Hiorg/Api4/Action/AbstractSynchronizeAction.php index 9306229..d690f43 100644 --- a/Civi/Hiorg/Api4/Action/AbstractSynchronizeAction.php +++ b/Civi/Hiorg/Api4/Action/AbstractSynchronizeAction.php @@ -80,6 +80,9 @@ protected function getQueue(): ?\CRM_Queue_Queue { : NULL; } + /** + * @phpstan-return list> + */ protected function runQueue(\CRM_Queue_Queue $queue, ?int $maxRunTime = NULL): array { $totalItems = $queue->getStatistic('total'); $runner = new \CRM_Queue_Runner([ @@ -129,7 +132,7 @@ private function validateTimeout(): ?int { || $this->timeout > $phpMaxExecutionTime ) ) { - throw new \Exception('The timeout exceeds the max_execution_time PHP setting value.'); + throw new \RuntimeException('The timeout exceeds the max_execution_time PHP setting value.'); } return isset($this->timeout) ? time() + $this->timeout : NULL; diff --git a/hiorg.php b/hiorg.php index fd474f4..8ad8601 100644 --- a/hiorg.php +++ b/hiorg.php @@ -29,7 +29,7 @@ * * @link https://docs.civicrm.org/dev/en/latest/hooks/hook_civicrm_config/ */ -function hiorg_civicrm_config(&$config): void { +function hiorg_civicrm_config(\CRM_Core_Config &$config): void { _hiorg_civix_civicrm_config($config); Civi::dispatcher()->addSubscriber(new OAuthProviderSubscriber());