Completed
Pull Request — master (#28)
by Matt
10:53 queued 08:33
created

Comparator::setScale()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 2
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 4
ccs 3
cts 3
cp 1
crap 1
rs 10
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, static::$scale);
0 ignored issues
show
Bug introduced by
Since $scale is declared private, accessing it with static will lead to errors in possible sub-classes; consider using self, or increasing the visibility of $scale to at least protected.

Let’s assume you have a class which uses late-static binding:

class YourClass
{
    private static $someVariable;

    public static function getSomeVariable()
    {
        return static::$someVariable;
    }
}

The code above will run fine in your PHP runtime. However, if you now create a sub-class and call the getSomeVariable() on that sub-class, you will receive a runtime error:

class YourSubClass extends YourClass { }

YourSubClass::getSomeVariable(); // Will cause an access error.

In the case above, it makes sense to update SomeClass to use self instead:

class SomeClass
{
    private static $someVariable;

    public static function getSomeVariable()
    {
        return self::$someVariable; // self works fine with private.
    }
}
Loading history...
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
        static::$scale = $value;
0 ignored issues
show
Bug introduced by
Since $scale is declared private, accessing it with static will lead to errors in possible sub-classes; consider using self, or increasing the visibility of $scale to at least protected.

Let’s assume you have a class which uses late-static binding:

class YourClass
{
    private static $someVariable;

    public static function getSomeVariable()
    {
        return static::$someVariable;
    }
}

The code above will run fine in your PHP runtime. However, if you now create a sub-class and call the getSomeVariable() on that sub-class, you will receive a runtime error:

class YourSubClass extends YourClass { }

YourSubClass::getSomeVariable(); // Will cause an access error.

In the case above, it makes sense to update SomeClass to use self instead:

class SomeClass
{
    private static $someVariable;

    public static function getSomeVariable()
    {
        return self::$someVariable; // self works fine with private.
    }
}
Loading history...
49 10
    }
50
}
51