From f7e6009b3767529852d796f017f6d1b8cb9db1db Mon Sep 17 00:00:00 2001 From: colemanw Date: Thu, 19 Oct 2023 17:05:40 -0400 Subject: [PATCH] APIv4 - Add type hints --- Civi/Api4/Service/Schema/SchemaMapBuilder.php | 4 +-- Civi/Api4/Utils/CoreUtil.php | 31 ++++++++++--------- 2 files changed, 18 insertions(+), 17 deletions(-) diff --git a/Civi/Api4/Service/Schema/SchemaMapBuilder.php b/Civi/Api4/Service/Schema/SchemaMapBuilder.php index 576b96ec6e..beb5862c6b 100644 --- a/Civi/Api4/Service/Schema/SchemaMapBuilder.php +++ b/Civi/Api4/Service/Schema/SchemaMapBuilder.php @@ -41,13 +41,13 @@ class SchemaMapBuilder extends AutoService { */ public function __construct(CiviEventDispatcherInterface $dispatcher) { $this->dispatcher = $dispatcher; - $this->apiEntities = array_keys((array) Entity::get(FALSE)->addSelect('name')->execute()->indexBy('name')); + $this->apiEntities = Entity::get(FALSE)->addSelect('name')->execute()->column('name'); } /** * @return SchemaMap */ - public function build() { + public function build(): SchemaMap { $map = new SchemaMap(); $this->loadTables($map); diff --git a/Civi/Api4/Utils/CoreUtil.php b/Civi/Api4/Utils/CoreUtil.php index b8945e94ca..bb53f94cb4 100644 --- a/Civi/Api4/Utils/CoreUtil.php +++ b/Civi/Api4/Utils/CoreUtil.php @@ -16,6 +16,7 @@ use Civi\API\Exception\NotImplementedException; use Civi\API\Exception\UnauthorizedException; use Civi\API\Request; use Civi\Api4\Generic\AbstractAction; +use Civi\Api4\Service\Schema\SchemaMap; use CRM_Core_DAO_AllCoreTables as AllCoreTables; class CoreUtil { @@ -31,7 +32,7 @@ class CoreUtil { * The BAO name for use in static calls. Return doc block is hacked to allow * auto-completion of static methods */ - public static function getBAOFromApiName($entityName) { + public static function getBAOFromApiName($entityName): ?string { // TODO: It would be nice to just call self::getInfoItem($entityName, 'dao') // but that currently causes test failures, probably due to early-bootstrap issues. if ($entityName === 'CustomValue' || strpos($entityName, 'Custom_') === 0) { @@ -54,16 +55,16 @@ class CoreUtil { * @param $baoClassName * @return string|null */ - public static function getApiNameFromBAO($baoClassName) { + public static function getApiNameFromBAO($baoClassName): ?string { $briefName = AllCoreTables::getBriefName($baoClassName); return $briefName && self::getApiClass($briefName) ? $briefName : NULL; } /** * @param string $entityName - * @return string|\Civi\Api4\Generic\AbstractEntity + * @return string|\Civi\Api4\Generic\AbstractEntity|null */ - public static function getApiClass($entityName) { + public static function getApiClass(string $entityName): ?string { $className = 'Civi\Api4\\' . $entityName; if (class_exists($className)) { return $className; @@ -127,19 +128,19 @@ class CoreUtil { * * @param string $entityName * - * @return string + * @return string|null */ - public static function getTableName(string $entityName) { + public static function getTableName(string $entityName): ?string { return self::getInfoItem($entityName, 'table_name'); } /** * Given a sql table name, return the name of the api entity. * - * @param $tableName + * @param string $tableName * @return string|NULL */ - public static function getApiNameFromTableName($tableName) { + public static function getApiNameFromTableName($tableName): ?string { $provider = \Civi::service('action_object_provider'); foreach ($provider->getEntities() as $entityName => $info) { if (($info['table_name'] ?? NULL) === $tableName) { @@ -156,7 +157,7 @@ class CoreUtil { /** * @return string[] */ - public static function getOperators() { + public static function getOperators(): array { $operators = \CRM_Core_DAO::acceptedSQLOperators(); $operators[] = 'CONTAINS'; $operators[] = 'NOT CONTAINS'; @@ -178,7 +179,7 @@ class CoreUtil { * @param string $entityName * @return array{extends: array, column: string, grouping: mixed}|null */ - public static function getCustomGroupExtends(string $entityName) { + public static function getCustomGroupExtends(string $entityName): ?array { $contactTypes = \CRM_Contact_BAO_ContactType::basicTypes(); // Custom_group.extends pretty much maps 1-1 with entity names, except for Contact. if (in_array($entityName, $contactTypes, TRUE)) { @@ -222,7 +223,7 @@ class CoreUtil { * @return bool * @throws \CRM_Core_Exception */ - public static function isCustomEntity($customGroupName) { + public static function isCustomEntity($customGroupName): bool { return $customGroupName && \CRM_Core_DAO::getFieldValue('CRM_Core_DAO_CustomGroup', $customGroupName, 'is_multiple', 'name'); } @@ -233,10 +234,10 @@ class CoreUtil { * @param array $record * @param int|null $userID * Contact ID of the user we are testing, 0 for the anonymous user. - * @return bool + * @return bool|null * @throws \CRM_Core_Exception */ - public static function checkAccessRecord(AbstractAction $apiRequest, array $record, int $userID = NULL) { + public static function checkAccessRecord(AbstractAction $apiRequest, array $record, int $userID = NULL): ?bool { $userID = $userID ?? \CRM_Core_Session::getLoggedInContactID() ?? 0; // Super-admins always have access to everything @@ -299,7 +300,7 @@ class CoreUtil { /** * @return \Civi\Api4\Service\Schema\SchemaMap */ - public static function getSchemaMap() { + public static function getSchemaMap(): SchemaMap { $cache = \Civi::cache('metadata'); $schemaMap = $cache->get('api4.schema.map'); if (!$schemaMap) { @@ -318,7 +319,7 @@ class CoreUtil { * @return array{name: string, type: string, count: int, table: string|null, key: string|null}[] * @throws NotImplementedException */ - public static function getRefCount(string $entityName, $entityId) { + public static function getRefCount(string $entityName, $entityId): array { $daoName = self::getInfoItem($entityName, 'dao'); if (!$daoName) { throw new NotImplementedException("Cannot getRefCount for $entityName - dao not found."); -- 2.25.1