Passed
Pull Request — master (#4466)
by Owen
14:38
created

Floor::floorMathTest()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
eloc 1
dl 0
loc 3
rs 10
c 0
b 0
f 0
ccs 0
cts 0
cp 0
cc 3
nc 3
nop 3
crap 12
1
<?php
2
3
namespace PhpOffice\PhpSpreadsheet\Calculation\MathTrig;
4
5
use PhpOffice\PhpSpreadsheet\Calculation\ArrayEnabled;
6
use PhpOffice\PhpSpreadsheet\Calculation\Exception;
7
use PhpOffice\PhpSpreadsheet\Calculation\Functions;
8
use PhpOffice\PhpSpreadsheet\Calculation\Information\ExcelError;
9
10
class Floor
11
{
12
    use ArrayEnabled;
13
14 4
    private static function floorCheck1Arg(): void
15
    {
16 4
        $compatibility = Functions::getCompatibilityMode();
17 4
        if ($compatibility === Functions::COMPATIBILITY_EXCEL) {
18 2
            throw new Exception('Excel requires 2 arguments for FLOOR');
19
        }
20
    }
21
22
    /**
23
     * FLOOR.
24
     *
25
     * Rounds number down, toward zero, to the nearest multiple of significance.
26
     *
27
     * Excel Function:
28
     *        FLOOR(number[,significance])
29
     *
30
     * @param mixed $number Expect float. Number to round
31
     *                      Or can be an array of values
32
     * @param mixed $significance Expect float. Significance
33
     *                      Or can be an array of values
34
     *
35
     * @return array<mixed>|float|string Rounded Number, or a string containing an error
36
     *         If an array of numbers is passed as an argument, then the returned result will also be an array
37
     *            with the same dimensions
38
     */
39 39
    public static function floor(mixed $number, mixed $significance = null)
40
    {
41 39
        if (is_array($number) || is_array($significance)) {
42 5
            return self::evaluateArrayArguments([self::class, __FUNCTION__], $number, $significance);
43
        }
44
45 39
        if ($significance === null) {
46 4
            self::floorCheck1Arg();
47
        }
48
49
        try {
50 37
            $number = Helpers::validateNumericNullBool($number);
51 35
            $significance = Helpers::validateNumericNullSubstitution($significance, ($number < 0) ? -1 : 1);
52 2
        } catch (Exception $e) {
53 2
            return $e->getMessage();
54
        }
55
56 35
        return self::argumentsOk((float) $number, (float) $significance);
57
    }
58
59
    /**
60
     * FLOOR.MATH.
61
     *
62
     * Round a number down to the nearest integer or to the nearest multiple of significance.
63
     *
64
     * Excel Function:
65
     *        FLOOR.MATH(number[,significance[,mode]])
66
     *
67
     * @param mixed $number Number to round
68
     *                      Or can be an array of values
69
     * @param mixed $significance Significance
70
     *                      Or can be an array of values
71
     * @param mixed $mode direction to round negative numbers
72
     *                      Or can be an array of values
73
     *
74
     * @return array<mixed>|float|string Rounded Number, or a string containing an error
75
     *         If an array of numbers is passed as an argument, then the returned result will also be an array
76
     *            with the same dimensions
77
     */
78 28
    public static function math(mixed $number, mixed $significance = null, mixed $mode = 0, bool $checkSigns = false)
79
    {
80 28
        if (is_array($number) || is_array($significance) || is_array($mode)) {
81 5
            return self::evaluateArrayArguments([self::class, __FUNCTION__], $number, $significance, $mode);
82
        }
83
84
        try {
85 28
            $number = Helpers::validateNumericNullBool($number);
86 26
            $significance = Helpers::validateNumericNullSubstitution($significance, ($number < 0) ? -1 : 1);
87 26
            $mode = Helpers::validateNumericNullSubstitution($mode, null);
88 2
        } catch (Exception $e) {
89 2
            return $e->getMessage();
90
        }
91
92 26
        if ($checkSigns) {
93
            if (($number > 0 && $significance < 0) || ($number < 0 && $significance > 0)) {
94
                return ExcelError::VALUE();
95
            }
96
        }
97
98
        return self::argsOk((float) $number, (float) $significance, (int) $mode);
99
    }
100
101
    /**
102
     * FLOOR.ODS, pseudo-function - FLOOR as implemented in ODS.
103
     *
104
     * Round a number down to the nearest integer or to the nearest multiple of significance.
105
     *
106
     * ODS Function (theoretical):
107
     *        FLOOR.ODS(number[,significance[,mode]])
108
     *
109
     * @param mixed $number Number to round
110
     * @param mixed $significance Significance
111
     * @param array<mixed>|int $mode direction to round negative numbers
112 22
     *
113
     * @return array<mixed>|float|string Rounded Number, or a string containing an error
114 22
     */
115 5
    public static function mathOds(mixed $number, mixed $significance = null, mixed $mode = 0)
116
    {
117
        return self::math($number, $significance, $mode, true);
118
    }
119 22
120 20
    /**
121 3
     * FLOOR.PRECISE.
122 3
     *
123
     * Rounds number down, toward zero, to the nearest multiple of significance.
124
     *
125 19
     * Excel Function:
126
     *        FLOOR.PRECISE(number[,significance])
127
     *
128
     * @param array<mixed>|float $number Number to round
129
     *                      Or can be an array of values
130
     * @param array<mixed>|float $significance Significance
131 19
     *                      Or can be an array of values
132
     *
133 19
     * @return array<mixed>|float|string Rounded Number, or a string containing an error
134 1
     *         If an array of numbers is passed as an argument, then the returned result will also be an array
135
     *            with the same dimensions
136 18
     */
137 3
    public static function precise($number, $significance = 1)
138
    {
139
        if (is_array($number) || is_array($significance)) {
140 15
            return self::evaluateArrayArguments([self::class, __FUNCTION__], $number, $significance);
141
        }
142
143
        try {
144
            $number = Helpers::validateNumericNullBool($number);
145
            $significance = Helpers::validateNumericNullSubstitution($significance, null);
146
        } catch (Exception $e) {
147
            return $e->getMessage();
148 26
        }
149
150 26
        return self::argumentsOkPrecise((float) $number, (float) $significance);
151 1
    }
152
153 25
    /**
154 4
     * Avoid Scrutinizer problems concerning complexity.
155
     */
156 21
    private static function argumentsOkPrecise(float $number, float $significance): string|float
157 6
    {
158
        if ($significance == 0.0) {
159
            return ExcelError::DIV0();
160 15
        }
161
        if ($number == 0.0) {
162
            return 0.0;
163
        }
164
165
        return floor($number / abs($significance)) * abs($significance);
166 21
    }
167
168 21
    /**
169
     * Avoid Scrutinizer complexity problems.
170
     *
171
     * @return float|string Rounded Number, or a string containing an error
172
     */
173
    private static function argsOk(float $number, float $significance, int $mode): string|float
174 35
    {
175
        if (!$significance) {
176 35
            return ExcelError::DIV0();
177 1
        }
178
        if (!$number) {
179 34
            return 0.0;
180 3
        }
181
        if (self::floorMathTest($number, $significance, $mode)) {
182 31
            return ceil($number / $significance) * $significance;
183 29
        }
184
185 2
        return floor($number / $significance) * $significance;
186 1
    }
187
188
    /**
189 1
     * Let FLOORMATH complexity pass Scrutinizer.
190
     */
191
    private static function floorMathTest(float $number, float $significance, int $mode): bool
192
    {
193
        return Helpers::returnSign($significance) == -1 || (Helpers::returnSign($number) == -1 && !empty($mode));
194
    }
195
196
    /**
197
     * Avoid Scrutinizer problems concerning complexity.
198
     */
199
    private static function argumentsOk(float $number, float $significance): string|float
200
    {
201
        if ($significance == 0.0) {
202
            return ExcelError::DIV0();
203
        }
204
        if ($number == 0.0) {
205
            return 0.0;
206
        }
207
        if (Helpers::returnSign($significance) == 1) {
208
            return floor($number / $significance) * $significance;
209
        }
210
        if (Helpers::returnSign($number) == -1 && Helpers::returnSign($significance) == -1) {
211
            return floor($number / $significance) * $significance;
212
        }
213
214
        return ExcelError::NAN();
215
    }
216
}
217