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.
Test Failed
Push — master ( 405cf3...79c9ba )
by Joni
04:48
created

AttributeContainer   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 142
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 13
eloc 30
dl 0
loc 142
ccs 39
cts 39
cp 1
rs 10
c 0
b 0
f 0

9 Methods

Rating   Name   Duplication   Size   Complexity  
A count() 0 3 1
A has() 0 3 1
A withAdditional() 0 7 2
A _findFirst() 0 9 3
A withUnique() 0 9 1
A all() 0 3 1
A firstOf() 0 7 2
A allOf() 0 8 1
A getIterator() 0 3 1
1
<?php
2
3
declare(strict_types = 1);
4
5
namespace Sop\X509\Feature;
6
7
use Sop\X501\ASN1\Attribute;
8
use Sop\X501\ASN1\AttributeType;
9
10
/**
11
 * Trait for objects containing X.501 attributes.
12
 *
13
 * Implements methods for Countable and IteratorAggregate interfaces.
14
 */
15
trait AttributeContainer
16
{
17
    /**
18
     * Array of attributes.
19
     *
20
     * @var Attribute[]
21
     */
22
    protected $_attributes;
23
24
    /**
25
     * Check whether attribute is present.
26
     *
27
     * @param string $name OID or attribute name
28
     *
29
     * @return bool
30 25
     */
31
    public function has(string $name): bool
32 25
    {
33 25
        return null !== $this->_findFirst($name);
34 24
    }
35 24
36
    /**
37
     * Get first attribute by OID or attribute name.
38 2
     *
39
     * @param string $name OID or attribute name
40
     *
41
     * @throws \OutOfBoundsException
42
     *
43
     * @return Attribute
44
     */
45
    public function firstOf(string $name): Attribute
46
    {
47 12
        $attr = $this->_findFirst($name);
48
        if (!$attr) {
49 12
            throw new \UnexpectedValueException("No {$name} attribute.");
50
        }
51
        return $attr;
52
    }
53
54
    /**
55
     * Get all attributes of given name.
56
     *
57
     * @param string $name OID or attribute name
58
     *
59 16
     * @return Attribute[]
60
     */
61 16
    public function allOf(string $name): array
62 16
    {
63 1
        $oid = AttributeType::attrNameToOID($name);
64
        $attrs = array_filter($this->_attributes,
65 15
            function (Attribute $attr) use ($oid) {
66
                return $attr->oid() === $oid;
67
            });
68
        return array_values($attrs);
69
    }
70
71
    /**
72
     * Get all attributes.
73
     *
74 3
     * @return Attribute[]
75
     */
76 3
    public function all(): array
77 3
    {
78 3
        return $this->_attributes;
79 3
    }
80 3
81 3
    /**
82
     * Get self with additional attributes added.
83
     *
84
     * @param Attribute ...$attribs
85
     *
86
     * @return self
87
     */
88
    public function withAdditional(Attribute ...$attribs): self
89 1
    {
90
        $obj = clone $this;
91 1
        foreach ($attribs as $attr) {
92
            $obj->_attributes[] = $attr;
93
        }
94
        return $obj;
95
    }
96
97
    /**
98
     * Get self with single unique attribute added.
99
     *
100 2
     * All previous attributes of the same type are removed.
101
     *
102 2
     * @param Attribute $attr
103 2
     *
104 2
     * @return self
105
     */
106 2
    public function withUnique(Attribute $attr): self
107
    {
108
        $obj = clone $this;
109
        $obj->_attributes = array_filter($obj->_attributes,
110
            function (Attribute $a) use ($attr) {
111
                return $a->oid() !== $attr->oid();
112
            });
113
        $obj->_attributes[] = $attr;
114
        return $obj;
115
    }
116
117 5
    /**
118
     * Get number of attributes.
119 5
     *
120 5
     * @see \Countable::count()
121 5
     *
122 3
     * @return int
123 5
     */
124 5
    public function count(): int
125 5
    {
126
        return count($this->_attributes);
127
    }
128
129
    /**
130
     * Get iterator for attributes.
131
     *
132
     * @see \IteratorAggregate::getIterator()
133
     *
134 6
     * @return \ArrayIterator
135
     */
136 6
    public function getIterator(): \ArrayIterator
137
    {
138
        return new \ArrayIterator($this->_attributes);
139
    }
140
141
    /**
142
     * Find first attribute of given name or OID.
143
     *
144
     * @param string $name
145 3
     *
146
     * @return null|Attribute
147 3
     */
148
    protected function _findFirst(string $name): ?Attribute
149
    {
150
        $oid = AttributeType::attrNameToOID($name);
151
        foreach ($this->_attributes as $attr) {
152
            if ($attr->oid() === $oid) {
153
                return $attr;
154
            }
155
        }
156
        return null;
157
    }
158
}
159