Table::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 5
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 2
1
<?php
2
3
namespace Bmatovu\RouteList\View\Components;
4
5
use Illuminate\Routing\Router;
6
use Illuminate\View\Component;
7
8
class Table extends Component
9
{
10
    /**
11
     * Router.
12
     *
13
     * @var \Illuminate\Routing\Router
14
     */
15
    protected $router;
16
17
    /**
18
     * Table ID.
19
     *
20
     * @var string
21
     */
22
    protected $id;
23
24
    /**
25
     * Create a new component instance.
26
     *
27
     * @param string                     $id
28
     * @param \Illuminate\Routing\Router $router
29
     *
30
     * @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...
31
     */
32
    public function __construct(Router $router, $id = 'routes')
33
    {
34
        $this->router = $router;
35
        $this->id = $id;
36
    }
37
38
    /**
39
     * Get the view / contents that represent the component.
40
     *
41
     * @return \Illuminate\View\View
42
     */
43
    public function render()
44
    {
45
        $routes = $this->getRoutes();
46
47
        return view('route-list::components.table', [
0 ignored issues
show
Bug Compatibility introduced by
The expression view('route-list::compon... 'routes' => $routes)); of type Illuminate\Contracts\Vie...\Contracts\View\Factory adds the type Illuminate\Contracts\View\Factory to the return on line 47 which is incompatible with the return type declared by the abstract method Illuminate\View\Component::render of type Illuminate\Contracts\Vie...Htmlable|Closure|string.
Loading history...
48
            'id' => $this->id,
49
            'routes' => $routes,
50
        ]);
51
    }
52
53
    protected function getRoutes()
54
    {
55
        return collect($this->router->getRoutes())
56
            ->filter(function ($route) {
57
                return ! $this->matches(config('route-list.excluded'), $route->uri);
58 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...
59
                $name = $route->action['as'] ?? '';
60
61
                if ($name && $this->matches(['/^generated::/'], $name)) {
62
                    $name = '';
63
                }
64
65
                return [
66
                    // 'host' => $route->action['where'],
67
                    'uri' => $route->uri,
68
                    'name' => $name,
69
                    'methods' => $route->methods,
70
                    'action' => $route->action['controller'] ?? 'Closure',
71
                    'middleware' => $this->getRouteMiddleware($route),
72
                ];
73
            });
74
    }
75
76
    /**
77
     * Get route middleware.
78
     *
79
     * @param \Illuminate\Routing\Route $route
80
     *
81
     * @return string
82
     */
83
    protected function getRouteMiddleware($route)
84
    {
85
        return collect($route->gatherMiddleware())->map(function ($middleware) {
86
            return $middleware instanceof \Closure ? 'Closure' : $middleware;
87
        })->implode(', ');
88
    }
89
90
    /**
91
     * Perform a regular expression match.
92
     *
93
     * @param array  $patterns
94
     * @param string $subject
95
     *
96
     * @return bool
97
     */
98 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...
99
    {
100
        $isMatched = false;
101
102
        foreach ($patterns as $pattern) {
103
            if (preg_match($pattern, $subject)) {
104
                $isMatched = true;
105
106
                break;
107
            }
108
        }
109
110
        return $isMatched;
111
    }
112
}
113