| Total Complexity | 79 | 
| Total Lines | 493 | 
| Duplicated Lines | 0 % | 
| Coverage | 0% | 
| Changes | 0 | ||
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.
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 | ||
| 23 | class RuleGroup extends Rule | ||
| 1 ignored issue–
                            show | |||
| 24 | { | ||
| 25 | // 分组路由(包括子分组) | ||
| 26 | protected $rules = [ | ||
| 27 | '*' => [], | ||
| 28 | 'get' => [], | ||
| 29 | 'post' => [], | ||
| 30 | 'put' => [], | ||
| 31 | 'patch' => [], | ||
| 32 | 'delete' => [], | ||
| 33 | 'head' => [], | ||
| 34 | 'options' => [], | ||
| 35 | ]; | ||
| 36 | |||
| 37 | protected $rule; | ||
| 38 | |||
| 39 | /** | ||
| 40 | * MISS路由 | ||
| 41 | * @var RuleItem | ||
| 42 | */ | ||
| 43 | protected $miss; | ||
| 44 | |||
| 45 | // 完整名称 | ||
| 46 | protected $fullName; | ||
| 47 | |||
| 48 | // 所在域名 | ||
| 49 | protected $domain; | ||
| 50 | |||
| 51 | /** | ||
| 52 | * 架构函数 | ||
| 53 | * @access public | ||
| 54 | * @param Route $router 路由对象 | ||
| 55 | * @param RuleGroup $parent 上级对象 | ||
| 56 | * @param string $name 分组名称 | ||
| 57 | * @param mixed $rule 分组路由 | ||
| 58 | */ | ||
| 59 | public function __construct(Route $router, RuleGroup $parent = null, string $name = '', $rule = null) | ||
| 60 |     { | ||
| 61 | $this->router = $router; | ||
| 62 | $this->parent = $parent; | ||
| 63 | $this->rule = $rule; | ||
| 64 | $this->name = trim($name, '/'); | ||
| 65 | |||
| 66 | $this->setFullName(); | ||
| 67 | |||
| 68 |         if ($this->parent) { | ||
| 69 | $this->domain = $this->parent->getDomain(); | ||
| 70 | $this->parent->addRuleItem($this); | ||
| 71 | } | ||
| 72 | |||
| 73 |         if ($router->isTest()) { | ||
| 74 | $this->lazy(false); | ||
| 75 | } | ||
| 76 | } | ||
| 77 | |||
| 78 | /** | ||
| 79 | * 设置分组的路由规则 | ||
| 80 | * @access public | ||
| 81 | * @return void | ||
| 82 | */ | ||
| 83 | protected function setFullName(): void | ||
| 93 | } | ||
| 94 | } | ||
| 95 | |||
| 96 | /** | ||
| 97 | * 获取所属域名 | ||
| 98 | * @access public | ||
| 99 | * @return string | ||
| 100 | */ | ||
| 101 | public function getDomain(): string | ||
| 102 |     { | ||
| 103 | return $this->domain ?: '-'; | ||
| 104 | } | ||
| 105 | |||
| 106 | /** | ||
| 107 | * 检测分组路由 | ||
| 108 | * @access public | ||
| 109 | * @param Request $request 请求对象 | ||
| 110 | * @param string $url 访问地址 | ||
| 111 | * @param bool $completeMatch 路由是否完全匹配 | ||
| 112 | * @return Dispatch|false | ||
| 113 | */ | ||
| 114 | public function check(Request $request, string $url, bool $completeMatch = false) | ||
| 115 |     { | ||
| 116 | // 检查分组有效性 | ||
| 117 |         if (!$this->checkOption($this->option, $request) || !$this->checkUrl($url)) { | ||
| 118 | return false; | ||
| 119 | } | ||
| 120 | |||
| 121 | // 解析分组路由 | ||
| 122 |         if ($this instanceof Resource) { | ||
| 123 | $this->buildResourceRule(); | ||
| 124 |         } elseif ($this->rule instanceof Response) { | ||
| 125 | return new ResponseDispatch($request, $this, $this->rule); | ||
| 126 |         } else { | ||
| 127 | $this->parseGroupRule($this->rule); | ||
| 128 | } | ||
| 129 | |||
| 130 | // 获取当前路由规则 | ||
| 131 | $method = strtolower($request->method()); | ||
| 132 | $rules = $this->getMethodRules($method); | ||
| 133 | |||
| 134 |         if ($this->parent) { | ||
| 135 | // 合并分组参数 | ||
| 136 | $this->mergeGroupOptions(); | ||
| 137 | // 合并分组变量规则 | ||
| 138 | $this->pattern = array_merge($this->parent->getPattern(), $this->pattern); | ||
| 139 | } | ||
| 140 | |||
| 141 |         if (isset($this->option['complete_match'])) { | ||
| 142 | $completeMatch = $this->option['complete_match']; | ||
| 143 | } | ||
| 144 | |||
| 145 |         if (!empty($this->option['merge_rule_regex'])) { | ||
| 146 | // 合并路由正则规则进行路由匹配检查 | ||
| 147 | $result = $this->checkMergeRuleRegex($request, $rules, $url, $completeMatch); | ||
| 148 | |||
| 149 |             if (false !== $result) { | ||
| 150 | return $result; | ||
| 151 | } | ||
| 152 | } | ||
| 153 | |||
| 154 | // 检查分组路由 | ||
| 155 |         foreach ($rules as $key => $item) { | ||
| 156 | $result = $item->check($request, $url, $completeMatch); | ||
| 157 | |||
| 158 |             if (false !== $result) { | ||
| 159 | return $result; | ||
| 160 | } | ||
| 161 | } | ||
| 162 | |||
| 163 |         if ($this->miss && in_array($this->miss->getMethod(), ['*', $method])) { | ||
| 164 | // 未匹配所有路由的路由规则处理 | ||
| 165 | $result = $this->parseRule($request, '', $this->miss->getRoute(), $url, $this->miss->mergeGroupOptions()); | ||
| 166 |         } else { | ||
| 167 | $result = false; | ||
| 168 | } | ||
| 169 | |||
| 170 | return $result; | ||
| 171 | } | ||
| 172 | |||
| 173 | /** | ||
| 174 | * 获取当前请求的路由规则(包括子分组、资源路由) | ||
| 175 | * @access protected | ||
| 176 | * @param string $method 请求类型 | ||
| 177 | * @return array | ||
| 178 | */ | ||
| 179 | protected function getMethodRules(string $method): array | ||
| 182 | } | ||
| 183 | |||
| 184 | /** | ||
| 185 | * 分组URL匹配检查 | ||
| 186 | * @access protected | ||
| 187 | * @param string $url URL | ||
| 188 | * @return bool | ||
| 189 | */ | ||
| 190 | protected function checkUrl(string $url): bool | ||
| 207 | } | ||
| 208 | |||
| 209 | /** | ||
| 210 | * 延迟解析分组的路由规则 | ||
| 211 | * @access public | ||
| 212 | * @param bool $lazy 路由是否延迟解析 | ||
| 213 | * @return $this | ||
| 214 | */ | ||
| 215 | public function lazy(bool $lazy = true) | ||
| 216 |     { | ||
| 217 |         if (!$lazy) { | ||
| 218 | $this->parseGroupRule($this->rule); | ||
| 219 | $this->rule = null; | ||
| 220 | } | ||
| 221 | |||
| 222 | return $this; | ||
| 223 | } | ||
| 224 | |||
| 225 | /** | ||
| 226 | * 解析分组和域名的路由规则及绑定 | ||
| 227 | * @access public | ||
| 228 | * @param mixed $rule 路由规则 | ||
| 229 | * @return void | ||
| 230 | */ | ||
| 231 | public function parseGroupRule($rule): void | ||
| 232 |     { | ||
| 233 | $origin = $this->router->getGroup(); | ||
| 234 | $this->router->setGroup($this); | ||
| 235 | |||
| 236 |         if ($rule instanceof \Closure) { | ||
| 237 | Container::getInstance()->invokeFunction($rule); | ||
| 238 |         } elseif (is_string($rule) && $rule) { | ||
| 239 | $this->router->bind($rule, $this->domain); | ||
| 240 | } | ||
| 241 | |||
| 242 | $this->router->setGroup($origin); | ||
| 243 | } | ||
| 244 | |||
| 245 | /** | ||
| 246 | * 检测分组路由 | ||
| 247 | * @access public | ||
| 248 | * @param Request $request 请求对象 | ||
| 249 | * @param array $rules 路由规则 | ||
| 250 | * @param string $url 访问地址 | ||
| 251 | * @param bool $completeMatch 路由是否完全匹配 | ||
| 252 | * @return Dispatch|false | ||
| 253 | */ | ||
| 254 | protected function checkMergeRuleRegex(Request $request, array &$rules, string $url, bool $completeMatch) | ||
| 255 |     { | ||
| 256 |         $depr  = $this->router->config('pathinfo_depr'); | ||
| 257 |         $url   = $depr . str_replace('|', $depr, $url); | ||
| 258 | $regex = []; | ||
| 259 | $items = []; | ||
| 260 | |||
| 261 |         foreach ($rules as $key => $item) { | ||
| 262 |             if ($item instanceof RuleItem) { | ||
| 263 |                 $rule = $depr . str_replace('/', $depr, $item->getRule()); | ||
| 264 |                 if ($depr == $rule && $depr != $url) { | ||
| 265 | unset($rules[$key]); | ||
| 266 | continue; | ||
| 267 | } | ||
| 268 | |||
| 269 |                 $complete = $item->getOption('complete_match', $completeMatch); | ||
| 270 | |||
| 271 |                 if (false === strpos($rule, '<')) { | ||
| 272 |                     if (0 === strcasecmp($rule, $url) || (!$complete && 0 === strncasecmp($rule, $url, strlen($rule)))) { | ||
| 273 | return $item->checkRule($request, $url, []); | ||
| 274 | } | ||
| 275 | |||
| 276 | unset($rules[$key]); | ||
| 277 | continue; | ||
| 278 | } | ||
| 279 | |||
| 280 |                 $slash = preg_quote('/-' . $depr, '/'); | ||
| 281 | |||
| 282 |                 if ($matchRule = preg_split('/[' . $slash . ']<\w+\??>/', $rule, 2)) { | ||
| 283 |                     if ($matchRule[0] && 0 !== strncasecmp($rule, $url, strlen($matchRule[0]))) { | ||
| 284 | unset($rules[$key]); | ||
| 285 | continue; | ||
| 286 | } | ||
| 287 | } | ||
| 288 | |||
| 289 |                 if (preg_match_all('/[' . $slash . ']?<?\w+\??>?/', $rule, $matches)) { | ||
| 290 | unset($rules[$key]); | ||
| 291 | $pattern = array_merge($this->getPattern(), $item->getPattern()); | ||
| 292 | $option = array_merge($this->getOption(), $item->getOption()); | ||
| 293 | |||
| 294 | $regex[$key] = $this->buildRuleRegex($rule, $matches[0], $pattern, $option, $complete, '_THINK_' . $key); | ||
| 295 | $items[$key] = $item; | ||
| 296 | } | ||
| 297 | } | ||
| 298 | } | ||
| 299 | |||
| 300 |         if (empty($regex)) { | ||
| 301 | return false; | ||
| 302 | } | ||
| 303 | |||
| 304 |         try { | ||
| 305 |             $result = preg_match('/^(?:' . implode('|', $regex) . ')/u', $url, $match); | ||
| 306 |         } catch (\Exception $e) { | ||
| 307 |             throw new Exception('route pattern error'); | ||
| 308 | } | ||
| 309 | |||
| 310 |         if ($result) { | ||
| 311 | $var = []; | ||
| 312 |             foreach ($match as $key => $val) { | ||
| 313 |                 if (is_string($key) && '' !== $val) { | ||
| 314 |                     list($name, $pos) = explode('_THINK_', $key); | ||
| 315 | |||
| 316 | $var[$name] = $val; | ||
| 317 | } | ||
| 318 | } | ||
| 319 | |||
| 320 |             if (!isset($pos)) { | ||
| 321 |                 foreach ($regex as $key => $item) { | ||
| 322 |                     if (0 === strpos(str_replace(['\/', '\-', '\\' . $depr], ['/', '-', $depr], $item), $match[0])) { | ||
| 323 | $pos = $key; | ||
| 324 | break; | ||
| 325 | } | ||
| 326 | } | ||
| 327 | } | ||
| 328 | |||
| 329 | $rule = $items[$pos]->getRule(); | ||
| 330 | $array = $this->router->getRule($rule); | ||
| 331 | |||
| 332 |             foreach ($array as $item) { | ||
| 333 |                 if (in_array($item->getMethod(), ['*', strtolower($request->method())])) { | ||
| 334 | $result = $item->checkRule($request, $url, $var); | ||
| 335 | |||
| 336 |                     if (false !== $result) { | ||
| 337 | return $result; | ||
| 338 | } | ||
| 339 | } | ||
| 340 | } | ||
| 341 | } | ||
| 342 | |||
| 343 | return false; | ||
| 344 | } | ||
| 345 | |||
| 346 | /** | ||
| 347 | * 获取分组的MISS路由 | ||
| 348 | * @access public | ||
| 349 | * @return RuleItem|null | ||
| 350 | */ | ||
| 351 | public function getMissRule(): ? RuleItem | ||
| 352 |     { | ||
| 353 | return $this->miss; | ||
| 354 | } | ||
| 355 | |||
| 356 | /** | ||
| 357 | * 注册MISS路由 | ||
| 358 | * @access public | ||
| 359 | * @param string|Closure $route 路由地址 | ||
| 360 | * @param string $method 请求类型 | ||
| 361 | * @return RuleItem | ||
| 362 | */ | ||
| 363 | public function miss($route, string $method = '*') : RuleItem | ||
| 364 |     { | ||
| 365 | // 创建路由规则实例 | ||
| 366 | $ruleItem = new RuleItem($this->router, $this, null, '', $route, strtolower($method)); | ||
| 367 | |||
| 368 | $this->miss = $ruleItem; | ||
| 369 | |||
| 370 | return $ruleItem; | ||
| 371 | } | ||
| 372 | |||
| 373 | /** | ||
| 374 | * 添加分组下的路由规则或者子分组 | ||
| 375 | * @access public | ||
| 376 | * @param string $rule 路由规则 | ||
| 377 | * @param mixed $route 路由地址 | ||
| 378 | * @param string $method 请求类型 | ||
| 379 | * @return RuleItem | ||
| 380 | */ | ||
| 381 | public function addRule(string $rule, $route = null, string $method = '*'): RuleItem | ||
| 382 |     { | ||
| 383 | // 读取路由标识 | ||
| 384 |         if (is_string($route)) { | ||
| 385 | $name = $route; | ||
| 386 |         } else { | ||
| 387 | $name = null; | ||
| 388 | } | ||
| 389 | |||
| 390 | $method = strtolower($method); | ||
| 391 | |||
| 392 |         if ('' === $rule || '/' === $rule) { | ||
| 393 | $rule .= '$'; | ||
| 394 | } | ||
| 395 | |||
| 396 | // 创建路由规则实例 | ||
| 397 | $ruleItem = new RuleItem($this->router, $this, $name, $rule, $route, $method); | ||
| 398 | |||
| 399 | $this->addRuleItem($ruleItem, $method); | ||
| 400 | |||
| 401 | return $ruleItem; | ||
| 402 | } | ||
| 403 | |||
| 404 | public function addRuleItem(Rule $rule, string $method = '*') | ||
| 414 | } | ||
| 415 | |||
| 416 | /** | ||
| 417 | * 设置分组的路由前缀 | ||
| 418 | * @access public | ||
| 419 | * @param string $prefix 路由前缀 | ||
| 420 | * @return $this | ||
| 421 | */ | ||
| 422 | public function prefix(string $prefix) | ||
| 423 |     { | ||
| 424 |         if ($this->parent && $this->parent->getOption('prefix')) { | ||
| 425 |             $prefix = $this->parent->getOption('prefix') . $prefix; | ||
| 426 | } | ||
| 427 | |||
| 428 |         return $this->setOption('prefix', $prefix); | ||
| 429 | } | ||
| 430 | |||
| 431 | /** | ||
| 432 | * 设置资源允许 | ||
| 433 | * @access public | ||
| 434 | * @param array $only 资源允许 | ||
| 435 | * @return $this | ||
| 436 | */ | ||
| 437 | public function only(array $only) | ||
| 438 |     { | ||
| 439 |         return $this->setOption('only', $only); | ||
| 440 | } | ||
| 441 | |||
| 442 | /** | ||
| 443 | * 设置资源排除 | ||
| 444 | * @access public | ||
| 445 | * @param array $except 排除资源 | ||
| 446 | * @return $this | ||
| 447 | */ | ||
| 448 | public function except(array $except) | ||
| 449 |     { | ||
| 450 |         return $this->setOption('except', $except); | ||
| 451 | } | ||
| 452 | |||
| 453 | /** | ||
| 454 | * 设置资源路由的变量 | ||
| 455 | * @access public | ||
| 456 | * @param array $vars 资源变量 | ||
| 457 | * @return $this | ||
| 458 | */ | ||
| 459 | public function vars(array $vars) | ||
| 460 |     { | ||
| 461 |         return $this->setOption('var', $vars); | ||
| 462 | } | ||
| 463 | |||
| 464 | /** | ||
| 465 | * 合并分组的路由规则正则 | ||
| 466 | * @access public | ||
| 467 | * @param bool $merge 是否合并 | ||
| 468 | * @return $this | ||
| 469 | */ | ||
| 470 | public function mergeRuleRegex(bool $merge = true) | ||
| 471 |     { | ||
| 472 |         return $this->setOption('merge_rule_regex', $merge); | ||
| 473 | } | ||
| 474 | |||
| 475 | /** | ||
| 476 | * 获取完整分组Name | ||
| 477 | * @access public | ||
| 478 | * @return string | ||
| 479 | */ | ||
| 480 | public function getFullName(): ? string | ||
| 483 | } | ||
| 484 | |||
| 485 | /** | ||
| 486 | * 获取分组的路由规则 | ||
| 487 | * @access public | ||
| 488 | * @param string $method 请求类型 | ||
| 489 | * @return array | ||
| 490 | */ | ||
| 491 | public function getRules(string $method = '') : array | ||
| 492 |     { | ||
| 493 |         if ('' === $method) { | ||
| 494 | return $this->rules; | ||
| 495 | } | ||
| 496 | |||
| 497 | return $this->rules[strtolower($method)] ?? []; | ||
| 498 | } | ||
| 499 | |||
| 500 | /** | ||
| 501 | * 清空分组下的路由规则 | ||
| 502 | * @access public | ||
| 503 | * @return void | ||
| 504 | */ | ||
| 505 | public function clear(): void | ||
| 516 | ]; | ||
| 517 | } | ||
| 518 | } | ||
| 519 |