Passed
Pull Request — master (#597)
by Dmitriy
26:53 queued 23:43
created

ValidatorCollector   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 41
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 20
c 1
b 0
f 0
dl 0
loc 41
rs 10
wmc 6

4 Methods

Rating   Name   Duplication   Size   Complexity  
A getSummary() 0 11 1
A getCollected() 0 3 1
A collect() 0 11 3
A reset() 0 3 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Validator\Debug;
6
7
use Traversable;
8
use Yiisoft\Validator\Result;
9
use Yiisoft\Yii\Debug\Collector\CollectorTrait;
10
use Yiisoft\Yii\Debug\Collector\SummaryCollectorInterface;
11
12
final class ValidatorCollector implements SummaryCollectorInterface
13
{
14
    use CollectorTrait;
15
16
    private array $validations = [];
17
18
    public function getCollected(): array
19
    {
20
        return $this->validations;
21
    }
22
23
    public function collect(mixed $value, Result $result, ?iterable $rules = null): void
24
    {
25
        if (!$this->isActive()) {
26
            return;
27
        }
28
29
        $this->validations[] = [
30
            'value' => $value,
31
            'rules' => $rules instanceof Traversable ? iterator_to_array($rules, true) : (array) $rules,
32
            'result' => $result->isValid(),
33
            'errors' => $result->getErrors(),
34
        ];
35
    }
36
37
    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...
38
    {
39
        $this->validations = [];
40
    }
41
42
    public function getSummary(): array
43
    {
44
        $count = count($this->validations);
45
        $countValid = count(array_filter($this->validations, fn (array $data) => $data['result']));
46
        $countInvalid = $count - $countValid;
47
48
        return [
49
            'validator' => [
50
                'total' => $count,
51
                'valid' => $countValid,
52
                'invalid' => $countInvalid,
53
            ],
54
        ];
55
    }
56
}
57