Passed
Pull Request — master (#364)
by
unknown
02:42
created

Result::addError()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
eloc 2
c 0
b 0
f 0
dl 0
loc 5
ccs 3
cts 3
cp 1
rs 10
cc 1
nc 1
nop 3
crap 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Validator;
6
7
use InvalidArgumentException;
8
9
use function array_slice;
10
use function implode;
11
use function is_string;
12
13
final class Result
14
{
15
    /**
16
     * @var Error[]
17
     */
18
    private array $errors = [];
19
20 686
    public function isValid(): bool
21
    {
22 686
        return $this->errors === [];
23
    }
24
25 4
    public function isAttributeValid(string $attribute): bool
26
    {
27 4
        foreach ($this->errors as $error) {
28 4
            $firstItem = $error->getValuePath()[0] ?? '';
29 4
            if ($firstItem === $attribute) {
30 4
                return false;
31
            }
32
        }
33
34 4
        return true;
35
    }
36
37
    /**
38
     * @return Error[]
39
     */
40 681
    public function getErrors(): array
41
    {
42 681
        return $this->errors;
43
    }
44
45
    /**
46
     * @return string[]
47
     */
48 6
    public function getErrorMessages(): array
49
    {
50 6
        return array_map(static fn (Error $error): string => $error->getMessage(), $this->errors);
51
    }
52
53
    /**
54
     * @return array<string, non-empty-list<string>>
0 ignored issues
show
Documentation Bug introduced by
The doc comment array<string, non-empty-list<string>> at position 4 could not be parsed: Unknown type name 'non-empty-list' at position 4 in array<string, non-empty-list<string>>.
Loading history...
55
     */
56 336
    public function getErrorMessagesIndexedByPath(string $separator = '.'): array
57
    {
58 336
        $errors = [];
59 336
        foreach ($this->errors as $error) {
60 335
            $stringValuePath = implode($separator, $error->getValuePath(true));
61 335
            $errors[$stringValuePath][] = $error->getMessage();
62
        }
63
64 336
        return $errors;
65
    }
66
67
    /**
68
     * @throws InvalidArgumentException
69
     *
70
     * @return array<string, non-empty-list<string>>
0 ignored issues
show
Documentation Bug introduced by
The doc comment array<string, non-empty-list<string>> at position 4 could not be parsed: Unknown type name 'non-empty-list' at position 4 in array<string, non-empty-list<string>>.
Loading history...
71
     */
72 16
    public function getErrorMessagesIndexedByAttribute(): array
73
    {
74 16
        $errors = [];
75 16
        foreach ($this->errors as $error) {
76 13
            $key = $error->getValuePath()[0] ?? '';
77 13
            if (!is_string($key)) {
78 1
                throw new InvalidArgumentException('Top level attributes can only have string type.');
79
            }
80
81 12
            $errors[$key][] = $error->getMessage();
82
        }
83
84 15
        return $errors;
85
    }
86
87
    /**
88
     * @return Error[]
89
     */
90 1
    public function getAttributeErrors(string $attribute): array
91
    {
92 1
        $errors = [];
93 1
        foreach ($this->errors as $error) {
94 1
            $firstItem = $error->getValuePath()[0] ?? '';
95 1
            if ($firstItem === $attribute) {
96 1
                $errors[] = $error;
97
            }
98
        }
99
100 1
        return $errors;
101
    }
102
103
    /**
104
     * @return string[]
105
     */
106 2
    public function getAttributeErrorMessages(string $attribute): array
107
    {
108 2
        $errors = [];
109 2
        foreach ($this->errors as $error) {
110 2
            $firstItem = $error->getValuePath()[0] ?? '';
111 2
            if ($firstItem === $attribute) {
112 2
                $errors[] = $error->getMessage();
113
            }
114
        }
115
116 2
        return $errors;
117
    }
118
119
    /**
120
     * @return array<string, non-empty-list<string>>
0 ignored issues
show
Documentation Bug introduced by
The doc comment array<string, non-empty-list<string>> at position 4 could not be parsed: Unknown type name 'non-empty-list' at position 4 in array<string, non-empty-list<string>>.
Loading history...
121
     */
122 1
    public function getAttributeErrorMessagesIndexedByPath(string $attribute, string $separator = '.'): array
123
    {
124 1
        $errors = [];
125 1
        foreach ($this->errors as $error) {
126 1
            $firstItem = $error->getValuePath()[0] ?? '';
127 1
            if ($firstItem !== $attribute) {
128 1
                continue;
129
            }
130
131 1
            $valuePath = implode($separator, array_slice($error->getValuePath(true), 1));
132 1
            $errors[$valuePath][] = $error->getMessage();
133
        }
134
135 1
        return $errors;
136
    }
137
138
    /**
139
     * @return string[]
140
     */
141 1
    public function getCommonErrorMessages(): array
142
    {
143 1
        return $this->getAttributeErrorMessages('');
144
    }
145
146
    /**
147
     * @param array<string,scalar|null> $parameters
148
     * @param list<int|string> $valuePath
0 ignored issues
show
Bug introduced by
The type Yiisoft\Validator\list was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
149
     */
150 406
    public function addError(string $message, array $parameters = [], array $valuePath = []): self
151
    {
152 406
        $this->errors[] = new Error($message, $parameters, $valuePath);
153
154 406
        return $this;
155
    }
156
}
157