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 ( 90b318...149c7a )
by Joni
04:00
created

Header::getIterator()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 3
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
crap 1
1
<?php
2
3
namespace JWX\JWT\Header;
4
5
use JWX\JWT\Parameter\JWTParameter;
6
7
8
/**
9
 * Represents a header used in JWS and JWE.
10
 */
11
class Header implements 
12
	\Countable, 
0 ignored issues
show
Coding Style introduced by
Expected 4 spaces before interface name; 1 found
Loading history...
13
	\IteratorAggregate
0 ignored issues
show
Coding Style introduced by
Expected 4 spaces before interface name; 1 found
Loading history...
14
{
15
	use TypedHeader;
16
	
17
	/**
18
	 * Parameters.
19
	 *
20
	 * @var JWTParameter[] $_parameters
21
	 */
22
	protected $_parameters;
23
	
24
	/**
25
	 * Constructor
26
	 *
27
	 * @param JWTParameter ...$params Parameters
28
	 */
29 131
	public function __construct(JWTParameter ...$params) {
30 131
		$this->_parameters = array();
31 131
		foreach ($params as $param) {
32 62
			$this->_parameters[$param->name()] = $param;
33 131
		}
34 131
	}
35
	
36
	/**
37
	 * Initialize from an array representing a JSON object.
38
	 *
39
	 * @param array $members
40
	 * @return self
41
	 */
42 45
	public static function fromArray(array $members) {
43 45
		$params = array();
44 45
		foreach ($members as $name => $value) {
45 45
			$params[] = JWTParameter::fromNameAndValue($name, $value);
46 45
		}
47 45
		return new self(...$params);
48 1
	}
49
	
50
	/**
51
	 * Initialize from a JSON.
52
	 *
53
	 * @param string $json
54
	 * @throws \UnexpectedValueException
55
	 * @return self
56
	 */
57 33 View Code Duplication
	public static function fromJSON($json) {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
58 33
		$members = json_decode($json, true, 32, JSON_BIGINT_AS_STRING);
59 33
		if (!is_array($members)) {
60 1
			throw new \UnexpectedValueException("Invalid JSON.");
61
		}
62 32
		return self::fromArray($members);
63
	}
64
	
65
	/**
66
	 * Get self with parameters added.
67
	 *
68
	 * @param JWTParameter ...$param
69
	 * @return self
70
	 */
71 32
	public function withParameters(JWTParameter ...$params) {
72 32
		$obj = clone $this;
73 32
		foreach ($params as $param) {
74 32
			$obj->_parameters[$param->name()] = $param;
75 32
		}
76 32
		return $obj;
77
	}
78
	
79
	/**
80
	 * Get all parameters.
81
	 *
82
	 * @return JWTParameter[]
83
	 */
84 28
	public function parameters() {
85 28
		return $this->_parameters;
86
	}
87
	
88
	/**
89
	 * Whether parameters are present.
90
	 *
91
	 * Returns false if any of the given parameters is not set.
92
	 *
93
	 * @param string ...$names Parameter names
94
	 * @return boolean
95
	 */
96 70 View Code Duplication
	public function has(...$names) {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
97 70
		foreach ($names as $name) {
98 70
			if (!isset($this->_parameters[$name])) {
99 41
				return false;
100
			}
101 43
		}
102 42
		return true;
103
	}
104
	
105
	/**
106
	 * Get a parameter.
107
	 *
108
	 * @param string $name Parameter name
109
	 * @throws \LogicException
110
	 * @return JWTParameter
111
	 */
112 37
	public function get($name) {
113 37
		if (!$this->has($name)) {
114 1
			throw new \LogicException("Parameter $name doesn't exists.");
115
		}
116 36
		return $this->_parameters[$name];
117
	}
118
	
119
	/**
120
	 * Convert to a JSON.
121
	 *
122
	 * @return string
123
	 */
124 70 View Code Duplication
	public function toJSON() {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
125 70
		if (empty($this->_parameters)) {
126 1
			return "";
127
		}
128 69
		$data = array();
129 69
		foreach ($this->_parameters as $param) {
130 69
			$data[$param->name()] = $param->value();
131 69
		}
132 69
		return json_encode((object) $data, JSON_UNESCAPED_SLASHES);
133
	}
134
	
135
	/**
136
	 * Get the number of parameters.
137
	 *
138
	 * @see Countable::count()
139
	 * @return int
140
	 */
141 3
	public function count() {
142 3
		return count($this->_parameters);
143
	}
144
	
145
	/**
146
	 * Get iterator for the parameters.
147
	 *
148
	 * @see IteratorAggregate::getIterator()
149
	 * @return \ArrayIterator
150
	 */
151 1
	public function getIterator() {
152 1
		return new \ArrayIterator($this->_parameters);
153
	}
154
}
155