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 ( 9bb5cc...d08348 )
by Joni
02:00
created

Flags::bitString()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 6
ccs 4
cts 4
cp 1
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 4
nc 2
nop 0
crap 2
1
<?php
2
3
declare(strict_types = 1);
4
5
namespace ASN1\Util;
6
7
use ASN1\Type\Primitive\BitString;
8
9
/**
10
 * Class to handle a bit string as a field of flags.
11
 */
12
class Flags
13
{
14
    /**
15
     * Flag octets.
16
     *
17
     * @var string $_flags
18
     */
19
    protected $_flags;
20
    
21
    /**
22
     * Number of flags.
23
     *
24
     * @var int $_width
25
     */
26
    protected $_width;
27
    
28
    /**
29
     * Constructor.
30
     *
31
     * @param int|string $flags Flags
32
     * @param int $width The number of flags. If width is larger than number of
33
     *        bits in $flags, zeroes are prepended to flag field.
34
     */
35 62
    public function __construct($flags, int $width)
36
    {
37 62
        if (!$width) {
38 2
            $this->_flags = "";
39
        } else {
40
            // calculate number of unused bits in last octet
41 60
            $last_octet_bits = $width % 8;
42 60
            $unused_bits = $last_octet_bits ? 8 - $last_octet_bits : 0;
43 60
            $num = gmp_init($flags);
44
            // mask bits outside bitfield width
45 60
            $mask = gmp_sub(gmp_init(1) << $width, 1);
46 60
            $num &= $mask;
47
            // shift towards MSB if needed
48 60
            $data = gmp_export($num << $unused_bits, 1,
49 60
                GMP_MSW_FIRST | GMP_BIG_ENDIAN);
50 60
            $octets = unpack("C*", $data);
51 60
            $bits = count($octets) * 8;
52
            // pad with zeroes
53 60
            while ($bits < $width) {
54 20
                array_unshift($octets, 0);
55 20
                $bits += 8;
56
            }
57 60
            $this->_flags = pack("C*", ...$octets);
58
        }
59 62
        $this->_width = $width;
60 62
    }
61
    
62
    /**
63
     * Initialize from BitString.
64
     *
65
     * @param BitString $bs
66
     * @param int $width
67
     * @return self
68
     */
69 8
    public static function fromBitString(BitString $bs, int $width): self
70
    {
71 8
        $num_bits = $bs->numBits();
72 8
        $num = gmp_import($bs->string(), 1, GMP_MSW_FIRST | GMP_BIG_ENDIAN);
73 8
        $num >>= $bs->unusedBits();
74 8
        if ($num_bits < $width) {
75 5
            $num <<= ($width - $num_bits);
76
        }
77 8
        return new self(gmp_strval($num, 10), $width);
78
    }
79
    
80
    /**
81
     * Check whether a bit at given index is set.
82
     * Index 0 is the leftmost bit.
83
     *
84
     * @param int $idx
85
     * @throws \OutOfBoundsException
86
     * @return bool
87
     */
88 15
    public function test(int $idx): bool
89
    {
90 15
        if ($idx >= $this->_width) {
91 1
            throw new \OutOfBoundsException("Index is out of bounds.");
92
        }
93
        // octet index
94 14
        $oi = (int) floor($idx / 8);
95 14
        $byte = $this->_flags[$oi];
96
        // bit index
97 14
        $bi = $idx % 8;
98
        // index 0 is the most significant bit in byte
99 14
        $mask = 0x01 << (7 - $bi);
100 14
        return (ord($byte) & $mask) > 0;
101
    }
102
    
103
    /**
104
     * Get flags as an octet string.
105
     * Zeroes are appended to the last octet if width is not divisible by 8.
106
     *
107
     * @return string
108
     */
109 23
    public function string(): string
110
    {
111 23
        return $this->_flags;
112
    }
113
    
114
    /**
115
     * Get flags as a base 10 integer.
116
     *
117
     * @return string Integer as a string
118
     */
119 15
    public function number(): string
120
    {
121 15
        $num = gmp_import($this->_flags, 1, GMP_MSW_FIRST | GMP_BIG_ENDIAN);
122 15
        $last_octet_bits = $this->_width % 8;
123 15
        $unused_bits = $last_octet_bits ? 8 - $last_octet_bits : 0;
124 15
        $num >>= $unused_bits;
125 15
        return gmp_strval($num, 10);
126
    }
127
    
128
    /**
129
     * Get flags as an integer.
130
     *
131
     * @return int
132
     */
133 1
    public function intNumber(): int
134
    {
135 1
        $num = new BigInt($this->number());
136 1
        return $num->intVal();
137
    }
138
    
139
    /**
140
     * Get flags as a BitString.
141
     * Unused bits are set accordingly. Trailing zeroes are not stripped.
142
     *
143
     * @return BitString
144
     */
145 9
    public function bitString(): BitString
146
    {
147 9
        $last_octet_bits = $this->_width % 8;
148 9
        $unused_bits = $last_octet_bits ? 8 - $last_octet_bits : 0;
149 9
        return new BitString($this->_flags, $unused_bits);
150
    }
151
}
152