Passed
Pull Request — master (#132)
by Dmitriy
12:39 queued 10s
created

ValidatorCollector::getIndexData()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 15
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 11
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 15
ccs 5
cts 5
cp 1
crap 1
rs 9.9
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Yii\Debug\Collector;
6
7
use JetBrains\PhpStorm\ArrayShape;
8
use Traversable;
9
use Yiisoft\Validator\Result;
10
11
final class ValidatorCollector implements ValidatorCollectorInterface, IndexCollectorInterface
12
{
13
    use CollectorTrait;
14
15
    private array $validations = [];
16
17 1
    public function getCollected(): array
18
    {
19 1
        return $this->validations;
20
    }
21
22 1
    public function collect(mixed $value, iterable $rules, Result $result): void
23
    {
24 1
        if (!$this->isActive()) {
25
            return;
26
        }
27
28 1
        $this->validations[] = [
29
            'value' => $value,
30 1
            'rules' => $rules instanceof Traversable ? iterator_to_array($rules, true) : (array) $rules,
31 1
            'result' => $result->isValid(),
32 1
            'errors' => $result->getErrors(),
33
        ];
34
    }
35
36 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...
37
    {
38 1
        $this->validations = [];
39
    }
40
41 1
    #[ArrayShape([
42
        'validator.count' => 'int',
43
        'validator.count_valid' => 'int',
44
        'validator.count_invalid' => 'int',
45
    ])]
46
    public function getIndexData(): array
47
    {
48 1
        $count = count($this->validations);
49 1
        $countValid = count(array_filter($this->validations, fn (array $data) => $data['result']));
50 1
        $countInvalid = $count - $countValid;
51
52
        return [
53 1
            'validator.count' => $count,
54
            'validator.count_valid' => $countValid,
55
            'validator.count_invalid' => $countInvalid,
56
        ];
57
    }
58
}
59