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.

HasNotKey::withKeyAndTranslator()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 14
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 4
nc 1
nop 2
dl 0
loc 14
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 function is_callable;
10
use Marcosh\PhpValidationDSL\Validation;
11
12
final class HasNotKey implements Validation
13
{
14
    public const PRESENT_KEY = 'has-not-key.present-key';
15
16
    /**
17
     * @var string
18
     */
19
    private $key;
20
21
    /**
22
     * @var callable with signature $key -> $data -> string[]
23
     */
24
    private $errorFormatter;
25
26
    private function __construct(string $key, ?callable $errorFormatter = null)
27
    {
28
        $this->key = $key;
29
        $this->errorFormatter = is_callable($errorFormatter) ?
30
            $errorFormatter :
31
            /**
32
             * @template T
33
             * @param string $key
34
             * @param mixed $data
35
             * @psalm-param T $data
36
             * @return string[]
37
             * @psalm-return array{0:string}
38
             */
39
            function (string $key, $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

39
            function (string $key, /** @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 $key 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

39
            function (/** @scrutinizer ignore-unused */ string $key, $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...
40
                return [self::PRESENT_KEY];
41
            };
42
    }
43
44
    public static function withKey(string $key): self
45
    {
46
        return new self($key);
47
    }
48
49
    public static function withKeyAndFormatter(string $key, callable $errorFormatter): self
50
    {
51
        return new self($key, $errorFormatter);
52
    }
53
54
    public static function withKeyAndTranslator(string $key, Translator $translator): self
55
    {
56
        return new self(
57
            $key,
58
            /**
59
             * @template T
60
             * @param string $key
61
             * @param mixed $data
62
             * @psalm-param T $data
63
             * @return string[]
64
             * @psalm-return array{0:string}
65
             */
66
            function (string $key, $data) use ($translator): 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

66
            function (string $key, /** @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...
Unused Code introduced by
The parameter $key 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

66
            function (/** @scrutinizer ignore-unused */ string $key, $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...
67
                return [$translator->translate(self::PRESENT_KEY)];
68
            }
69
        );
70
    }
71
72
    public function validate($data, array $context = []): ValidationResult
73
    {
74
        if (array_key_exists($this->key, $data)) {
75
            return ValidationResult::errors(($this->errorFormatter)($this->key, $data));
76
        }
77
78
        return ValidationResult::valid($data);
79
    }
80
}
81