Browse Source

Replace type callable with Closure in Routing package.

ADmad 4 years ago
parent
commit
9977384c27

+ 16 - 19
src/Routing/RouteBuilder.php

@@ -351,12 +351,12 @@ class RouteBuilder
      *   is available at `/posts`
      *
      * @param string $name A controller name to connect resource routes for.
-     * @param callable|array $options Options to use when generating REST routes, or a callback.
-     * @param callable|null $callback An optional callback to be executed in a nested scope. Nested
+     * @param \Closure|array $options Options to use when generating REST routes, or a callback.
+     * @param \Closure|null $callback An optional callback to be executed in a nested scope. Nested
      *   scopes inherit the existing path and 'id' parameter.
      * @return $this
      */
-    public function resources(string $name, callable|array $options = [], ?callable $callback = null)
+    public function resources(string $name, Closure|array $options = [], ?Closure $callback = null)
     {
         if (!is_array($options)) {
             $callback = $options;
@@ -827,13 +827,13 @@ class RouteBuilder
      * ```
      *
      * @param string $name The prefix name to use.
-     * @param callable|array $params An array of routing defaults to add to each connected route.
-     *   If you have no parameters, this argument can be a callable.
-     * @param callable|null $callback The callback to invoke that builds the prefixed routes.
+     * @param \Closure|array $params An array of routing defaults to add to each connected route.
+     *   If you have no parameters, this argument can be a Closure.
+     * @param \Closure|null $callback The callback to invoke that builds the prefixed routes.
      * @return $this
      * @throws \InvalidArgumentException If a valid callback is not passed
      */
-    public function prefix(string $name, callable|array $params = [], ?callable $callback = null)
+    public function prefix(string $name, Closure|array $params = [], ?Closure $callback = null)
     {
         if (!is_array($params)) {
             $callback = $params;
@@ -873,12 +873,12 @@ class RouteBuilder
      *   name of any route created in a scope callback.
      *
      * @param string $name The plugin name to build routes for
-     * @param callable|array $options Either the options to use, or a callback to build routes.
-     * @param callable|null $callback The callback to invoke that builds the plugin routes
+     * @param \Closure|array $options Either the options to use, or a callback to build routes.
+     * @param \Closure|null $callback The callback to invoke that builds the plugin routes
      *   Only required when $options is defined.
      * @return $this
      */
-    public function plugin(string $name, callable|array $options = [], ?callable $callback = null)
+    public function plugin(string $name, Closure|array $options = [], ?Closure $callback = null)
     {
         if (!is_array($options)) {
             $callback = $options;
@@ -906,23 +906,20 @@ class RouteBuilder
      *   name of any route created in a scope callback.
      *
      * @param string $path The path to create a scope for.
-     * @param callable|array $params Either the parameters to add to routes, or a callback.
-     * @param callable|null $callback The callback to invoke that builds the plugin routes.
+     * @param \Closure|array $params Either the parameters to add to routes, or a callback.
+     * @param \Closure|null $callback The callback to invoke that builds the plugin routes.
      *   Only required when $params is defined.
      * @return $this
      * @throws \InvalidArgumentException when there is no callable parameter.
      */
-    public function scope(string $path, callable|array $params, ?callable $callback = null)
+    public function scope(string $path, Closure|array $params, ?Closure $callback = null)
     {
-        if (is_callable($params)) {
+        if ($params instanceof Closure) {
             $callback = $params;
             $params = [];
         }
-        if (!is_callable($callback)) {
-            throw new InvalidArgumentException(sprintf(
-                'Need a valid callable to connect routes. Got `%s` instead.',
-                get_debug_type($callback)
-            ));
+        if ($callback === null) {
+            throw new InvalidArgumentException('Need a valid Closure to connect routes.');
         }
 
         if ($this->_path !== '/') {

+ 5 - 10
src/Routing/Router.php

@@ -20,10 +20,10 @@ use Cake\Core\Configure;
 use Cake\Http\ServerRequest;
 use Cake\Routing\Exception\MissingRouteException;
 use Cake\Routing\Route\Route;
+use Closure;
 use InvalidArgumentException;
 use Psr\Http\Message\UriInterface;
 use ReflectionFunction;
-use ReflectionMethod;
 use RuntimeException;
 use Throwable;
 
@@ -146,7 +146,7 @@ class Router
      * The stack of URL filters to apply against routing URLs before passing the
      * parameters to the route collection.
      *
-     * @var array<callable>
+     * @var array<\Closure>
      */
     protected static array $_urlFilters = [];
 
@@ -305,10 +305,10 @@ class Router
      * });
      * ```
      *
-     * @param callable $function The function to add
+     * @param \Closure $function The function to add
      * @return void
      */
-    public static function addUrlFilter(callable $function): void
+    public static function addUrlFilter(Closure $function): void
     {
         static::$_urlFilters[] = $function;
     }
@@ -328,12 +328,7 @@ class Router
             try {
                 $url = $filter($url, $request);
             } catch (Throwable $e) {
-                if (is_array($filter)) {
-                    $ref = new ReflectionMethod($filter[0], $filter[1]);
-                } else {
-                    /** @psalm-var \Closure|callable-string $filter */
-                    $ref = new ReflectionFunction($filter);
-                }
+                $ref = new ReflectionFunction($filter);
                 $message = sprintf(
                     'URL filter defined in %s on line %s could not be applied. The filter failed with: %s',
                     $ref->getFileName(),

+ 1 - 1
tests/TestCase/Routing/RouteBuilderTest.php

@@ -885,7 +885,7 @@ class RouteBuilderTest extends TestCase
     public function testScopeException(): void
     {
         $this->expectException(InvalidArgumentException::class);
-        $this->expectExceptionMessage('Need a valid callable to connect routes. Got `null` instead.');
+        $this->expectExceptionMessage('Need a valid Closure to connect routes.');
 
         $routes = new RouteBuilder($this->collection, '/api', ['prefix' => 'Api']);
         $routes->scope('/v1', ['fail'], null);

+ 3 - 1
tests/TestCase/Routing/RouterTest.php

@@ -1145,7 +1145,9 @@ class RouterTest extends TestCase
         ]);
         Router::setRequest($request);
 
-        Router::addUrlFilter([$this, 'badFilter']);
+        Router::addUrlFilter(function () {
+            throw new Exception();
+        });
         Router::url(['controller' => 'Posts', 'action' => 'index', 'lang' => 'en']);
     }