Completed
Push — master ( 4e0b70...c818a0 )
by Peter
01:42 queued 12s
created

FloatingTimeGenerator   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 97
Duplicated Lines 6.19 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 10
lcom 1
cbo 4
dl 6
loc 97
ccs 31
cts 31
cp 1
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A generate() 0 17 1
B __construct() 6 44 9

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\ProcessorArchitectureException;
16
use GpsLab\Component\Base64UID\Exception\ZeroArgumentException;
17
18
class FloatingTimeGenerator implements BinaryGenerator
19
{
20
    /**
21
     * @var int
22
     */
23
    private $time_length;
24
25
    /**
26
     * @var int
27
     */
28
    private $time_offset;
29
30
    /**
31
     * Bitmap with floating time.
32
     *
33
     * The time length defines the limit of the stored date:
34
     *  40-bits = 1111111111111111111111111111111111111111      = 1099511627775  = 2004-11-03 19:53:48 (UTC)
35
     *  41-bits = 11111111111111111111111111111111111111111     = 2199023255551  = 2039-09-07 15:47:36 (UTC)
36
     *  42-bits = 111111111111111111111111111111111111111111    = 4398046511103  = 2109-05-15 07:35:11 (UTC)
37
     *  43-bits = 1111111111111111111111111111111111111111111   = 8796093022207  = 2248-09-26 15:10:22 (UTC)
38
     *  44-bits = 11111111111111111111111111111111111111111111  = 17592186044415 = 2527-06-23 06:20:44 (UTC)
39
     *  45-bits = 111111111111111111111111111111111111111111111 = 35184372088831 = 3084-12-12 12:41:29 (UTC)
40
     *
41
     * The time offset allows to move the starting point of time in microseconds,
42
     * which reduces the size of the stored time:
43
     *  0             = 1970-01-01 00:00:00 (UTC)
44
     *  1577836800000 = 2020-01-01 00:00:00 (UTC)
45
     *
46
     * @param int $time_length
47
     * @param int $time_offset
48
     */
49 15
    public function __construct($time_length = 45, $time_offset = 0)
50
    {
51
        // @codeCoverageIgnoreStart
52
        // can't reproduce this condition in tests
53 View Code Duplication
        if (PHP_INT_SIZE * 8 < 64) {
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...
54
            throw new ProcessorArchitectureException(sprintf('This generator require 64-bit mode of processor architecture. Your processor architecture support %d-bit mode.', PHP_INT_SIZE * 8));
55
        }
56
        // @codeCoverageIgnoreEnd
57
58 15
        if (!is_int($time_length)) {
59 1
            throw new ArgumentTypeException(sprintf('Length of time for UID should be integer, got "%s" instead.', gettype($time_length)));
60
        }
61
62 14
        if (!is_int($time_offset)) {
63 1
            throw new ArgumentTypeException(sprintf('Time offset should be integer, got "%s" instead.', gettype($time_offset)));
64
        }
65
66 13
        if ($time_length < 0) {
67 1
            throw new ZeroArgumentException(sprintf('Length of time for UID should be grate then or equal to "0", got "%d" instead.', $time_length));
68
        }
69
70 12
        if ($time_offset < 0) {
71 1
            throw new ZeroArgumentException(sprintf('Time offset should be grate then or equal to "0", got "%d" instead.', $time_offset));
72
        }
73
74 11 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...
75 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));
76
        }
77
78 9
        $now = (int) floor(microtime(true) * 1000);
79
80 9
        if ($time_offset > $now) {
81 1
            throw new ArgumentRangeException(sprintf('Time offset should be grate then or equal to current time "%d", got "%d" instead.', $now, $time_offset));
82
        }
83
84 8
        $min_time_length = strlen(decbin($now - $time_offset));
85
86 8
        if ($time_length < $min_time_length) {
87 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_length));
88
        }
89
90 6
        $this->time_length = $time_length;
91 6
        $this->time_offset = $time_offset;
92 6
    }
93
94
    /**
95
     * @return int
96
     */
97 6
    public function generate()
98
    {
99 6
        $time = ((int) floor(microtime(true) * 1000) - $this->time_offset);
100
101 6
        $prefix_length = random_int(0, 64 - 1 - $this->time_length);
102 6
        $prefix = random_int(0, (int) bindec(str_repeat('1', $prefix_length)));
103
104 6
        $suffix_length = 64 - 1 - $prefix_length - $this->time_length;
105 6
        $suffix = random_int(0, (int) bindec(str_repeat('1', $suffix_length)));
106
107 6
        $uid = 1 << 64 - 1; // first bit is a bitmap limiter
108 6
        $uid |= $prefix << $this->time_length + $suffix_length;
109 6
        $uid |= $time << $suffix_length;
110 6
        $uid |= $suffix;
111
112 6
        return $uid;
113
    }
114
}
115