lib/Twitter/ID.pm

filter: Statementstmt 100% 59 / 59 Branchbran 100% 28 / 28 Conditioncond 90.0% 9 / 10 MC/DCmcdc 66.6% 4 / 6 Subroutinesub 100% 9 / 9 Podpod 100% 5 / 5 totaltotal 97.4% 114 / 117 CC 19 SCAR 15.6
CC
19
Cov
99%
_set
9.0
new
3.0
timestamp
3.0
CRAP 4.8
↑ Summary
1 4 ▶use v5.26;
Subroutine BEGIN: called CC=1 SCAR=0.0
2 4 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
3  
4 package Twitter::ID;
5 # ABSTRACT: Parse the date from a Twitter Snowflake ID
6 4 $Twitter::ID::VERSION = '1.00';
7  
8 4 ▶use Carp qw(croak);
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10  
11 4 my $TW_EPOCH = 1288834974657;
12 4 my $WORKER_BITS = 10;
13 4 my $SEQUENCE_BITS = 12;
14  
15 4 my $TIMESTAMP_SHIFT = $WORKER_BITS + $SEQUENCE_BITS;
16 4 my $WORKER_MASK = (-1 ^ (-1 << $WORKER_BITS)) << $SEQUENCE_BITS;
17 4 my $SEQUENCE_MASK = -1 ^ (-1 << $SEQUENCE_BITS);
18 4 my $MAX_WORKER = 1 << $WORKER_BITS;
19 4 my $MAX_SEQUENCE = 1 << $SEQUENCE_BITS;
20  
21 4 my $LAST_PRE_SNOWFLAKE_ID = 29700859247;
22  
23  
24 18 ▶sub new {
Subroutine new: called CC=3 SCAR=11.0
Pod documented
25 18 my ($class, $id) = @_;
26
27 18 ▶ if (ref $id eq 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $id eq "HASH") 8 10 18
28 8 my $self = bless \(my $o = 0), $class;
29 8 $self->_set( $id->{timestamp}, $id->{worker}, $id->{sequence} );
30 2 return $self;
31 }
32
33 10 ▶ if (! $id) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($id) 2 8 10
34 2 return bless \(my $o = 0), $class;
35 }
36
37 8 bless \$id, $class;
38 }
39  
40  
41 11 ▶sub _set {
Subroutine _set: called CC=9 SCAR=22.0
42 11 my ($self, $timestamp, $worker, $sequence) = @_;
43 11 ▶ $timestamp //= $TW_EPOCH;
Condition66%2/3
$timestamp //= $TW_EPOCH
l!l&&r!l&&!r
1010
MC/DC0%0/2
$timestamp //= $TW_EPOCH
$timestamp $TW_EPOCH
Truth table66%2/3
A //= B
A= $timestampB= $TW_EPOCH
ABresult
1X1
011
000
44 11 ▶ $worker //= 0;
Condition100%2/2
$worker //= 0
l!l
74
MC/DC100%1/1
$worker //= 0
$worker
Truth table100%2/2
A
A= $worker
Aresult
11
00
45 11 ▶ $sequence //= 0;
Condition100%2/2
$sequence //= 0
l!l
56
MC/DC100%1/1
$sequence //= 0
$sequence
Truth table100%2/2
A
A= $sequence
Aresult
11
00
46
47 11 ▶ croak "Twitter timestamps before $TW_EPOCH unsupported" if $timestamp < $TW_EPOCH;
Branch: 1 branch
BranchTrueFalseTotal
if $timestamp < $TW_EPOCH 2 9 11
48 9 ▶ croak "Twitter ID components must be positive" if $worker < 0 || $sequence < 0;
Branch: 1 branch
BranchTrueFalseTotal
if $worker < 0 or $sequence < 0 2 7 9
Condition100%3/3
$worker < 0 or $sequence < 0
l!l&&r!l&&!r
117
MC/DC100%2/2
$worker < 0 or $sequence < 0
$worker < 0 $sequence < 0
Truth table100%3/3
A or B
A= $worker < 0B= $sequence < 0
ABresult
1X1
011
000
49 7 ▶ croak "Worker ID $worker too large (max $MAX_WORKER)" if $worker >= $MAX_WORKER;
Branch: 1 branch
BranchTrueFalseTotal
if $worker >= $MAX_WORKER 1 6 7
50 6 ▶ croak "Sequence number $sequence too large (max $MAX_SEQUENCE)" if $sequence >= $MAX_SEQUENCE;
Branch: 1 branch
BranchTrueFalseTotal
if $sequence >= $MAX_SEQUENCE 1 5 6
51
52 5 $$self = ($timestamp - $TW_EPOCH) << $TIMESTAMP_SHIFT
53 | $worker << $SEQUENCE_BITS
54 | $sequence;
55 }
56  
57  
58 11 ▶sub timestamp {
Subroutine timestamp: called CC=3 SCAR=11.0
Pod documented
59 11 my ($self, $timestamp) = @_;
60
61 11 ▶ if (defined $timestamp) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $timestamp) 1 10 11
62 1 $self->_set( $timestamp, $self->worker, $self->sequence );
63 1 return;
64 }
65
66 10 ▶ return if $$self <= $LAST_PRE_SNOWFLAKE_ID;
Branch: 1 branch
BranchTrueFalseTotal
if $$self <= $LAST_PRE_SNOWFLAKE_ID 4 6 10
67
68 6 return ($$self >> $TIMESTAMP_SHIFT) + $TW_EPOCH;
69 }
70  
71  
72 8 ▶sub worker {
Subroutine worker: called CC=3 SCAR=11.0
Pod documented
73 8 my ($self, $worker) = @_;
74
75 8 ▶ if (defined $worker) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $worker) 1 7 8
76 1 $self->_set( $self->timestamp, $worker, $self->sequence );
77 1 return;
78 }
79
80 7 ▶ return if $$self <= $LAST_PRE_SNOWFLAKE_ID;
Branch: 1 branch
BranchTrueFalseTotal
if $$self <= $LAST_PRE_SNOWFLAKE_ID 4 3 7
81
82 3 return ($$self & $WORKER_MASK) >> $SEQUENCE_BITS;
83 }
84  
85  
86 8 ▶sub sequence {
Subroutine sequence: called CC=3 SCAR=11.0
Pod documented
87 8 my ($self, $sequence) = @_;
88
89 8 ▶ if (defined $sequence) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $sequence) 1 7 8
90 1 $self->_set( $self->timestamp, $self->worker, $sequence );
91 1 return;
92 }
93
94 7 ▶ return if $$self <= $LAST_PRE_SNOWFLAKE_ID;
Branch: 1 branch
BranchTrueFalseTotal
if $$self <= $LAST_PRE_SNOWFLAKE_ID 4 3 7
95
96 3 return $$self & $SEQUENCE_MASK;
97 }
98  
99  
100 4 ▶sub epoch {
Subroutine epoch: called CC=3 SCAR=11.0
Pod documented
101 4 my ($self, $epoch) = @_;
102
103 4 ▶ croak "epoch() is read-only" if defined $epoch;
Branch: 1 branch
BranchTrueFalseTotal
if defined $epoch 1 3 4
104
105 3 my $timestamp = $self->timestamp;
106 3 ▶ return unless defined $timestamp;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $timestamp 1 2 3
107 2 return $timestamp / 1000;
108 }
109  
110  
111 4 1;
112  
113 __END__
↑ Summary