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

functions.php ➔ mceil()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 12

Duplication

Lines 12
Ratio 100 %

Code Coverage

Tests 6
CRAP Score 4.0466

Importance

Changes 0
Metric Value
cc 4
nc 4
nop 2
dl 12
loc 12
ccs 6
cts 7
cp 0.8571
crap 4.0466
rs 9.8666
c 0
b 0
f 0
1
<?php
2
namespace FilmTools\MRounder;
3
4
5
/**
6
 * Returns the given number rounded to the given multiple.
7
 *
8
 * PHP implementation by Nasser Hekmati on StackOverflow
9
 * @see https://stackoverflow.com/a/48643210/3143771
10
 * @see https://support.office.com/en-us/article/mround-function-c299c3b0-15a5-426d-aa4b-d2d5b3baf427
11
 *
12
 * @param int|float $number
13
 * @param int|float $multiple
14
 */
15 View Code Duplication
function mround( $number, $multiple)
0 ignored issues
show
Duplication introduced by
This function seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
16
{
17 72
    if (!is_numeric($number))
18 18
        throw new MRoundInvalidArgumentException("First parameter must be numeric.");
19
20 72
    if (!is_numeric($multiple))
21 18
        throw new MRoundInvalidArgumentException("Second parameter 'multiple' must be numeric.");
22 72
    elseif ( $multiple == 0)
23
        return 0;
24
25 72
    return round( $number/$multiple, 0 ) * $multiple;
26
27
}
28
29
// This snippet is not required any longer since we're in namespace here.
30
# if (!function_exists("mround")):
31
# endif;
32
33
34
/**
35
 * Returns the given number rounded up to the given multiple.
36
 *
37
 * This function corresponds to CEILING function known from Microsoft Excel in LibreOffice/OpenOffice.
38
 *
39
 * @see https://support.office.com/en-us/article/ceiling-function-0a5cd7c8-0720-4f0a-bd2c-c943e510899f
40
 * @see https://help.libreoffice.org/Calc/Mathematical_Functions#CEILING
41
 *
42
 * @param int|float $number
43
 * @param int|float $multiple
44
 */
45 View Code Duplication
function mceil( $number, $multiple)
0 ignored issues
show
Duplication introduced by
This function seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
46
{
47 8
    if (!is_numeric($number))
48 2
        throw new MRoundInvalidArgumentException("First parameter must be numeric.");
49
50 8
    if (!is_numeric($multiple))
51 2
        throw new MRoundInvalidArgumentException("Second parameter 'multiple' must be numeric.");
52 8
    elseif ( $multiple == 0)
53
        return 0;
54
55 8
    return ceil( $number/$multiple ) * $multiple;
56
}
57
58
59
/**
60
 * Returns the given number rounded down to the given multiple.
61
 *
62
 * This function corresponds the the FLOOR function known from Microsoft Excel in LibreOffice/OpenOffice.
63
 *
64
 * @see https://support.office.com/en-us/article/floor-function-14bb497c-24f2-4e04-b327-b0b4de5a8886
65
 * @see https://help.libreoffice.org/Calc/Mathematical_Functions#FLOOR
66
 *
67
 * @param int|float $number
68
 * @param int|float $multiple
69
 */
70 View Code Duplication
function mfloor( $number, $multiple)
0 ignored issues
show
Duplication introduced by
This function seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
71
{
72 8
    if (!is_numeric($number))
73 2
        throw new MRoundInvalidArgumentException("First parameter must be numeric.");
74
75 8
    if (!is_numeric($multiple))
76 2
        throw new MRoundInvalidArgumentException("Second parameter 'multiple' must be numeric.");
77 8
    elseif ( $multiple == 0)
78
        return 0;
79
80 8
    return floor( $number/$multiple ) * $multiple;
81
}
82