Passed
Push — main ( c6deb1...79ccdf )
by Dimitri
12:23
created

RoutesCollector::display()   B

Complexity

Conditions 7
Paths 18

Size

Total Lines 61
Code Lines 35

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 7
eloc 35
nc 18
nop 0
dl 0
loc 61
rs 8.4266
c 0
b 0
f 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
/**
4
 * This file is part of Blitz PHP framework.
5
 *
6
 * (c) 2022 Dimitri Sitchet Tomkeu <[email protected]>
7
 *
8
 * For the full copyright and license information, please view
9
 * the LICENSE file that was distributed with this source code.
10
 */
11
12
namespace BlitzPHP\Debug\Toolbar\Collectors;
13
14
use BlitzPHP\Container\Services;
15
use BlitzPHP\Router\DefinedRouteCollector;
16
use BlitzPHP\Router\Router;
17
use ReflectionException;
18
use ReflectionFunction;
19
use ReflectionMethod;
20
21
/**
22
 * Collecteur de routes pour la barre d'outils de débogage
23
 *
24
 * @credit	<a href="https://codeigniter.com">CodeIgniter 4.2 - CodeIgniter\Debug\Toolbar\Collectors\Routes</a>
25
 */
26
class RoutesCollector extends BaseCollector
27
{
28
    /**
29
     * {@inheritDoc}
30
     */
31
    protected bool $hasTimeline = false;
32
33
    /**
34
     * {@inheritDoc}
35
     */
36
    protected bool $hasTabContent = true;
37
38
    /**
39
     * {@inheritDoc}
40
     */
41
    protected string $title = 'Routes';
42
43
	private DefinedRouteCollector $definedRouteCollector;
44
	private Router $router;
45
46
	public function __construct()
47
	{
48
		$rawRoutes                   = Services::routes(true);
49
		$this->router                = Services::router($rawRoutes, null, true);
50
		$this->definedRouteCollector = new DefinedRouteCollector($rawRoutes);
51
	}
52
53
    /**
54
     * {@inheritDoc}
55
     *
56
     * @throws ReflectionException
57
     */
58
    public function display(): array
59
    {
60
        // Récupère nos paramètres
61
        // Route sous forme de callback
62
        if (is_callable($this->router->controllerName())) {
63
            $method = new ReflectionFunction($this->router->controllerName());
64
        } else {
65
            try {
66
                $method = new ReflectionMethod($this->router->controllerName(), $this->router->methodName());
67
            } catch (ReflectionException $e) {
68
                // Si nous sommes ici, la méthode n'existe pas
69
                // et est probablement calculé dans _remap.
70
                $method = new ReflectionMethod($this->router->controllerName(), '_remap');
71
            }
72
        }
73
74
        $rawParams = $method->getParameters();
75
76
        $params = [];
77
78
        foreach ($rawParams as $key => $param) {
79
            $params[] = [
80
                'name'  => '$' . $param->getName() . ' = ',
81
                'value' => $this->router->params()[$key] ??
82
                    ' <empty> | default: '
83
                    . var_export(
84
                        $param->isDefaultValueAvailable() ? $param->getDefaultValue() : null,
85
                        true
86
                    ),
87
            ];
88
        }
89
90
        $matchedRoute = [
91
            [
92
                'directory'  => $this->router->directory(),
93
                'controller' => $this->router->controllerName(),
94
                'method'     => $this->router->methodName(),
95
                'paramCount' => count($this->router->params()),
96
                'truePCount' => count($params),
97
                'params'     => $params ?? [],
98
            ],
99
        ];
100
101
        // Routes définies
102
        $routes  = [];
103
		
104
		foreach ($this->definedRouteCollector->collect(false) as $route) {
105
            // filtre pour les chaînes, car les rappels ne sont pas affichable
106
			if ($route['handler'] !== '(Closure)') {
107
				$routes[] = [
108
					'method'  => strtoupper($route['method']),
109
					'route'   => $route['route'],
110
					'name'    => $route['name'],
111
					'handler' => $route['handler'],
112
				];
113
			}
114
        }
115
116
        return [
117
            'matchedRoute' => $matchedRoute,
118
            'routes'       => $routes,
119
        ];
120
    }
121
122
    /**
123
     * {@inheritDoc}
124
     */
125
    public function getBadgeValue(): int
126
    {
127
		$count = 0;
128
		
129
		foreach ($this->definedRouteCollector->collect(false) as $route) {
130
            if ($route['handler'] !== '(Closure)') {
131
				$count++;
132
			}
133
        }
134
135
		return $count;
136
    }
137
138
    /**
139
     * {@inheritDoc}
140
     *
141
     * Icon from https://icons8.com - 1em package
142
     */
143
    public function icon(): string
144
    {
145
        return 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABgAAAAYCAYAAADgdz34AAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsMAAA7DAcdvqGQAAAFDSURBVEhL7ZRNSsNQFIUjVXSiOFEcuQIHDpzpxC0IGYeE/BEInbWlCHEDLsSiuANdhKDjgm6ggtSJ+l25ldrmmTwIgtgDh/t37r1J+16cX0dRFMtpmu5pWAkrvYjjOB7AETzStBFW+inxu3KUJMmhludQpoflS1zXban4LYqiO224h6VLTHr8Z+z8EpIHFF9gG78nDVmW7UgTHKjsCyY98QP+pcq+g8Ku2s8G8X3f3/I8b038WZTp+bO38zxfFd+I6YY6sNUvFlSDk9CRhiAI1jX1I9Cfw7GG1UB8LAuwbU0ZwQnbRDeEN5qqBxZMLtE1ti9LtbREnMIuOXnyIf5rGIb7Wq8HmlZgwYBH7ORTcKH5E4mpjeGt9fBZcHE2GCQ3Vt7oTNPNg+FXLHnSsHkw/FR+Gg2bB8Ptzrst/v6C/wrH+QB+duli6MYJdQAAAABJRU5ErkJggg==';
146
    }
147
}
148