Skip to content

Commit

Permalink
WIP: Fix PHPStan issues
Browse files Browse the repository at this point in the history
  • Loading branch information
jensschuppe committed Dec 4, 2024
1 parent 4edbe04 commit f347dd5
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 29 deletions.
14 changes: 7 additions & 7 deletions Civi/Api4/Hiorg.php
Original file line number Diff line number Diff line change
Expand Up @@ -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__,
Expand All @@ -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);
}
Expand Down
12 changes: 6 additions & 6 deletions Civi/Hiorg/Api4/Action/AbstractHiorgAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
use Civi\Hiorg\ConfigProfiles\ConfigProfile;

/**
* @method int getConfigProfileId
* @method int getConfigProfileId()
*/
abstract class AbstractHiorgAction extends AbstractAction {

Expand All @@ -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<array<string, mixed>>
*/
public static function fields(): array {
return [
Expand Down
34 changes: 20 additions & 14 deletions Civi/Hiorg/Api4/Action/AbstractHiorgApiAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down Expand Up @@ -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)) {

Check failure on line 78 in Civi/Hiorg/Api4/Action/AbstractHiorgApiAction.php

View workflow job for this annotation

GitHub Actions / PHPStan with PHP 8.3 prefer-stable

Construct empty() is not allowed. Use more strict comparison.

Check failure on line 78 in Civi/Hiorg/Api4/Action/AbstractHiorgApiAction.php

View workflow job for this annotation

GitHub Actions / PHPStan with PHP 8.0 prefer-lowest

Construct empty() is not allowed. Use more strict comparison.

Check failure on line 78 in Civi/Hiorg/Api4/Action/AbstractHiorgApiAction.php

View workflow job for this annotation

GitHub Actions / PHPStan with PHP 8.0 prefer-stable

Construct empty() is not allowed. Use more strict comparison.

Check failure on line 78 in Civi/Hiorg/Api4/Action/AbstractHiorgApiAction.php

View workflow job for this annotation

GitHub Actions / PHPStan with PHP 7.4 prefer-stable

Construct empty() is not allowed. Use more strict comparison.

Check failure on line 78 in Civi/Hiorg/Api4/Action/AbstractHiorgApiAction.php

View workflow job for this annotation

GitHub Actions / PHPStan with PHP 8.3 prefer-lowest

Construct empty() is not allowed. Use more strict comparison.

Check failure on line 78 in Civi/Hiorg/Api4/Action/AbstractHiorgApiAction.php

View workflow job for this annotation

GitHub Actions / PHPStan with PHP 7.4 prefer-lowest

Construct empty() is not allowed. Use more strict comparison.
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<object{type: string, id: int, attributes: array<string, mixed>}> $included
*/
protected static function addRelationshipIncludeData(\stdClass $relationship, array $included): void {
foreach ($included as $include) {
if (
$include->type == $relationship->data->type
Expand Down
5 changes: 4 additions & 1 deletion Civi/Hiorg/Api4/Action/AbstractSynchronizeAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,9 @@ protected function getQueue(): ?\CRM_Queue_Queue {
: NULL;
}

/**
* @phpstan-return list<array<string, mixed>>
*/
protected function runQueue(\CRM_Queue_Queue $queue, ?int $maxRunTime = NULL): array {
$totalItems = $queue->getStatistic('total');
$runner = new \CRM_Queue_Runner([
Expand Down Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion hiorg.php
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand Down

0 comments on commit f347dd5

Please sign in to comment.