Complex classes like RuleGroup often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use RuleGroup, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
26 | class RuleGroup extends Rule |
||
27 | { |
||
28 | /** |
||
29 | * 分组路由(包括子分组) |
||
30 | * @var array |
||
31 | */ |
||
32 | protected $rules = []; |
||
33 | |||
34 | /** |
||
35 | * 分组路由规则 |
||
36 | * @var mixed |
||
37 | */ |
||
38 | protected $rule; |
||
39 | |||
40 | /** |
||
41 | * MISS路由 |
||
42 | * @var RuleItem |
||
43 | */ |
||
44 | protected $miss; |
||
45 | |||
46 | /** |
||
47 | * 完整名称 |
||
48 | * @var string |
||
49 | */ |
||
50 | protected $fullName; |
||
51 | |||
52 | /** |
||
53 | * 分组别名 |
||
54 | * @var string |
||
55 | */ |
||
56 | protected $alias; |
||
57 | |||
58 | /** |
||
59 | * 架构函数 |
||
60 | * @access public |
||
61 | * @param Route $router 路由对象 |
||
62 | * @param RuleGroup $parent 上级对象 |
||
63 | * @param string $name 分组名称 |
||
64 | * @param mixed $rule 分组路由 |
||
65 | */ |
||
66 | 6 | public function __construct(Route $router, RuleGroup $parent = null, string $name = '', $rule = null) |
|
84 | |||
85 | /** |
||
86 | * 设置分组的路由规则 |
||
87 | * @access public |
||
88 | * @return void |
||
89 | */ |
||
90 | 6 | protected function setFullName(): void |
|
106 | |||
107 | /** |
||
108 | * 获取所属域名 |
||
109 | * @access public |
||
110 | * @return string |
||
111 | */ |
||
112 | 6 | public function getDomain(): string |
|
116 | |||
117 | /** |
||
118 | * 获取分组别名 |
||
119 | * @access public |
||
120 | * @return string |
||
121 | */ |
||
122 | public function getAlias(): string |
||
126 | |||
127 | /** |
||
128 | * 检测分组路由 |
||
129 | * @access public |
||
130 | * @param Request $request 请求对象 |
||
131 | * @param string $url 访问地址 |
||
132 | * @param bool $completeMatch 路由是否完全匹配 |
||
133 | * @return Dispatch|false |
||
134 | */ |
||
135 | 18 | public function check(Request $request, string $url, bool $completeMatch = false) |
|
193 | |||
194 | /** |
||
195 | * 分组URL匹配检查 |
||
196 | * @access protected |
||
197 | * @param string $url URL |
||
198 | * @return bool |
||
199 | */ |
||
200 | 18 | protected function checkUrl(string $url): bool |
|
218 | |||
219 | /** |
||
220 | * 设置路由分组别名 |
||
221 | * @access public |
||
222 | * @param string $alias 路由分组别名 |
||
223 | * @return $this |
||
224 | */ |
||
225 | public function alias(string $alias) |
||
232 | |||
233 | /** |
||
234 | * 延迟解析分组的路由规则 |
||
235 | * @access public |
||
236 | * @param bool $lazy 路由是否延迟解析 |
||
237 | * @return $this |
||
238 | */ |
||
239 | 6 | public function lazy(bool $lazy = true) |
|
248 | |||
249 | /** |
||
250 | * 解析分组和域名的路由规则及绑定 |
||
251 | * @access public |
||
252 | * @param mixed $rule 路由规则 |
||
253 | * @return void |
||
254 | */ |
||
255 | 18 | public function parseGroupRule($rule): void |
|
268 | |||
269 | /** |
||
270 | * 检测分组路由 |
||
271 | * @access public |
||
272 | * @param Request $request 请求对象 |
||
273 | * @param array $rules 路由规则 |
||
274 | * @param string $url 访问地址 |
||
275 | * @param bool $completeMatch 路由是否完全匹配 |
||
276 | * @return Dispatch|false |
||
277 | */ |
||
278 | protected function checkMergeRuleRegex(Request $request, array &$rules, string $url, bool $completeMatch) |
||
370 | |||
371 | /** |
||
372 | * 获取分组的MISS路由 |
||
373 | * @access public |
||
374 | * @return RuleItem|null |
||
375 | */ |
||
376 | public function getMissRule(): ? RuleItem |
||
380 | |||
381 | /** |
||
382 | * 注册MISS路由 |
||
383 | * @access public |
||
384 | * @param string|Closure $route 路由地址 |
||
385 | * @param string $method 请求类型 |
||
386 | * @return RuleItem |
||
387 | */ |
||
388 | public function miss($route, string $method = '*') : RuleItem |
||
398 | |||
399 | /** |
||
400 | * 添加分组下的路由规则 |
||
401 | * @access public |
||
402 | * @param string $rule 路由规则 |
||
403 | * @param mixed $route 路由地址 |
||
404 | * @param string $method 请求类型 |
||
405 | * @return RuleItem |
||
406 | */ |
||
407 | 18 | public function addRule(string $rule, $route = null, string $method = '*'): RuleItem |
|
429 | |||
430 | /** |
||
431 | * 注册分组下的路由规则 |
||
432 | * @access public |
||
433 | * @param Rule $rule 路由规则 |
||
434 | * @param string $method 请求类型 |
||
435 | * @return $this |
||
436 | */ |
||
437 | 18 | public function addRuleItem(Rule $rule, string $method = '*') |
|
452 | |||
453 | /** |
||
454 | * 设置分组的路由前缀 |
||
455 | * @access public |
||
456 | * @param string $prefix 路由前缀 |
||
457 | * @return $this |
||
458 | */ |
||
459 | public function prefix(string $prefix) |
||
467 | |||
468 | /** |
||
469 | * 合并分组的路由规则正则 |
||
470 | * @access public |
||
471 | * @param bool $merge 是否合并 |
||
472 | * @return $this |
||
473 | */ |
||
474 | 6 | public function mergeRuleRegex(bool $merge = true) |
|
478 | |||
479 | /** |
||
480 | * 获取完整分组Name |
||
481 | * @access public |
||
482 | * @return string |
||
483 | */ |
||
484 | 18 | public function getFullName(): string |
|
488 | |||
489 | /** |
||
490 | * 获取分组的路由规则 |
||
491 | * @access public |
||
492 | * @param string $method 请求类型 |
||
493 | * @return array |
||
494 | */ |
||
495 | 18 | public function getRules(string $method = ''): array |
|
505 | |||
506 | /** |
||
507 | * 清空分组下的路由规则 |
||
508 | * @access public |
||
509 | * @return void |
||
510 | */ |
||
511 | public function clear(): void |
||
515 | } |
||
516 |
If you implement
__call
and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.This is often the case, when
__call
is implemented by a parent class and only the child class knows which methods exist: