Complex classes like Validate 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 Validate, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
9 | class Validate { |
||
10 | // 实例 |
||
11 | protected static $instance; |
||
12 | |||
13 | // 自定义的验证类型 |
||
14 | protected static $type=[]; |
||
15 | |||
16 | // 验证类型别名 |
||
17 | protected $alias=[ |
||
18 | '>' => 'gt', '>=' => 'egt', '<' => 'lt', '<=' => 'elt', '=' => 'eq', 'same' => 'eq', |
||
19 | ]; |
||
20 | |||
21 | // 当前验证的规则 |
||
22 | protected $rule=[]; |
||
23 | |||
24 | // 验证提示信息 |
||
25 | protected $message=[]; |
||
26 | // 验证字段描述 |
||
27 | protected $field=[]; |
||
28 | |||
29 | // 验证规则默认提示信息 |
||
30 | protected static $typeMsg=[ |
||
31 | 'require' => ':attribute不能为空', |
||
32 | 'number' => ':attribute必须是数字', |
||
33 | 'float' => ':attribute必须是浮点数', |
||
34 | 'boolean' => ':attribute必须是布尔值', |
||
35 | 'email' => ':attribute格式不符', |
||
36 | 'array' => ':attribute必须是数组', |
||
37 | 'accepted' => ':attribute必须是yes、on或者1', |
||
38 | 'date' => ':attribute格式不符合', |
||
39 | 'file' => ':attribute不是有效的上传文件', |
||
40 | 'image' => ':attribute不是有效的图像文件', |
||
41 | 'alpha' => ':attribute只能是字母', |
||
42 | 'alphaNum' => ':attribute只能是字母和数字', |
||
43 | 'alphaDash' => ':attribute只能是字母、数字和下划线_及破折号-', |
||
44 | 'activeUrl' => ':attribute不是有效的域名或者IP', |
||
45 | 'chs' => ':attribute只能是汉字', |
||
46 | 'chsAlpha' => ':attribute只能是汉字、字母', |
||
47 | 'chsAlphaNum' => ':attribute只能是汉字、字母和数字', |
||
48 | 'chsDash' => ':attribute只能是汉字、字母、数字和下划线_及破折号-', |
||
49 | 'url' => ':attribute不是有效的URL地址', |
||
50 | 'ip' => ':attribute不是有效的IP地址', |
||
51 | 'dateFormat' => ':attribute必须使用日期格式 :rule', |
||
52 | 'in' => ':attribute必须在 :rule 范围内', |
||
53 | 'notIn' => ':attribute不能在 :rule 范围内', |
||
54 | 'between' => ':attribute只能在 :1 - :2 之间', |
||
55 | 'notBetween' => ':attribute不能在 :1 - :2 之间', |
||
56 | 'length' => ':attribute长度不符合要求 :rule', |
||
57 | 'max' => ':attribute长度不能超过 :rule', |
||
58 | 'min' => ':attribute长度不能小于 :rule', |
||
59 | 'after' => ':attribute日期不能小于 :rule', |
||
60 | 'before' => ':attribute日期不能超过 :rule', |
||
61 | 'expire' => '不在有效期内 :rule', |
||
62 | 'allowIp' => '不允许的IP访问', |
||
63 | 'denyIp' => '禁止的IP访问', |
||
64 | 'confirm' => ':attribute和确认字段:2不一致', |
||
65 | 'different' => ':attribute和比较字段:2不能相同', |
||
66 | 'egt' => ':attribute必须大于等于 :rule', |
||
67 | 'gt' => ':attribute必须大于 :rule', |
||
68 | 'elt' => ':attribute必须小于等于 :rule', |
||
69 | 'lt' => ':attribute必须小于 :rule', |
||
70 | 'eq' => ':attribute必须等于 :rule', |
||
71 | 'unique' => ':attribute已存在', |
||
72 | 'regex' => ':attribute不符合指定规则', |
||
73 | 'method' => '无效的请求类型', |
||
74 | 'token' => '令牌数据无效', |
||
75 | 'fileSize' => '上传文件大小不符', |
||
76 | 'fileExt' => '上传文件后缀不符', |
||
77 | 'fileMime' => '上传文件类型不符', |
||
78 | |||
79 | ]; |
||
80 | |||
81 | // 当前验证场景 |
||
82 | protected $currentScene=null; |
||
83 | |||
84 | // 正则表达式 regex = ['zip'=>'\d{6}',...] |
||
|
|||
85 | protected $regex=[]; |
||
86 | |||
87 | // 验证场景 scene = ['edit'=>'name1,name2,...'] |
||
88 | protected $scene=[]; |
||
89 | |||
90 | // 验证失败错误信息 |
||
91 | protected $error=[]; |
||
92 | |||
93 | // 批量验证 |
||
94 | protected $batch=false; |
||
95 | |||
96 | /** |
||
97 | * 构造函数 |
||
98 | * @access public |
||
99 | * @param array $rules 验证规则 |
||
100 | * @param array $message 验证提示信息 |
||
101 | * @param array $field 验证字段描述信息 |
||
102 | */ |
||
103 | public function __construct(array $rules=[], $message=[], $field=[]) |
||
109 | |||
110 | /** |
||
111 | * 实例化验证 |
||
112 | * @access public |
||
113 | * @param array $rules 验证规则 |
||
114 | * @param array $message 验证提示信息 |
||
115 | * @param array $field 验证字段描述信息 |
||
116 | * @return Validate |
||
117 | */ |
||
118 | public static function make($rules=[], $message=[], $field=[]) |
||
125 | |||
126 | /** |
||
127 | * 添加字段验证规则 |
||
128 | * @access protected |
||
129 | * @param string|array $name 字段名称或者规则数组 |
||
130 | * @param mixed $rule 验证规则 |
||
131 | * @return Validate |
||
132 | */ |
||
133 | public function rule($name, $rule='') |
||
142 | |||
143 | /** |
||
144 | * 注册验证(类型)规则 |
||
145 | * @access public |
||
146 | * @param string $type 验证规则类型 |
||
147 | * @param mixed $callback callback方法(或闭包) |
||
148 | * @return void |
||
149 | */ |
||
150 | public static function extend($type, $callback=null) |
||
158 | |||
159 | /** |
||
160 | * 获取验证规则的默认提示信息 |
||
161 | * @access protected |
||
162 | * @param string|array $type 验证规则类型名称或者数组 |
||
163 | * @param string $msg 验证提示信息 |
||
164 | * @return void |
||
165 | */ |
||
166 | public static function setTypeMsg($type, $msg=null) |
||
174 | |||
175 | /** |
||
176 | * 设置提示信息 |
||
177 | * @access public |
||
178 | * @param string|array $name 字段名称 |
||
179 | * @param string $message 提示信息 |
||
180 | * @return Validate |
||
181 | */ |
||
182 | public function message($name, $message='') |
||
191 | |||
192 | /** |
||
193 | * 设置验证场景 |
||
194 | * @access public |
||
195 | * @param string|array $name 场景名或者场景设置数组 |
||
196 | * @param mixed $fields 要验证的字段 |
||
197 | * @return Validate |
||
198 | */ |
||
199 | public function scene($name, $fields=null) |
||
212 | |||
213 | /** |
||
214 | * 判断是否存在某个验证场景 |
||
215 | * @access public |
||
216 | * @param string $name 场景名 |
||
217 | * @return bool |
||
218 | */ |
||
219 | public function hasScene($name) |
||
223 | |||
224 | /** |
||
225 | * 设置批量验证 |
||
226 | * @access public |
||
227 | * @param bool $batch 是否批量验证 |
||
228 | * @return Validate |
||
229 | */ |
||
230 | public function batch($batch=true) |
||
235 | |||
236 | /** |
||
237 | * 数据自动验证 |
||
238 | * @access public |
||
239 | * @param array $data 数据 |
||
240 | * @param mixed $rules 验证规则 |
||
241 | * @param string $scene 验证场景 |
||
242 | * @return bool |
||
243 | */ |
||
244 | public function check($data, $rules=[], $scene='') |
||
333 | |||
334 | /** |
||
335 | * 验证单个字段规则 |
||
336 | * @access protected |
||
337 | * @param string $field 字段名 |
||
338 | * @param mixed $value 字段值 |
||
339 | * @param mixed $rules 验证规则 |
||
340 | * @param array $data 数据 |
||
341 | * @param string $title 字段描述 |
||
342 | * @param array $msg 提示信息 |
||
343 | * @return mixed |
||
344 | */ |
||
345 | protected function checkItem($field, $value, $rules, $data, $title='', $msg=[]) |
||
411 | |||
412 | /** |
||
413 | * 验证是否和某个字段的值一致 |
||
414 | * @access protected |
||
415 | * @param mixed $value 字段值 |
||
416 | * @param mixed $rule 验证规则 |
||
417 | * @param array $data 数据 |
||
418 | * @param string $field 字段名 |
||
419 | * @return bool |
||
420 | */ |
||
421 | protected function confirm($value, $rule, $data, $field='') |
||
432 | |||
433 | /** |
||
434 | * 验证是否和某个字段的值是否不同 |
||
435 | * @access protected |
||
436 | * @param mixed $value 字段值 |
||
437 | * @param mixed $rule 验证规则 |
||
438 | * @param array $data 数据 |
||
439 | * @return bool |
||
440 | */ |
||
441 | protected function different($value, $rule, $data) |
||
445 | |||
446 | /** |
||
447 | * 验证是否大于等于某个值 |
||
448 | * @access protected |
||
449 | * @param mixed $value 字段值 |
||
450 | * @param mixed $rule 验证规则 |
||
451 | * @return bool |
||
452 | */ |
||
453 | protected function egt($value, $rule) |
||
457 | |||
458 | /** |
||
459 | * 验证是否大于某个值 |
||
460 | * @access protected |
||
461 | * @param mixed $value 字段值 |
||
462 | * @param mixed $rule 验证规则 |
||
463 | * @return bool |
||
464 | */ |
||
465 | protected function gt($value, $rule) |
||
469 | |||
470 | /** |
||
471 | * 验证是否小于等于某个值 |
||
472 | * @access protected |
||
473 | * @param mixed $value 字段值 |
||
474 | * @param mixed $rule 验证规则 |
||
475 | * @return bool |
||
476 | */ |
||
477 | protected function elt($value, $rule) |
||
481 | |||
482 | /** |
||
483 | * 验证是否小于某个值 |
||
484 | * @access protected |
||
485 | * @param mixed $value 字段值 |
||
486 | * @param mixed $rule 验证规则 |
||
487 | * @return bool |
||
488 | */ |
||
489 | protected function lt($value, $rule) |
||
493 | |||
494 | /** |
||
495 | * 验证是否等于某个值 |
||
496 | * @access protected |
||
497 | * @param mixed $value 字段值 |
||
498 | * @param mixed $rule 验证规则 |
||
499 | * @return bool |
||
500 | */ |
||
501 | protected function eq($value, $rule) |
||
505 | |||
506 | /** |
||
507 | * 验证字段值是否为有效格式 |
||
508 | * @access protected |
||
509 | * @param mixed $value 字段值 |
||
510 | * @param string $rule 验证规则 |
||
511 | * @param array $data 验证数据 |
||
512 | * @return bool |
||
513 | */ |
||
514 | protected function is($value, $rule, $data=[]) |
||
612 | |||
613 | // 判断图像类型 |
||
614 | protected function getImageType($image) |
||
623 | |||
624 | /** |
||
625 | * 验证是否为合格的域名或者IP 支持A,MX,NS,SOA,PTR,CNAME,AAAA,A6, SRV,NAPTR,TXT 或者 ANY类型 |
||
626 | * @access protected |
||
627 | * @param mixed $value 字段值 |
||
628 | * @param mixed $rule 验证规则 |
||
629 | * @return bool |
||
630 | */ |
||
631 | protected function activeUrl($value, $rule) |
||
638 | |||
639 | /** |
||
640 | * 验证是否有效IP |
||
641 | * @access protected |
||
642 | * @param mixed $value 字段值 |
||
643 | * @param mixed $rule 验证规则 ipv4 ipv6 |
||
644 | * @return bool |
||
645 | */ |
||
646 | protected function ip($value, $rule) |
||
653 | |||
654 | /** |
||
655 | * 验证上传文件后缀 |
||
656 | * @access protected |
||
657 | * @param mixed $file 上传文件 |
||
658 | * @param mixed $rule 验证规则 |
||
659 | * @return bool |
||
660 | */ |
||
661 | protected function fileExt($file, $rule) |
||
680 | |||
681 | /** |
||
682 | * 验证上传文件类型 |
||
683 | * @access protected |
||
684 | * @param mixed $file 上传文件 |
||
685 | * @param mixed $rule 验证规则 |
||
686 | * @return bool |
||
687 | */ |
||
688 | protected function fileMime($file, $rule) |
||
707 | |||
708 | /** |
||
709 | * 验证上传文件大小 |
||
710 | * @access protected |
||
711 | * @param mixed $file 上传文件 |
||
712 | * @param mixed $rule 验证规则 |
||
713 | * @return bool |
||
714 | */ |
||
715 | protected function fileSize($file, $rule) |
||
731 | |||
732 | /** |
||
733 | * 验证图片的宽高及类型 |
||
734 | * @access protected |
||
735 | * @param mixed $file 上传文件 |
||
736 | * @param mixed $rule 验证规则 |
||
737 | * @return bool |
||
738 | */ |
||
739 | protected function image($file, $rule) |
||
763 | |||
764 | /** |
||
765 | * 验证请求类型 |
||
766 | * @access protected |
||
767 | * @param mixed $value 字段值 |
||
768 | * @param mixed $rule 验证规则 |
||
769 | * @return bool |
||
770 | */ |
||
771 | protected function method($value, $rule) |
||
776 | |||
777 | /** |
||
778 | * 验证时间和日期是否符合指定格式 |
||
779 | * @access protected |
||
780 | * @param mixed $value 字段值 |
||
781 | * @param mixed $rule 验证规则 |
||
782 | * @return bool |
||
783 | */ |
||
784 | protected function dateFormat($value, $rule) |
||
789 | |||
790 | /** |
||
791 | * 验证是否唯一 |
||
792 | * @access protected |
||
793 | * @param mixed $value 字段值 |
||
794 | * @param mixed $rule 验证规则 格式:数据表,字段名,排除ID,主键名 |
||
795 | * @param array $data 数据 |
||
796 | * @param string $field 验证字段名 |
||
797 | * @return bool |
||
798 | */ |
||
799 | protected function unique($value, $rule, $data, $field) |
||
840 | |||
841 | /** |
||
842 | * 使用行为类验证 |
||
843 | * @access protected |
||
844 | * @param mixed $value 字段值 |
||
845 | * @param mixed $rule 验证规则 |
||
846 | * @param array $data 数据 |
||
847 | * @return mixed |
||
848 | */ |
||
849 | protected function behavior($value, $rule, $data) |
||
853 | |||
854 | /** |
||
855 | * 使用filter_var方式验证 |
||
856 | * @access protected |
||
857 | * @param mixed $value 字段值 |
||
858 | * @param mixed $rule 验证规则 |
||
859 | * @return bool |
||
860 | */ |
||
861 | protected function filter($value, $rule) |
||
873 | |||
874 | /** |
||
875 | * 验证某个字段等于某个值的时候必须 |
||
876 | * @access protected |
||
877 | * @param mixed $value 字段值 |
||
878 | * @param mixed $rule 验证规则 |
||
879 | * @param array $data 数据 |
||
880 | * @return bool |
||
881 | */ |
||
882 | protected function requireIf($value, $rule, $data) |
||
891 | |||
892 | /** |
||
893 | * 通过回调方法验证某个字段是否必须 |
||
894 | * @access protected |
||
895 | * @param mixed $value 字段值 |
||
896 | * @param mixed $rule 验证规则 |
||
897 | * @param array $data 数据 |
||
898 | * @return bool |
||
899 | */ |
||
900 | protected function requireCallback($value, $rule, $data) |
||
909 | |||
910 | /** |
||
911 | * 验证某个字段有值的情况下必须 |
||
912 | * @access protected |
||
913 | * @param mixed $value 字段值 |
||
914 | * @param mixed $rule 验证规则 |
||
915 | * @param array $data 数据 |
||
916 | * @return bool |
||
917 | */ |
||
918 | protected function requireWith($value, $rule, $data) |
||
927 | |||
928 | /** |
||
929 | * 验证是否在范围内 |
||
930 | * @access protected |
||
931 | * @param mixed $value 字段值 |
||
932 | * @param mixed $rule 验证规则 |
||
933 | * @return bool |
||
934 | */ |
||
935 | protected function in($value, $rule) |
||
939 | |||
940 | /** |
||
941 | * 验证是否不在某个范围 |
||
942 | * @access protected |
||
943 | * @param mixed $value 字段值 |
||
944 | * @param mixed $rule 验证规则 |
||
945 | * @return bool |
||
946 | */ |
||
947 | protected function notIn($value, $rule) |
||
951 | |||
952 | /** |
||
953 | * between验证数据 |
||
954 | * @access protected |
||
955 | * @param mixed $value 字段值 |
||
956 | * @param mixed $rule 验证规则 |
||
957 | * @return bool |
||
958 | */ |
||
959 | protected function between($value, $rule) |
||
967 | |||
968 | /** |
||
969 | * 使用notbetween验证数据 |
||
970 | * @access protected |
||
971 | * @param mixed $value 字段值 |
||
972 | * @param mixed $rule 验证规则 |
||
973 | * @return bool |
||
974 | */ |
||
975 | protected function notBetween($value, $rule) |
||
983 | |||
984 | /** |
||
985 | * 验证数据长度 |
||
986 | * @access protected |
||
987 | * @param mixed $value 字段值 |
||
988 | * @param mixed $rule 验证规则 |
||
989 | * @return bool |
||
990 | */ |
||
991 | protected function length($value, $rule) |
||
1010 | |||
1011 | /** |
||
1012 | * 验证数据最大长度 |
||
1013 | * @access protected |
||
1014 | * @param mixed $value 字段值 |
||
1015 | * @param mixed $rule 验证规则 |
||
1016 | * @return bool |
||
1017 | */ |
||
1018 | protected function max($value, $rule) |
||
1029 | |||
1030 | /** |
||
1031 | * 验证数据最小长度 |
||
1032 | * @access protected |
||
1033 | * @param mixed $value 字段值 |
||
1034 | * @param mixed $rule 验证规则 |
||
1035 | * @return bool |
||
1036 | */ |
||
1037 | protected function min($value, $rule) |
||
1048 | |||
1049 | /** |
||
1050 | * 验证日期 |
||
1051 | * @access protected |
||
1052 | * @param mixed $value 字段值 |
||
1053 | * @param mixed $rule 验证规则 |
||
1054 | * @return bool |
||
1055 | */ |
||
1056 | protected function after($value, $rule) |
||
1060 | |||
1061 | /** |
||
1062 | * 验证日期 |
||
1063 | * @access protected |
||
1064 | * @param mixed $value 字段值 |
||
1065 | * @param mixed $rule 验证规则 |
||
1066 | * @return bool |
||
1067 | */ |
||
1068 | protected function before($value, $rule) |
||
1072 | |||
1073 | /** |
||
1074 | * 验证有效期 |
||
1075 | * @access protected |
||
1076 | * @param mixed $value 字段值 |
||
1077 | * @param mixed $rule 验证规则 |
||
1078 | * @return bool |
||
1079 | */ |
||
1080 | protected function expire($value, $rule) |
||
1095 | |||
1096 | /** |
||
1097 | * 验证IP许可 |
||
1098 | * @access protected |
||
1099 | * @param string $value 字段值 |
||
1100 | * @param mixed $rule 验证规则 |
||
1101 | * @return boolean |
||
1102 | */ |
||
1103 | protected function allowIp($value, $rule) |
||
1107 | |||
1108 | /** |
||
1109 | * 验证IP禁用 |
||
1110 | * @access protected |
||
1111 | * @param string $value 字段值 |
||
1112 | * @param mixed $rule 验证规则 |
||
1113 | * @return boolean |
||
1114 | */ |
||
1115 | protected function denyIp($value, $rule) |
||
1119 | |||
1120 | /** |
||
1121 | * 使用正则验证数据 |
||
1122 | * @access protected |
||
1123 | * @param mixed $value 字段值 |
||
1124 | * @param string $rule 验证规则 正则规则或者预定义正则名 |
||
1125 | * @return boolean |
||
1126 | */ |
||
1127 | protected function regex($value, $rule) |
||
1138 | |||
1139 | /** |
||
1140 | * 验证表单令牌 |
||
1141 | * @access protected |
||
1142 | * @param mixed $value 字段值 |
||
1143 | * @param string $rule 验证规则 |
||
1144 | * @param array $data 数据 |
||
1145 | * @return bool |
||
1146 | */ |
||
1147 | protected function token($value, $rule, $data) |
||
1165 | |||
1166 | // 获取错误信息 |
||
1167 | public function getError() |
||
1171 | |||
1172 | /** |
||
1173 | * 获取数据值 |
||
1174 | * @access protected |
||
1175 | * @param array $data 数据 |
||
1176 | * @param string $key 数据标识 支持二维 |
||
1177 | * @return mixed |
||
1178 | */ |
||
1179 | protected function getDataValue($data, $key) |
||
1190 | |||
1191 | /** |
||
1192 | * 获取验证规则的错误提示信息 |
||
1193 | * @access protected |
||
1194 | * @param string $attribute 字段英文名 |
||
1195 | * @param string $title 字段描述名 |
||
1196 | * @param string $type 验证规则名称 |
||
1197 | * @param mixed $rule 验证规则数据 |
||
1198 | * @return string |
||
1199 | */ |
||
1200 | protected function getRuleMsg($attribute, $title, $type, $rule) |
||
1232 | |||
1233 | /** |
||
1234 | * 获取数据验证的场景 |
||
1235 | * @access protected |
||
1236 | * @param string $scene 验证场景 |
||
1237 | * @return array |
||
1238 | */ |
||
1239 | protected function getScene($scene='') |
||
1257 | |||
1258 | public static function __callStatic($method, $params) |
||
1267 | } |
Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.
The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.
This check looks for comments that seem to be mostly valid code and reports them.