Completed
Pull Request — master (#8)
by Peter
01:19
created

FloatingTimeGenerator   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 90
Duplicated Lines 6.67 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 96.77%

Importance

Changes 0
Metric Value
wmc 9
lcom 1
cbo 3
dl 6
loc 90
ccs 30
cts 31
cp 0.9677
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
B __construct() 6 37 8
A generate() 0 17 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
/**
4
 * GpsLab component.
5
 *
6
 * @author    Peter Gribanov <[email protected]>
7
 * @copyright Copyright (c) 2011, Peter Gribanov
8
 * @license   http://opensource.org/licenses/MIT
9
 */
10
11
namespace GpsLab\Component\Base64UID\Generator\Binary;
12
13
use GpsLab\Component\Base64UID\Exception\ArgumentRangeException;
14
use GpsLab\Component\Base64UID\Exception\ArgumentTypeException;
15
use GpsLab\Component\Base64UID\Exception\ZeroArgumentException;
16
17
class FloatingTimeGenerator implements BinaryGenerator
18
{
19
    /**
20
     * @var int
21
     */
22
    private $time_length;
23
24
    /**
25
     * @var int
26
     */
27
    private $time_offset;
28
29
    /**
30
     * Bitmap with floating time.
31
     *
32
     * The time length defines the limit of the stored date:
33
     *  40-bits = 1111111111111111111111111111111111111111      = 1099511627775  = 2004-11-03 19:53:47 (UTC)
34
     *  41-bits = 11111111111111111111111111111111111111111     = 2199023255551  = 2039-09-07 16:47:35 (UTC)
35
     *  42-bits = 111111111111111111111111111111111111111111    = 4398046511103  = 2109-05-15 08:35:11 (UTC)
36
     *  43-bits = 1111111111111111111111111111111111111111111   = 8796093022207  = 2248-09-26 16:10:22 (UTC)
37
     *  44-bits = 11111111111111111111111111111111111111111111  = 17592186044415 = 2527-06-23 07:20:44 (UTC)
38
     *  45-bits = 111111111111111111111111111111111111111111111 = 35184372088831 = 3084-12-12 12:41:28 (UTC)
39
     *
40
     * The time offset allows to move the starting point of time in microseconds,
41
     * which reduces the size of the stored time:
42
     *  0             = 1970-01-01 00:00:00 (UTC)
43
     *  1577833200000 = 2020-01-01 00:00:00 (UTC)
44
     *
45
     * @param int $time_length
46
     * @param int $time_offset
47
     */
48 13
    public function __construct($time_length = 45, $time_offset = 0)
49
    {
50 13
        if (!is_int($time_length)) {
51 1
            throw new ArgumentTypeException(sprintf('Length of time for UID should be integer, got "%s" instead.', gettype($time_length)));
52
        }
53
54 12
        if (!is_int($time_offset)) {
55 1
            throw new ArgumentTypeException(sprintf('Time offset should be integer, got "%s" instead.', gettype($time_offset)));
56
        }
57
58 11
        if ($time_length < 0) {
59 1
            throw new ZeroArgumentException(sprintf('Length of time for UID should be grate then or equal to "0", got "%d" instead.', $time_length));
60
        }
61
62 10
        if ($time_offset < 0) {
63
            throw new ZeroArgumentException(sprintf('Time offset should be grate then or equal to "0", got "%d" instead.', $time_length));
64
        }
65
66 10 View Code Duplication
        if ($time_length > 64 - 1) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
67 2
            throw new ArgumentRangeException(sprintf('Length of time and prefix for UID should be less than or equal to "%d", got "%d" instead.', 64 - 1, $time_length));
68
        }
69
70 8
        $now = (int) floor(microtime(true) * 1000);
71
72 8
        if ($time_offset > $now) {
73 1
            throw new ArgumentRangeException(sprintf('Time offset should be grate then or equal to current time "%d", got "%d" instead.', $now, $time_offset));
74
        }
75
76 7
        $min_time_length = strlen(decbin($now));
77
78 7 View Code Duplication
        if ($time_length < $min_time_length - $time_offset) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
79 2
            throw new ArgumentRangeException(sprintf('Length of time for UID should be grate then or equal to "%d", got "%d" instead.', $min_time_length - $time_offset, $time_length));
80
        }
81
82 5
        $this->time_length = $time_length;
83 5
        $this->time_offset = $time_offset;
84 5
    }
85
86
    /**
87
     * @return int
88
     */
89 5
    public function generate()
90
    {
91 5
        $time = ((int) floor(microtime(true) * 1000) - $this->time_offset);
92
93 5
        $prefix_length = random_int(0, 64 - $this->time_length - 1);
94 5
        $prefix = random_int(0, bindec(str_repeat('1', $prefix_length)));
95
96 5
        $suffix_length = 64 - $this->time_length - 1 - $prefix_length;
97 5
        $suffix = random_int(0, bindec(str_repeat('1', $suffix_length)));
98
99 5
        $uid = 1 << 64 - 1; // first bit is a bitmap limiter
100 5
        $uid |= $prefix << $this->time_length + $suffix_length;
101 5
        $uid |= $time << $suffix_length;
102 5
        $uid |= $suffix;
103
104 5
        return $uid;
105
    }
106
}
107