Test Failed
Pull Request — master (#364)
by
unknown
03:02
created

Result::getCommonErrorMessages()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 1
CRAP Score 1

Importance

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