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

TimeBinaryGenerator   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 126
Duplicated Lines 4.76 %

Coupling/Cohesion

Components 1
Dependencies 5

Test Coverage

Coverage 97.37%

Importance

Changes 0
Metric Value
wmc 13
lcom 1
cbo 5
dl 6
loc 126
ccs 37
cts 38
cp 0.9737
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
C __construct() 6 52 11
A generate() 0 22 2

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

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
116 6
        $this->suffix_max_value = bindec(str_repeat('1', $this->suffix_length));
0 ignored issues
show
Documentation Bug introduced by
It seems like bindec(str_repeat('1', $this->suffix_length)) can also be of type double. However, the property $suffix_max_value is declared as type integer. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
117 6
    }
118
119
    /**
120
     * @return int
121
     */
122 6
    public function generate()
123
    {
124 6
        $time = ((int) floor(microtime(true) * 1000) - $this->time_offset);
125
126
        // @codeCoverageIgnoreStart
127
        // overflow validation is in the constructor,
128
        // but there is a chance that overflow will occur in the process of using this service
129
        if ($time >= 1 << $this->time_length) {
130
            throw new BitmapOverflowException(sprintf('Bitmap for time is overflow of %d bits.', $this->time_length));
131
        }
132
        // @codeCoverageIgnoreEnd
133
134 6
        $prefix = random_int(0, $this->prefix_max_value);
135 6
        $suffix = random_int(0, $this->suffix_max_value);
136
137 6
        $uid = 1 << 64 - 1; // first bit is a bitmap limiter
138 6
        $uid |= $prefix << $this->time_length + $this->suffix_length;
139 6
        $uid |= $time << $this->suffix_length;
140 6
        $uid |= $suffix;
141
142 6
        return $uid;
143
    }
144
}
145