Base64UID::generate()   A
last analyzed

Complexity

Conditions 3
Paths 4

Size

Total Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 3

Importance

Changes 0
Metric Value
cc 3
nc 4
nop 2
dl 0
loc 11
ccs 7
cts 7
cp 1
crap 3
rs 9.9
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * GpsLab component.
5
 *
6
 * @author    Peter Gribanov <[email protected]>
7
 * @copyright Copyright (c) 2011, Peter Gribanov
8
 * @license   http://opensource.org/licenses/MIT
9
 */
10
11
namespace GpsLab\Component\Base64UID;
12
13
class Base64UID
14
{
15
    /**
16
     * TODO use private const after drop PHP < 7.1.
17
     *
18
     * @var string
19
     */
20
    private static $DEFAULT_CHARSET = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ_-';
21
22
    /**
23
     * @param int    $length
24
     * @param string $charset
25
     *
26
     * @return string
27
     */
28 5
    public static function generate($length = 10, $charset = '')
29
    {
30 5
        $charset = $charset ?: self::$DEFAULT_CHARSET;
31 5
        $charset_size = strlen($charset);
32 5
        $uid = '';
33 5
        while ($length-- > 0) {
34 4
            $uid .= $charset[random_int(0, $charset_size - 1)];
35
        }
36
37 5
        return $uid;
38
    }
39
}
40