Test Failed
Pull Request — master (#11)
by
unknown
01:53
created

BC   B

Complexity

Total Complexity 54

Size/Duplication

Total Lines 351
Duplicated Lines 22.79 %

Coupling/Cohesion

Components 1
Dependencies 0

Test Coverage

Coverage 97.48%

Importance

Changes 0
Metric Value
wmc 54
lcom 1
cbo 0
dl 80
loc 351
ccs 116
cts 119
cp 0.9748
rs 7.0642
c 0
b 0
f 0

24 Methods

Rating   Name   Duplication   Size   Complexity  
A setScale() 0 4 1
A ceil() 14 14 4
A checkIsFloat() 0 4 1
A checkIsFloatCleanZeros() 0 4 1
A isNegative() 0 4 1
A checkNumber() 0 8 3
A round() 0 14 3
A floor() 14 14 4
A abs() 0 10 2
A rand() 0 10 1
B max() 19 19 5
B min() 19 19 5
A roundDown() 7 7 2
A roundUp() 7 7 2
A getScale() 0 5 1
A add() 0 7 2
A comp() 0 7 2
A div() 0 7 2
A mod() 0 7 2
A mul() 0 7 2
A pow() 0 7 2
A powMod() 0 7 2
A sqrt() 0 7 2
A sub() 0 7 2

How to fix   Duplicated Code    Complexity   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

Complex Class

 Tip:   Before tackling complexity, make sure that you eliminate any duplication first. This often can reduce the size of classes significantly.

Complex classes like BC 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. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.

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 BC, and based on these observations, apply Extract Interface, too.

1
<?php
2
3
namespace BCMathExtended;
4
5
/**
6
 * Class BC
7
 * @package BCMathExtended
8
 */
9
class BC
10
{
11
    const COMPARE_EQUAL = 0;
12
    const COMPARE_LEFT_GRATER = 1;
13
    const COMPARE_RIGHT_GRATER = -1;
14
15
    /**
16
     * @param int $scale
17
     */
18 2
    public static function setScale($scale)
19
    {
20 2
        bcscale($scale);
21 2
    }
22
23
    /**
24
     * @param int|string $number
25
     * @return string
26
     */
27 2 View Code Duplication
    public static function ceil($number)
0 ignored issues
show
Duplication introduced by
This method 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...
28
    {
29 2
        $number = (string)$number;
30
31 2
        if (self::checkIsFloat($number) && self::checkIsFloatCleanZeros($number)) {
32 2
            $result = 1;
33 2
            if (self::isNegative($number)) {
34 2
                --$result;
35
            }
36 2
            $number = self::add($number, $result, 0);
37
        }
38
39 2
        return self::checkNumber($number);
40
    }
41
42
    /**
43
     * @param int|string $number
44
     * @return bool
45
     */
46 5
    private static function checkIsFloat($number)
47
    {
48 5
        return false !== strpos($number, '.');
49
    }
50
51
    /**
52
     * @param int|string $number
53
     * @return bool
54
     */
55 4
    private static function checkIsFloatCleanZeros(&$number)
56
    {
57 4
        return false !== strpos($number = rtrim(rtrim($number, '0'), '.'), '.');
58
    }
59
60
    /**
61
     * @param $number
62
     * @return bool
63
     */
64 6
    private static function isNegative($number)
65
    {
66 6
        return 0 === strncmp('-', $number, 1);
67
    }
68
69
    /**
70
     * @param int|string $number
71
     * @return int|string
72
     */
73 6
    private static function checkNumber($number)
74
    {
75 6
        $number = str_replace('+', '', filter_var($number, FILTER_SANITIZE_NUMBER_FLOAT, FILTER_FLAG_ALLOW_FRACTION));
76 6
        if ('-0' === $number || !is_numeric($number)) {
77 4
            return '0';
78
        }
79 6
        return $number;
80
    }
81
82
    /**
83
     * @param int|string $number
84
     * @param int $precision
85
     * @return string
86
     */
87 1
    public static function round($number, $precision = 0)
88
    {
89 1
        $number = (string)$number;
90
91 1
        if (self::checkIsFloat($number)) {
92 1
            if (self::isNegative($number)) {
93 1
                return self::sub($number, '0.' . str_repeat('0', $precision) . '5', $precision);
94
            }
95
96 1
            return self::add($number, '0.' . str_repeat('0', $precision) . '5', $precision);
97
        }
98
99 1
        return self::checkNumber($number);
100
    }
101
102
    /**
103
     * @param int|string $number
104
     * @return string
105
     */
106 2 View Code Duplication
    public static function floor($number)
0 ignored issues
show
Duplication introduced by
This method 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...
107
    {
108 2
        $number = (string)$number;
109
110 2
        if (self::checkIsFloat($number) && self::checkIsFloatCleanZeros($number)) {
111 2
            $result = 0;
112 2
            if (self::isNegative($number)) {
113 2
                --$result;
114
            }
115 2
            $number = self::add($number, $result, 0);
116
        }
117
118 2
        return self::checkNumber($number);
119
    }
120
121
    /**
122
     * @param int|string $number
123
     * @return string
124
     */
125 1
    public static function abs($number)
126
    {
127 1
        $number = (string)$number;
128
129 1
        if (self::isNegative($number)) {
130 1
            $number = (string)substr($number, 1);
131
        }
132
133 1
        return self::checkNumber($number);
134
    }
135
136
    /**
137
     * @param int|string $min
138
     * @param int|string $max
139
     * @return string
140
     */
141 1
    public static function rand($min, $max)
142
    {
143 1
        $max = (string)$max;
144 1
        $min = (string)$min;
145
146 1
        $difference = self::add(self::sub($max, $min), 1);
147 1
        $randPercent = self::div(mt_rand(), mt_getrandmax(), 8);
148
149 1
        return self::add($min, self::mul($difference, $randPercent, 8), 0);
150
    }
151
152
    /**
153
     * @param array|int|string,...
154
     * @return null|int|string
155
     */
156 1 View Code Duplication
    public static function max()
0 ignored issues
show
Duplication introduced by
This method 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...
157
    {
158 1
        $max = null;
159 1
        $args = func_get_args();
160 1
        if (is_array($args[0])) {
161 1
            $args = $args[0];
162
        }
163 1
        foreach ($args as $value) {
164 1
            if (null === $max) {
165 1
                $max = $value;
166
            } else {
167 1
                if (self::comp($max, $value) < 0) {
168 1
                    $max = $value;
169
                }
170
            }
171
        }
172
173 1
        return $max;
174
    }
175
176
    /**
177
     * @param array|int|string,...
178
     * @return null|int|string
179
     */
180 1 View Code Duplication
    public static function min()
0 ignored issues
show
Duplication introduced by
This method 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...
181
    {
182 1
        $min = null;
183 1
        $args = func_get_args();
184 1
        if (is_array($args[0])) {
185 1
            $args = $args[0];
186
        }
187 1
        foreach ($args as $value) {
188 1
            if (null === $min) {
189 1
                $min = $value;
190
            } else {
191 1
                if (self::comp($min, $value) > 0) {
192 1
                    $min = $value;
193
                }
194
            }
195
        }
196
197 1
        return $min;
198
    }
199
200
    /**
201
     * @param int|string $number
202
     * @param int $precision
203
     * @return string
204
     */
205 1 View Code Duplication
    public static function roundDown($number, $precision = 0)
0 ignored issues
show
Duplication introduced by
This method 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...
206
    {
207 1
        $multiply = self::pow(10, (string)abs($precision));
208 1
        return $precision < 0 ?
209 1
            self::mul(self::floor(self::div($number, $multiply)), $multiply, $precision) :
210 1
            self::div(self::floor(self::mul($number, $multiply)), $multiply, $precision);
211
    }
212
213
    /**
214
     * @param int|string $number
215
     * @param int $precision
216
     * @return string
217
     */
218 1 View Code Duplication
    public static function roundUp($number, $precision = 0)
0 ignored issues
show
Duplication introduced by
This method 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...
219
    {
220 1
        $multiply = self::pow(10, (string)abs($precision));
221 1
        return $precision < 0 ?
222 1
            self::mul(self::ceil(self::div($number, $multiply)), $multiply, $precision) :
223 1
            self::div(self::ceil(self::mul($number, $multiply)), $multiply, $precision);
224
    }
225
226
    /**
227
     * @return int
228
     */
229 1
    public static function getScale()
230
    {
231 1
        $sqrt = bcsqrt('2');
232 1
        return strlen(substr($sqrt, strpos($sqrt, '.') + 1));
233
    }
234
235
    /**
236
     * @param string $leftOperand
237
     * @param string $rightOperand
238
     * @param int $scale
239
     * @return string
240
     */
241 7
    public static function add($leftOperand, $rightOperand, $scale = null)
242
    {
243 7
        if (null === $scale) {
244 2
            return bcadd($leftOperand, $rightOperand);
245
        }
246 6
        return bcadd($leftOperand, $rightOperand, $scale);
247
    }
248
249
    /**
250
     * @param string $leftOperand
251
     * @param string $rightOperand
252
     * @param int $scale
253
     * @return int
254
     */
255 3
    public static function comp($leftOperand, $rightOperand, $scale = null)
256
    {
257 3
        if (null === $scale) {
258 3
            return bccomp($leftOperand, $rightOperand);
259
        }
260 1
        return bccomp($leftOperand, $rightOperand, $scale);
261
    }
262
263
    /**
264
     * @param string $leftOperand
265
     * @param string $rightOperand
266
     * @param int $scale
267
     * @return string
268
     */
269 4
    public static function div($leftOperand, $rightOperand, $scale = null)
270
    {
271 4
        if (null === $scale) {
272 3
            return bcdiv($leftOperand, $rightOperand);
273
        }
274 4
        return bcdiv($leftOperand, $rightOperand, $scale);
275
    }
276
277
    /**
278
     * @param string $leftOperand
279
     * @param string $modulus
280
     * @return string
281
     */
282 1
    public static function mod($leftOperand, $modulus)
283
    {
284 1
        if (null === $scale) {
0 ignored issues
show
Bug introduced by
The variable $scale does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
285
            return bcmod($leftOperand, $rightOperand);
0 ignored issues
show
Bug introduced by
The variable $rightOperand does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
286
        }
287
        return bcmod($leftOperand, $modulus);
288
    }
289
290
    /**
291
     * @param string $leftOperand
292
     * @param string $rightOperand
293
     * @param int $scale
294
     * @return string
295
     */
296 4
    public static function mul($leftOperand, $rightOperand, $scale = null)
297
    {
298 4
        if (null === $scale) {
299 3
            return bcmul($leftOperand, $rightOperand);
300
        }
301 4
        return bcmul($leftOperand, $rightOperand, $scale);
302
    }
303
304
    /**
305
     * @param string $leftOperand
306
     * @param string $rightOperand
307
     * @param int $scale
308
     * @return string
309
     */
310 3
    public static function pow($leftOperand, $rightOperand, $scale = null)
311
    {
312 3
        if (null === $scale) {
313 3
            return bcpow($leftOperand, $rightOperand);
314
        }
315 1
        return bcpow($leftOperand, $rightOperand, $scale);
316
    }
317
318
    /**
319
     * @param string $leftOperand
320
     * @param string $rightOperand
321
     * @param string $modulus
322
     * @param int $scale
323
     * @return string
324
     */
325 1
    public static function powMod($leftOperand, $rightOperand, $modulus, $scale = null)
326
    {
327 1
        if (null === $scale) {
328 1
            return bcpowmod($leftOperand, $rightOperand, $modulus);
329
        }
330
        return bcpowmod($leftOperand, $rightOperand, $modulus, $scale);
331
    }
332
333
    /**
334
     * @param string $operand
335
     * @param int $scale
336
     * @return string
337
     */
338 1
    public static function sqrt($operand, $scale = null)
339
    {
340 1
        if (null === $scale) {
341 1
            return bcsqrt($operand);
342
        }
343 1
        return bcsqrt($operand, $scale);
344
    }
345
346
    /**
347
     * @param string $leftOperand
348
     * @param string $rightOperand
349
     * @param int $scale
350
     * @return string
351
     */
352 3
    public static function sub($leftOperand, $rightOperand, $scale = null)
353
    {
354 3
        if (null === $scale) {
355 2
            return bcsub($leftOperand, $rightOperand);
356
        }
357 1
        return bcsub($leftOperand, $rightOperand, $scale);
358
    }
359
}
360