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.

InArray::validate()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 3
nc 2
nop 2
dl 0
loc 7
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Marcosh\PhpValidationDSL\Basic;
6
7
use Marcosh\PhpValidationDSL\Result\ValidationResult;
8
use Marcosh\PhpValidationDSL\Translator\Translator;
9
use Marcosh\PhpValidationDSL\Validation;
10
use function in_array;
11
use function is_callable;
12
13
final class InArray implements Validation
14
{
15
    public const NOT_IN_ARRAY = 'in-array.not-in-array';
16
17
    /**
18
     * @var array
19
     */
20
    private $values;
21
22
    /**
23
     * @var callable with signature $values -> $data -> string[]
24
     */
25
    private $errorFormatter;
26
27
    private function __construct(array $values, ?callable $errorFormatter = null)
28
    {
29
        $this->values = $values;
30
        $this->errorFormatter = is_callable($errorFormatter) ?
31
            $errorFormatter :
32
            /**
33
             * @template T
34
             * @param array $values
35
             * @param mixed $data
36
             * @psalm-param T $data
37
             * @return string[]
38
             * @psalm-return array{0:string}
39
             */
40
            function (array $values, $data): array {
0 ignored issues
show
Unused Code introduced by
The parameter $data is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

40
            function (array $values, /** @scrutinizer ignore-unused */ $data): array {

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $values is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

40
            function (/** @scrutinizer ignore-unused */ array $values, $data): array {

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
41
                return [self::NOT_IN_ARRAY];
42
            };
43
    }
44
45
    public static function withValues(array $values): self
46
    {
47
        return new self($values);
48
    }
49
50
    public static function withValuesAndFormatter(array $values, callable $errorFormatter): self
51
    {
52
        return new self($values, $errorFormatter);
53
    }
54
55
    public static function withValuesAndTranslator(array $values, Translator $translator): self
56
    {
57
        return new self(
58
            $values,
59
            /**
60
             * @template T
61
             * @param array $values
62
             * @param mixed $data
63
             * @psalm-param T $data
64
             * @return string[]
65
             * @psalm-return array{0:string}
66
             */
67
            function (array $values, $data) use ($translator): array {
0 ignored issues
show
Unused Code introduced by
The parameter $values is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

67
            function (/** @scrutinizer ignore-unused */ array $values, $data) use ($translator): array {

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $data is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

67
            function (array $values, /** @scrutinizer ignore-unused */ $data) use ($translator): array {

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
68
                return [$translator->translate(self::NOT_IN_ARRAY)];
69
            }
70
        );
71
    }
72
73
    public function validate($data, array $context = []): ValidationResult
74
    {
75
        if (! in_array($data, $this->values, true)) {
76
            return ValidationResult::errors(($this->errorFormatter)($this->values, $data));
77
        }
78
79
        return ValidationResult::valid($data);
80
    }
81
}
82