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.

Utils   A
last analyzed

Complexity

Total Complexity 18

Size/Duplication

Total Lines 132
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 1

Test Coverage

Coverage 100%

Importance

Changes 3
Bugs 0 Features 1
Metric Value
wmc 18
c 3
b 0
f 1
lcom 0
cbo 1
dl 0
loc 132
ccs 54
cts 54
cp 1
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A getPlainNumber() 0 8 2
B convertToBase10() 0 34 6
D convertBase() 0 44 9
A multiply() 0 6 1
1
<?php
2
3
namespace PHP\Math\BigNumber;
4
5
use InvalidArgumentException;
6
use RuntimeException;
7
8
/**
9
 * A utility class.
10
 */
11
final class Utils
12
{
13
    const BASE32_ALPHABET = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ';
14
15
    /**
16
     * Gets the plain number from the given input. Converts a BigNumber to a string.
17
     *
18
     * @param string|int|BigNumber $number The number to convert.
19
     * @return string
20
     */
21 17
    public static function getPlainNumber($number)
22
    {
23 17
        if ($number instanceof BigNumber) {
24 14
            $number = $number->getValue();
25 14
        }
26
27 17
        return (string)$number;
28
    }
29
30
    /**
31
     * Converts the provided number from an arbitrary base to to a base 10 number.
32
     *
33
     * @param string|int|BigNumber $number The number to convert.
34
     * @param int $fromBase The base to convert the number from.
35
     * @return string
36
     * @throws InvalidArgumentException Thrown when the base is out of reach.
37
     */
38 10
    public static function convertToBase10($number, $fromBase)
39
    {
40 10
        $number = (string)ceil(static::getPlainNumber($number));
41
42 10
        if ($fromBase == 10) {
43 2
            return $number;
44 8
        } elseif ($fromBase < 2 || $fromBase > 32) {
45 2
            throw new InvalidArgumentException(sprintf(
46 2
                'Base %d is unsupported, should be between 2 and 32.',
47
                $fromBase
48 2
            ));
49
        }
50
51 6
        $result = '0';
52 6
        $chars = self::BASE32_ALPHABET;
53
54 6
        for ($i = 0, $len = strlen($number); $i < $len; $i++) {
55 6
            $index = strpos($chars, $number[$i]);
56
57 6
            if ($index >= $fromBase) {
58 2
                throw new RuntimeException(sprintf(
59 2
                    'The digit %s in the number %s is an invalid digit for base-%s.',
60 2
                    $chars[$index],
61 2
                    $number,
62
                    $fromBase
63 2
                ));
64
            }
65
66 4
            $result = bcmul($result, $fromBase);
67 4
            $result = bcadd($result, strpos($chars, $number[$i]));
68 4
        }
69
70 4
        return $result;
71
    }
72
73
    /**
74
     * Converts the provided number from an arbitrary base to another arbitrary base (from 2 to 36).
75
     *
76
     * @param string|int|BigNumber $number The number to convert.
77
     * @param int $fromBase The base to convert the number from.
78
     * @param int $toBase The base to convert the number to.
79
     * @return string
80
     * @throws InvalidArgumentException Thrown when the base is out of reach.
81
     */
82 6
    public static function convertBase($number, $fromBase, $toBase)
83
    {
84 6
        $number = static::getPlainNumber($number);
85
86 6
        if ($fromBase == $toBase) {
87 1
            return $number;
88
        }
89
90 5
        if ($fromBase < 2 || $fromBase > 32) {
91 1
            throw new InvalidArgumentException(sprintf(
92 1
                'Base %d is unsupported, should be between 2 and 32.',
93
                $fromBase
94 1
            ));
95
        }
96
97 4
        if ($toBase < 2 || $toBase > 32) {
98 1
            throw new InvalidArgumentException(sprintf(
99 1
                'Base %d is unsupported, should be between 2 and 32.',
100
                $toBase
101 1
            ));
102
        }
103
104
        // Save the sign and trim it off so we can easier calculate the number:
105 3
        $sign = (strpos($number, '-') === 0) ? '-' : '';
106 3
        $number = ltrim($number, '-+');
107
108
        // First we convert the number to a decimal value:
109 3
        $decimal = static::convertToBase10($number, $fromBase);
110 2
        if ($toBase == 10) {
111 1
            return $decimal;
112
        }
113
114
        // Next we convert to the correct base:
115 1
        $result = '';
116 1
        $chars = self::BASE32_ALPHABET;
117
118
        do {
119 1
            $remainder = bcmod($decimal, $toBase);
120 1
            $decimal = bcdiv($decimal, $toBase);
121 1
            $result = $chars[$remainder] . $result;
122 1
        } while (bccomp($decimal, '0'));
123
124 1
        return $sign . ltrim($result, '0');
125
    }
126
127
    /**
128
     * Multiplies the two given numbers.
129
     *
130
     * @param BigNumber $lft The left number.
131
     * @param BigNumber $rgt The right number.
132
     * @param int $scale The scale of the calculated number.
133
     * @param bool $mutable Whether or not the result is mutable.
134
     * @return BigNumber
135
     */
136 4
    public static function multiply(BigNumber $lft, BigNumber $rgt, $scale = 10, $mutable = true)
137
    {
138 4
        $bigNumber = new BigNumber($lft, $scale, $mutable);
0 ignored issues
show
Documentation introduced by
$lft is of type object<PHP\Math\BigNumber\BigNumber>, but the function expects a integer.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
139
140 4
        return $bigNumber->multiply($rgt);
141
    }
142
}
143