diff --git a/src/Group.php b/src/Group.php index 0d6e1b5..0099cb1 100644 --- a/src/Group.php +++ b/src/Group.php @@ -6,6 +6,7 @@ use InvalidArgumentException; use RuntimeException; +use Yiisoft\Router\Internal\MiddlewareFilter; use function in_array; @@ -18,6 +19,7 @@ final class Group /** * @var array[]|callable[]|string[] + * @psalm-var list */ private array $middlewares = []; @@ -209,12 +211,7 @@ private function getEnabledMiddlewares(): array return $this->enabledMiddlewaresCache; } - $this->enabledMiddlewaresCache = array_values( - array_filter( - $this->middlewares, - fn ($definition) => !in_array($definition, $this->disabledMiddlewares, true) - ) - ); + $this->enabledMiddlewaresCache = MiddlewareFilter::filter($this->middlewares, $this->disabledMiddlewares); return $this->enabledMiddlewaresCache; } diff --git a/src/Internal/MiddlewareFilter.php b/src/Internal/MiddlewareFilter.php new file mode 100644 index 0000000..e70f6f2 --- /dev/null +++ b/src/Internal/MiddlewareFilter.php @@ -0,0 +1,33 @@ + $middlewares + * @psalm-return list + */ + public static function filter(array $middlewares, array $disabledMiddlewares): array + { + $result = []; + + foreach ($middlewares as $middleware) { + if (in_array($middleware, $disabledMiddlewares, true)) { + continue; + } + + $result[] = $middleware; + } + + return $result; + } +} diff --git a/src/Route.php b/src/Route.php index abfd935..d594070 100644 --- a/src/Route.php +++ b/src/Route.php @@ -8,6 +8,7 @@ use RuntimeException; use Stringable; use Yiisoft\Http\Method; +use Yiisoft\Router\Internal\MiddlewareFilter; use function in_array; @@ -309,12 +310,7 @@ private function getEnabledMiddlewares(): array return $this->enabledMiddlewaresCache; } - $this->enabledMiddlewaresCache = array_values( - array_filter( - $this->middlewares, - fn ($definition) => !in_array($definition, $this->disabledMiddlewares, true) - ) - ); + $this->enabledMiddlewaresCache = MiddlewareFilter::filter($this->middlewares, $this->disabledMiddlewares); return $this->enabledMiddlewaresCache; }