Passed
Push — master ( 1b3b54...96eb95 )
by Dmitriy
03:22 queued 11s
created

ValidatorCollector   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 41
Duplicated Lines 0 %

Test Coverage

Coverage 93.33%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 20
c 2
b 0
f 0
dl 0
loc 41
ccs 14
cts 15
cp 0.9333
rs 10
wmc 6

4 Methods

Rating   Name   Duplication   Size   Complexity  
A collect() 0 11 3
A reset() 0 3 1
A getCollected() 0 3 1
A getIndexData() 0 11 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Yii\Debug\Collector;
6
7
use Traversable;
8
use Yiisoft\Validator\Result;
9
10
final class ValidatorCollector implements CollectorInterface, IndexCollectorInterface
11
{
12
    use CollectorTrait;
13
14
    private array $validations = [];
15
16 1
    public function getCollected(): array
17
    {
18 1
        return $this->validations;
19
    }
20
21 1
    public function collect(mixed $value, iterable $rules, Result $result): void
22
    {
23 1
        if (!$this->isActive()) {
24
            return;
25
        }
26
27 1
        $this->validations[] = [
28
            'value' => $value,
29 1
            'rules' => $rules instanceof Traversable ? iterator_to_array($rules, true) : (array) $rules,
30 1
            'result' => $result->isValid(),
31 1
            'errors' => $result->getErrors(),
32
        ];
33
    }
34
35 1
    private function reset(): void
0 ignored issues
show
Unused Code introduced by
The method reset() is not used, and could be removed.

This check looks for private methods that have been defined, but are not used inside the class.

Loading history...
36
    {
37 1
        $this->validations = [];
38
    }
39
40 1
    public function getIndexData(): array
41
    {
42 1
        $count = count($this->validations);
43 1
        $countValid = count(array_filter($this->validations, fn (array $data) => $data['result']));
44 1
        $countInvalid = $count - $countValid;
45
46
        return [
47
            'validator' => [
48
                'total' => $count,
49
                'valid' => $countValid,
50
                'invalid' => $countInvalid,
51
            ],
52
        ];
53
    }
54
}
55