UUID::v4()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 1

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 6
c 2
b 0
f 0
dl 0
loc 9
ccs 7
cts 7
cp 1
rs 10
cc 1
nc 1
nop 0
crap 1
1
<?php
2
3
namespace Vectorface\UUID;
4
5
class UUID
6
{
7
    /**
8
     * Generate a random universally unique identifier; a UUIDv4
9
     *
10
     * @return string A UUID, This should look something like c2b134ae-04a4-4e82-89e0-51cbe9639dfb
11
     */
12 1
    public static function v4()
13
    {
14 1
        $uuidStr = random_bytes(16); // 128 bits
15 1
        $uuidStr[6] = chr(0x40 | (ord($uuidStr[6]) & 0x0f)); // Set the high nibble of the 7th byte to 4
16 1
        $uuidStr[8] = chr(0x80 | (ord($uuidStr[8]) & 0x3f)); // Set highest two bits of the 9th byte to 0b10
17
18 1
        return sprintf(
19 1
            "%s%s-%s-%s-%s-%s%s%s",
20 1
            ...str_split(bin2hex($uuidStr), 4)
21
        );
22
    }
23
}
24
25