RequestValidationException   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 47
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 4
Bugs 2 Features 0
Metric Value
wmc 8
eloc 13
c 4
b 2
f 0
dl 0
loc 47
ccs 15
cts 15
cp 1
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A getFirstErrors() 0 14 4
A getErrors() 0 3 1
A __construct() 0 4 1
A getFirstError() 0 5 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\RequestModel;
6
7
use RuntimeException;
8
9
final class RequestValidationException extends RuntimeException
10
{
11
    private const MESSAGE = 'Request model validation error';
12
13
    /**
14
     * @param string[] $errors
15
     *
16
     * @psalm-param array<string,string[]> $errors
17
     */
18 5
    public function __construct(
19
        private array $errors
20
    ) {
21 5
        parent::__construct(self::MESSAGE);
22
    }
23
24
    /**
25
     * @psalm-return array<string,string[]>
26
     */
27 1
    public function getErrors(): array
28
    {
29 1
        return $this->errors;
30
    }
31
32
    /**
33
     * @psalm-return array<string,string>
34
     */
35 3
    public function getFirstErrors(): array
36
    {
37 3
        if (empty($this->errors)) {
38 1
            return [];
39
        }
40
41 2
        $result = [];
42 2
        foreach ($this->errors as $name => $errors) {
43 2
            if (!empty($errors)) {
44 2
                $result[$name] = reset($errors);
45
            }
46
        }
47
48 2
        return $result;
49
    }
50
51 2
    public function getFirstError(): ?string
52
    {
53 2
        $errors = $this->getFirstErrors();
54
55 2
        return $errors === [] ? null : reset($errors);
56
    }
57
}
58