Passed
Pull Request — master (#195)
by Dmitriy
24:02 queued 21:27
created

DebugRoutesCommand::execute()   C

Complexity

Conditions 13
Paths 52

Size

Total Lines 72
Code Lines 48

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 100.7996

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 13
eloc 48
c 1
b 0
f 0
nc 52
nop 2
dl 0
loc 72
ccs 10
cts 51
cp 0.1961
crap 100.7996
rs 6.6166

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
declare(strict_types=1);
4
5
namespace Yiisoft\Router\Debug;
6
7
use Symfony\Component\Console\Command\Command;
8
use Symfony\Component\Console\Helper\Table;
9
use Symfony\Component\Console\Input\InputArgument;
10
use Symfony\Component\Console\Input\InputInterface;
11
use Symfony\Component\Console\Output\OutputInterface;
12
use Symfony\Component\Console\Style\SymfonyStyle;
13
use Yiisoft\Router\RouteCollectionInterface;
14
use Yiisoft\VarDumper\VarDumper;
15
use Yiisoft\Yii\Debug\Debugger;
16
17
final class DebugRoutesCommand extends Command
18
{
19
    public const COMMAND_NAME = 'debug:routes';
20
    protected static $defaultName = self::COMMAND_NAME;
21
22 1
    public function __construct(
23
        private RouteCollectionInterface $routeCollection,
24
        private Debugger $debugger,
25
    ) {
26 1
        parent::__construct();
27
    }
28
29 1
    protected function configure(): void
30
    {
31 1
        $this
32 1
            ->setDescription('Show information about registered routes')
33 1
            ->addArgument('route', InputArgument::IS_ARRAY, 'Route name');
34
    }
35
36
    /**
37
     * @psalm-suppress MixedArgument, MixedAssignment, MixedArrayAccess
38
     */
39 1
    protected function execute(InputInterface $input, OutputInterface $output): int
40
    {
41 1
        $this->debugger->stop();
42
43 1
        $io = new SymfonyStyle($input, $output);
44
45 1
        if ($input->hasArgument('route') && !empty($input->getArgument('route'))) {
46
            /**
47
             * @var string[] $routes
48
             */
49
            $routes = (array) $input->getArgument('route');
50
            foreach ($routes as $route) {
51
                $route = $this->routeCollection->getRoute($route);
52
                $data = $route->__debugInfo();
53
                $action = '';
54
                $middlewares = [];
55
                if (!empty($data['middlewareDefinitions'])) {
56
                    $middlewareDefinitions = $data['middlewareDefinitions'];
57
                    $action = array_pop($middlewareDefinitions);
58
                    $middlewares = $middlewareDefinitions;
59
                }
60
61
                $io->title($data['name']);
0 ignored issues
show
Bug introduced by
It seems like $data['name'] can also be of type null; however, parameter $message of Symfony\Component\Consol...e\SymfonyStyle::title() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

61
                $io->title(/** @scrutinizer ignore-type */ $data['name']);
Loading history...
62
                $definitionList = [
63
                    ['Methods' => $this->export($data['methods'])],
64
                    ['Name' => $data['name']],
65
                    ['Pattern' => $data['pattern']],
66
                ];
67
                if (!empty($action)) {
68
                    $definitionList[] = ['Action' => $this->export($action)];
69
                }
70
                if (!empty($data['defaults'])) {
71
                    $definitionList[] = ['Defaults' => $this->export($data['defaults'])];
72
                }
73
                if (!empty($data['hosts'])) {
74
                    $definitionList[] = ['Hosts' => $this->export($data['hosts'])];
75
                }
76
77
                $io->definitionList(...$definitionList);
78
                if (!empty($middlewares)) {
79
                    $io->section('Middlewares');
80
                    foreach ($middlewares as $middleware) {
81
                        $io->writeln(is_string($middleware) ? $middleware : $this->export($middleware));
82
                    }
83
                }
84
            }
85
86
            return 0;
87
        }
88
89 1
        $table = new Table($output);
90 1
        $rows = [];
91 1
        foreach ($this->routeCollection->getRoutes() as $route) {
92
            $data = $route->__debugInfo();
93
            $action = '';
94
            if (!empty($data['middlewareDefinitions'])) {
95
                $middlewareDefinitions = $data['middlewareDefinitions'];
96
                $action = array_pop($middlewareDefinitions);
97
            }
98
            $rows[] = [
99
                'methods' => $this->export($data['methods']),
100
                'name' => $data['name'],
101
                'hosts' => $this->export($data['hosts']),
102
                'pattern' => $data['pattern'],
103
                'defaults' => $this->export($data['defaults']),
104
                'action' => $this->export($action),
105
            ];
106
        }
107 1
        $table->addRows($rows);
108 1
        $table->render();
109
110 1
        return 0;
111
    }
112
113
    protected function export(mixed $value): string
114
    {
115
        if (is_array($value)
116
            && count($value) === 2
117
            && isset($value[0], $value[1])
118
            && is_string($value[0])
119
            && is_string($value[1])
120
        ) {
121
            return $value[0] . '::' . $value[1];
122
        }
123
        if (is_array($value) && $this->isArrayList($value)) {
124
            return implode(', ', array_map(fn ($item) => $this->export($item), $value));
125
        }
126
        if (is_string($value)) {
127
            return $value;
128
        }
129
        return VarDumper::create($value)->asString();
130
    }
131
132
    /**
133
     * Polyfill for is_array_list() function.
134
     * It is available since PHP 8.1.
135
     */
136
    private function isArrayList(array $array): bool
137
    {
138
        if ([] === $array) {
139
            return true;
140
        }
141
142
        $nextKey = -1;
143
144
        foreach ($array as $k => $_) {
145
            if ($k !== ++$nextKey) {
146
                return false;
147
            }
148
        }
149
150
        return true;
151
    }
152
}
153