RouteListController::__invoke()   A
last analyzed

Complexity

Conditions 3
Paths 1

Size

Total Lines 26

Duplication

Lines 16
Ratio 61.54 %

Code Coverage

Tests 15
CRAP Score 3.0021

Importance

Changes 0
Metric Value
dl 16
loc 26
ccs 15
cts 16
cp 0.9375
rs 9.504
c 0
b 0
f 0
cc 3
nc 1
nop 0
crap 3.0021
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 \Illuminate\Routing\Router $router
24
     *
25
     * @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...
26
     */
27 1
    public function __construct(Router $router)
28
    {
29 1
        $this->router = $router;
30 1
    }
31
32
    /**
33
     * Render routes.
34
     *
35
     * @return \Illuminate\View\View
36
     */
37 1
    public function __invoke()
38
    {
39 1
        $routes = collect($this->router->getRoutes())
40 1
            ->filter(function ($route) {
41 1
                return ! $this->matches(config('route-list.excluded'), $route->uri);
42 1 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...
43 1
                $name = $route->action['as'] ?? '';
44
45 1
                if ($name && $this->matches(['/^generated::/'], $name)) {
46
                    $name = '';
47
                }
48
49
                return [
50
                    // 'host' => $route->action['where'],
51 1
                    'uri' => $route->uri,
52 1
                    'name' => $name,
53 1
                    'methods' => $route->methods,
54 1
                    'action' => $route->action['controller'] ?? 'Closure',
55 1
                    'middleware' => $this->getRouteMiddleware($route),
56
                ];
57 1
            });
58
59 1
        return view('route-list::index', [
0 ignored issues
show
Bug Compatibility introduced by
The expression view('route-list::index'...('routes' => $routes)); of type Illuminate\Contracts\Vie...\Contracts\View\Factory adds the type Illuminate\Contracts\View\Factory to the return on line 59 which is incompatible with the return type documented by Bmatovu\RouteList\Http\R...istController::__invoke of type Illuminate\View\View.
Loading history...
60 1
            'routes' => $routes,
61
        ]);
62
    }
63
64
    /**
65
     * Get route middleware.
66
     *
67
     * @param \Illuminate\Routing\Route $route
68
     *
69
     * @return string
70
     */
71 1
    protected function getRouteMiddleware($route)
72
    {
73 1
        $middlewares = collect($route->gatherMiddleware())->map(function ($middleware) {
74 1
            return $middleware instanceof \Closure ? 'Closure' : $middleware;
75 1
        });
76
77 1
        return $middlewares->implode(', ');
78
    }
79
80
    /**
81
     * Perform a regular expression match.
82
     *
83
     * @param array  $patterns
84
     * @param string $subject
85
     *
86
     * @return bool
87
     */
88 1 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...
89
    {
90 1
        $isMatched = false;
91
92 1
        foreach ($patterns as $pattern) {
93 1
            if (preg_match($pattern, $subject)) {
94
                $isMatched = true;
95
96
                break;
97
            }
98
        }
99
100 1
        return $isMatched;
101
    }
102
}
103