Completed
Pull Request — master (#5)
by ARCANEDEV
05:39
created

RouteCollection::load()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 18

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 18
ccs 0
cts 18
cp 0
rs 9.6666
c 0
b 0
f 0
cc 1
nc 1
nop 4
crap 2
1
<?php namespace Arcanedev\RouteViewer\Entities;
2
3
use Illuminate\Routing\Route as IlluminateRoute;
4
use Illuminate\Support\Arr;
5
use Illuminate\Support\Collection;
6
use Illuminate\Support\Str;
7
8
/**
9
 * Class     RouteCollection
10
 *
11
 * @package  Arcanedev\RouteViewer\Entities
12
 * @author   ARCANEDEV <[email protected]>
13
 */
14
class RouteCollection extends Collection
15
{
16
    /* -----------------------------------------------------------------
17
     |  Main Methods
18
     | -----------------------------------------------------------------
19
     */
20
21
    /**
22
     * Load the routes.
23
     *
24
     * @param  array  $routes
25
     * @param  array  $excludedUris
26
     * @param  array  $excludedMethods
27
     * @param  array  $methodColors
28
     *
29
     * @return self
30
     */
31
    public static function load(
32
        array $routes, array $excludedUris = [], array $excludedMethods = [], array $methodColors = []
33
    ) {
34
        return static::make($routes)
35
            ->filter(function (IlluminateRoute $route) use ($excludedUris) {
36
                return ! Str::startsWith($route->uri(), $excludedUris);
37
            })
38
            ->transform(function (IlluminateRoute $route) use ($excludedMethods, $methodColors) {
39
                return new Route(
40
                    static::prepareMethods($route, $excludedMethods, $methodColors),
0 ignored issues
show
Bug introduced by
Since prepareMethods() is declared private, calling it with static will lead to errors in possible sub-classes. You can either use self, or increase the visibility of prepareMethods() to at least protected.

Let’s assume you have a class which uses late-static binding:

class YourClass
{
    private static function getTemperature() {
        return "3422 °C";
}

public static function getSomeVariable()
{
    return static::getTemperature();
}

}

The code above will run fine in your PHP runtime. However, if you now create a sub-class and call the getSomeVariable() on that sub-class, you will receive a runtime error:

class YourSubClass extends YourClass {
      private static function getTemperature() {
        return "-182 °C";
    }
}

print YourSubClass::getSomeVariable(); // Will cause an access error.

In the case above, it makes sense to update SomeClass to use self instead:

class YourClass
{
    private static function getTemperature() {
        return "3422 °C";
    }

    public static function getSomeVariable()
    {
        return self::getTemperature();
    }
}
Loading history...
41
                    $route->uri(),
42
                    $route->getActionName(),
43
                    $route->getName(),
44
                    static::prepareMiddleware($route),
0 ignored issues
show
Bug introduced by
Since prepareMiddleware() is declared private, calling it with static will lead to errors in possible sub-classes. You can either use self, or increase the visibility of prepareMiddleware() to at least protected.

Let’s assume you have a class which uses late-static binding:

class YourClass
{
    private static function getTemperature() {
        return "3422 °C";
}

public static function getSomeVariable()
{
    return static::getTemperature();
}

}

The code above will run fine in your PHP runtime. However, if you now create a sub-class and call the getSomeVariable() on that sub-class, you will receive a runtime error:

class YourSubClass extends YourClass {
      private static function getTemperature() {
        return "-182 °C";
    }
}

print YourSubClass::getSomeVariable(); // Will cause an access error.

In the case above, it makes sense to update SomeClass to use self instead:

class YourClass
{
    private static function getTemperature() {
        return "3422 °C";
    }

    public static function getSomeVariable()
    {
        return self::getTemperature();
    }
}
Loading history...
45
                    $route->domain()
0 ignored issues
show
Bug introduced by
It seems like $route->domain() targeting Illuminate\Routing\Route::domain() can also be of type object<Illuminate\Routing\Route>; however, Arcanedev\RouteViewer\En...es\Route::__construct() does only seem to accept string|null, maybe add an additional type check?

This check looks at variables that are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
46
                );
47
            });
48
    }
49
50
    /* -----------------------------------------------------------------
51
     |  Other Methods
52
     | -----------------------------------------------------------------
53
     */
54
55
    /**
56
     * Prepare route methods.
57
     *
58
     * @param  \Illuminate\Routing\Route  $route
59
     * @param  array                      $excluded
60
     * @param  array                      $colors
61
     *
62
     * @return array
63
     */
64
    private static function prepareMethods(IlluminateRoute $route, array $excluded, array $colors)
65
    {
66
        return array_map(function ($method) use ($colors) {
67
            return [
68
                'name'  => $method,
69
                'color' => Arr::get($colors, $method),
70
            ];
71
        }, array_diff($route->methods(), $excluded));
72
    }
73
74
    /**
75
     * Prepare route middleware.
76
     *
77
     * @param  \Illuminate\Routing\Route  $route
78
     *
79
     * @return array
80
     */
81
    private static function prepareMiddleware(IlluminateRoute $route)
82
    {
83
        return array_map(function ($value) {
84
            return $value instanceof \Closure ? 'Closure' : $value;
85
        }, self::gatherMiddleware($route));
86
    }
87
88
    /**
89
     * Gather all the route middleware.
90
     *
91
     * @param  \Illuminate\Routing\Route  $route
92
     *
93
     * @return array
94
     */
95
    private static function gatherMiddleware(IlluminateRoute $route)
96
    {
97
        /** @var  array  $middleware */
98
        $middleware = $route->middleware();
99
100
        return is_callable([$route, 'controllerMiddleware'])
101
            ? array_unique(array_merge($middleware, $route->controllerMiddleware()), SORT_REGULAR)
102
            : $middleware;
103
    }
104
}
105