Conditions | 16 |
Paths | 54 |
Total Lines | 38 |
Code Lines | 27 |
Lines | 0 |
Ratio | 0 % |
Changes | 1 | ||
Bugs | 0 | Features | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
1 | <?php |
||
39 | public function init(array $rules, $input = '', ?callable $callable = null): array |
||
40 | { |
||
41 | if (is_string($input)) { |
||
|
|||
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()); |
||
77 | } |
||
78 | } |