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.
Passed
Branch php72 (880eb0)
by Joni
05:58
created

Claims   A

Complexity

Total Complexity 18

Size/Duplication

Total Lines 164
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 18
eloc 34
dl 0
loc 164
ccs 42
cts 42
cp 1
rs 10
c 0
b 0
f 0

11 Methods

Rating   Name   Duplication   Size   Complexity  
A toJSON() 0 7 2
A __toString() 0 3 1
A withClaims() 0 7 2
A all() 0 3 1
A fromJSON() 0 11 3
A has() 0 3 1
A count() 0 3 1
A __construct() 0 5 2
A getIterator() 0 3 1
A isValid() 0 8 2
A get() 0 6 2
1
<?php
2
3
declare(strict_types = 1);
4
5
namespace Sop\JWX\JWT;
6
7
use Sop\JWX\JWT\Claim\Claim;
8
use Sop\JWX\JWT\Claim\TypedClaims;
9
10
/**
11
 * Represents a set of Claim objects.
12
 *
13
 * @see https://tools.ietf.org/html/rfc7519#section-4
14
 */
15
class Claims implements \Countable, \IteratorAggregate
16
{
17
    use TypedClaims;
18
19
    /**
20
     * Claims.
21
     *
22
     * @var Claim[]
23
     */
24
    protected $_claims;
25
26
    /**
27
     * Constructor.
28
     *
29
     * @param Claim ...$claims Zero or more claims
30
     */
31 28
    public function __construct(Claim ...$claims)
32
    {
33 28
        $this->_claims = [];
34 28
        foreach ($claims as $claim) {
35 26
            $this->_claims[$claim->name()] = $claim;
36
        }
37 28
    }
38
39
    /**
40
     * Convert to string.
41
     *
42
     * @return string
43
     */
44 1
    public function __toString(): string
45
    {
46 1
        return $this->toJSON();
47
    }
48
49
    /**
50
     * Initialize from a JSON string.
51
     *
52
     * @param string $json JSON
53
     *
54
     * @throws \UnexpectedValueException If JSON is malformed
55
     *
56
     * @return self
57
     */
58 8
    public static function fromJSON(string $json): self
59
    {
60 8
        $claims = [];
61 8
        $fields = json_decode($json, true, 32, JSON_BIGINT_AS_STRING);
62 8
        if (!is_array($fields)) {
63 1
            throw new \UnexpectedValueException('Invalid JSON.');
64
        }
65 7
        foreach ($fields as $name => $value) {
66 7
            $claims[] = Claim::fromNameAndValue($name, $value);
67
        }
68 7
        return new self(...$claims);
69
    }
70
71
    /**
72
     * Get self with Claim objects added.
73
     *
74
     * @param Claim ...$claims One or more Claim objects
75
     *
76
     * @return self
77
     */
78 1
    public function withClaims(Claim ...$claims): self
79
    {
80 1
        $obj = clone $this;
81 1
        foreach ($claims as $claim) {
82 1
            $obj->_claims[$claim->name()] = $claim;
83
        }
84 1
        return $obj;
85
    }
86
87
    /**
88
     * Get all claims.
89
     *
90
     * @return Claim[]
91
     */
92 1
    public function all(): array
93
    {
94 1
        return $this->_claims;
95
    }
96
97
    /**
98
     * Check whether claim is present.
99
     *
100
     * @param string $name Claim name
101
     *
102
     * @return true
103
     */
104 8
    public function has(string $name): bool
105
    {
106 8
        return isset($this->_claims[$name]);
0 ignored issues
show
Bug Best Practice introduced by
The expression return IssetNode returns the type boolean which is incompatible with the documented return type true.
Loading history...
107
    }
108
109
    /**
110
     * Get claim by name.
111
     *
112
     * @param string $name Claim name
113
     *
114
     * @throws \LogicException If claim is not present
115
     *
116
     * @return Claim
117
     */
118 10
    public function get(string $name): Claim
119
    {
120 10
        if (!isset($this->_claims[$name])) {
121 1
            throw new \LogicException("Claim {$name} not set.");
122
        }
123 9
        return $this->_claims[$name];
124
    }
125
126
    /**
127
     * Convert to a JSON.
128
     *
129
     * @return string
130
     */
131 9
    public function toJSON(): string
132
    {
133 9
        $data = [];
134 9
        foreach ($this->_claims as $claim) {
135 7
            $data[$claim->name()] = $claim->value();
136
        }
137 9
        return json_encode((object) $data, JSON_UNESCAPED_SLASHES);
138
    }
139
140
    /**
141
     * Check whether a claims set is valid in the given context.
142
     *
143
     * @param ValidationContext $ctx Validation context
144
     *
145
     * @return bool
146
     */
147 4
    public function isValid(ValidationContext $ctx): bool
148
    {
149
        try {
150 4
            $ctx->validate($this);
151 2
        } catch (\RuntimeException $e) {
152 2
            return false;
153
        }
154 2
        return true;
155
    }
156
157
    /**
158
     * Get the number of claims.
159
     *
160
     * @see \Countable::count()
161
     *
162
     * @return int
163
     */
164 2
    public function count(): int
165
    {
166 2
        return count($this->_claims);
167
    }
168
169
    /**
170
     * Get iterator for Claim objects keyed by claim name.
171
     *
172
     * @see \IteratorAggregate::getIterator()
173
     *
174
     * @return \ArrayIterator
175
     */
176 26
    public function getIterator(): \ArrayIterator
177
    {
178 26
        return new \ArrayIterator($this->_claims);
179
    }
180
}
181