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

Result::getErrorMessagesIndexedByPath()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 2

Importance

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