RandomStrings::generate()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 1
c 0
b 0
f 0
nc 1
nop 1
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
1
<?php
2
3
namespace kalanis\UploadPerPartes\Uploader;
4
5
6
/**
7
 * Class RandomStrings
8
 * @package kalanis\UploadPerPartes\Uploader
9
 * Generating random strings
10
 */
11
class RandomStrings
12
{
13
    /** @var string[] */
14
    public static array $possibilities = ['a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z'];
15
16 8
    public static function generate(int $length = 64): string
17
    {
18 8
        return static::generateRandomText($length, static::$possibilities);
19
    }
20
21
    /**
22
     * @param int $length
23
     * @param string[] $possibilities
24
     * @return string
25
     */
26 9
    public static function generateRandomText(int $length, array $possibilities): string
27
    {
28 9
        $string = '';
29 9
        for ($i = 0; $i < $length; $i++) {
30 9
            $rand = mt_rand(0, count($possibilities) - 1);
31 9
            $string .= $possibilities[$rand];
32
        }
33 9
        return $string;
34
    }
35
36 16
    public static function randomLength(): string
37
    {
38 16
        $which = md5(strval(rand()));
39 16
        return strval(substr($which, 0, intval(hexdec(bin2hex(substr($which, -1))))));
40
    }
41
}
42