Completed
Push — master ( e5bccc...a43cbf )
by Matt
02:23
created

Comparator::compare()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 12
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 4

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 4
eloc 6
c 1
b 0
f 0
nc 4
nop 2
dl 0
loc 12
ccs 6
cts 6
cp 1
crap 4
rs 9.2
1
<?php
2
3
namespace League\JsonGuard;
4
5
class Comparator
6
{
7
    const DEFAULT_SCALE = 10;
8
9
    /**
10
     * The number of digits after the decimal place which will be used in the comparison.
11
     * The scale will only be used if bccomp is available.  Otherwise your system precision
12
     * will be used.
13
     *
14
     * @var int
15
     */
16
    private static $scale = self::DEFAULT_SCALE;
17
18
    /**
19
     * Compare two arbitrary precision numbers.  This method will use bccomp for the comparison,
20
     * or fall back to using standard comparison operators if ext-bcmath is not available.
21
     *
22
     * @param string|double|int $leftOperand
23
     * @param string|double|int $rightOperand
24
     *
25
     * @return int Returns 0 if the two operands are equal, 1 if the left_operand is larger than the right_operand,
26
     * -1 otherwise.
27
     */
28 60
    public static function compare($leftOperand, $rightOperand)
29
    {
30 60
        if (function_exists('bccomp')) {
31 36
            return bccomp($leftOperand, $rightOperand, self::$scale);
32
        }
33
34 24
        if ($leftOperand === $rightOperand) {
35 6
            return 0;
36
        }
37
38 22
        return $leftOperand > $rightOperand ? 1 : -1;
39
    }
40
41
    /**
42
     * Set the number of digits after the decimal place which will be used in the comparison.
43
     *
44
     * @param int $value
45
     */
46 10
    public static function setScale($value)
47
    {
48 10
        self::$scale = $value;
49 10
    }
50
}
51