MRounder::__construct()   A
last analyzed

Complexity

Conditions 4
Paths 4

Size

Total Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 4

Importance

Changes 0
Metric Value
cc 4
nc 4
nop 2
dl 0
loc 13
ccs 8
cts 8
cp 1
crap 4
rs 9.8333
c 0
b 0
f 0
1
<?php
2
namespace FilmTools\MRounder;
3
4
/**
5
 * Callable: Returns a given number rounded to the desired multiple.
6
 *
7
 * PHP implementation by Nasser Hekmati on StackOverflow
8
 * @see https://stackoverflow.com/a/48643210/3143771
9
 * @see https://support.office.com/en-us/article/mround-function-c299c3b0-15a5-426d-aa4b-d2d5b3baf427
10
 */
11
class MRounder
12
{
13
14
    /**
15
     * Default rounding behaviour
16
     * @var string
17
     */
18
    const FLOOR="floor";
19
20
    /**
21
     * Turn on CEILING functionality
22
     * @var string
23
     */
24
    const CEIL="ceil";
25
26
    /**
27
     * Turn on FLOOR functionality
28
     * @var string
29
     */
30
    const ROUND="round";
31
32
    /**
33
     * @var numeric
34
     */
35
    public $multiple;
36
37
    /**
38
     *
39
     * @var string
40
     */
41
    public $mode;
42
43
44
    /**
45
     * @param numeric $multiple
46
     */
47 120
    public function __construct( $multiple = 1, $mode = null )
48
    {
49 120
        if (is_numeric($multiple))
50 112
            $this->multiple = $multiple;
0 ignored issues
show
Documentation Bug introduced by
It seems like $multiple of type integer or double or string is incompatible with the declared type object<FilmTools\MRounder\numeric> of property $multiple.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
51
        else
52 8
            throw new MRoundInvalidArgumentException("Parameter 'multiple' must be numeric.");
53
54
55 112
        if (in_array($mode, [ null, static::FLOOR, static::CEIL, static::ROUND ]))
56 108
            $this->mode = $mode ?: static::ROUND;
57
        else
58 4
            throw new MRoundInvalidArgumentException("Parameter 'mode' must be one of 'round', floor', or 'ceil'.");
59 108
    }
60
61
62
    /**
63
     * Accepts a number or array with numbers.
64
     * @param numeric[] $number
65
     */
66 108
    public function __invoke( $number )
67
    {
68 108
        if (is_numeric($number)):
69 104
            if ($this->multiple == 0)
70 12
                return 0;
71
72 92
            switch ($this->mode):
73 92
                case static::ROUND:
74 72
                    return mround( $number, $this->multiple );
75
76 20
                case static::FLOOR:
77 8
                    return mfloor( $number, $this->multiple );
78
79 12
                case static::CEIL:
80 8
                    return mceil( $number, $this->multiple );
81
82
                default:
83 4
                    throw new MRoundUnexpectedValueException("Unexpected round mode value");
84
85
            endswitch;
86
87 16
        elseif (is_array( $number)):
88 12
            return array_map($this, $number);
89
        endif;
90
91 4
        throw new MRoundInvalidArgumentException("Number must be numeric or array of numbers.");
92
    }
93
}
94