GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Completed
Pull Request — master (#7)
by t
02:23
created

Numbers::load()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 5
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 7
ccs 0
cts 7
cp 0
crap 2
rs 10
1
<?php
2
/**
3
 * Class Numbers
4
 *
5
 * @link https://www.icy2003.com/
6
 * @author icy2003 <[email protected]>
7
 * @copyright Copyright (c) 2017, icy2003
8
 */
9
namespace icy2003\php\ihelpers;
10
11
use icy2003\php\I;
12
13
/**
14
 * 数字处理
15
 */
16
class Numbers
17
{
18
    /**
19
     * 比较两个任意精度的数字
20
     *
21
     * @param string $number1 数字1
22
     * @param string $number2 数字2
23
     * @param integer $scale 精确度,默认精确到小数点后 2 位
24
     *
25
     * @return boolean
26
     */
27
    public static function isEquals($number1, $number2, $scale = 2)
28
    {
29
        return 0 === bccomp($number1, $number2, $scale);
30
    }
31
32
    /**
33
     * 加载一些奇怪的常量
34
     *
35
     * 我知道这些符号不好打,所以从注释里复制喽
36
     *
37
     * @constant double π 圆周率
38
     * @constant double e 自然常数
39
     * @constant double γ 欧拉常数
40
     * @constant double φ 黄金比
41
     * @constant double c 光速
42
     *
43
     * @return void
44
     */
45
    public static function load()
46
    {
47
        I::def('π', M_PI);
48
        I::def('e', M_E);
49
        I::def('γ', M_EULER);
50
        I::def('φ', (sqrt(5) - 1) / 2);
51
        I::def('c', 2.99792458e8);
52
    }
53
54
    /**
55
     * 获取数字在指定长度的位置
56
     *
57
     * @param integer $number
58
     * @param integer $length
59
     *
60
     * @return integer
61
     */
62
    public static function position($number, $length)
63
    {
64
        while ($number < 0) {
65
            $number += $length;
66
        }
67
        return $number % $length;
68
    }
69
70
    /**
71
     * 转成字节数
72
     *
73
     * @param string $size 例如:10m、10M、10Tb、10kB 等
74
     *
75
     * @return integer
76
     */
77
    public static function toBytes($size)
78
    {
79
        $callback = function ($matches) {
80
            $sizeMap = [
81
                '' => 0,
82
                'b' => 0, // 为了简化正则
83
                'k' => 1,
84
                'm' => 2,
85
                'g' => 3,
86
                't' => 4,
87
                'p' => 5,
88
            ];
89
90
            return $matches[1] * pow(1024, $sizeMap[strtolower($matches[2])]);
91
        };
92
93
        return preg_replace_callback('/(\d*)([a-z]?)b?/i', $callback, $size, 1);
0 ignored issues
show
Bug Best Practice introduced by
The expression return preg_replace_call...', $callback, $size, 1) returns the type string which is incompatible with the documented return type integer.
Loading history...
94
    }
95
96
    /**
97
     * 字节数尽可能转成 k、m、g 等形式
98
     *
99
     * @param integer $bytes
100
     *
101
     * @return string
102
     */
103
    public static function fromBytes($bytes)
104
    {
105
        $unit = array('b', 'kb', 'mb', 'gb', 'tb', 'pb');
106
        return @round($bytes / pow(1024, ($i = floor(log($bytes, 1024)))), 2) . ' ' . $unit[$i];
107
    }
108
109
    /**
110
     * 比较两个值(如:10m、10M、10Tb、10kB 等)的大小
111
     *
112
     * - 0:相等,-1:左小于右,1:右小于左
113
     *
114
     * @param string $size1
115
     * @param string $size2
116
     *
117
     * @return integer
118
     */
119
    public static function compareSize($size1, $size2)
120
    {
121
        $bytes1 = self::toBytes($size1);
122
        $bytes2 = self::toBytes($size2);
123
        return ($v = $bytes1 - $bytes2) == 0 ? 0 : intval(($v) / abs($v));
124
    }
125
}
126