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.

IsAsAsserted::withAssertionAndErrorFormatter()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

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

38
            function (/** @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...
39
                return [self::NOT_AS_ASSERTED];
40
            };
41
    }
42
43
    public static function withAssertion(callable $assertion): self
44
    {
45
        return new self($assertion);
46
    }
47
48
    public static function withAssertionAndErrorFormatter(callable $assertion, callable $errorFormatter): self
49
    {
50
        return new self($assertion, $errorFormatter);
51
    }
52
53
    public static function withAssertionAndTranslator(callable $assertion, Translator $translator): self
54
    {
55
        return new self(
56
            $assertion,
57
            /**
58
             * @template T
59
             * @param mixed $data
60
             * @psalm-param T $data
61
             * @return string[]
62
             * @psalm-return array{0:string}
63
             */
64
            function ($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

64
            function (/** @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...
65
                return [$translator->translate(self::NOT_AS_ASSERTED)];
66
            }
67
        );
68
    }
69
70
    public function validate($data, array $context = []): ValidationResult
71
    {
72
        if (! ($this->assertion)($data)) {
73
            return ValidationResult::errors(($this->errorFormatter)($data));
74
        }
75
76
        return ValidationResult::valid($data);
77
    }
78
}
79