Completed
Push — master ( 67e465...aa77c9 )
by Alejandro
13s queued 10s
created

StringUtilsTrait   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 34
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
eloc 16
dl 0
loc 34
ccs 18
cts 18
cp 1
rs 10
c 0
b 0
f 0
wmc 3

2 Methods

Rating   Name   Duplication   Size   Complexity  
A generateV4Uuid() 0 20 1
A generateRandomString() 0 10 2
1
<?php
2
declare(strict_types=1);
3
4
namespace Shlinkio\Shlink\Common\Util;
5
6
use function random_int;
7
use function sprintf;
8
use function strlen;
9
10
trait StringUtilsTrait
11
{
12 3
    private function generateRandomString(int $length = 10): string
13
    {
14 3
        $characters = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
15 3
        $charactersLength = strlen($characters);
16 3
        $randomString = '';
17 3
        for ($i = 0; $i < $length; $i++) {
18 3
            $randomString .= $characters[random_int(0, $charactersLength - 1)];
19
        }
20
21 3
        return $randomString;
22
    }
23
24 14
    private function generateV4Uuid(): string
25
    {
26 14
        return sprintf(
27 14
            '%04x%04x-%04x-%04x-%04x-%04x%04x%04x',
28
            // 32 bits for "time_low"
29 14
            random_int(0, 0xffff),
30 14
            random_int(0, 0xffff),
31
            // 16 bits for "time_mid"
32 14
            random_int(0, 0xffff),
33
            // 16 bits for "time_hi_and_version",
34
            // four most significant bits holds version number 4
35 14
            random_int(0, 0x0fff) | 0x4000,
36
            // 16 bits, 8 bits for "clk_seq_hi_res",
37
            // 8 bits for "clk_seq_low",
38
            // two most significant bits holds zero and one for variant DCE1.1
39 14
            random_int(0, 0x3fff) | 0x8000,
40
            // 48 bits for "node"
41 14
            random_int(0, 0xffff),
42 14
            random_int(0, 0xffff),
43 14
            random_int(0, 0xffff)
44
        );
45
    }
46
}
47