Helper::random()   A
last analyzed

Complexity

Conditions 3
Paths 4

Size

Total Lines 11
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 3

Importance

Changes 2
Bugs 0 Features 1
Metric Value
c 2
b 0
f 1
dl 0
loc 11
ccs 7
cts 7
cp 1
rs 9.4286
cc 3
eloc 6
nc 4
nop 2
crap 3
1
<?php
2
/*
3
 * This file is part of badcow-common.
4
 *
5
 * (c) Samuel Williams <[email protected]>
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
11
namespace Badcow\Common;
12
13
class Helper
14
{
15
    /**
16
     * Generates the default characters
17
     *
18
     * @return array
19
     */
20 18
    private static function defaultCharacters()
21
    {
22 18
        return array_merge(range(0, 9), range('a', 'z'), range('A', 'Z'));
23
    }
24
25
    /**
26
     * @param int $len
27
     * @param array $chars Use custom characters. Defaults to upper and lower case alphanumeric characters.
28
     * @return string
29
     */
30 18
    public static function random($len = 32, array $chars = null)
31
    {
32 18
        $chars = $chars ?: self::defaultCharacters();
33 18
        $output = '';
34
35 18
        for ($i = 0; $i < $len; $i++) {
36 18
            $output .= $chars[array_rand($chars)];
37 12
        }
38
39 18
        return $output;
40
    }
41
}