Validator::create()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
crap 1
1
<?php
2
3
namespace Bricks\Data;
4
5
use Bricks\Exception\ValidationException;
6
7
/**
8
 * Class Validator
9
 * @package Bricks\Data
10
 */
11
class Validator extends \Valitron\Validator
12
{
13
    /**
14
     * @return bool
15
     * @throws ValidationException
16
     */
17 5
    public function validate(): bool
18
    {
19 5
        if (!parent::validate()) {
20 3
            throw new ValidationException($this->errors());
0 ignored issues
show
Bug introduced by
It seems like $this->errors() targeting Valitron\Validator::errors() can also be of type boolean; however, Bricks\Exception\Validat...xception::__construct() does only seem to accept array, maybe add an additional type check?

This check looks at variables that are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
21
        }
22 2
        return true;
23
    }
24
25
    /**
26
     * @param array $data
27
     * @return static
28
     */
29 4
    public static function create(array $data = [])
30
    {
31 4
        return new static($data);
32
    }
33
34
    /**
35
     * @param array $rules
36
     * @param array $messages
37
     */
38 6
    public function rules($rules, $messages = [])
39
    {
40 6
        foreach ($rules as $ruleType => $params) {
41 6
            if (is_array($params)) {
42 6
                foreach ($params as $innerParams) {
43 6
                    if (!is_array($innerParams)) {
44 3
                        $innerParams = (array)$innerParams;
45
                    }
46 6
                    $fieldName = $innerParams[0];
47 6
                    $this->rule($ruleType, ...$innerParams);
48 6
                    if (isset($messages[$ruleType][$fieldName])) {
49 6
                        $this->message($messages[$ruleType][$fieldName]);
50
                    }
51
                }
52
            } else {
53 6
                $this->rule($ruleType, $params);
54
            }
55
        }
56
    }
57
}