Passed
Push — master ( 54a972...ce09f5 )
by Carsten
05:46 queued 10s
created

MRounder::__invoke()   B

Complexity

Conditions 7
Paths 7

Size

Total Lines 30

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 17
CRAP Score 7

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 7
nc 7
nop 1
dl 0
loc 30
ccs 17
cts 17
cp 1
crap 7
rs 8.5066
c 1
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 114
            $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 109
            $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 35
                return 0;
71
72 92
            switch ($this->mode):
73 92
                case static::ROUND:
74 72
                    return mround( $number, $this->multiple );
75
                    break;
0 ignored issues
show
Unused Code introduced by
break is not strictly necessary here and could be removed.

The break statement is not necessary if it is preceded for example by a return statement:

switch ($x) {
    case 1:
        return 'foo';
        break; // This break is not necessary and can be left off.
}

If you would like to keep this construct to be consistent with other case statements, you can safely mark this issue as a false-positive.

Loading history...
76
77 20
                case static::FLOOR:
78 8
                    return mfloor( $number, $this->multiple );
79
                    break;
0 ignored issues
show
Unused Code introduced by
break is not strictly necessary here and could be removed.

The break statement is not necessary if it is preceded for example by a return statement:

switch ($x) {
    case 1:
        return 'foo';
        break; // This break is not necessary and can be left off.
}

If you would like to keep this construct to be consistent with other case statements, you can safely mark this issue as a false-positive.

Loading history...
80
81 12
                case static::CEIL:
82 8
                    return mceil( $number, $this->multiple );
83
                    break;
0 ignored issues
show
Unused Code introduced by
break is not strictly necessary here and could be removed.

The break statement is not necessary if it is preceded for example by a return statement:

switch ($x) {
    case 1:
        return 'foo';
        break; // This break is not necessary and can be left off.
}

If you would like to keep this construct to be consistent with other case statements, you can safely mark this issue as a false-positive.

Loading history...
84
85 1
                default:
86 4
                    throw new MRoundUnexpectedValueException("Unexpected round mode value");
87
88 1
            endswitch;
89
90 16
        elseif (is_array( $number)):
91 12
            return array_map($this, $number);
92
        endif;
93
94 4
        throw new MRoundInvalidArgumentException("Number must be numeric or array of numbers.");
95
    }
96
}
97