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

Fact::funcFact()   A

Complexity

Conditions 5
Paths 7

Size

Total Lines 22
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 13
CRAP Score 5

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 5
eloc 13
c 1
b 0
f 0
nc 7
nop 1
dl 0
loc 22
ccs 13
cts 13
cp 1
crap 5
rs 9.5222
1
<?php
2
3
namespace PhpOffice\PhpSpreadsheet\Calculation\MathTrig;
4
5
use Exception;
6
use PhpOffice\PhpSpreadsheet\Calculation\Functions;
7
use PhpOffice\PhpSpreadsheet\Calculation\Statistical;
8
9
class Fact
10
{
11
    /**
12
     * FACT.
13
     *
14
     * Returns the factorial of a number.
15
     * The factorial of a number is equal to 1*2*3*...* number.
16
     *
17
     * Excel Function:
18
     *        FACT(factVal)
19
     *
20
     * @param float $factVal Factorial Value
21
     *
22
     * @return int|string Factorial, or a string containing an error
23
     */
24 81
    public static function funcFact($factVal)
25
    {
26
        try {
27 81
            $factVal = Helpers::validateNumericNullBool($factVal);
28 79
            Helpers::validateNotNegative($factVal);
29 4
        } catch (Exception $e) {
30 4
            return $e->getMessage();
31
        }
32
33 77
        $factLoop = floor($factVal);
34 77
        if ($factVal > $factLoop) {
35 5
            if (Functions::getCompatibilityMode() == Functions::COMPATIBILITY_GNUMERIC) {
36 2
                return Statistical::GAMMAFunction($factVal + 1);
37
            }
38
        }
39
40 75
        $factorial = 1;
41 75
        while ($factLoop > 1) {
42 69
            $factorial *= $factLoop--;
43
        }
44
45 75
        return $factorial;
46
    }
47
}
48