Completed
Push — master ( 8addfe...62bc4d )
by Brian
07:34 queued 11s
created

RouteListController::getRouteMiddleware()   A

Complexity

Conditions 2
Paths 1

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
rs 10
c 0
b 0
f 0
cc 2
nc 1
nop 1
1
<?php
2
3
namespace Bmatovu\RouteList\Http;
4
5
use Illuminate\Routing\Controller;
6
use Illuminate\Routing\Router;
7
8
/**
9
 * @see \Illuminate\Foundation\Console\RouteListCommand
10
 */
11
class RouteListController extends Controller
12
{
13
    /**
14
     * Router.
15
     *
16
     * @var \Illuminate\Routing\Router
17
     */
18
    protected $router;
19
20
    /**
21
     * Create a controller instance.
22
     *
23
     * @param string $id
0 ignored issues
show
Bug introduced by
There is no parameter named $id. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
24
     * @param Router $router
25
     *
26
     * @return void
0 ignored issues
show
Comprehensibility Best Practice introduced by
Adding a @return annotation to constructors is generally not recommended as a constructor does not have a meaningful return value.

Adding a @return annotation to a constructor is not recommended, since a constructor does not have a meaningful return value.

Please refer to the PHP core documentation on constructors.

Loading history...
27
     */
28
    public function __construct(Router $router)
29
    {
30
        $this->router = $router;
31
    }
32
33
    /**
34
     * Render routes.
35
     *
36
     * @return \Illuminate\Http\Response
37
     */
38
    public function __invoke()
39
    {
40
        $routes = collect($this->router->getRoutes())
41
            ->filter(function ($route) {
42
                return ! $this->matches(config('route-list.excluded'), $route->uri);
43 View Code Duplication
            })->map(function ($route) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
44
                $name = $route->action['as'] ?? '';
45
46
                if ($name && $this->matches(['/^generated::/'], $name)) {
47
                    $name = '';
48
                }
49
50
                return [
51
                    // 'host' => $route->action['where'],
52
                    'uri' => $route->uri,
53
                    'name' => $name,
54
                    'methods' => $route->methods,
55
                    'action' => $route->action['controller'] ?? 'Closure',
56
                    'middleware' => $this->getRouteMiddleware($route),
57
                ];
58
            });
59
60
        return view('route-list::index', [
61
            'routes' => $routes,
62
        ]);
63
    }
64
65
    /**
66
     * Get route middleware.
67
     *
68
     * @param \Illuminate\Routing\Route $route
69
     *
70
     * @return string
71
     */
72
    protected function getRouteMiddleware($route)
73
    {
74
        return collect($route->gatherMiddleware())->map(function ($middleware) {
75
            return $middleware instanceof \Closure ? 'Closure' : $middleware;
76
        })->implode(', ');
77
    }
78
79
    /**
80
     * Perform a regular expression match.
81
     *
82
     * @param array  $patterns
83
     * @param string $subject
84
     *
85
     * @return bool
86
     */
87 View Code Duplication
    protected function matches($patterns, $subject)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
88
    {
89
        $isMatched = false;
90
91
        foreach ($patterns as $pattern) {
92
            if (preg_match($pattern, $subject)) {
93
                $isMatched = true;
94
95
                break;
96
            }
97
        }
98
99
        return $isMatched;
100
    }
101
}
102