Thinkphp 5.0 的代码我真是没法说了 太渣了吧 写框架的人不懂 OOP 思想?

2016-12-18 01:07:25 +08:00
 1762628386

/** * 验证单个字段规则 * @access protected * @param string $field 字段名 * @param mixed $value 字段值 * @param mixed $rules 验证规则 * @param array $data 数据 * @param string $title 字段描述 * @param array $msg 提示信息 * @return mixed */ protected function checkItem($field, $value, $rules, $data, $title = '', $msg = []) { if ($rules instanceof \Closure) { // 匿名函数验证 支持传入当前字段和所有字段两个数据 $result = call_user_func_array($rules, [$value, $data]); } else { // 支持多规则验证 require|in:a,b,c|... 或者 ['require','in'=>'a,b,c',...] if (is_string($rules)) { $rules = explode('|', $rules); } $i = 0; foreach ($rules as $key => $rule) {

            if ($rule instanceof \Closure) {
                $result = call_user_func_array($rule, [$value, $data]);
            } else {
                // 验证
                if (is_numeric($key)) {
                    if (strpos($rule, ':')) {
                        list($type, $rule) = explode(':', $rule, 2);
                        if (isset($this->alias[$type])) {
                            // 判断别名
                            $type = $this->alias[$type];
                        }
                        $info = $type;
                    } elseif (method_exists($this, $rule)) {
                        $type = $rule;
                        $info = $rule;
                        $rule = '';
                    }else {
                        $type = 'is';
                        $info = $rule;
                    }
                } else {
                    $info = $type = $key;
                }

                // 如果不是 require 有数据才会行验证
                if (0 === strpos($info, 'require') || (!is_null($value) && '' !== $value)) {
                    // 验证类型
                    $callback = isset(self::$type[$type]) ? self::$type[$type] : [$this, $type];
                    // 验证数据
                    $result = call_user_func_array($callback, [$value, $rule, $data, $field]);
                } else {
                    $result = true;
                }
            }

            if (false === $result) {
                // 验证失败 返回错误信息
                if (isset($msg[$i])) {
                    $message = $msg[$i];
                    if (is_string($message) && strpos($message, '{%') === 0) {
                        $message = (substr($message, 2, -1));
                    }
                } else {
                    $message = $this->getRuleMsg($field, $title, $info, $rule);
                }
                return $message;
            } elseif (true !== $result) {
                // 返回自定义错误信息
                return $result;
            }
            $i++;
        }
    }
    return true !== $result ? $result : true;
}

我的天 这是啥~

尤其是下面这几行代码 我感觉要是在公司里写类似的 会被别人打死的

if (is_numeric($key)) { if (strpos($rule, ':')) { list($type, $rule) = explode(':', $rule, 2); if (isset($this->alias[$type])) { // 判断别名 $type = $this->alias[$type]; } $info = $type; } elseif (method_exists($this, $rule)) { $type = $rule; $info = $rule; $rule = ''; }else { $type = 'is'; $info = $rule; } } else { $info = $type = $key; }

11362 次点击
所在节点    PHP
115 条回复
1762628386
2016-12-18 01:07:53 +08:00
```
if (is_numeric($key)) { if (strpos($rule, ':')) { list($type, $rule) = explode(':', $rule, 2); if (isset($this->alias[$type])) { // 判断别名 $type = $this->alias[$type]; } $info = $type; } elseif (method_exists($this, $rule)) { $type = $rule; $info = $rule; $rule = ''; }else { $type = 'is'; $info = $rule; } } else { $info = $type = $key; }
```
1762628386
2016-12-18 01:13:21 +08:00
本来打算重写 tp 的验证放自己的项目上用 一看这代码完全就是单纯为了实现[验证]这个功能,根本没法复用, 貌似连个最基本的后期静态绑定都没有啊
hronro
2016-12-18 02:07:24 +08:00
这个排版,我也受不了
orderc
2016-12-18 08:24:58 +08:00
论代码可读性的重要
vugusurk
2016-12-18 08:52:44 +08:00
日常黑 ThinkPHP
瞅瞅这个? https://github.com/illuminate/validation
vugusurk
2016-12-18 08:55:16 +08:00
哦,不对,是这个: https://symfony.com/doc/current/validation.html (逃
rogerchen
2016-12-18 09:01:59 +08:00
滑稽脸,啥是 OOP 思想
zachlhb
2016-12-18 09:21:04 +08:00
你牛逼自己写个,没人逼你用
holyghost
2016-12-18 09:38:26 +08:00
return true !== $result ? $result : true;

被这句搞崩溃了
misaka20038numbe
2016-12-18 09:47:33 +08:00
@holyghost 这个很容易理解啊,只要 $result 不完全是 true 就返回 $result , 否则返回 true .
zoues
2016-12-18 09:50:19 +08:00
@zachlhb 我发现说 u can u up no can no bb 的是不是..
forvtwoex
2016-12-18 09:55:07 +08:00
LZ 应该收购了 TP ,然后把自己不满意的地方重写一遍。
realpg
2016-12-18 10:51:02 +08:00
刚在别的帖子吐槽了一下 thinkphp
然后就被一个利益关系不明确的喷了一遍说我没看过 thinkphp 5.0
更加坚定了我以后不给任何简历上提到 thinkphp 的人面试机会的决心

@misaka20038numbe
正常人应该是 $result!==true?$result:true 吧
为了方便阅读习惯,变量放在左边是基本常识吧
1762628386
2016-12-18 11:06:57 +08:00
@zachlhb 你还说对了 就是打算自己写个
jok3r
2016-12-18 11:22:06 +08:00
@realpg 这种写法的也比较多,主要是防止把==写成=。不过可读性较差,我也不喜欢
1762628386
2016-12-18 11:23:31 +08:00
逻辑判断是小事 关键是方法的职责没有分离,一个方法应该是处理逻辑业务可复用的最小颗粒度
TP 的这个验证方法包含了太多的逻辑处理, 甚至连解析验证规则里面都有,
一个验证至少是分为这几部分
1 用户设置
2 解析用户设置的验证规则
3 保存验证规则在集合体中
4 循环对各个字段验证
5 根据验证规则讲错误信息保存在集合体中 ,返回给用户

- - ! 这代码真头疼
wy315700
2016-12-18 11:25:31 +08:00
@realpg 写 true == $result 是为了防止写错
比如你写 $result == true ,手一抖中间少写个等号,结果就完全不一样了
c 语言教程里都会提到这个
Doubear
2016-12-18 11:25:33 +08:00
呵呵,又没人逼你用对吧?那框架我也只是看过一点,后来想想用着不如自己写方便,就没搞了。现在还是投入框架的怀抱,不过是 laravel 。
1762628386
2016-12-18 11:27:30 +08:00
@vugusurk 至少 laravel 中不会出现 4 层的 if else 嵌套加循环加字符串处理转数组吧
yxzblue
2016-12-18 11:28:23 +08:00
You can you up

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/328375

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX