Completed
Pull Request — master (#4)
by
unknown
03:08
created

BC::roundDown()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 5

Duplication

Lines 7
Ratio 100 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
dl 7
loc 7
ccs 0
cts 5
cp 0
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 5
nc 2
nop 2
crap 6
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 1 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 1
        $number = (string)$number;
26
27 1
        if (self::checkIsFloat($number) && self::checkIsFloatCleanZeros($number)) {
28 1
            $result = 1;
29 1
            if (self::isNegative($number)) {
30 1
                --$result;
31
            }
32 1
            $number = bcadd($number, $result, 0);
33
        }
34
35 1
        return self::checkNumber($number);
36
    }
37
38
    /**
39
     * @param int|string $number
40
     * @return bool
41
     */
42 3
    private static function checkIsFloat($number)
43
    {
44 3
        return false !== strpos($number, '.');
45
    }
46
47
    /**
48
     * @param int|string $number
49
     * @return bool
50
     */
51 2
    private static function checkIsFloatCleanZeros(&$number)
52
    {
53 2
        return false !== strpos($number = rtrim(rtrim($number, '0'), '.'), '.');
54
    }
55
56
    /**
57
     * @param $number
58
     * @return bool
59
     */
60 4
    private static function isNegative($number)
61
    {
62 4
        return 0 === strncmp('-', $number, 1);
63
    }
64
65
    /**
66
     * @param int|string $number
67
     * @return int|string
68
     */
69 4
    private static function checkNumber($number)
70
    {
71 4
        $number = str_replace('+', '', filter_var($number, FILTER_SANITIZE_NUMBER_FLOAT, FILTER_FLAG_ALLOW_FRACTION));
72 4
        if ('-0' === $number || !is_numeric($number)) {
73 4
            return '0';
74
        }
75 4
        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 1 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 1
        $number = (string)$number;
105
106 1
        if (self::checkIsFloat($number) && self::checkIsFloatCleanZeros($number)) {
107 1
            $result = 0;
108 1
            if (self::isNegative($number)) {
109 1
                --$result;
110
            }
111 1
            $number = bcadd($number, $result, 0);
112
        }
113
114 1
        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])) $args = $args[0];
157 1
        foreach ($args as $value) {
158 1
            if (null === $max) {
159 1
                $max = $value;
160
            } else {
161 1
                if (bccomp($max, $value) < 0) {
162 1
                    $max = $value;
163
                }
164
            }
165
        }
166
167 1
        return $max;
168
    }
169
170
    /**
171
     * @param array|int|string,...
172
     * @return null|int|string
173
     */
174 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...
175
    {
176 1
        $min = null;
177 1
        $args = func_get_args();
178 1
        if(is_array($args[0])) $args = $args[0];
179 1
        foreach ($args as $value) {
180 1
            if (null === $min) {
181 1
                $min = $value;
182
            } else {
183 1
                if (bccomp($min, $value) > 0) {
184 1
                    $min = $value;
185
                }
186
            }
187
        }
188
189 1
        return $min;
190
    }
191
    
192
    /**
193
     * @param int|string $value
194
     * @param int $places
195
     * @return string
196
     */
197 View Code Duplication
    function roundDown($value, int $places) : string
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
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...
198
    {
199
        $mult = bcpow(10, abs($places));
200
        return $places < 0 ?
201
            bcmul(BC::floor(bcdiv($value, $mult)), $mult) :
202
            bcdiv(BC::floor(bcmul($value, $mult)), $mult);
203
    }
204
    
205
    /**
206
     * @param int|string $value
207
     * @param int $places
208
     * @return string
209
     */
210 View Code Duplication
    function roundUp($value, $places) : string
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
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...
211
    {
212
        $mult = bcpow(10, abs($places));
213
        return $places < 0 ?
214
            bcmul(BC::ceil(bcdiv($value, $mult)), $mult) :
215
            bcdiv(BC::ceil(bcmul($value, $mult)), $mult);
216
    }
217
218
}
219