Complex classes like Request 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 Request, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
7 | class Request |
||
8 | { |
||
9 | /** |
||
10 | * @var object 对象实例 |
||
11 | */ |
||
12 | protected $instance; |
||
13 | protected $config; |
||
14 | |||
15 | protected $method; |
||
16 | /** |
||
17 | * @var string 域名(含协议和端口) |
||
18 | */ |
||
19 | protected $domain; |
||
20 | |||
21 | /** |
||
22 | * @var string URL地址 |
||
23 | */ |
||
24 | protected $url; |
||
25 | |||
26 | /** |
||
27 | * @var string 基础URL |
||
28 | */ |
||
29 | protected $baseUrl; |
||
30 | |||
31 | /** |
||
32 | * @var string 当前执行的文件 |
||
33 | */ |
||
34 | protected $baseFile; |
||
35 | |||
36 | /** |
||
37 | * @var string 访问的ROOT地址 |
||
38 | */ |
||
39 | protected $root; |
||
40 | |||
41 | /** |
||
42 | * @var string pathinfo |
||
43 | */ |
||
44 | protected $pathinfo; |
||
45 | |||
46 | /** |
||
47 | * @var string pathinfo(不含后缀) |
||
48 | */ |
||
49 | protected $path; |
||
50 | |||
51 | /** |
||
52 | * @var array 当前路由信息 |
||
53 | */ |
||
54 | protected $routeInfo = []; |
||
55 | |||
56 | /** |
||
57 | * @var array 当前调度信息 |
||
58 | */ |
||
59 | protected $dispatch = []; |
||
60 | protected $module; |
||
61 | protected $controller; |
||
62 | protected $action; |
||
63 | // 当前语言集 |
||
64 | protected $langset; |
||
65 | |||
66 | /** |
||
67 | * @var array 请求参数 |
||
68 | */ |
||
69 | protected $param = []; |
||
70 | protected $get = []; |
||
71 | protected $post = []; |
||
72 | protected $request = []; |
||
73 | protected $route = []; |
||
74 | protected $put; |
||
75 | protected $session = []; |
||
76 | protected $file = []; |
||
77 | protected $cookie = []; |
||
78 | protected $server = []; |
||
79 | protected $header = []; |
||
80 | |||
81 | /** |
||
82 | * @var array 资源类型 |
||
83 | */ |
||
84 | protected $mimeType = [ |
||
85 | 'xml' => 'application/xml,text/xml,application/x-xml', |
||
86 | 'json' => 'application/json,text/x-json,application/jsonrequest,text/json', |
||
87 | 'js' => 'text/javascript,application/javascript,application/x-javascript', |
||
88 | 'css' => 'text/css', |
||
89 | 'rss' => 'application/rss+xml', |
||
90 | 'yaml' => 'application/x-yaml,text/yaml', |
||
91 | 'atom' => 'application/atom+xml', |
||
92 | 'pdf' => 'application/pdf', |
||
93 | 'text' => 'text/plain', |
||
94 | 'png' => 'image/png', |
||
95 | 'jpg' => 'image/jpg,image/jpeg,image/pjpeg', |
||
96 | 'gif' => 'image/gif', |
||
97 | 'csv' => 'text/csv', |
||
98 | 'html' => 'text/html,application/xhtml+xml,*/*', |
||
99 | ]; |
||
100 | |||
101 | protected $content; |
||
102 | |||
103 | // 全局过滤规则 |
||
104 | protected $filter; |
||
105 | // Hook扩展方法 |
||
106 | protected static $hook = []; |
||
107 | // 绑定的属性 |
||
108 | protected $bind = []; |
||
109 | // php://input |
||
110 | protected $input; |
||
111 | // 请求缓存 |
||
112 | protected $cache; |
||
113 | // 缓存是否检查 |
||
114 | protected $isCheckCache; |
||
115 | |||
116 | /** |
||
117 | * 架构函数 |
||
118 | * @access public |
||
119 | * @param array $options 参数 |
||
120 | */ |
||
121 | public function __construct(Config $config, $options = []) |
||
136 | |||
137 | public function __call($method, $args) |
||
146 | |||
147 | /** |
||
148 | * Hook 方法注入 |
||
149 | * @access public |
||
150 | * @param string|array $method 方法名 |
||
151 | * @param mixed $callback callable |
||
152 | * @return void |
||
153 | */ |
||
154 | public function hook($method, $callback = null) |
||
162 | |||
163 | /** |
||
164 | * 创建一个URL请求 |
||
165 | * @access public |
||
166 | * @param string $uri URL地址 |
||
167 | * @param string $method 请求类型 |
||
168 | * @param array $params 请求参数 |
||
169 | * @param array $cookie |
||
170 | * @param array $files |
||
171 | * @param array $server |
||
172 | * @param string $content |
||
173 | * @return \think\Request |
||
174 | */ |
||
175 | public function create($uri, $method = 'GET', $params = [], $cookie = [], $files = [], $server = [], $content = null) |
||
245 | |||
246 | /** |
||
247 | * 设置或获取当前包含协议的域名 |
||
248 | * @access public |
||
249 | * @param string $domain 域名 |
||
250 | * @return string |
||
251 | */ |
||
252 | public function domain($domain = null) |
||
262 | |||
263 | /** |
||
264 | * 设置或获取当前完整URL 包括QUERY_STRING |
||
265 | * @access public |
||
266 | * @param string|true $url URL地址 true 带域名获取 |
||
267 | * @return string |
||
268 | */ |
||
269 | public function url($url = null) |
||
289 | |||
290 | /** |
||
291 | * 设置或获取当前URL 不含QUERY_STRING |
||
292 | * @access public |
||
293 | * @param string $url URL地址 |
||
294 | * @return string |
||
295 | */ |
||
296 | public function baseUrl($url = null) |
||
307 | |||
308 | /** |
||
309 | * 设置或获取当前执行的文件 SCRIPT_NAME |
||
310 | * @access public |
||
311 | * @param string $file 当前执行的文件 |
||
312 | * @return string |
||
313 | */ |
||
314 | public function baseFile($file = null) |
||
339 | |||
340 | /** |
||
341 | * 设置或获取URL访问根地址 |
||
342 | * @access public |
||
343 | * @param string $url URL地址 |
||
344 | * @return string |
||
345 | */ |
||
346 | public function root($url = null) |
||
360 | |||
361 | /** |
||
362 | * 获取URL访问根目录 |
||
363 | * @access public |
||
364 | * @return string |
||
365 | */ |
||
366 | public function rootUrl() |
||
375 | |||
376 | |||
377 | /** |
||
378 | * 获取当前请求URL的pathinfo信息(不含URL后缀) |
||
379 | * @access public |
||
380 | * @return string |
||
381 | */ |
||
382 | public function path() |
||
400 | |||
401 | /** |
||
402 | * 当前URL的访问后缀 |
||
403 | * @access public |
||
404 | * @return string |
||
405 | */ |
||
406 | public function ext() |
||
410 | |||
411 | /** |
||
412 | * 获取当前请求的时间 |
||
413 | * @access public |
||
414 | * @param bool $float 是否使用浮点类型 |
||
415 | * @return integer|float |
||
416 | */ |
||
417 | public function time($float = false) |
||
421 | |||
422 | /** |
||
423 | * 当前请求的资源类型 |
||
424 | * @access public |
||
425 | * @return false|string |
||
426 | */ |
||
427 | public function type() |
||
444 | |||
445 | /** |
||
446 | * 设置资源类型 |
||
447 | * @access public |
||
448 | * @param string|array $type 资源类型名 |
||
449 | * @param string $val 资源类型 |
||
450 | * @return void |
||
451 | */ |
||
452 | public function mimeType($type, $val = '') |
||
460 | |||
461 | /** |
||
462 | * 当前的请求类型 |
||
463 | * @access public |
||
464 | * @param bool $method true 获取原始请求类型 |
||
465 | * @return string |
||
466 | */ |
||
467 | public function method($method = false) |
||
484 | |||
485 | /** |
||
486 | * 是否为GET请求 |
||
487 | * @access public |
||
488 | * @return bool |
||
489 | */ |
||
490 | public function isGet() |
||
494 | |||
495 | /** |
||
496 | * 是否为POST请求 |
||
497 | * @access public |
||
498 | * @return bool |
||
499 | */ |
||
500 | public function isPost() |
||
504 | |||
505 | /** |
||
506 | * 是否为PUT请求 |
||
507 | * @access public |
||
508 | * @return bool |
||
509 | */ |
||
510 | public function isPut() |
||
514 | |||
515 | /** |
||
516 | * 是否为DELTE请求 |
||
517 | * @access public |
||
518 | * @return bool |
||
519 | */ |
||
520 | public function isDelete() |
||
524 | |||
525 | /** |
||
526 | * 是否为HEAD请求 |
||
527 | * @access public |
||
528 | * @return bool |
||
529 | */ |
||
530 | public function isHead() |
||
534 | |||
535 | /** |
||
536 | * 是否为PATCH请求 |
||
537 | * @access public |
||
538 | * @return bool |
||
539 | */ |
||
540 | public function isPatch() |
||
544 | |||
545 | /** |
||
546 | * 是否为OPTIONS请求 |
||
547 | * @access public |
||
548 | * @return bool |
||
549 | */ |
||
550 | public function isOptions() |
||
554 | |||
555 | /** |
||
556 | * 是否为cli |
||
557 | * @access public |
||
558 | * @return bool |
||
559 | */ |
||
560 | public function isCli() |
||
564 | |||
565 | /** |
||
566 | * 是否为cgi |
||
567 | * @access public |
||
568 | * @return bool |
||
569 | */ |
||
570 | public function isCgi() |
||
574 | |||
575 | /** |
||
576 | * 获取获取当前请求的参数 |
||
577 | * @access public |
||
578 | * @param string|array $name 变量名 |
||
579 | * @param mixed $default 默认值 |
||
580 | * @param string|array $filter 过滤方法 |
||
581 | * @return mixed |
||
582 | */ |
||
583 | public function param($name = '', $default = null, $filter = '') |
||
611 | |||
612 | /** |
||
613 | * 设置获取获取路由参数 |
||
614 | * @access public |
||
615 | * @param string|array $name 变量名 |
||
616 | * @param mixed $default 默认值 |
||
617 | * @param string|array $filter 过滤方法 |
||
618 | * @return mixed |
||
619 | */ |
||
620 | public function route($name = '', $default = null, $filter = '') |
||
628 | |||
629 | /** |
||
630 | * 设置获取获取GET参数 |
||
631 | * @access public |
||
632 | * @param string|array $name 变量名 |
||
633 | * @param mixed $default 默认值 |
||
634 | * @param string|array $filter 过滤方法 |
||
635 | * @return mixed |
||
636 | */ |
||
637 | public function get($name = '', $default = null, $filter = '') |
||
648 | |||
649 | /** |
||
650 | * 设置获取获取POST参数 |
||
651 | * @access public |
||
652 | * @param string $name 变量名 |
||
653 | * @param mixed $default 默认值 |
||
654 | * @param string|array $filter 过滤方法 |
||
655 | * @return mixed |
||
656 | */ |
||
657 | public function post($name = '', $default = null, $filter = '') |
||
673 | |||
674 | /** |
||
675 | * 设置获取获取PUT参数 |
||
676 | * @access public |
||
677 | * @param string|array $name 变量名 |
||
678 | * @param mixed $default 默认值 |
||
679 | * @param string|array $filter 过滤方法 |
||
680 | * @return mixed |
||
681 | */ |
||
682 | public function put($name = '', $default = null, $filter = '') |
||
699 | |||
700 | /** |
||
701 | * 设置获取获取DELETE参数 |
||
702 | * @access public |
||
703 | * @param string|array $name 变量名 |
||
704 | * @param mixed $default 默认值 |
||
705 | * @param string|array $filter 过滤方法 |
||
706 | * @return mixed |
||
707 | */ |
||
708 | public function delete($name = '', $default = null, $filter = '') |
||
712 | |||
713 | /** |
||
714 | * 设置获取获取PATCH参数 |
||
715 | * @access public |
||
716 | * @param string|array $name 变量名 |
||
717 | * @param mixed $default 默认值 |
||
718 | * @param string|array $filter 过滤方法 |
||
719 | * @return mixed |
||
720 | */ |
||
721 | public function patch($name = '', $default = null, $filter = '') |
||
725 | |||
726 | /** |
||
727 | * 获取request变量 |
||
728 | * @param string $name 数据名称 |
||
729 | * @param string $default 默认值 |
||
730 | * @param string|array $filter 过滤方法 |
||
731 | * @return mixed |
||
732 | */ |
||
733 | public function request($name = '', $default = null, $filter = '') |
||
744 | |||
745 | /** |
||
746 | * 获取session数据 |
||
747 | * @access public |
||
748 | * @param string|array $name 数据名称 |
||
749 | * @param string $default 默认值 |
||
750 | * @param string|array $filter 过滤方法 |
||
751 | * @return mixed |
||
752 | */ |
||
753 | public function session($name = '', $default = null, $filter = '') |
||
763 | |||
764 | /** |
||
765 | * 获取cookie参数 |
||
766 | * @access public |
||
767 | * @param string|array $name 数据名称 |
||
768 | * @param string $default 默认值 |
||
769 | * @param string|array $filter 过滤方法 |
||
770 | * @return mixed |
||
771 | */ |
||
772 | public function cookie($name = '', $default = null, $filter = '') |
||
782 | |||
783 | /** |
||
784 | * 获取server参数 |
||
785 | * @access public |
||
786 | * @param string|array $name 数据名称 |
||
787 | * @param string $default 默认值 |
||
788 | * @param string|array $filter 过滤方法 |
||
789 | * @return mixed |
||
790 | */ |
||
791 | public function server($name = '', $default = null, $filter = '') |
||
801 | |||
802 | /** |
||
803 | * 获取上传的文件信息 |
||
804 | * @access public |
||
805 | * @param string|array $name 名称 |
||
806 | * @return null|array|\think\File |
||
807 | */ |
||
808 | public function file($name = '') |
||
861 | |||
862 | /** |
||
863 | * 获取环境变量 |
||
864 | * @param string|array $name 数据名称 |
||
865 | * @param string $default 默认值 |
||
866 | * @param string|array $filter 过滤方法 |
||
867 | * @return mixed |
||
868 | */ |
||
869 | public function env($name = '', $default = null, $filter = '') |
||
879 | |||
880 | /** |
||
881 | * 设置或者获取当前的Header |
||
882 | * @access public |
||
883 | * @param string|array $name header名称 |
||
884 | * @param string $default 默认值 |
||
885 | * @return string |
||
886 | */ |
||
887 | public function header($name = '', $default = null) |
||
919 | |||
920 | /** |
||
921 | * 获取变量 支持过滤和默认值 |
||
922 | * @param array $data 数据源 |
||
923 | * @param string|false $name 字段名 |
||
924 | * @param mixed $default 默认值 |
||
925 | * @param string|array $filter 过滤函数 |
||
926 | * @return mixed |
||
927 | */ |
||
928 | public function input($data = [], $name = '', $default = null, $filter = '') |
||
982 | |||
983 | /** |
||
984 | * 设置或获取当前的过滤规则 |
||
985 | * @param mixed $filter 过滤规则 |
||
986 | * @return mixed |
||
987 | */ |
||
988 | public function filter($filter = null) |
||
996 | |||
997 | /** |
||
998 | * 递归过滤给定的值 |
||
999 | * @param mixed $value 键值 |
||
1000 | * @param mixed $key 键名 |
||
1001 | * @param array $filters 过滤方法+默认值 |
||
1002 | * @return mixed |
||
1003 | */ |
||
1004 | private function filterValue(&$value, $key, $filters) |
||
1032 | |||
1033 | /** |
||
1034 | * 过滤表单中的表达式 |
||
1035 | * @param string $value |
||
1036 | * @return void |
||
1037 | */ |
||
1038 | public function filterExp(&$value) |
||
1046 | |||
1047 | /** |
||
1048 | * 强制类型转换 |
||
1049 | * @param string $data |
||
1050 | * @param string $type |
||
1051 | * @return mixed |
||
1052 | */ |
||
1053 | private function typeCast(&$data, $type) |
||
1082 | |||
1083 | /** |
||
1084 | * 是否存在某个请求参数 |
||
1085 | * @access public |
||
1086 | * @param string $name 变量名 |
||
1087 | * @param string $type 变量类型 |
||
1088 | * @param bool $checkEmpty 是否检测空值 |
||
1089 | * @return mixed |
||
1090 | */ |
||
1091 | public function has($name, $type = 'param', $checkEmpty = false) |
||
1108 | |||
1109 | /** |
||
1110 | * 获取指定的参数 |
||
1111 | * @access public |
||
1112 | * @param string|array $name 变量名 |
||
1113 | * @param string $type 变量类型 |
||
1114 | * @return mixed |
||
1115 | */ |
||
1116 | public function only($name, $type = 'param') |
||
1130 | |||
1131 | /** |
||
1132 | * 排除指定参数获取 |
||
1133 | * @access public |
||
1134 | * @param string|array $name 变量名 |
||
1135 | * @param string $type 变量类型 |
||
1136 | * @return mixed |
||
1137 | */ |
||
1138 | public function except($name, $type = 'param') |
||
1151 | |||
1152 | /** |
||
1153 | * 当前是否ssl |
||
1154 | * @access public |
||
1155 | * @return bool |
||
1156 | */ |
||
1157 | public function isSsl() |
||
1171 | |||
1172 | /** |
||
1173 | * 当前是否Ajax请求 |
||
1174 | * @access public |
||
1175 | * @param bool $ajax true 获取原始ajax请求 |
||
1176 | * @return bool |
||
1177 | */ |
||
1178 | public function isAjax($ajax = false) |
||
1188 | |||
1189 | /** |
||
1190 | * 当前是否Pjax请求 |
||
1191 | * @access public |
||
1192 | * @param bool $pjax true 获取原始pjax请求 |
||
1193 | * @return bool |
||
1194 | */ |
||
1195 | public function isPjax($pjax = false) |
||
1204 | |||
1205 | /** |
||
1206 | * 获取客户端IP地址 |
||
1207 | * @param integer $type 返回类型 0 返回IP地址 1 返回IPV4地址数字 |
||
1208 | * @param boolean $adv 是否进行高级模式获取(有可能被伪装) |
||
1209 | * @return mixed |
||
1210 | */ |
||
1211 | public function ip($type = 0, $adv = false) |
||
1240 | |||
1241 | /** |
||
1242 | * 检测是否使用手机访问 |
||
1243 | * @access public |
||
1244 | * @return bool |
||
1245 | */ |
||
1246 | public function isMobile() |
||
1260 | |||
1261 | /** |
||
1262 | * 当前URL地址中的scheme参数 |
||
1263 | * @access public |
||
1264 | * @return string |
||
1265 | */ |
||
1266 | public function scheme() |
||
1270 | |||
1271 | /** |
||
1272 | * 当前请求URL地址中的query参数 |
||
1273 | * @access public |
||
1274 | * @return string |
||
1275 | */ |
||
1276 | public function query() |
||
1280 | |||
1281 | /** |
||
1282 | * 当前请求的host |
||
1283 | * @access public |
||
1284 | * @return string |
||
1285 | */ |
||
1286 | public function host() |
||
1290 | |||
1291 | /** |
||
1292 | * 当前请求URL地址中的port参数 |
||
1293 | * @access public |
||
1294 | * @return integer |
||
1295 | */ |
||
1296 | public function port() |
||
1300 | |||
1301 | /** |
||
1302 | * 当前请求 SERVER_PROTOCOL |
||
1303 | * @access public |
||
1304 | * @return integer |
||
1305 | */ |
||
1306 | public function protocol() |
||
1310 | |||
1311 | /** |
||
1312 | * 当前请求 REMOTE_PORT |
||
1313 | * @access public |
||
1314 | * @return integer |
||
1315 | */ |
||
1316 | public function remotePort() |
||
1320 | |||
1321 | /** |
||
1322 | * 当前请求 HTTP_CONTENT_TYPE |
||
1323 | * @access public |
||
1324 | * @return string |
||
1325 | */ |
||
1326 | public function contentType() |
||
1339 | |||
1340 | /** |
||
1341 | * 获取当前请求的路由信息 |
||
1342 | * @access public |
||
1343 | * @param array $route 路由名称 |
||
1344 | * @return array |
||
1345 | */ |
||
1346 | public function routeInfo($route = []) |
||
1354 | |||
1355 | /** |
||
1356 | * 设置或者获取当前请求的调度信息 |
||
1357 | * @access public |
||
1358 | * @param array $dispatch 调度信息 |
||
1359 | * @return array |
||
1360 | */ |
||
1361 | public function dispatch($dispatch = null) |
||
1368 | |||
1369 | /** |
||
1370 | * 设置或者获取当前的模块名 |
||
1371 | * @access public |
||
1372 | * @param string $module 模块名 |
||
1373 | * @return string|Request |
||
1374 | */ |
||
1375 | public function module($module = null) |
||
1384 | |||
1385 | /** |
||
1386 | * 设置或者获取当前的控制器名 |
||
1387 | * @access public |
||
1388 | * @param string $controller 控制器名 |
||
1389 | * @return string|Request |
||
1390 | */ |
||
1391 | public function controller($controller = null) |
||
1400 | |||
1401 | /** |
||
1402 | * 设置或者获取当前的操作名 |
||
1403 | * @access public |
||
1404 | * @param string $action 操作名 |
||
1405 | * @return string|Request |
||
1406 | */ |
||
1407 | public function action($action = null) |
||
1416 | |||
1417 | /** |
||
1418 | * 设置或者获取当前的语言 |
||
1419 | * @access public |
||
1420 | * @param string $lang 语言名 |
||
1421 | * @return string|Request |
||
1422 | */ |
||
1423 | public function langset($lang = null) |
||
1432 | |||
1433 | /** |
||
1434 | * 设置或者获取当前请求的content |
||
1435 | * @access public |
||
1436 | * @return string |
||
1437 | */ |
||
1438 | public function getContent() |
||
1445 | |||
1446 | /** |
||
1447 | * 获取当前请求的php://input |
||
1448 | * @access public |
||
1449 | * @return string |
||
1450 | */ |
||
1451 | public function getInput() |
||
1455 | |||
1456 | /** |
||
1457 | * 生成请求令牌 |
||
1458 | * @access public |
||
1459 | * @param string $name 令牌名称 |
||
1460 | * @param mixed $type 令牌生成方法 |
||
1461 | * @return string |
||
1462 | */ |
||
1463 | public function token($name = '__token__', $type = 'md5') |
||
1473 | |||
1474 | /** |
||
1475 | * 设置当前地址的请求缓存 |
||
1476 | * @access public |
||
1477 | * @param string $key 缓存标识,支持变量规则 ,例如 item/:name/:id |
||
1478 | * @param mixed $expire 缓存有效期 |
||
1479 | * @param array $except 缓存排除 |
||
1480 | * @return void |
||
1481 | */ |
||
1482 | public function cache($key, $expire = null, $except = []) |
||
1541 | |||
1542 | /** |
||
1543 | * 读取请求缓存设置 |
||
1544 | * @access public |
||
1545 | * @return array |
||
1546 | */ |
||
1547 | public function getCache() |
||
1551 | |||
1552 | /** |
||
1553 | * 设置当前请求绑定的对象实例 |
||
1554 | * @access public |
||
1555 | * @param string $name 绑定的对象标识 |
||
1556 | * @param mixed $obj 绑定的对象实例 |
||
1557 | * @return mixed |
||
1558 | */ |
||
1559 | public function bind($name, $obj = null) |
||
1567 | |||
1568 | public function getBind($name) |
||
1572 | |||
1573 | public function __set($name, $value) |
||
1577 | |||
1578 | public function __get($name) |
||
1582 | |||
1583 | public function __isset($name) |
||
1587 | } |
Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable: