Passed
Push — master ( c380b2...9239b3 )
by Adrien
10:06
created

Base   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 40
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 15
c 1
b 0
f 0
dl 0
loc 40
ccs 14
cts 14
cp 1
rs 10
wmc 9

1 Method

Rating   Name   Duplication   Size   Complexity  
B funcBase() 0 24 9
1
<?php
2
3
namespace PhpOffice\PhpSpreadsheet\Calculation\MathTrig;
4
5
use Exception;
6
use PhpOffice\PhpSpreadsheet\Calculation\Functions;
7
8
class Base
9
{
10
    /**
11
     * BASE.
12
     *
13
     * Converts a number into a text representation with the given radix (base).
14
     *
15
     * Excel Function:
16
     *        BASE(Number, Radix [Min_length])
17
     *
18
     * @param mixed $number expect float
19
     * @param mixed $radix expect float
20
     * @param mixed $minLength expect int or null
21
     *
22
     * @return string the text representation with the given radix (base)
23
     */
24 15
    public static function funcBase($number, $radix, $minLength = null)
25
    {
26
        try {
27 15
            $number = (int) Helpers::validateNumericNullBool($number);
28 14
            $radix = (int) Helpers::validateNumericNullBool($radix);
29 2
        } catch (Exception $e) {
30 2
            return $e->getMessage();
31
        }
32 13
        $minLength = Functions::flattenSingleValue($minLength);
33
34 13
        if ($minLength === null || is_numeric($minLength)) {
35 11
            if ($number < 0 || $number >= 2 ** 53 || $radix < 2 || $radix > 36) {
36 4
                return Functions::NAN(); // Numeric range constraints
37
            }
38
39 7
            $outcome = strtoupper((string) base_convert($number, 10, $radix));
40 7
            if ($minLength !== null) {
41 3
                $outcome = str_pad($outcome, (int) $minLength, '0', STR_PAD_LEFT); // String padding
42
            }
43
44 7
            return $outcome;
45
        }
46
47 2
        return Functions::VALUE();
48
    }
49
}
50