Completed
Push — master ( cad594...d96ad8 )
by Brian
02:27
created

RouteListController::getRouteMiddleware()   A

Complexity

Conditions 2
Paths 1

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 2.0625

Importance

Changes 0
Metric Value
dl 0
loc 6
ccs 3
cts 4
cp 0.75
rs 10
c 0
b 0
f 0
cc 2
nc 1
nop 1
crap 2.0625
1
<?php
2
3
namespace Bmatovu\RouteList\Controllers;
4
5
use Illuminate\Routing\Controller;
6
use Illuminate\Support\Facades\Route;
7
8
class RouteListController extends Controller
9
{
10
    /**
11
     * Render routes.
12
     *
13
     * @return \Illuminate\Http\Response
14
     */
15 1
    public function __invoke()
16
    {
17 1
        $routes = collect(Route::getRoutes())
18 1
            ->filter(function ($route) {
19 1
                return ! $this->matches(config('route-list.excluded'), $route->uri);
20 1
            })->map(function ($route) {
21
                return [
22
                    // 'host' => $route->action['where'],
23 1
                    'uri' => $route->uri,
24 1
                    'name' => $route->action['as'] ?? '',
25 1
                    'methods' => $route->methods,
26 1
                    'action' => $route->action['controller'] ?? 'Closure',
27 1
                    'middleware' => $this->getRouteMiddleware($route),
28
                ];
29 1
            });
30
31 1
        return view('route-list::index', [
32 1
            'routes' => $routes,
33
        ]);
34
    }
35
36
    /**
37
     * Get route middleware.
38
     *
39
     * @param \Illuminate\Routing\Route $route
40
     *
41
     * @return string
42
     */
43 1
    protected function getRouteMiddleware($route)
44
    {
45 1
        return collect($route->gatherMiddleware())->map(function ($middleware) {
46
            return $middleware instanceof Closure ? 'Closure' : $middleware;
0 ignored issues
show
Bug introduced by
The class Bmatovu\RouteList\Controllers\Closure does not exist. Did you forget a USE statement, or did you not list all dependencies?

This error could be the result of:

1. Missing dependencies

PHP Analyzer uses your composer.json file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects the composer.json to be in the root folder of your repository.

Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the require or require-dev section?

2. Missing use statement

PHP does not complain about undefined classes in ìnstanceof checks. For example, the following PHP code will work perfectly fine:

if ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
47 1
        })->implode(', ');
48
    }
49
50
    /**
51
     * Perform a regular expression match.
52
     *
53
     * @param array  $patterns
54
     * @param string $subject
55
     *
56
     * @return bool
57
     */
58 1
    protected function matches($patterns, $subject)
59
    {
60 1
        $isMatched = false;
61
62 1
        foreach ($patterns as $pattern) {
63 1
            if (preg_match($pattern, $subject)) {
64
                $isMatched = true;
65
66
                break;
67
            }
68
        }
69
70 1
        return $isMatched;
71
    }
72
}
73