From a15a82a209fc1d8195ea8de68031af1ae3c485e6 Mon Sep 17 00:00:00 2001 From: fadrian06 Date: Thu, 1 Feb 2024 19:42:31 -0400 Subject: [PATCH] It throws an Exception for invalid Filters array --- flight/core/Dispatcher.php | 8 ++++++-- tests/DispatcherTest.php | 14 ++++++++++++++ 2 files changed, 20 insertions(+), 2 deletions(-) diff --git a/flight/core/Dispatcher.php b/flight/core/Dispatcher.php index f7d2a8a..0fcc39a 100644 --- a/flight/core/Dispatcher.php +++ b/flight/core/Dispatcher.php @@ -165,9 +165,13 @@ class Dispatcher * * @throws Exception If an event throws an `Exception`. */ - public function filter(array $filters, array &$params, &$output): void + public static function filter(array $filters, array &$params, &$output): void { - foreach ($filters as $callback) { + foreach ($filters as $key => $callback) { + if (!is_callable($callback)) { + throw new InvalidArgumentException("Invalid callable \$filters[$key]."); + } + $continue = $callback($params, $output); if ($continue === false) { diff --git a/tests/DispatcherTest.php b/tests/DispatcherTest.php index 0be7235..795d01d 100644 --- a/tests/DispatcherTest.php +++ b/tests/DispatcherTest.php @@ -194,6 +194,20 @@ class DispatcherTest extends TestCase restore_error_handler(); } + public function testItThrowsAnExceptionForInvalidFilters(): void + { + $this->expectException(Exception::class); + $this->expectExceptionMessage('Invalid callable $filters[1]'); + + $params = []; + $output = ''; + $validCallable = function (): void { + }; + $invalidCallable = 'invalidGlobalFunction'; + + Dispatcher::filter([$validCallable, $invalidCallable], $params, $output); + } + public function testCallFunction4Params(): void { $myFunction = function ($param1, $param2, $param3, $param4) {