Passed
Push — master ( 2d1f4e...e6aacf )
by
unknown
26:57 queued 19:21
created

Floor   B

Complexity

Total Complexity 44

Size/Duplication

Total Lines 213
Duplicated Lines 0 %

Test Coverage

Coverage 95.52%

Importance

Changes 0
Metric Value
wmc 44
eloc 64
c 0
b 0
f 0
dl 0
loc 213
ccs 64
cts 67
cp 0.9552
rs 8.8798

9 Methods

Rating   Name   Duplication   Size   Complexity  
A floor() 0 18 6
A floorCheck1Arg() 0 5 2
A mathOds() 0 3 1
A precise() 0 17 5
A argumentsOkPrecise() 0 10 3
C math() 0 24 12
B argumentsOk() 0 18 8
A argsOk() 0 13 4
A floorMathTest() 0 3 3

How to fix   Complexity   

Complex Class

Complex classes like Floor often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

While breaking up the class, it is a good idea to analyze how other classes use Floor, and based on these observations, apply Extract Interface, too.

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 44
    public static function floor(mixed $number, mixed $significance = null)
40
    {
41 44
        if (is_array($number) || is_array($significance)) {
42 5
            return self::evaluateArrayArguments([self::class, __FUNCTION__], $number, $significance);
43
        }
44
45 44
        if ($significance === null) {
46 4
            self::floorCheck1Arg();
47
        }
48
49
        try {
50 42
            $number = Helpers::validateNumericNullBool($number);
51 40
            $significance = Helpers::validateNumericNullSubstitution($significance, ($number < 0) ? -1 : 1);
52 2
        } catch (Exception $e) {
53 2
            return $e->getMessage();
54
        }
55
56 40
        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 30
    public static function math(mixed $number, mixed $significance = null, mixed $mode = 0, bool $checkSigns = false)
79
    {
80 30
        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 30
            $number = Helpers::validateNumericNullBool($number);
86 28
            $significance = Helpers::validateNumericNullSubstitution($significance, ($number < 0) ? -1 : 1);
87 28
            $mode = Helpers::validateNumericNullSubstitution($mode, null);
88 2
        } catch (Exception $e) {
89 2
            return $e->getMessage();
90
        }
91
92 28
        if (empty($significance * $number)) {
93 5
            return 0.0;
94
        }
95 23
        if ($checkSigns) {
96 2
            if (($number > 0 && $significance < 0) || ($number < 0 && $significance > 0)) {
97 2
                return ExcelError::NAN();
98
            }
99
        }
100
101 23
        return self::argsOk((float) $number, (float) $significance, (int) $mode);
102
    }
103
104
    /**
105
     * FLOOR.ODS, pseudo-function - FLOOR as implemented in ODS.
106
     *
107
     * Round a number down to the nearest integer or to the nearest multiple of significance.
108
     *
109
     * ODS Function (theoretical):
110
     *        FLOOR.ODS(number[,significance[,mode]])
111
     *
112
     * @param mixed $number Number to round
113
     * @param mixed $significance Significance
114
     * @param array<mixed>|int $mode direction to round negative numbers
115
     *
116
     * @return array<mixed>|float|string Rounded Number, or a string containing an error
117
     */
118 2
    public static function mathOds(mixed $number, mixed $significance = null, mixed $mode = 0)
119
    {
120 2
        return self::math($number, $significance, $mode, true);
121
    }
122
123
    /**
124
     * FLOOR.PRECISE.
125
     *
126
     * Rounds number down, toward zero, to the nearest multiple of significance.
127
     *
128
     * Excel Function:
129
     *        FLOOR.PRECISE(number[,significance])
130
     *
131
     * @param array<mixed>|float $number Number to round
132
     *                      Or can be an array of values
133
     * @param array<mixed>|float $significance Significance
134
     *                      Or can be an array of values
135
     *
136
     * @return array<mixed>|float|string Rounded Number, or a string containing an error
137
     *         If an array of numbers is passed as an argument, then the returned result will also be an array
138
     *            with the same dimensions
139
     */
140 24
    public static function precise($number, $significance = 1)
141
    {
142 24
        if (is_array($number) || is_array($significance)) {
143 5
            return self::evaluateArrayArguments([self::class, __FUNCTION__], $number, $significance);
144
        }
145
146
        try {
147 24
            $number = Helpers::validateNumericNullBool($number);
148 22
            $significance = Helpers::validateNumericNullSubstitution($significance, null);
149 3
        } catch (Exception $e) {
150 3
            return $e->getMessage();
151
        }
152 21
        if (!$significance) {
153 1
            return 0.0;
154
        }
155
156 20
        return self::argumentsOkPrecise((float) $number, (float) $significance);
157
    }
158
159
    /**
160
     * Avoid Scrutinizer problems concerning complexity.
161
     */
162 20
    private static function argumentsOkPrecise(float $number, float $significance): string|float
163
    {
164 20
        if ($significance == 0.0) {
165
            return ExcelError::DIV0();
166
        }
167 20
        if ($number == 0.0) {
168 3
            return 0.0;
169
        }
170
171 17
        return floor($number / abs($significance)) * abs($significance);
172
    }
173
174
    /**
175
     * Avoid Scrutinizer complexity problems.
176
     *
177
     * @return float|string Rounded Number, or a string containing an error
178
     */
179 23
    private static function argsOk(float $number, float $significance, int $mode): string|float
180
    {
181 23
        if (!$significance) {
182
            return ExcelError::DIV0();
183
        }
184 23
        if (!$number) {
185
            return 0.0;
186
        }
187 23
        if (self::floorMathTest($number, $significance, $mode)) {
188 7
            return ceil($number / $significance) * $significance;
189
        }
190
191 17
        return floor($number / $significance) * $significance;
192
    }
193
194
    /**
195
     * Let FLOORMATH complexity pass Scrutinizer.
196
     */
197 23
    private static function floorMathTest(float $number, float $significance, int $mode): bool
198
    {
199 23
        return Helpers::returnSign($significance) == -1 || (Helpers::returnSign($number) == -1 && !empty($mode));
200
    }
201
202
    /**
203
     * Avoid Scrutinizer problems concerning complexity.
204
     */
205 40
    private static function argumentsOk(float $number, float $significance): string|float
206
    {
207 40
        if ($significance == 0.0) {
208 1
            return ExcelError::DIV0();
209
        }
210 39
        if ($number == 0.0) {
211 3
            return 0.0;
212
        }
213 36
        $signSig = Helpers::returnSign($significance);
214 36
        $signNum = Helpers::returnSign($number);
215
        if (
216 36
            ($signSig === 1 && ($signNum === 1 || Functions::getCompatibilityMode() !== Functions::COMPATIBILITY_GNUMERIC))
217 36
            || ($signNum === -1 && $signSig === -1)
218
        ) {
219 35
            return floor($number / $significance) * $significance;
220
        }
221
222 2
        return ExcelError::NAN();
223
    }
224
}
225