Completed
Push — 6.0 ( e89165...09da7f )
by liu
06:38 queued 15s
created

RouteList::getRouteList()   F

Complexity

Conditions 12
Paths 720

Size

Total Lines 70
Code Lines 39

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 156

Importance

Changes 3
Bugs 0 Features 0
Metric Value
cc 12
eloc 39
c 3
b 0
f 0
nc 720
nop 1
dl 0
loc 70
ccs 0
cts 37
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()
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 (empty($app) && $this->isMultiApp()) {
46
            $output->writeln('<error>Miss app name!</error>');
47
            return false;
48
        }
49
50
        if ($app) {
51
            $filename = $this->app->getRootPath() . 'runtime' . DIRECTORY_SEPARATOR . $app . DIRECTORY_SEPARATOR . 'route_list.php';
52
        } else {
53
            $filename = $this->app->getRootPath() . 'runtime' . DIRECTORY_SEPARATOR . 'route_list.php';
54
        }
55
56
        if (is_file($filename)) {
57
            unlink($filename);
58
        } elseif (!is_dir(dirname($filename))) {
59
            mkdir(dirname($filename), 0755);
60
        }
61
62
        $content = $this->getRouteList($app);
63
        file_put_contents($filename, 'Route List' . PHP_EOL . $content);
64
    }
65
66
    protected function getRouteList(string $app = null): string
0 ignored issues
show
Coding Style introduced by
Missing doc comment for function getRouteList()
Loading history...
67
    {
68
        $this->app->route->setTestMode(true);
69
        $this->app->route->clear();
70
71
        if ($app) {
72
            $path = $this->app->getRootPath() . 'route' . DIRECTORY_SEPARATOR . $app . DIRECTORY_SEPARATOR;
73
        } else {
74
            $path = $this->app->getRootPath() . 'route' . DIRECTORY_SEPARATOR;
75
        }
76
77
        $files = is_dir($path) ? scandir($path) : [];
78
79
        foreach ($files as $file) {
80
            if (strpos($file, '.php')) {
81
                include $path . $file;
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