Completed
Push — 6.0 ( 4b2d81...f54ee3 )
by liu
05:35
created

RouteList::getRouteList()   D

Complexity

Conditions 15
Paths 240

Size

Total Lines 75
Code Lines 43

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 240

Importance

Changes 3
Bugs 0 Features 0
Metric Value
cc 15
eloc 43
c 3
b 0
f 0
nc 240
nop 1
dl 0
loc 75
ccs 0
cts 40
cp 0
crap 240
rs 4.5833

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()
0 ignored issues
show
Coding Style introduced by
Missing doc comment for function configure()
Loading history...
32
    {
33
        $this->setName('route:list')
34
            ->addArgument('app', Argument::OPTIONAL, 'app 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)
0 ignored issues
show
Coding Style introduced by
Missing doc comment for function execute()
Loading history...
42
    {
43
        $app = $input->getArgument('app');
44
45
        if ($app) {
46
            $filename = $this->app->getRootPath() . 'runtime' . DIRECTORY_SEPARATOR . $app . DIRECTORY_SEPARATOR . 'route_list.php';
0 ignored issues
show
Bug introduced by
Are you sure $app of type mixed|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

46
            $filename = $this->app->getRootPath() . 'runtime' . DIRECTORY_SEPARATOR . /** @scrutinizer ignore-type */ $app . DIRECTORY_SEPARATOR . 'route_list.php';
Loading history...
47
        } else {
48
            $filename = $this->app->getRootPath() . 'runtime' . DIRECTORY_SEPARATOR . 'route_list.php';
49
        }
50
51
        if (is_file($filename)) {
52
            unlink($filename);
53
        } elseif (!is_dir(dirname($filename))) {
54
            mkdir(dirname($filename), 0755);
55
        }
56
57
        $content = $this->getRouteList($app);
58
        file_put_contents($filename, 'Route List' . PHP_EOL . $content);
59
    }
60
61
    protected function getRouteList(string $app = null): string
0 ignored issues
show
Coding Style introduced by
Missing doc comment for function getRouteList()
Loading history...
62
    {
63
        $this->app->route->setTestMode(true);
64
        $this->app->route->clear();
65
66
        if ($app && $this->isMultiApp()) {
67
            $file = $this->app->getRootPath() . 'route' . DIRECTORY_SEPARATOR . $app . '.php';
68
            if (is_file($file)) {
69
                include $file;
70
            }
71
        } else {
72
            $path  = $this->app->getRootPath() . 'route' . DIRECTORY_SEPARATOR;
73
            $files = is_dir($path) ? scandir($path) : [];
74
75
            foreach ($files as $file) {
76
                if (strpos($file, '.php')) {
77
                    if ($this->isMultiApp()) {
78
                        $this->app->route->setAppName(pathinfo($file, PATHINFO_FILENAME));
79
                    }
80
                    include $path . $file;
81
                }
82
            }
83
        }
84
85
        //触发路由载入完成事件
86
        $this->app->event->trigger(RouteLoaded::class);
87
88
        $table = new Table();
89
90
        if ($this->input->hasOption('more')) {
91
            $header = ['Rule', 'Route', 'Method', 'Name', 'Domain', 'Option', 'Pattern'];
92
        } else {
93
            $header = ['Rule', 'Route', 'Method', 'Name'];
94
        }
95
96
        $table->setHeader($header);
97
98
        $routeList = $this->app->route->getRuleList();
99
        $rows      = [];
100
101
        foreach ($routeList as $item) {
102
            $item['route'] = $item['route'] instanceof \Closure ? '<Closure>' : $item['route'];
103
104
            if ($this->input->hasOption('more')) {
105
                $item = [$item['rule'], $item['route'], $item['method'], $item['name'], $item['domain'], json_encode($item['option']), json_encode($item['pattern'])];
106
            } else {
107
                $item = [$item['rule'], $item['route'], $item['method'], $item['name']];
108
            }
109
110
            $rows[] = $item;
111
        }
112
113
        if ($this->input->getOption('sort')) {
114
            $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 $str 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

114
            $sort = strtolower(/** @scrutinizer ignore-type */ $this->input->getOption('sort'));
Loading history...
115
116
            if (isset($this->sortBy[$sort])) {
117
                $sort = $this->sortBy[$sort];
118
            }
119
120
            uasort($rows, function ($a, $b) use ($sort) {
0 ignored issues
show
Coding Style introduced by
The opening parenthesis of a multi-line function call should be the last content on the line.
Loading history...
121
                $itemA = $a[$sort] ?? null;
122
                $itemB = $b[$sort] ?? null;
123
124
                return strcasecmp($itemA, $itemB);
125
            });
0 ignored issues
show
Coding Style introduced by
For multi-line function calls, the closing parenthesis should be on a new line.

If a function call spawns multiple lines, the coding standard suggests to move the closing parenthesis to a new line:

someFunctionCall(
    $firstArgument,
    $secondArgument,
    $thirdArgument
); // Closing parenthesis on a new line.
Loading history...
126
        }
127
128
        $table->setRows($rows);
129
130
        if ($this->input->getArgument('style')) {
131
            $style = $this->input->getArgument('style');
132
            $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

132
            $table->setStyle(/** @scrutinizer ignore-type */ $style);
Loading history...
133
        }
134
135
        return $this->table($table);
136
    }
137
138
}
139