Issues (1482)

src/helper/ValidateHelper.php (2 issues)

1
<?php
2
3
// +----------------------------------------------------------------------
4
// | ThinkLibrary 6.0 for ThinkPhP 6.0
5
// +----------------------------------------------------------------------
6
// | 版权所有 2017~2020 [ https://www.dtapp.net ]
7
// +----------------------------------------------------------------------
8
// | 官方网站: https://gitee.com/liguangchun/ThinkLibrary
9
// +----------------------------------------------------------------------
10
// | 开源协议 ( https://mit-license.org )
11
// +----------------------------------------------------------------------
12
// | gitee 仓库地址 :https://gitee.com/liguangchun/ThinkLibrary
13
// | github 仓库地址 :https://github.com/GC0202/ThinkLibrary
14
// | Packagist 地址 :https://packagist.org/packages/liguangchun/think-library
15
// +----------------------------------------------------------------------
16
17
namespace DtApp\ThinkLibrary\helper;
18
19
use DtApp\ThinkLibrary\Helper;
20
use think\Validate;
21
22
class ValidateHelper extends Helper
23
{
24
    /**
25
     * 快捷输入并验证( 支持 规则 # 别名 )
26
     *
27
     *  age.require => message // 最大值限定
28
     *  age.between:1,120 => message // 范围限定
29
     *  name.require => message // 必填内容
30
     *  name.default => 100 // 获取并设置默认值
31
     *  region.value => value // 固定字段数值内容
32
     *  更多规则参照 ThinkPHP 官方的验证类
33
     *
34
     * @param array $rules 验证规则( 验证信息数组 )
35
     * @param string $input 输入内容 ( post. 或 get. )
36
     * @param callable|null $callable 异常处理操作
37
     * @return array
38
     */
39
    public function init(array $rules, $input = '', ?callable $callable = null): array
40
    {
41
        if (is_string($input)) {
0 ignored issues
show
The condition is_string($input) is always true.
Loading history...
42
            $type = trim($input, '.') ?: 'request';
43
            $input = $this->app->request->$type();
44
        }
45
        [$data, $rule, $info] = [[], [], []];
46
        foreach ($rules as $name => $message) {
47
            if (is_numeric($name)) {
48
                [$name, $alias] = explode('#', $message . '#');
49
                $data[$name] = $input[($alias ?: $name)] ?? null;
50
            } elseif (strpos($name, '.') === false) {
51
                $data[$name] = $message;
52
            } elseif (preg_match('|^(.*?)\.(.*?)#(.*?)#?$|', $name . '#', $matches)) {
53
                [, $_key, $_rule, $alias] = $matches;
54
                if (in_array($_rule, ['value', 'default'])) {
55
                    if ($_rule === 'value') {
56
                        $data[$_key] = $message;
57
                    } elseif ($_rule === 'default') {
58
                        $data[$_key] = $input[($alias ?: $_key)] ?? $message;
59
                    }
60
                } else {
61
                    $info[explode(':', $name)[0]] = $message;
62
                    $data[$_key] = $data[$_key] ?? ($input[($alias ?: $_key)] ?? null);
63
                    $rule[$_key] = isset($rule[$_key]) ? ($rule[$_key] . '|' . $_rule) : $_rule;
64
                }
65
            }
66
        }
67
        $validate = new Validate();
68
        if ($validate->rule($rule)->message($info)->check($data)) {
69
            return $data;
70
        }
71
72
        if (is_callable($callable)) {
73
            return $callable($validate->getError());
74
        }
75
76
        $this->class->error($validate->getError());
0 ignored issues
show
Bug Best Practice introduced by
In this branch, the function will implicitly return null which is incompatible with the type-hinted return array. Consider adding a return statement or allowing null as return value.

For hinted functions/methods where all return statements with the correct type are only reachable via conditions, ?null? gets implicitly returned which may be incompatible with the hinted type. Let?s take a look at an example:

interface ReturnsInt {
    public function returnsIntHinted(): int;
}

class MyClass implements ReturnsInt {
    public function returnsIntHinted(): int
    {
        if (foo()) {
            return 123;
        }
        // here: null is implicitly returned
    }
}
Loading history...
77
    }
78
}