RouteList::getRouteList()   F
last analyzed

Complexity

Conditions 12
Paths 720

Size

Total Lines 69
Code Lines 38

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 156

Importance

Changes 3
Bugs 1 Features 0
Metric Value
cc 12
eloc 38
c 3
b 1
f 0
nc 720
nop 1
dl 0
loc 69
ccs 0
cts 36
cp 0
crap 156
rs 3.1888

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
// | ThinkPHP [ WE CAN DO IT JUST THINK IT ]
4
// +----------------------------------------------------------------------
5
// | Copyright (c) 2006-2016 http://thinkphp.cn All rights reserved.
6
// +----------------------------------------------------------------------
7
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
8
// +----------------------------------------------------------------------
9
// | Author: yunwuxin <[email protected]>
10
// +----------------------------------------------------------------------
11
namespace think\console\command;
12
13
use think\console\Command;
14
use think\console\Input;
15
use think\console\input\Argument;
16
use think\console\input\Option;
17
use think\console\Output;
18
use think\console\Table;
19
use think\event\RouteLoaded;
20
21
class RouteList extends Command
22
{
23
    protected $sortBy = [
24
        'rule'   => 0,
25
        'route'  => 1,
26
        'method' => 2,
27
        'name'   => 3,
28
        'domain' => 4,
29
    ];
30
31
    protected function configure()
32
    {
33
        $this->setName('route:list')
34
            ->addArgument('dir', Argument::OPTIONAL, 'dir name .')
35
            ->addArgument('style', Argument::OPTIONAL, "the style of the table.", 'default')
36
            ->addOption('sort', 's', Option::VALUE_OPTIONAL, 'order by rule name.', 0)
37
            ->addOption('more', 'm', Option::VALUE_NONE, 'show route options.')
38
            ->setDescription('show route list.');
39
    }
40
41
    protected function execute(Input $input, Output $output)
42
    {
43
        $dir = $input->getArgument('dir') ?: '';
44
45
        $filename = $this->app->getRootPath() . 'runtime' . DIRECTORY_SEPARATOR . ($dir ? $dir . DIRECTORY_SEPARATOR : '') . 'route_list.php';
0 ignored issues
show
Bug introduced by
Are you sure $dir of type mixed|string|think\console\input\Argument can be used in concatenation? ( Ignorable by Annotation )

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

45
        $filename = $this->app->getRootPath() . 'runtime' . DIRECTORY_SEPARATOR . ($dir ? /** @scrutinizer ignore-type */ $dir . DIRECTORY_SEPARATOR : '') . 'route_list.php';
Loading history...
46
47
        if (is_file($filename)) {
48
            unlink($filename);
49
        } elseif (!is_dir(dirname($filename))) {
50
            mkdir(dirname($filename), 0755);
51
        }
52
53
        $content = $this->getRouteList($dir);
0 ignored issues
show
Bug introduced by
It seems like $dir can also be of type think\console\input\Argument; however, parameter $dir of think\console\command\RouteList::getRouteList() does only seem to accept null|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

53
        $content = $this->getRouteList(/** @scrutinizer ignore-type */ $dir);
Loading history...
54
        file_put_contents($filename, 'Route List' . PHP_EOL . $content);
55
    }
56
57
    protected function getRouteList(string $dir = null): string
58
    {
59
        $this->app->route->setTestMode(true);
60
        $this->app->route->clear();
61
62
        if ($dir) {
63
            $path = $this->app->getRootPath() . 'route' . DIRECTORY_SEPARATOR . $dir . DIRECTORY_SEPARATOR;
64
        } else {
65
            $path = $this->app->getRootPath() . 'route' . DIRECTORY_SEPARATOR;
66
        }
67
68
        $files = is_dir($path) ? scandir($path) : [];
69
70
        foreach ($files as $file) {
71
            if (strpos($file, '.php')) {
72
                include $path . $file;
73
            }
74
        }
75
76
        //触发路由载入完成事件
77
        $this->app->event->trigger(RouteLoaded::class);
78
79
        $table = new Table();
80
81
        if ($this->input->hasOption('more')) {
82
            $header = ['Rule', 'Route', 'Method', 'Name', 'Domain', 'Option', 'Pattern'];
83
        } else {
84
            $header = ['Rule', 'Route', 'Method', 'Name'];
85
        }
86
87
        $table->setHeader($header);
88
89
        $routeList = $this->app->route->getRuleList();
90
        $rows      = [];
91
92
        foreach ($routeList as $item) {
93
            $item['route'] = $item['route'] instanceof \Closure ? '<Closure>' : $item['route'];
94
            $row           = [$item['rule'], $item['route'], $item['method'], $item['name']];
95
96
            if ($this->input->hasOption('more')) {
97
                array_push($row, $item['domain'], json_encode($item['option']), json_encode($item['pattern']));
98
            }
99
100
            $rows[] = $row;
101
        }
102
103
        if ($this->input->getOption('sort')) {
104
            $sort = strtolower($this->input->getOption('sort'));
0 ignored issues
show
Bug introduced by
It seems like $this->input->getOption('sort') can also be of type think\console\input\Option; however, parameter $string of strtolower() 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

104
            $sort = strtolower(/** @scrutinizer ignore-type */ $this->input->getOption('sort'));
Loading history...
105
106
            if (isset($this->sortBy[$sort])) {
107
                $sort = $this->sortBy[$sort];
108
            }
109
110
            uasort($rows, function ($a, $b) use ($sort) {
111
                $itemA = $a[$sort] ?? null;
112
                $itemB = $b[$sort] ?? null;
113
114
                return strcasecmp($itemA, $itemB);
0 ignored issues
show
Bug introduced by
It seems like $itemB can also be of type null; however, parameter $string2 of strcasecmp() 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

114
                return strcasecmp($itemA, /** @scrutinizer ignore-type */ $itemB);
Loading history...
Bug introduced by
It seems like $itemA can also be of type null; however, parameter $string1 of strcasecmp() 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

114
                return strcasecmp(/** @scrutinizer ignore-type */ $itemA, $itemB);
Loading history...
115
            });
116
        }
117
118
        $table->setRows($rows);
119
120
        if ($this->input->getArgument('style')) {
121
            $style = $this->input->getArgument('style');
122
            $table->setStyle($style);
0 ignored issues
show
Bug introduced by
It seems like $style can also be of type think\console\input\Argument; however, parameter $style of think\console\Table::setStyle() 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

122
            $table->setStyle(/** @scrutinizer ignore-type */ $style);
Loading history...
123
        }
124
125
        return $this->table($table);
126
    }
127
128
}
129