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 ( 59f4b4...e271c1 )
by Joni
03:51
created

Claim   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 115
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 10
eloc 23
dl 0
loc 115
ccs 26
cts 26
cp 1
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A value() 0 3 1
A name() 0 3 1
A validate() 0 7 2
A __construct() 0 5 1
A fromNameAndValue() 0 7 2
A validateWithContext() 0 15 3
1
<?php
2
3
declare(strict_types = 1);
4
5
namespace Sop\JWX\JWT\Claim;
6
7
use Sop\JWX\JWT\Claim\Validator\Validator;
8
use Sop\JWX\JWT\ValidationContext;
9
10
/**
11
 * Represents a JWT claim.
12
 *
13
 * @see https://tools.ietf.org/html/rfc7519#section-4
14
 */
15
class Claim
16
{
17
    /**
18
     * Claim name.
19
     *
20
     * @var string
21
     */
22
    protected $_name;
23
24
    /**
25
     * Claim value.
26
     *
27
     * @var mixed
28
     */
29
    protected $_value;
30
31
    /**
32
     * Claim validator.
33
     *
34
     * @var null|Validator
35
     */
36
    protected $_validator;
37
38
    /**
39
     * Constructor.
40
     *
41
     * @param string         $name      Claim name
42
     * @param mixed          $value     Claim value
43
     * @param null|Validator $validator Claim validator or null if claim doesn't
44
     *                                  provide validation
45
     */
46 66
    public function __construct(string $name, $value, ?Validator $validator = null)
47
    {
48 66
        $this->_name = $name;
49 66
        $this->_value = $value;
50 66
        $this->_validator = $validator;
51 66
    }
52
53
    /**
54
     * Initialize from a name and a value.
55
     *
56
     * Returns a specific claim object if applicable.
57
     *
58
     * @param string $name  Claim name
59
     * @param mixed  $value Claim value
60
     *
61
     * @return Claim
62
     */
63 8
    public static function fromNameAndValue(string $name, $value): Claim
64
    {
65 8
        if (array_key_exists($name, RegisteredClaim::MAP_NAME_TO_CLASS)) {
66 7
            $cls = RegisteredClaim::MAP_NAME_TO_CLASS[$name];
67 7
            return $cls::fromJSONValue($value);
68
        }
69 1
        return new self($name, $value);
70
    }
71
72
    /**
73
     * Get the claim name.
74
     *
75
     * @return string
76
     */
77 43
    public function name(): string
78
    {
79 43
        return $this->_name;
80
    }
81
82
    /**
83
     * Get the claim value.
84
     *
85
     * @return mixed
86
     */
87 12
    public function value()
88
    {
89 12
        return $this->_value;
90
    }
91
92
    /**
93
     * Validate the claim against a given constraint.
94
     *
95
     * @param mixed $constraint Constraint value
96
     *
97
     * @return bool True if the claim is valid
98
     */
99 43
    public function validate($constraint): bool
100
    {
101
        // if claim has no validator, consider validation failed
102 43
        if (!isset($this->_validator)) {
103 2
            return false;
104
        }
105 41
        return $this->_validator->validate($this->_value, $constraint);
106
    }
107
108
    /**
109
     * Validate the claim in a given context.
110
     *
111
     * @param ValidationContext $ctx
112
     *
113
     * @return bool True if claim is valid
114
     */
115 29
    public function validateWithContext(ValidationContext $ctx): bool
116
    {
117
        // if validator has no constraint for the claim
118 29
        if (!$ctx->hasConstraint($this->_name)) {
119 21
            return true;
120
        }
121 16
        $constraint = $ctx->constraint($this->_name);
122
        // if validation context has an explicitly
123
        // defined validator for the claim
124 16
        if ($ctx->hasValidator($this->_name)) {
125 2
            return $ctx->validator($this->_name)
126 2
                ->validate($this->_value, $constraint);
127
        }
128
        // validate using claim's default validator
129 14
        return $this->validate($ctx->constraint($this->_name));
130
    }
131
}
132