From e435f1789f027d325a34c60add85c354aea08e4c Mon Sep 17 00:00:00 2001 From: Tim Otten Date: Mon, 31 May 2021 00:32:53 -0700 Subject: [PATCH] For Symfony-style event listeners, encourage use of `void` return When using Symfony-style listeners, all inputs and outputs for the event go through the event object. Note that `hook_*()` notation does allow return values, For these functions, it is *normal* to return void, but some existing hooks rely on returning array-data. It could be misleading if we made it appear that all `hook_*()` examples have to return void. --- CRM/Contact/BAO/RelationshipCache.php | 2 +- Civi/Test/HookInterface.php | 4 ++-- tests/phpunit/Civi/Test/ExampleHookTest.php | 6 +++--- tests/phpunit/Civi/Test/ExampleSubscriberTest.php | 6 +++--- 4 files changed, 9 insertions(+), 9 deletions(-) diff --git a/CRM/Contact/BAO/RelationshipCache.php b/CRM/Contact/BAO/RelationshipCache.php index b7622dfc65..8681158e7a 100644 --- a/CRM/Contact/BAO/RelationshipCache.php +++ b/CRM/Contact/BAO/RelationshipCache.php @@ -69,7 +69,7 @@ class CRM_Contact_BAO_RelationshipCache extends CRM_Contact_DAO_RelationshipCach * @param \Civi\Core\Event\GenericHookEvent $e * @see \CRM_Utils_Hook::triggerInfo */ - public static function on_hook_civicrm_triggerInfo($e) { + public static function on_hook_civicrm_triggerInfo($e): void { $relUpdates = self::createInsertUpdateQueries(); // Use utf8mb4_bin or utf8_bin, depending on what's in use. $collation = preg_replace('/^(utf8(?:mb4)?)_.*$/', '$1_bin', CRM_Core_BAO_SchemaHandler::getInUseCollation()); diff --git a/Civi/Test/HookInterface.php b/Civi/Test/HookInterface.php index bd3fee8acf..12b0cb7588 100644 --- a/Civi/Test/HookInterface.php +++ b/Civi/Test/HookInterface.php @@ -21,10 +21,10 @@ namespace Civi\Test; * * ``` * class MyTest extends \PHPUnit_Framework_TestCase implements \Civi\Test\HookInterface { - * public function on_civi_api_authorize(AuthorizeEvent $e) { + * public function on_civi_api_authorize(AuthorizeEvent $e): void { * echo "Running civi.api.authorize\n"; * } - * public function on_hook_civicrm_post(GenericHookEvent $e) { + * public function on_hook_civicrm_post(GenericHookEvent $e): void { * echo "Running hook_civicrm_post\n"; * } * } diff --git a/tests/phpunit/Civi/Test/ExampleHookTest.php b/tests/phpunit/Civi/Test/ExampleHookTest.php index 2dda38d483..c9a51ad8cd 100644 --- a/tests/phpunit/Civi/Test/ExampleHookTest.php +++ b/tests/phpunit/Civi/Test/ExampleHookTest.php @@ -57,7 +57,7 @@ class ExampleHookTest extends \PHPUnit\Framework\TestCase implements HeadlessInt * * @see \CRM_Utils_Hook::alterContent */ - public function on_hook_civicrm_alterContent(\Civi\Core\Event\GenericHookEvent $event) { + public function on_hook_civicrm_alterContent(\Civi\Core\Event\GenericHookEvent $event): void { $event->content .= ' ' . __FUNCTION__; } @@ -66,7 +66,7 @@ class ExampleHookTest extends \PHPUnit\Framework\TestCase implements HeadlessInt * * @see \Civi\API\Event\ResolveEvent */ - public function on_civi_api_resolve(\Civi\API\Event\ResolveEvent $event) { + public function on_civi_api_resolve(\Civi\API\Event\ResolveEvent $event): void { $this->tracker['civi.api.resolve'][__FUNCTION__] = TRUE; } @@ -75,7 +75,7 @@ class ExampleHookTest extends \PHPUnit\Framework\TestCase implements HeadlessInt * * @see \Civi\API\Event\PrepareEvent */ - public function on_civi_api_prepare(\Civi\API\Event\PrepareEvent $event) { + public function on_civi_api_prepare(\Civi\API\Event\PrepareEvent $event): void { $this->tracker['civi.api.prepare'][__FUNCTION__] = TRUE; } diff --git a/tests/phpunit/Civi/Test/ExampleSubscriberTest.php b/tests/phpunit/Civi/Test/ExampleSubscriberTest.php index d1aa4f55bd..abf8d1a161 100644 --- a/tests/phpunit/Civi/Test/ExampleSubscriberTest.php +++ b/tests/phpunit/Civi/Test/ExampleSubscriberTest.php @@ -51,15 +51,15 @@ class ExampleSubscriberTest extends \PHPUnit\Framework\TestCase implements Headl ]; } - public function myCiviApiResolve(\Civi\API\Event\ResolveEvent $event) { + public function myCiviApiResolve(\Civi\API\Event\ResolveEvent $event): void { $this->tracker['civi.api.resolve'][__FUNCTION__] = TRUE; } - public function myCiviApiPrepare(\Civi\API\Event\PrepareEvent $event) { + public function myCiviApiPrepare(\Civi\API\Event\PrepareEvent $event): void { $this->tracker['civi.api.prepare'][__FUNCTION__] = TRUE; } - public function myAlterContentObject(GenericHookEvent $event) { + public function myAlterContentObject(GenericHookEvent $event): void { $event->content .= ' ' . __FUNCTION__; } -- 2.25.1