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 |
||
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 = []) |
|
92 | |||
93 | /** |
||
94 | * 生成资源路由规则 |
||
95 | * @access protected |
||
96 | * @return void |
||
97 | */ |
||
98 | 3 | protected function buildResourceRule(): void |
|
145 | |||
146 | /** |
||
147 | * 设置资源允许 |
||
148 | * @access public |
||
149 | * @param array $only 资源允许 |
||
150 | * @return $this |
||
151 | */ |
||
152 | public function only(array $only) |
||
156 | |||
157 | /** |
||
158 | * 设置资源排除 |
||
159 | * @access public |
||
160 | * @param array $except 排除资源 |
||
161 | * @return $this |
||
162 | */ |
||
163 | public function except(array $except) |
||
167 | |||
168 | /** |
||
169 | * 设置资源路由的变量 |
||
170 | * @access public |
||
171 | * @param array $vars 资源变量 |
||
172 | * @return $this |
||
173 | */ |
||
174 | public function vars(array $vars) |
||
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 = []) |
|
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 = []) |
|
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 = []) |
|
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 = []) |
|
250 | |||
251 | } |
||
252 |
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.