RouteCollection::prepareMethods()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 1

Importance

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