From 27784541f184a1ea3b6656fc8a882bb9adf45fc2 Mon Sep 17 00:00:00 2001 From: John Blackbourn Date: Thu, 1 Dec 2022 14:56:51 +0000 Subject: [PATCH] Support multiple variants (#135) * Add a failing test for #133. * Avoid a PHPStan exception when a callback has multiple variants. --- src/HookCallbackRule.php | 3 ++- tests/data/hook-callback.php | 18 ++++++++++++++++++ 2 files changed, 20 insertions(+), 1 deletion(-) diff --git a/src/HookCallbackRule.php b/src/HookCallbackRule.php index 6a0ad72..011b071 100644 --- a/src/HookCallbackRule.php +++ b/src/HookCallbackRule.php @@ -80,7 +80,8 @@ public function processNode(Node $node, Scope $scope): array return []; } - $callbackAcceptor = ParametersAcceptorSelector::selectSingle($callbackType->getCallableParametersAcceptors($scope)); + $parametersAcceptors = $callbackType->getCallableParametersAcceptors($scope); + $callbackAcceptor = ParametersAcceptorSelector::selectFromArgs($scope, $args, $parametersAcceptors); try { if ($name->toString() === 'add_action') { diff --git a/tests/data/hook-callback.php b/tests/data/hook-callback.php index 840e4d4..9b937b5 100644 --- a/tests/data/hook-callback.php +++ b/tests/data/hook-callback.php @@ -234,6 +234,24 @@ add_filter('filter', __NAMESPACE__ . '\\filter_variadic_typed', 10, 2); add_filter('filter', __NAMESPACE__ . '\\filter_variadic_typed', 10, 999); +// Multiple callbacks with varying signatures +class MultipleSignatures { + const ACTIONS = array( + 'one', + 'two', + ); + + public static function init(): void { + foreach ( self::ACTIONS as $action ) { + add_action( 'action', array( self::class, $action ) ); + } + } + + public static function one( int $param ): void {} + + public static function two( string $param ): void {} +} + /** * Symbol definitions for use in these tests. */