Completed
Push — master ( 6b0e91...baa484 )
by ARCANEDEV
03:10
created

RouteCollection::prepareMiddleware()   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
cc 2
eloc 5
nc 2
nop 1
dl 0
loc 9
ccs 0
cts 7
cp 0
crap 6
rs 9.6666
c 0
b 0
f 0
1
<?php namespace Arcanesoft\Foundation\Services\RoutesViewer\Entities;
2
3
use Illuminate\Support\Arr;
4
use Illuminate\Support\Collection;
5
use Illuminate\Support\Str;
6
use Illuminate\Routing\Route as IlluminateRoute;
7
8
/**
9
 * Class     RouteCollection
10
 *
11
 * @package  Arcanesoft\Foundation\Services\RoutesViewer\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
    public static function load(
31
        array $routes, array $excludedUris = [], array $excludedMethods = [], array $methodColors = []
32
    ) {
33
        return static::make($routes)
34
            ->filter(function (IlluminateRoute $route) use ($excludedUris) {
35
                return ! Str::startsWith($route->uri(), $excludedUris);
36
            })
37
            ->transform(function (IlluminateRoute $route) use ($excludedMethods, $methodColors) {
38
                return new Route(
39
                    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
                    $route->uri(),
41
                    $route->getActionName(),
42
                    $route->getName(),
43
                    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
                    $route->domain()
45
                );
46
            });
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
    private static function prepareMethods(IlluminateRoute $route, array $excluded, array $colors)
59
    {
60
        return array_map(function ($method) use ($colors) {
61
            return [
62
                'name'  => $method,
63
                'color' => Arr::get($colors, $method),
64
            ];
65
        }, 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
    private static function prepareMiddleware(IlluminateRoute $route)
76
    {
77
        /** @var array $middleware */
78
        $middleware = $route->middleware();
79
80
        return method_exists($route, 'controllerMiddleware')
81
            ? array_merge($middleware, $route->controllerMiddleware())
82
            : $middleware;
83
    }
84
}
85