Passed
Push — sequential-generator ( f17b30 )
by Marcel
02:49
created

SequentialGenerator   A

Complexity

Total Complexity 2

Size/Duplication

Total Lines 33
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 2
lcom 1
cbo 1
dl 0
loc 33
ccs 6
cts 6
cp 1
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A generate() 0 4 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace UMA\Uuid;
6
7
/**
8
 * An UuidGenerator that returns sequential UUIDs, starting
9
 * from the NIL Uuid and going up. This can be useful in a unit testing
10
 * context, where deterministic and easily readable UUIDs might be a boon.
11
 *
12
 * Note that only the lowest 64 bits of the UUID are incremented. That should
13
 * be enough, though.
14
 *
15
 * Don't use this one in production.
16
 */
17
class SequentialGenerator implements UuidGenerator
18
{
19
    /**
20
     * @var int
21
     */
22
    private $counter;
23
24
    /**
25
     * @var int
26
     */
27
    private $head;
28
29
    /**
30
     * @example $mark = 15 and $start = 10 will generate:
31
     *  '00000000-0000-000f-0000-00000000000a'
32
     *  '00000000-0000-000f-0000-00000000000b'
33
     *  '00000000-0000-000f-0000-00000000000c'
34
     *  ...
35
     */
36 3
    public function __construct(int $mark = 0, int $start = 0)
37
    {
38 3
        $this->head = \pack('J', $mark);
0 ignored issues
show
Documentation Bug introduced by
The property $head was declared of type integer, but \pack('J', $mark) is of type string. Maybe add a type cast?

This check looks for assignments to scalar types that may be of the wrong type.

To ensure the code behaves as expected, it may be a good idea to add an explicit type cast.

$answer = 42;

$correct = false;

$correct = (bool) $answer;
Loading history...
39 3
        $this->counter = $start;
40 3
    }
41
42
    /**
43
     * {@inheritdoc}
44
     */
45 3
    public function generate(string $name = null): Uuid
46
    {
47 3
        return Uuid::fromBytes($this->head . \pack('J', $this->counter++));
48
    }
49
}
50