Completed
Push — master ( c9976f...2ceba6 )
by ARCANEDEV
21:56
created

RouteCollection::gatherMiddleware()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
dl 0
loc 9
ccs 0
cts 0
cp 0
rs 9.6666
c 0
b 0
f 0
cc 2
eloc 5
nc 2
nop 1
crap 6
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 Functions
18
     | ------------------------------------------------------------------------------------------------
19
     */
20
    /**
21
     * Load the routes.
22
     *
23
     * @param  array  $routes
24
     * @param  array  $excludedUris
25
     * @param  array  $excludedMethods
26
     * @param  array  $methodColors
27
     *
28
     * @return self
29
     */
30 27
    public static function load(
31
        array $routes, array $excludedUris = [], array $excludedMethods = [], array $methodColors = []
32
    ) {
33 27
        return static::make($routes)
34
            ->filter(function (IlluminateRoute $route) use ($excludedUris) {
35 27
                return ! Str::startsWith($route->uri(), $excludedUris);
36 27
            })
37
            ->transform(function (IlluminateRoute $route) use ($excludedMethods, $methodColors) {
38 27
                return new Route(
39 27
                    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...
40 27
                    $route->uri(),
41 27
                    $route->getActionName(),
42 27
                    $route->getName(),
43 27
                    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...
44 27
                    $route->domain()
45 9
                );
46 27
            });
47
    }
48
49
    /**
50
     * Prepare route methods.
51
     *
52
     * @param  \Illuminate\Routing\Route  $route
53
     * @param  array                      $excluded
54
     * @param  array                      $colors
55
     *
56
     * @return array
57
     */
58 27
    private static function prepareMethods(IlluminateRoute $route, array $excluded, array $colors)
59
    {
60
        return array_map(function ($method) use ($colors) {
61
            return [
62 27
                'name'  => $method,
63 27
                'color' => Arr::get($colors, $method),
64 9
            ];
65 27
        }, array_diff($route->getMethods(), $excluded));
66
    }
67
68
    /**
69
     * Prepare route middleware.
70
     *
71
     * @param  \Illuminate\Routing\Route  $route
72
     *
73
     * @return array
74
     */
75 27
    private static function prepareMiddleware(IlluminateRoute $route)
76
    {
77
        return array_map(function ($value) {
78 27
            return $value instanceof \Closure ? 'Closure' : $value;
79 27
        }, self::gatherMiddleware($route));
80 15
    }
81 27
82
    /**
83 27
     * Gather all the route middleware.
84 27
     *
85 27
     * @param  \Illuminate\Routing\Route  $route
86
     *
87
     * @return array
88
     */
89
    private static function gatherMiddleware(IlluminateRoute $route)
90
    {
91
        /** @var  array  $middleware */
92
        $middleware = $route->middleware();
93
94
        return is_callable([$route, 'controllerMiddleware'])
95
            ? array_unique(array_merge($middleware, $route->controllerMiddleware()), SORT_REGULAR)
96
            : $middleware;
97
    }
98
}
99