UUID::generate()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 13
rs 9.8333
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
3
namespace BWC\Share\Object;
4
5
6
final class UUID
7
{
8
    /**
9
     * Generates random 32 hexadecimal digits long string
10
     * @return string
11
     */
12
    static function generate() {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
13
        $uuid = sprintf('%04x%04x%04x%03x4%04x%04x%04x%04x',
14
            mt_rand(0, 65535), mt_rand(0, 65535), // 32 bits for "time_low"
15
            mt_rand(0, 65535), // 16 bits for "time_mid"
16
            mt_rand(0, 4095),  // 12 bits before the 0100 of (version) 4 for "time_hi_and_version"
17
            bindec(substr_replace(sprintf('%016b', mt_rand(0, 65535)), '01', 6, 2)),
18
            // 8 bits, the last two of which (positions 6 and 7) are 01, for "clk_seq_hi_res"
19
            // (hence, the 2nd hex digit after the 3rd hyphen can only be 1, 5, 9 or d)
20
            // 8 bits for "clk_seq_low"
21
            mt_rand(0, 65535), mt_rand(0, 65535), mt_rand(0, 65535) // 48 bits for "node"
22
        );
23
        return $uuid;
24
    }
25
}