GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.

TranslateErrors::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 2
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Marcosh\PhpValidationDSL\Combinator;
6
7
use Marcosh\PhpValidationDSL\Result\ValidationResult;
8
use Marcosh\PhpValidationDSL\Translator\Translator;
9
use Marcosh\PhpValidationDSL\Validation;
10
use function is_array;
11
use function is_string;
12
13
final class TranslateErrors implements Validation
14
{
15
    /**
16
     * @var Validation
17
     */
18
    private $validation;
19
20
    /**
21
     * @var Translator
22
     */
23
    private $translator;
24
25
    public function __construct(Validation $validation, Translator $translator)
26
    {
27
        $this->validation = $validation;
28
        $this->translator = $translator;
29
    }
30
31
    public static function validationWithTranslator(Validation $validation, Translator $translator): self
32
    {
33
        return new self($validation, $translator);
34
    }
35
36
    public function validate($data, array $context = []): ValidationResult
37
    {
38
        return MapErrors::to($this->validation, [$this, 'translateNestedErrors'])->validate($data, $context);
39
    }
40
41
    public function translateNestedErrors(array $messages): array
42
    {
43
        $translator = $this->translator;
44
45
        return array_map(
46
            /**
47
             * @template T
48
             * @psalm-param T $message
49
             * @return T
50
             */
51
            function ($message) use ($translator) {
52
                if (is_string($message)) {
53
                    return $translator->translate($message);
0 ignored issues
show
Bug Best Practice introduced by
The expression return $translator->translate($message) returns the type string which is incompatible with the documented return type Marcosh\PhpValidationDSL\Combinator\T.
Loading history...
54
                }
55
56
                if (is_array($message)) {
57
                    return $this->translateNestedErrors($message);
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->translateNestedErrors($message) returns the type array which is incompatible with the documented return type Marcosh\PhpValidationDSL\Combinator\T.
Loading history...
58
                }
59
60
                return $message;
61
            },
62
            $messages
63
        );
64
    }
65
}
66