Validator   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 47
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 2

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 9
lcom 0
cbo 2
dl 0
loc 47
ccs 18
cts 18
cp 1
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A validate() 0 7 2
A create() 0 4 1
A rules() 0 19 6
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
}