Passed
Push — main ( dd4eea...91afa7 )
by Dimitri
03:22
created

Routes::execute()   C

Complexity

Conditions 11
Paths 60

Size

Total Lines 84
Code Lines 52

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 11
eloc 52
c 0
b 0
f 0
nc 60
nop 1
dl 0
loc 84
rs 6.9006

How to fix   Long Method    Complexity   

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\Cli\Commands\Routes;
13
14
use BlitzPHP\Cli\Commands\Utilities\Routes\AutoRouteCollector;
0 ignored issues
show
Bug introduced by
The type BlitzPHP\Cli\Commands\Ut...utes\AutoRouteCollector was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
Bug introduced by
This use statement conflicts with another class in this namespace, BlitzPHP\Cli\Commands\Routes\AutoRouteCollector. Consider defining an alias.

Let?s assume that you have a directory layout like this:

.
|-- OtherDir
|   |-- Bar.php
|   `-- Foo.php
`-- SomeDir
    `-- Foo.php

and let?s assume the following content of Bar.php:

// Bar.php
namespace OtherDir;

use SomeDir\Foo; // This now conflicts the class OtherDir\Foo

If both files OtherDir/Foo.php and SomeDir/Foo.php are loaded in the same runtime, you will see a PHP error such as the following:

PHP Fatal error:  Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php

However, as OtherDir/Foo.php does not necessarily have to be loaded and the error is only triggered if it is loaded before OtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias:

// Bar.php
namespace OtherDir;

use SomeDir\Foo as SomeDirFoo; // There is no conflict anymore.
Loading history...
15
use BlitzPHP\Cli\Commands\Utilities\Routes\MiddlewareCollector;
0 ignored issues
show
Bug introduced by
This use statement conflicts with another class in this namespace, BlitzPHP\Cli\Commands\Routes\MiddlewareCollector. Consider defining an alias.

Let?s assume that you have a directory layout like this:

.
|-- OtherDir
|   |-- Bar.php
|   `-- Foo.php
`-- SomeDir
    `-- Foo.php

and let?s assume the following content of Bar.php:

// Bar.php
namespace OtherDir;

use SomeDir\Foo; // This now conflicts the class OtherDir\Foo

If both files OtherDir/Foo.php and SomeDir/Foo.php are loaded in the same runtime, you will see a PHP error such as the following:

PHP Fatal error:  Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php

However, as OtherDir/Foo.php does not necessarily have to be loaded and the error is only triggered if it is loaded before OtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias:

// Bar.php
namespace OtherDir;

use SomeDir\Foo as SomeDirFoo; // There is no conflict anymore.
Loading history...
Bug introduced by
The type BlitzPHP\Cli\Commands\Ut...tes\MiddlewareCollector was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
16
use BlitzPHP\Cli\Commands\Utilities\Routes\SampleURIGenerator;
0 ignored issues
show
Bug introduced by
The type BlitzPHP\Cli\Commands\Ut...utes\SampleURIGenerator was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
Bug introduced by
This use statement conflicts with another class in this namespace, BlitzPHP\Cli\Commands\Routes\SampleURIGenerator. Consider defining an alias.

Let?s assume that you have a directory layout like this:

.
|-- OtherDir
|   |-- Bar.php
|   `-- Foo.php
`-- SomeDir
    `-- Foo.php

and let?s assume the following content of Bar.php:

// Bar.php
namespace OtherDir;

use SomeDir\Foo; // This now conflicts the class OtherDir\Foo

If both files OtherDir/Foo.php and SomeDir/Foo.php are loaded in the same runtime, you will see a PHP error such as the following:

PHP Fatal error:  Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php

However, as OtherDir/Foo.php does not necessarily have to be loaded and the error is only triggered if it is loaded before OtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias:

// Bar.php
namespace OtherDir;

use SomeDir\Foo as SomeDirFoo; // There is no conflict anymore.
Loading history...
17
use BlitzPHP\Cli\Console\Command;
18
use BlitzPHP\Loader\Services;
19
use BlitzPHP\Utilities\Helpers;
20
use Closure;
21
22
/**
23
 * Répertorie toutes les routes.
24
 * Cela inclura tous les fichiers Routes qui peuvent être découverts, et inclura les routes qui ne sont pas définies
25
 * dans les fichiers de routes, mais sont plutôt découverts via le routage automatique.
26
 */
27
class Routes extends Command
28
{
29
    /**
30
     * @var string Groupe
31
     */
32
    protected $group = 'BlitzPHP';
33
34
    /**
35
     * @var string Nom
36
     */
37
    protected $name = 'route:list';
38
39
    /**
40
     * @var string Description
41
     */
42
    protected $description = 'Affiche toutes les routes.';
43
44
    /**
45
     * @var string
46
     */
47
    protected $service = 'Service de routing';
48
49
    /**
50
     * Les options de la commande.
51
     *
52
     * @var array<string, string>
53
     */
54
    protected $options = [
55
        '-h' => 'Trier par gestionnaire..',
56
    ];
57
58
    /**
59
     * {@inheritDoc}
60
     */
61
    public function execute(array $params)
62
    {
63
        $sortByHandler = $this->option('h', false);
64
65
        $collection = Services::routes()->loadRoutes();
66
        $methods    = [
67
            'get',
68
            'head',
69
            'post',
70
            'patch',
71
            'put',
72
            'delete',
73
            'options',
74
            'trace',
75
            'connect',
76
            'cli',
77
        ];
78
79
        $tbody               = [];
80
        $uriGenerator        = new SampleURIGenerator($collection);
81
        $middlewareCollector = new MiddlewareCollector();
82
83
        foreach ($methods as $method) {
84
            $routes = $collection->getRoutes($method);
85
86
            foreach ($routes as $route => $handler) {
87
                if (is_string($handler) || $handler instanceof Closure) {
88
                    $sampleUri = $uriGenerator->get($route);
89
                    $filters   = $middlewareCollector->get($method, $sampleUri);
90
                    
91
                    if ($handler instanceof Closure) {
92
                        $handler = '(Closure)';
93
                    }
94
95
                    $routeName = $collection->getRoutesOptions($route)['as'] ?? '»';
96
97
                    
98
                    $tbody[] = [
99
                        strtoupper($method),
100
                        $route,
101
                        $routeName,
102
                        $handler,
103
                        implode(' ', array_map([Helpers::class, 'classBasename'], $filters)),
104
                    ];
105
                }
106
            }
107
        }
108
109
        if ($collection->shouldAutoRoute()) {
110
            $autoRouteCollector = new AutoRouteCollector(
111
                $collection->getDefaultNamespace(),
112
                $collection->getDefaultController(),
113
                $collection->getDefaultMethod()
114
            );
115
116
            $autoRoutes = $autoRouteCollector->get();
117
118
            foreach ($autoRoutes as &$routes) {
119
                // Il n'y a pas de méthode "auto", mais il est intentionnel de ne pas obtenir de middlewares de route.
120
                $filters = $middlewareCollector->get('auto', $uriGenerator->get($routes[1]));
121
122
                $routes[] = implode(' ', array_map([Helpers::class, 'classBasename'], $filters));
123
            }
124
  
125
            $tbody = [...$tbody, ...$autoRoutes];
126
        }
127
128
        // Trier par gestionnaire.
129
        if ($sortByHandler) {
130
            usort($tbody, static fn ($handler1, $handler2) => strcmp($handler1[3], $handler2[3]));
131
        }
132
133
        $table = [];
134
        foreach ($tbody as $route) {
135
            $table[] = [
136
                               'Methode'                           => $route[0],
137
                               'Route'                             => $route[1],
138
                               'Nom'                               => $route[2],
139
                $sortByHandler ? 'Gestionnaire ↓' : 'Gestionnaire' => $route[3],
140
                               'Middlewares'                       => $route[4],
141
            ];
142
        }
143
144
        $this->table($table);
145
    }
146
}
147