Passed
Push — 8.0 ( d0e1cd...7f09d5 )
by liu
02:13
created

RuleName::getName()   B

Complexity

Conditions 10
Paths 4

Size

Total Lines 26
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 110

Importance

Changes 0
Metric Value
cc 10
eloc 15
nc 4
nop 3
dl 0
loc 26
ccs 0
cts 15
cp 0
crap 110
rs 7.6666
c 0
b 0
f 0

How to fix   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 ]
4
// +----------------------------------------------------------------------
5
// | Copyright (c) 2006~2023 http://thinkphp.cn All rights reserved.
6
// +----------------------------------------------------------------------
7
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
8
// +----------------------------------------------------------------------
9
// | Author: liu21st <[email protected]>
10
// +----------------------------------------------------------------------
11
declare(strict_types=1);
12
13
namespace think\route;
14
15
/**
16
 * 路由标识管理类
17
 */
18
class RuleName
19
{
20
    /**
21
     * 路由标识
22
     * @var array
23
     */
24
    protected $item = [];
25
26
    /**
27
     * 路由规则
28
     * @var array
29
     */
30
    protected $rule = [];
31
32
    /**
33
     * 路由分组
34
     * @var array
35
     */
36
    protected $group = [];
37
38
    /**
39
     * 注册路由标识
40
     * @access public
41
     * @param  string   $name  路由标识
42
     * @param  RuleItem $ruleItem 路由规则
43
     * @param  bool     $first 是否优先
44
     * @return void
45
     */
46 12
    public function setName(string $name, RuleItem $ruleItem, bool $first = false): void
47
    {
48 12
        $name = strtolower($name);
49 12
        $item = $this->getRuleItemInfo($ruleItem);
50 12
        if ($first && isset($this->item[$name])) {
51
            array_unshift($this->item[$name], $item);
52
        } else {
53 12
            $this->item[$name][] = $item;
54
        }
55
    }
56
57
    /**
58
     * 注册路由分组标识
59
     * @access public
60
     * @param  string    $name  路由分组标识
61
     * @param  RuleGroup $group 路由分组
62
     * @return void
63
     */
64
    public function setGroup(string $name, RuleGroup $group): void
65
    {
66
        $this->group[strtolower($name)] = $group;
67
    }
68
69
    /**
70
     * 注册路由规则
71
     * @access public
72
     * @param  string   $rule  路由规则
73
     * @param  RuleItem $ruleItem 路由
74
     * @return void
75
     */
76 27
    public function setRule(string $rule, RuleItem $ruleItem): void
77
    {
78 27
        $route = $ruleItem->getRoute();
79
80 27
        if (is_string($route)) {
81 12
            $this->rule[$rule][$route] = $ruleItem;
82
        } else {
83 27
            $this->rule[$rule][] = $ruleItem;
84
        }
85
    }
86
87
    /**
88
     * 根据路由规则获取路由对象(列表)
89
     * @access public
90
     * @param  string $rule   路由标识
91
     * @return RuleItem[]
92
     */
93
    public function getRule(string $rule): array
94
    {
95
        return $this->rule[$rule] ?? [];
96
    }
97
98
    /**
99
     * 根据路由分组标识获取分组
100
     * @access public
101
     * @param  string $name 路由分组标识
102
     * @return RuleGroup|null
103
     */
104
    public function getGroup(string $name): ?RuleGroup
105
    {
106
        return $this->group[strtolower($name)] ?? null;
107
    }
108
109
    /**
110
     * 清空路由规则
111
     * @access public
112
     * @return void
113
     */
114
    public function clear(): void
115
    {
116
        $this->item = [];
117
        $this->rule = [];
118
        $this->group = [];
119
    }
120
121
    /**
122
     * 获取全部路由列表
123
     * @access public
124
     * @return array
125
     */
126
    public function getRuleList(): array
127
    {
128
        $list = [];
129
130
        foreach ($this->rule as $rule => $rules) {
131
            foreach ($rules as $item) {
132
                $val = [];
133
134
                foreach (['method', 'rule', 'name', 'route', 'domain', 'pattern', 'option'] as $param) {
135
                    $call        = 'get' . $param;
136
                    $val[$param] = $item->$call();
137
                }
138
139
                if ($item->isMiss()) {
140
                    $val['rule'] .= '<MISS>';
141
                }
142
143
                $list[] = $val;
144
            }
145
        }
146
147
        return $list;
148
    }
149
150
    /**
151
     * 导入路由标识
152
     * @access public
153
     * @param  array $item 路由标识
154
     * @return void
155
     */
156
    public function import(array $item): void
157
    {
158
        $this->item = $item;
159
    }
160
161
    /**
162
     * 根据路由标识获取路由信息(用于URL生成)
163
     * @access public
164
     * @param  string $name   路由标识
165
     * @param  string $domain 域名
166
     * @param  string $method 请求类型
167
     * @return array
168
     */
169
    public function getName(?string $name = null, ?string $domain = null, string $method = '*'): array
170
    {
171
        if (is_null($name)) {
172
            return $this->item;
173
        }
174
175
        $name   = strtolower($name);
176
        $method = strtolower($method);
177
        $result = [];
178
179
        if (isset($this->item[$name])) {
180
            if (is_null($domain)) {
181
                $result = $this->item[$name];
182
            } else {
183
                foreach ($this->item[$name] as $item) {
184
                    $itemDomain = $item['domain'];
185
                    $itemMethod = $item['method'];
186
187
                    if (($itemDomain == $domain || '-' == $itemDomain) && ('*' == $itemMethod || '*' == $method || $method == $itemMethod)) {
188
                        $result[] = $item;
189
                    }
190
                }
191
            }
192
        }
193
194
        return $result;
195
    }
196
197
    /**
198
     * 获取路由信息
199
     * @access protected
200
     * @param  RuleItem $item 路由规则
201
     * @return array
202
     */
203 12
    protected function getRuleItemInfo(RuleItem $item): array
204
    {
205 12
        return [
206 12
            'rule'   => $item->getRule(),
207 12
            'domain' => $item->getDomain(),
208 12
            'method' => $item->getMethod(),
209 12
            'suffix' => $item->getSuffix(),
210 12
        ];
211
    }
212
}
213