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 ( 06daff...a25647 )
by Joni
03:03
created

Flags   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 123
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 13
c 0
b 0
f 0
lcom 1
cbo 1
dl 0
loc 123
ccs 48
cts 48
cp 1
rs 10

6 Methods

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