RandomStrings   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 29
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
eloc 10
c 0
b 0
f 0
dl 0
loc 29
ccs 11
cts 11
cp 1
rs 10
wmc 4

3 Methods

Rating   Name   Duplication   Size   Complexity  
A generateRandomText() 0 8 2
A generate() 0 3 1
A randomLength() 0 4 1
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