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.
Completed
Push — master ( 51efb0...4d5462 )
by Marco
01:21
created

HasKey::withKeyAndTranslator()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 4
nc 1
nop 2
dl 0
loc 6
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 array_key_exists;
11
use function is_callable;
12
13
final class HasKey implements Validation
14
{
15
    public const MISSING_KEY = 'has-key.missing-key';
16
17
    /**
18
     * @var string
19
     */
20
    private $key;
21
22
    /**
23
     * @var callable $key -> $data -> string[]
24
     */
25
    private $errorFormatter;
26
27
    private function __construct(string $key, ?callable $errorFormatter = null)
28
    {
29
        $this->key = $key;
30
        $this->errorFormatter = is_callable($errorFormatter) ?
31
            $errorFormatter :
32
            function (string $key, $data) {
0 ignored issues
show
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

32
            function (/** @scrutinizer ignore-unused */ string $key, $data) {

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

32
            function (string $key, /** @scrutinizer ignore-unused */ $data) {

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...
33
                return [self::MISSING_KEY];
34
            };
35
    }
36
37
    public static function withKey(string $key): self
38
    {
39
        return new self($key);
40
    }
41
42
    public static function withKeyAndFormatter(string $key, callable $errorFormatter): self
43
    {
44
        return new self($key, $errorFormatter);
45
    }
46
47
    public static function withKeyAndTranslator(string $key, Translator $translator): self
48
    {
49
        return new self(
50
            $key,
51
            function (string $key, $data) use ($translator) {
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

51
            function (string $key, /** @scrutinizer ignore-unused */ $data) use ($translator) {

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

51
            function (/** @scrutinizer ignore-unused */ string $key, $data) use ($translator) {

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...
52
                return [$translator->translate(self::MISSING_KEY)];
53
            }
54
        );
55
    }
56
57
    public function validate($data, array $context = []): ValidationResult
58
    {
59
        if (! array_key_exists($this->key, $data)) {
60
            return ValidationResult::errors(($this->errorFormatter)($this->key, $data));
61
        }
62
63
        return ValidationResult::valid($data);
64
    }
65
}
66