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.
Passed
Push — master ( 95c197...564e5e )
by Joni
04:32
created

BigInt   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 105
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 12
lcom 1
cbo 0
dl 0
loc 105
ccs 28
cts 28
cp 1
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A intVal() 0 13 4
A __toString() 0 3 1
A _intMaxGmp() 0 7 2
A gmpObj() 0 3 1
A __construct() 0 3 1
A base10() 0 3 1
1
<?php
2
3
declare(strict_types = 1);
4
5
namespace Sop\ASN1\Util;
6
7
/**
8
 * Class to wrap an integer of arbirtary length.
9
 */
10
class BigInt
11
{
12
    /**
13
     * Number as a base10 integer string.
14
     *
15
     * @var string
16
     */
17
    private $_num;
18
19
    /**
20
     * Number as an integer type.
21
     *
22
     * @internal Lazily initialized
23
     *
24
     * @var null|int
25
     */
26
    private $_intNum;
27
28
    /**
29
     * Constructor.
30
     *
31
     * @param int|string $num
32
     */
33 399
    public function __construct($num)
34
    {
35 399
        $this->_num = strval($num);
36 399
    }
37
38
    /**
39
     * @return string
40
     */
41 1
    public function __toString()
42
    {
43 1
        return $this->base10();
44
    }
45
46
    /**
47
     * Get the number as a base10 integer string.
48
     *
49
     * @return string
50
     */
51 34
    public function base10(): string
52
    {
53 34
        return $this->_num;
54
    }
55
56
    /**
57
     * Get the number as an integer.
58
     *
59
     * @throws \RuntimeException If number overflows integer size
60
     *
61
     * @return int
62
     */
63 254
    public function intVal(): int
64
    {
65 254
        if (!isset($this->_intNum)) {
66 254
            $num = gmp_init($this->_num, 10);
67 254
            if (gmp_cmp($num, $this->_intMaxGmp()) > 0) {
68 4
                throw new \RuntimeException('Integer overflow.');
69
            }
70 250
            if (gmp_cmp($num, $this->_intMinGmp()) < 0) {
71 1
                throw new \RuntimeException('Integer underflow.');
72
            }
73 249
            $this->_intNum = gmp_intval($num);
74
        }
75 249
        return $this->_intNum;
76
    }
77
78
    /**
79
     * Get the number as a GMP object.
80
     *
81
     * @return \GMP
82
     */
83 150
    public function gmpObj(): \GMP
84
    {
85 150
        return gmp_init($this->_num, 10);
86
    }
87
88
    /**
89
     * Get the maximum integer value.
90
     *
91
     * @return \GMP
92
     */
93 254
    private function _intMaxGmp(): \GMP
94
    {
95 254
        static $gmp;
96 254
        if (!isset($gmp)) {
97 1
            $gmp = gmp_init(PHP_INT_MAX, 10);
98
        }
99 254
        return $gmp;
100
    }
101
102
    /**
103
     * Get the minimum integer value.
104
     *
105
     * @return \GMP
106
     */
107 250
    private function _intMinGmp(): \GMP
108
    {
109 250
        static $gmp;
110 250
        if (!isset($gmp)) {
111 1
            $gmp = gmp_init(PHP_INT_MIN, 10);
112
        }
113 250
        return $gmp;
114
    }
115
}
116