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.

Header::count()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
eloc 1
dl 0
loc 3
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
crap 1
1
<?php
2
3
declare(strict_types = 1);
4
5
namespace Sop\JWX\JWT\Header;
6
7
use Sop\JWX\JWT\Parameter\JWTParameter;
8
9
/**
10
 * Represents a header used in JWS and JWE.
11
 */
12
class Header implements \Countable, \IteratorAggregate
13
{
14
    use TypedHeader;
15
16
    /**
17
     * Parameters.
18
     *
19
     * @var JWTParameter[]
20
     */
21
    protected $_parameters;
22
23
    /**
24
     * Constructor.
25
     *
26
     * @param JWTParameter ...$params Parameters
27
     */
28 189
    public function __construct(JWTParameter ...$params)
29
    {
30 189
        $this->_parameters = [];
31 189
        foreach ($params as $param) {
32 117
            $this->_parameters[$param->name()] = $param;
33
        }
34 189
    }
35
36
    /**
37
     * Initialize from an array representing a JSON object.
38
     */
39 53
    public static function fromArray(array $members): self
40
    {
41 53
        $params = [];
42 53
        foreach ($members as $name => $value) {
43 53
            $params[] = JWTParameter::fromNameAndValue($name, $value);
44
        }
45 53
        return new self(...$params);
46
    }
47
48
    /**
49
     * Initialize from a JSON.
50
     *
51
     * @throws \UnexpectedValueException
52
     */
53 40
    public static function fromJSON(string $json): self
54
    {
55 40
        $members = json_decode($json, true, 32, JSON_BIGINT_AS_STRING);
56 40
        if (!is_array($members)) {
57 1
            throw new \UnexpectedValueException('Invalid JSON.');
58
        }
59 39
        return self::fromArray($members);
60
    }
61
62
    /**
63
     * Get self with parameters added.
64
     *
65
     * @param JWTParameter ...$param
66
     */
67 36
    public function withParameters(JWTParameter ...$params): self
68
    {
69 36
        $obj = clone $this;
70 36
        foreach ($params as $param) {
71 36
            $obj->_parameters[$param->name()] = $param;
72
        }
73 36
        return $obj;
74
    }
75
76
    /**
77
     * Get all parameters.
78
     *
79
     * @return JWTParameter[]
80
     */
81 52
    public function parameters(): array
82
    {
83 52
        return $this->_parameters;
84
    }
85
86
    /**
87
     * Whether parameters are present.
88
     *
89
     * Returns false if any of the given parameters is not set.
90
     *
91
     * @param string ...$names Parameter names
92
     */
93 176
    public function has(string ...$names): bool
94
    {
95 176
        foreach ($names as $name) {
96 176
            if (!isset($this->_parameters[$name])) {
97 176
                return false;
98
            }
99
        }
100 145
        return true;
101
    }
102
103
    /**
104
     * Get a parameter.
105
     *
106
     * @param string $name Parameter name
107
     *
108
     * @throws \LogicException
109
     */
110 123
    public function get(string $name): JWTParameter
111
    {
112 123
        if (!$this->has($name)) {
113 1
            throw new \LogicException("Parameter {$name} doesn't exists.");
114
        }
115 122
        return $this->_parameters[$name];
116
    }
117
118
    /**
119
     * Convert to a JSON.
120
     */
121 77
    public function toJSON(): string
122
    {
123 77
        if (empty($this->_parameters)) {
124 1
            return '';
125
        }
126 76
        $data = [];
127 76
        foreach ($this->_parameters as $param) {
128 76
            $data[$param->name()] = $param->value();
129
        }
130 76
        return json_encode((object) $data, JSON_UNESCAPED_SLASHES);
131
    }
132
133
    /**
134
     * Get the number of parameters.
135
     *
136
     * @see \Countable::count()
137
     */
138 3
    public function count(): int
139
    {
140 3
        return count($this->_parameters);
141
    }
142
143
    /**
144
     * Get iterator for the parameters.
145
     *
146
     * @see \IteratorAggregate::getIterator()
147
     */
148 1
    public function getIterator(): \ArrayIterator
149
    {
150 1
        return new \ArrayIterator($this->_parameters);
151
    }
152
}
153