Complex classes like RuleItem 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 RuleItem, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
22 | class RuleItem extends Rule |
||
23 | { |
||
24 | /** |
||
25 | * 是否为MISS规则 |
||
26 | * @var bool |
||
27 | */ |
||
28 | protected $miss = false; |
||
29 | |||
30 | /** |
||
31 | * 是否为额外自动注册的OPTIONS规则 |
||
32 | * @var bool |
||
33 | */ |
||
34 | protected $autoOption = false; |
||
35 | |||
36 | /** |
||
37 | * 架构函数 |
||
38 | * @access public |
||
39 | * @param Route $router 路由实例 |
||
40 | * @param RuleGroup $parent 上级对象 |
||
41 | * @param string $name 路由标识 |
||
42 | * @param string $rule 路由规则 |
||
43 | * @param string|\Closure $route 路由地址 |
||
44 | * @param string $method 请求类型 |
||
45 | */ |
||
46 | 18 | public function __construct(Route $router, RuleGroup $parent, string $name = null, string $rule = '', $route = null, string $method = '*') |
|
58 | |||
59 | /** |
||
60 | * 设置当前路由规则为MISS路由 |
||
61 | * @access public |
||
62 | * @return $this |
||
63 | */ |
||
64 | public function setMiss() |
||
69 | |||
70 | /** |
||
71 | * 判断当前路由规则是否为MISS路由 |
||
72 | * @access public |
||
73 | * @return bool |
||
74 | */ |
||
75 | public function isMiss(): bool |
||
79 | |||
80 | /** |
||
81 | * 设置当前路由为自动注册OPTIONS |
||
82 | * @access public |
||
83 | * @return $this |
||
84 | */ |
||
85 | 18 | public function setAutoOptions() |
|
90 | |||
91 | /** |
||
92 | * 判断当前路由规则是否为自动注册的OPTIONS路由 |
||
93 | * @access public |
||
94 | * @return bool |
||
95 | */ |
||
96 | 6 | public function isAutoOptions(): bool |
|
100 | |||
101 | /** |
||
102 | * 获取当前路由的URL后缀 |
||
103 | * @access public |
||
104 | * @return string|null |
||
105 | */ |
||
106 | public function getSuffix() |
||
118 | |||
119 | /** |
||
120 | * 路由规则预处理 |
||
121 | * @access public |
||
122 | * @param string $rule 路由规则 |
||
123 | * @return void |
||
124 | */ |
||
125 | 18 | public function setRule(string $rule): void |
|
149 | |||
150 | /** |
||
151 | * 设置别名 |
||
152 | * @access public |
||
153 | * @param string $name |
||
154 | * @return $this |
||
155 | */ |
||
156 | public function name(string $name) |
||
163 | |||
164 | /** |
||
165 | * 设置路由标识 用于URL反解生成 |
||
166 | * @access protected |
||
167 | * @param bool $first 是否插入开头 |
||
168 | * @return void |
||
169 | */ |
||
170 | 18 | protected function setRuleName(bool $first = false): void |
|
176 | |||
177 | /** |
||
178 | * 检测路由 |
||
179 | * @access public |
||
180 | * @param Request $request 请求对象 |
||
181 | * @param string $url 访问地址 |
||
182 | * @param array $match 匹配路由变量 |
||
183 | * @param bool $completeMatch 路由是否完全匹配 |
||
184 | * @return Dispatch|false |
||
185 | */ |
||
186 | 18 | public function checkRule(Request $request, string $url, $match = null, bool $completeMatch = false) |
|
208 | |||
209 | /** |
||
210 | * 检测路由(含路由匹配) |
||
211 | * @access public |
||
212 | * @param Request $request 请求对象 |
||
213 | * @param string $url 访问地址 |
||
214 | * @param bool $completeMatch 路由是否完全匹配 |
||
215 | * @return Dispatch|false |
||
216 | */ |
||
217 | 18 | public function check(Request $request, string $url, bool $completeMatch = false) |
|
221 | |||
222 | /** |
||
223 | * URL后缀及Slash检查 |
||
224 | * @access protected |
||
225 | * @param Request $request 请求对象 |
||
226 | * @param string $url 访问地址 |
||
227 | * @param array $option 路由参数 |
||
228 | * @return string |
||
229 | */ |
||
230 | 18 | protected function urlSuffixCheck(Request $request, string $url, array $option = []): string |
|
245 | |||
246 | /** |
||
247 | * 检测URL和规则路由是否匹配 |
||
248 | * @access private |
||
249 | * @param string $url URL地址 |
||
250 | * @param array $option 路由参数 |
||
251 | * @param bool $completeMatch 路由是否完全匹配 |
||
252 | * @return array|false |
||
253 | */ |
||
254 | 18 | private function match(string $url, array $option, bool $completeMatch) |
|
312 | |||
313 | /** |
||
314 | * 设置路由所属分组(用于注解路由) |
||
315 | * @access public |
||
316 | * @param string $name 分组名称或者标识 |
||
317 | * @return $this |
||
318 | */ |
||
319 | public function group(string $name) |
||
330 | } |
||
331 |