Passed
Push — master ( 6d6f78...8010b8 )
by mingyoung
03:12
created

str_random()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 11
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 6
nc 2
nop 1
dl 0
loc 11
rs 10
c 0
b 0
f 0
1
<?php
2
3
/*
4
 * This file is part of the mingyoung/dingtalk.
5
 *
6
 * (c) 张铭阳 <[email protected]>
7
 *
8
 * This source file is subject to the MIT license that is bundled
9
 * with this source code in the file LICENSE.
10
 */
11
12
namespace EasyDingTalk;
13
14
/**
15
 * @param mixed    $value
16
 * @param callable $callback
17
 *
18
 * @return mixed
19
 */
20
function tap($value, $callback)
21
{
22
    $callback($value);
23
24
    return $value;
25
}
26
27
/**
28
 * Generate a more truly "random" alpha-numeric string.
29
 *
30
 * @param int $length
31
 *
32
 * @return string
33
 */
34
function str_random($length = 16)
35
{
36
    $string = '';
37
38
    while (($len = strlen($string)) < $length) {
39
        $size = $length - $len;
40
        $bytes = random_bytes($size);
41
        $string .= substr(str_replace(['/', '+', '='], '', base64_encode($bytes)), 0, $size);
42
    }
43
44
    return $string;
45
}
46