Completed
Branch 6.0 (d30585)
by yun
04:17
created

Resource   A

Complexity

Total Complexity 31

Size/Duplication

Total Lines 232
Duplicated Lines 17.24 %

Coupling/Cohesion

Components 2
Dependencies 2

Test Coverage

Coverage 47.83%

Importance

Changes 0
Metric Value
dl 40
loc 232
rs 9.92
c 0
b 0
f 0
ccs 33
cts 69
cp 0.4783
wmc 31
lcom 2
cbo 2

9 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 25 4
C buildResourceRule() 0 47 15
A only() 0 4 1
A except() 0 4 1
A vars() 0 4 1
A withValidate() 10 10 2
A withModel() 10 10 2
A withMiddleware() 10 10 2
A rest() 10 10 3

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
// +----------------------------------------------------------------------
3
// | ThinkPHP [ WE CAN DO IT JUST THINK ]
4
// +----------------------------------------------------------------------
5
// | Copyright (c) 2006~2019 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
use think\Route;
16
17
/**
18
 * 资源路由类
19
 */
20
class Resource extends RuleGroup
21
{
22
    /**
23
     * 资源路由名称
24
     * @var string
25
     */
26
    protected $resource;
27
28
    /**
29
     * 资源路由地址
30
     * @var string
31
     */
32
    protected $route;
33
34
    /**
35
     * REST方法定义
36
     * @var array
37
     */
38
    protected $rest = [];
39
40
    /**
41
     * 模型绑定
42
     * @var array
43
     */
44
    protected $model = [];
45
46
    /**
47
     * 数据验证
48
     * @var array
49
     */
50
    protected $validate = [];
51
52
    /**
53
     * 中间件
54
     * @var array
55
     */
56
    protected $middleware = [];
57
58
    /**
59
     * 架构函数
60
     * @access public
61
     * @param  Route         $router     路由对象
62
     * @param  RuleGroup     $parent     上级对象
63
     * @param  string        $name       资源名称
64
     * @param  string        $route      路由地址
65
     * @param  array         $rest       资源定义
66
     */
67 3
    public function __construct(Route $router, RuleGroup $parent = null, string $name = '', string $route = '', array $rest = [])
68
    {
69 3
        $name           = ltrim($name, '/');
70 3
        $this->router   = $router;
71 3
        $this->parent   = $parent;
72 3
        $this->resource = $name;
73 3
        $this->route    = $route;
74 3
        $this->name     = strpos($name, '.') ? strstr($name, '.', true) : $name;
75
76 3
        $this->setFullName();
77
78
        // 资源路由默认为完整匹配
79 3
        $this->option['complete_match'] = true;
80
81 3
        $this->rest = $rest;
82
83 3
        if ($this->parent) {
84 3
            $this->domain = $this->parent->getDomain();
85 3
            $this->parent->addRuleItem($this);
86
        }
87
88 3
        if ($router->isTest()) {
89
            $this->buildResourceRule();
90
        }
91 3
    }
92
93
    /**
94
     * 生成资源路由规则
95
     * @access protected
96
     * @return void
97
     */
98 3
    protected function buildResourceRule(): void
99
    {
100 3
        $rule   = $this->resource;
101 3
        $option = $this->option;
102 3
        $origin = $this->router->getGroup();
103 3
        $this->router->setGroup($this);
104
105 3
        if (strpos($rule, '.')) {
106
            // 注册嵌套资源路由
107
            $array = explode('.', $rule);
108
            $last  = array_pop($array);
109
            $item  = [];
110
111
            foreach ($array as $val) {
112
                $item[] = $val . '/<' . ($option['var'][$val] ?? $val . '_id') . '>';
113
            }
114
115
            $rule = implode('/', $item) . '/' . $last;
116
        }
117
118 3
        $prefix = substr($rule, strlen($this->name) + 1);
119
120
        // 注册资源路由
121 3
        foreach ($this->rest as $key => $val) {
122 3
            if ((isset($option['only']) && !in_array($key, $option['only']))
123 3
                || (isset($option['except']) && in_array($key, $option['except']))) {
124
                continue;
125
            }
126
127 3
            if (isset($last) && strpos($val[1], '<id>') && isset($option['var'][$last])) {
128
                $val[1] = str_replace('<id>', '<' . $option['var'][$last] . '>', $val[1]);
129 3
            } elseif (strpos($val[1], '<id>') && isset($option['var'][$rule])) {
130
                $val[1] = str_replace('<id>', '<' . $option['var'][$rule] . '>', $val[1]);
131
            }
132
133 3
            $ruleItem = $this->addRule(trim($prefix . $val[1], '/'), $this->route . '/' . $val[2], $val[0]);
134
135 3
            foreach (['model', 'validate', 'middleware'] as $name) {
136 3
                if (isset($this->$name[$key])) {
137 1
                    call_user_func_array([$ruleItem, $name], (array) $this->$name[$key]);
138
                }
139
140
            }
141
        }
142
143 3
        $this->router->setGroup($origin);
144 3
    }
145
146
    /**
147
     * 设置资源允许
148
     * @access public
149
     * @param  array $only 资源允许
150
     * @return $this
151
     */
152
    public function only(array $only)
153
    {
154
        return $this->setOption('only', $only);
155
    }
156
157
    /**
158
     * 设置资源排除
159
     * @access public
160
     * @param  array $except 排除资源
161
     * @return $this
162
     */
163
    public function except(array $except)
164
    {
165
        return $this->setOption('except', $except);
166
    }
167
168
    /**
169
     * 设置资源路由的变量
170
     * @access public
171
     * @param  array $vars 资源变量
172
     * @return $this
173
     */
174
    public function vars(array $vars)
175
    {
176
        return $this->setOption('var', $vars);
177
    }
178
179
    /**
180
     * 绑定资源验证
181
     * @access public
182
     * @param  array|string $name 资源类型或者验证信息
183
     * @param  array|string $validate 验证信息
184
     * @return $this
185
     */
186 View Code Duplication
    public function withValidate($name, $validate = [])
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
187
    {
188
        if (is_array($name)) {
189
            $this->validate = array_merge($this->validate, $name);
190
        } else {
191
            $this->validate[$name] = $validate;
192
        }
193
194
        return $this;
195
    }
196
197
    /**
198
     * 绑定资源模型
199
     * @access public
200
     * @param  array|string $name 资源类型或者模型绑定
201
     * @param  array|string $model 模型绑定
202
     * @return $this
203
     */
204 View Code Duplication
    public function withModel($name, $model = [])
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
205
    {
206
        if (is_array($name)) {
207
            $this->model = array_merge($this->model, $name);
208
        } else {
209
            $this->model[$name] = $model;
210
        }
211
212
        return $this;
213
    }
214
215
    /**
216
     * 绑定资源模型
217
     * @access public
218
     * @param  array|string $name 资源类型或者中间件定义
219
     * @param  array|string $middleware 中间件定义
220
     * @return $this
221
     */
222 View Code Duplication
    public function withMiddleware($name, $middleware = [])
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
223
    {
224
        if (is_array($name)) {
225
            $this->middleware = array_merge($this->middleware, $name);
226
        } else {
227
            $this->middleware[$name] = $middleware;
228
        }
229
230
        return $this;
231
    }
232
233
    /**
234
     * rest方法定义和修改
235
     * @access public
236
     * @param  array|string  $name 方法名称
237
     * @param  array|bool    $resource 资源
238
     * @return $this
239
     */
240 View Code Duplication
    public function rest($name, $resource = [])
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
241
    {
242
        if (is_array($name)) {
243
            $this->rest = $resource ? $name : array_merge($this->rest, $name);
244
        } else {
245
            $this->rest[$name] = $resource;
246
        }
247
248
        return $this;
249
    }
250
251
}
252