Passed
Push — master ( 418702...215024 )
by kacper
02:16
created

BC::roundDown()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 5

Duplication

Lines 7
Ratio 100 %

Code Coverage

Tests 5
CRAP Score 2

Importance

Changes 0
Metric Value
dl 7
loc 7
ccs 5
cts 5
cp 1
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 5
nc 2
nop 2
crap 2
1
<?php
2
3
namespace BCMathExtended;
4
5
/**
6
 * Class BC
7
 * @package BCMathExtended
8
 */
9
class BC
10
{
11
    /**
12
     * @param int $scale
13
     */
14 1
    public static function setScale($scale)
15
    {
16 1
        bcscale($scale);
17 1
    }
18
19
    /**
20
     * @param int|string $number
21
     * @return string
22
     */
23 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...
24
    {
25 2
        $number = (string)$number;
26
27 2
        if (self::checkIsFloat($number) && self::checkIsFloatCleanZeros($number)) {
28 2
            $result = 1;
29 2
            if (self::isNegative($number)) {
30 2
                --$result;
31
            }
32 2
            $number = bcadd($number, $result, 0);
33
        }
34
35 2
        return self::checkNumber($number);
36
    }
37
38
    /**
39
     * @param int|string $number
40
     * @return bool
41
     */
42 5
    private static function checkIsFloat($number)
43
    {
44 5
        return false !== strpos($number, '.');
45
    }
46
47
    /**
48
     * @param int|string $number
49
     * @return bool
50
     */
51 4
    private static function checkIsFloatCleanZeros(&$number)
52
    {
53 4
        return false !== strpos($number = rtrim(rtrim($number, '0'), '.'), '.');
54
    }
55
56
    /**
57
     * @param $number
58
     * @return bool
59
     */
60 6
    private static function isNegative($number)
61
    {
62 6
        return 0 === strncmp('-', $number, 1);
63
    }
64
65
    /**
66
     * @param int|string $number
67
     * @return int|string
68
     */
69 6
    private static function checkNumber($number)
70
    {
71 6
        $number = str_replace('+', '', filter_var($number, FILTER_SANITIZE_NUMBER_FLOAT, FILTER_FLAG_ALLOW_FRACTION));
72 6
        if ('-0' === $number || !is_numeric($number)) {
73 4
            return '0';
74
        }
75 6
        return $number;
76
    }
77
78
    /**
79
     * @param int|string $number
80
     * @param int $precision
81
     * @return string
82
     */
83 1
    public static function round($number, $precision = 0)
84
    {
85 1
        $number = (string)$number;
86
87 1
        if (self::checkIsFloat($number)) {
88 1
            if (self::isNegative($number)) {
89 1
                return bcsub($number, '0.' . str_repeat('0', $precision) . '5', $precision);
90
            }
91
92 1
            return bcadd($number, '0.' . str_repeat('0', $precision) . '5', $precision);
93
        }
94
95 1
        return self::checkNumber($number);
96
    }
97
98
    /**
99
     * @param int|string $number
100
     * @return string
101
     */
102 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...
103
    {
104 2
        $number = (string)$number;
105
106 2
        if (self::checkIsFloat($number) && self::checkIsFloatCleanZeros($number)) {
107 2
            $result = 0;
108 2
            if (self::isNegative($number)) {
109 2
                --$result;
110
            }
111 2
            $number = bcadd($number, $result, 0);
112
        }
113
114 2
        return self::checkNumber($number);
115
    }
116
117
    /**
118
     * @param int|string $number
119
     * @return string
120
     */
121 1
    public static function abs($number)
122
    {
123 1
        $number = (string)$number;
124
125 1
        if (self::isNegative($number)) {
126 1
            $number = (string)substr($number, 1);
127
        }
128
129 1
        return self::checkNumber($number);
130
    }
131
132
    /**
133
     * @param int|string $min
134
     * @param int|string $max
135
     * @return string
136
     */
137 1
    public static function rand($min, $max)
138
    {
139 1
        $max = (string)$max;
140 1
        $min = (string)$min;
141
142 1
        $difference = bcadd(bcsub($max, $min), 1);
143 1
        $randPercent = bcdiv(mt_rand(), mt_getrandmax(), 8);
144
145 1
        return bcadd($min, bcmul($difference, $randPercent, 8), 0);
146
    }
147
148
    /**
149
     * @param array|int|string,...
150
     * @return null|int|string
151
     */
152 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...
153
    {
154 1
        $max = null;
155 1
        $args = func_get_args();
156 1
        if (is_array($args[0])) {
157 1
            $args = $args[0];
158
        }
159 1
        foreach ($args as $value) {
160 1
            if (null === $max) {
161 1
                $max = $value;
162
            } else {
163 1
                if (bccomp($max, $value) < 0) {
164 1
                    $max = $value;
165
                }
166
            }
167
        }
168
169 1
        return $max;
170
    }
171
172
    /**
173
     * @param array|int|string,...
174
     * @return null|int|string
175
     */
176 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...
177
    {
178 1
        $min = null;
179 1
        $args = func_get_args();
180 1
        if (is_array($args[0])) {
181 1
            $args = $args[0];
182
        }
183 1
        foreach ($args as $value) {
184 1
            if (null === $min) {
185 1
                $min = $value;
186
            } else {
187 1
                if (bccomp($min, $value) > 0) {
188 1
                    $min = $value;
189
                }
190
            }
191
        }
192
193 1
        return $min;
194
    }
195
    
196
    /**
197
     * @param int|string $value
198
     * @param int $precision
199
     * @return string
200
     */
201 1 View Code Duplication
    public static function roundDown($value, $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...
202
    {
203 1
        $multiply = bcpow(10, abs($precision));
204 1
        return $precision < 0 ?
205 1
            bcmul(BC::floor(bcdiv($value, $multiply)), $multiply, $precision) :
206 1
            bcdiv(BC::floor(bcmul($value, $multiply)), $multiply, $precision);
207
    }
208
209
    /**
210
     * @param int|string $value
211
     * @param int $precision
212
     * @return string
213
     */
214 1 View Code Duplication
    public static function roundUp($value, $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...
215
    {
216 1
        $multiply = bcpow(10, abs($precision));
217 1
        return $precision < 0 ?
218 1
            bcmul(BC::ceil(bcdiv($value, $multiply)), $multiply, $precision) :
219 1
            bcdiv(BC::ceil(bcmul($value, $multiply)), $multiply, $precision);
220
    }
221
}