Passed
Pull Request — master (#216)
by Alexander
03:22
created

DebugRoutesCommand::execute()   C

Complexity

Conditions 13
Paths 52

Size

Total Lines 69
Code Lines 48

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 1 Features 0
Metric Value
cc 13
eloc 48
c 2
b 1
f 0
nc 52
nop 2
dl 0
loc 69
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\Yii\Debug\Command;
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;
0 ignored issues
show
Bug introduced by
The type Yiisoft\Router\RouteCollectionInterface 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...
14
use Yiisoft\VarDumper\VarDumper;
15
use Yiisoft\Yii\Console\ExitCode;
16
use Yiisoft\Yii\Debug\Debugger;
17
18
final class DebugRoutesCommand extends Command
19
{
20
    public const COMMAND_NAME = 'debug:routes';
21
    protected static $defaultName = self::COMMAND_NAME;
22
23
    public function __construct(
24
        private RouteCollectionInterface $routeCollection,
25
        private Debugger $debugger,
26
    ) {
27
        parent::__construct();
28
    }
29
30
    protected function configure(): void
31
    {
32
        $this
33
            ->setDescription('Show information about registered routes')
34
            ->addArgument('route', InputArgument::IS_ARRAY, 'Route name');
35
    }
36
37
    protected function execute(InputInterface $input, OutputInterface $output): int
38
    {
39
        $this->debugger->stop();
40
41
        $io = new SymfonyStyle($input, $output);
42
43
        if ($input->hasArgument('route') && !empty($input->getArgument('route'))) {
44
            $routes = (array) $input->getArgument('route');
45
            foreach ($routes as $route) {
46
                $route = $this->routeCollection->getRoute($route);
47
                $data = $route->__debugInfo();
48
                $action = '';
49
                $middlewares = [];
50
                if (!empty($data['middlewareDefinitions'])) {
51
                    $middlewareDefinitions = $data['middlewareDefinitions'];
52
                    $action = array_pop($middlewareDefinitions);
53
                    $middlewares = $middlewareDefinitions;
54
                }
55
56
                $io->title($data['name']);
57
                $definitionList = [
58
                    ['Methods' => $this->export($data['methods'])],
59
                    ['Name' => $data['name']],
60
                    ['Pattern' => $data['pattern']],
61
                ];
62
                if (!empty($action)) {
63
                    $definitionList[] = ['Action' => $this->export($action)];
64
                }
65
                if (!empty($data['defaults'])) {
66
                    $definitionList[] = ['Defaults' => $this->export($data['defaults'])];
67
                }
68
                if (!empty($data['hosts'])) {
69
                    $definitionList[] = ['Hosts' => $this->export($data['hosts'])];
70
                }
71
72
                $io->definitionList(...$definitionList);
73
                if (!empty($middlewares)) {
74
                    $io->section('Middlewares');
75
                    foreach ($middlewares as $middleware) {
76
                        $io->writeln(is_string($middleware) ? $middleware : $this->export($middleware));
77
                    }
78
                }
79
            }
80
81
            return ExitCode::OK;
82
        }
83
84
        $table = new Table($output);
85
        $rows = [];
86
        foreach ($this->routeCollection->getRoutes() as $route) {
87
            $data = $route->__debugInfo();
88
            $action = '';
89
            if (!empty($data['middlewareDefinitions'])) {
90
                $middlewareDefinitions = $data['middlewareDefinitions'];
91
                $action = array_pop($middlewareDefinitions);
92
            }
93
            $rows[] = [
94
                'methods' => $this->export($data['methods']),
95
                'name' => $data['name'],
96
                'hosts' => $this->export($data['hosts']),
97
                'pattern' => $data['pattern'],
98
                'defaults' => $this->export($data['defaults']),
99
                'action' => $this->export($action),
100
            ];
101
        }
102
        $table->addRows($rows);
103
        $table->render();
104
105
        return ExitCode::OK;
106
    }
107
108
    protected function export(mixed $value): string
109
    {
110
        if (is_array($value)
0 ignored issues
show
Coding Style introduced by
The first expression of a multi-line control structure must be on the line after the opening parenthesis
Loading history...
111
            && count($value) === 2
112
            && isset($value[0], $value[1])
113
            && is_string($value[0])
114
            && is_string($value[1])
115
        ) {
116
            return $value[0] . '::' . $value[1];
117
        }
118
        if (is_array($value) && array_is_list($value)) {
0 ignored issues
show
Bug introduced by
The function array_is_list was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

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

118
        if (is_array($value) && /** @scrutinizer ignore-call */ array_is_list($value)) {
Loading history...
119
            return implode(', ', array_map(fn ($item) => $this->export($item), $value));
120
        }
121
        if (is_string($value)) {
122
            return $value;
123
        }
124
        return VarDumper::create($value)->asString();
125
    }
126
}
127