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
Branch php72 (a7f01e)
by Joni
04:53
created

PolicyNode::isAnyPolicy()   A

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\X509\CertificationPath\Policy;
6
7
use Sop\X509\Certificate\Extension\CertificatePolicy\PolicyInformation;
8
use Sop\X509\Certificate\Extension\CertificatePolicy\PolicyQualifierInfo;
9
10
/**
11
 * Policy node class for certification path validation.
12
 *
13
 * @internal Mutable class used by PolicyTree
14
 *
15
 * @see https://tools.ietf.org/html/rfc5280#section-6.1.2
16
 */
17
class PolicyNode implements \IteratorAggregate, \Countable
18
{
19
    /**
20
     * Policy OID.
21
     *
22
     * @var string
23
     */
24
    protected $_validPolicy;
25
26
    /**
27
     * List of qualifiers.
28
     *
29
     * @var PolicyQualifierInfo[]
30
     */
31
    protected $_qualifiers;
32
33
    /**
34
     * List of expected policy OIDs.
35
     *
36
     * @var string[]
37
     */
38
    protected $_expectedPolicies;
39
40
    /**
41
     * List of child nodes.
42
     *
43
     * @var PolicyNode[]
44
     */
45
    protected $_children;
46
47
    /**
48
     * Reference to the parent node.
49
     *
50
     * @var null|PolicyNode
51
     */
52
    protected $_parent;
53
54
    /**
55
     * Constructor.
56
     *
57
     * @param string                $valid_policy      Policy OID
58
     * @param PolicyQualifierInfo[] $qualifiers
59
     * @param string[]              $expected_policies
60
     */
61 53
    public function __construct(string $valid_policy, array $qualifiers,
62
        array $expected_policies)
63
    {
64 53
        $this->_validPolicy = $valid_policy;
65 53
        $this->_qualifiers = $qualifiers;
66 53
        $this->_expectedPolicies = $expected_policies;
67 53
        $this->_children = [];
68 53
    }
69
70
    /**
71
     * Create initial node for the policy tree.
72
     *
73
     * @return self
74
     */
75 52
    public static function anyPolicyNode(): self
76
    {
77 52
        return new self(PolicyInformation::OID_ANY_POLICY, [],
78 52
            [PolicyInformation::OID_ANY_POLICY]);
79
    }
80
81
    /**
82
     * Get the valid policy OID.
83
     *
84
     * @return string
85
     */
86 10
    public function validPolicy(): string
87
    {
88 10
        return $this->_validPolicy;
89
    }
90
91
    /**
92
     * Check whether node has anyPolicy as a valid policy.
93
     *
94
     * @return bool
95
     */
96 14
    public function isAnyPolicy(): bool
97
    {
98 14
        return PolicyInformation::OID_ANY_POLICY === $this->_validPolicy;
99
    }
100
101
    /**
102
     * Get the qualifier set.
103
     *
104
     * @return PolicyQualifierInfo[]
105
     */
106 5
    public function qualifiers(): array
107
    {
108 5
        return $this->_qualifiers;
109
    }
110
111
    /**
112
     * Check whether node has OID as an expected policy.
113
     *
114
     * @param string $oid
115
     *
116
     * @return bool
117
     */
118 12
    public function hasExpectedPolicy(string $oid): bool
119
    {
120 12
        return in_array($oid, $this->_expectedPolicies);
121
    }
122
123
    /**
124
     * Get the expected policy set.
125
     *
126
     * @return string[]
127
     */
128 6
    public function expectedPolicies(): array
129
    {
130 6
        return $this->_expectedPolicies;
131
    }
132
133
    /**
134
     * Set expected policies.
135
     *
136
     * @param string ...$oids Policy OIDs
137
     */
138 1
    public function setExpectedPolicies(string ...$oids): void
139
    {
140 1
        $this->_expectedPolicies = $oids;
141 1
    }
142
143
    /**
144
     * Check whether node has a child node with given valid policy OID.
145
     *
146
     * @param string $oid
147
     *
148
     * @return bool
149
     */
150 7
    public function hasChildWithValidPolicy(string $oid): bool
151
    {
152 7
        foreach ($this->_children as $node) {
153 1
            if ($node->validPolicy() == $oid) {
154 1
                return true;
155
            }
156
        }
157 6
        return false;
158
    }
159
160
    /**
161
     * Add child node.
162
     *
163
     * @param PolicyNode $node
164
     *
165
     * @return self
166
     */
167 18
    public function addChild(PolicyNode $node): self
168
    {
169 18
        $id = spl_object_hash($node);
170 18
        $node->_parent = $this;
171 18
        $this->_children[$id] = $node;
172 18
        return $this;
173
    }
174
175
    /**
176
     * Get the child nodes.
177
     *
178
     * @return PolicyNode[]
179
     */
180 12
    public function children(): array
181
    {
182 12
        return array_values($this->_children);
183
    }
184
185
    /**
186
     * Remove this node from the tree.
187
     *
188
     * @return self The removed node
189
     */
190 7
    public function remove(): self
191
    {
192 7
        if ($this->_parent) {
193 7
            $id = spl_object_hash($this);
194 7
            unset($this->_parent->_children[$id], $this->_parent);
195
        }
196 7
        return $this;
197
    }
198
199
    /**
200
     * Check whether node has a parent.
201
     *
202
     * @return bool
203
     */
204 4
    public function hasParent(): bool
205
    {
206 4
        return isset($this->_parent);
207
    }
208
209
    /**
210
     * Get the parent node.
211
     *
212
     * @return null|PolicyNode
213
     */
214 1
    public function parent(): ?PolicyNode
215
    {
216 1
        return $this->_parent;
217
    }
218
219
    /**
220
     * Get chain of parent nodes from this node's parent to the root node.
221
     *
222
     * @return PolicyNode[]
223
     */
224 6
    public function parents(): array
225
    {
226 6
        if (!$this->_parent) {
227 6
            return [];
228
        }
229 6
        $nodes = $this->_parent->parents();
230 6
        $nodes[] = $this->_parent;
231 6
        return array_reverse($nodes);
232
    }
233
234
    /**
235
     * Walk tree from this node, applying a callback for each node.
236
     *
237
     * Nodes are traversed depth-first and callback shall be applied post-order.
238
     *
239
     * @param callable $fn
240
     */
241 6
    public function walkNodes(callable $fn): void
242
    {
243 6
        foreach ($this->_children as $node) {
244 6
            $node->walkNodes($fn);
245
        }
246 6
        $fn($this);
247 6
    }
248
249
    /**
250
     * Get the total number of nodes in a tree.
251
     *
252
     * @return int
253
     */
254 14
    public function nodeCount(): int
255
    {
256 14
        $c = 1;
257 14
        foreach ($this->_children as $child) {
258 14
            $c += $child->nodeCount();
259
        }
260 14
        return $c;
261
    }
262
263
    /**
264
     * Get the number of child nodes.
265
     *
266
     * @see \Countable::count()
267
     */
268 14
    public function count(): int
269
    {
270 14
        return count($this->_children);
271
    }
272
273
    /**
274
     * Get iterator for the child nodes.
275
     *
276
     * @see \IteratorAggregate::getIterator()
277
     */
278 1
    public function getIterator(): \ArrayIterator
279
    {
280 1
        return new \ArrayIterator($this->_children);
281
    }
282
}
283