Sqids.pm

filter: Statementstmt 100% 126 / 126 Branchbran 94.8% 55 / 58 Conditioncond 91.6% 11 / 12 MC/DCmcdc 87.5% 7 / 8 Subroutinesub 100% 20 / 20 Podpod 28.5% 2 / 7 totaltotal 95.6% 221 / 231 CC 37 SCAR 18.3
CC
37
Cov
98%
decode
10.0
is_blocked_id
10.0
_encode_numbers
8.0
CRAP 6.3
↑ Summary Sqids/Constants.pm »
1 package Sqids;
2 4 ▶use 5.008001;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 4 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 4 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 4 ▶use bignum;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 4 ▶use Carp 'croak';
Subroutine BEGIN: called CC=1 SCAR=0.0
7 4 ▶use Encode qw(encode_utf8);
Subroutine BEGIN: called CC=1 SCAR=0.0
8 4 ▶use List::Util qw(first min reduce);
Subroutine BEGIN: called CC=1 SCAR=0.0
9 4 ▶use Sqids::Constants;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 4 ▶use Class::Tiny {
Subroutine BEGIN: called CC=1 SCAR=0.0
11 alphabet => Sqids::Constants::DEFAULT_ALPHABET,
12 min_length => Sqids::Constants::DEFAULT_MIN_LENGTH,
13 63 ▶ blocklist => sub { Sqids::Constants::DEFAULT_BLOCKLIST },
Subroutine __ANON__: called CC=1 SCAR=0.0
14 4 };
15  
16 4 our $VERSION = "0.03";
17  
18 70 ▶sub BUILD {
Subroutine BUILD: called CC=7 SCAR=19.5
Pod undocumented
19 70 my ($self, $args) = @_;
20  
21 70 my $alphabet = $self->alphabet;
22 70 my $min_length = $self->min_length;
23 70 my $blocklist = $self->blocklist;
24  
25 70 ▶ croak 'Alphabet cannot contain multibyte characters' if length $alphabet != length encode_utf8 $alphabet;
Branch: 1 branch
BranchTrueFalseTotal
if length $alphabet != length &encode_utf8($alphabet) 1 69 70
26  
27 69 ▶ croak 'Alphabet length must be at least 3' if length $alphabet < 3;
Branch: 1 branch
BranchTrueFalseTotal
if length $alphabet < bless( {"sign" => "+","value" => bless( [3], 'Math::BigInt::Calc' )}, 'Math::BigInt' ) 1 68 69
28  
29 68 my %alphabet_hash = map { $_ => undef } split '', $alphabet;
30 68 ▶ croak 'Alphabet must contain unique characters' if keys %alphabet_hash != length $alphabet;
Branch: 1 branch
BranchTrueFalseTotal
if keys %alphabet_hash != length $alphabet 1 67 68
31  
32 67 my $min_length_limit = 255;
33 67 ▶ croak "Minimum length has to be between 0 and $min_length_limit" if $min_length < 0 || $min_length > $min_length_limit;
Branch: 1 branch
BranchTrueFalseTotal
if $min_length < bless( {"sign" => "+","value" => bless( [0], 'Math::BigInt::Calc' )}, 'Math::BigInt' ) or $min_length > $min_length_limit 2 65 67
Condition100%3/3
$min_length < bless( {"sign" => "+","value" => bless( [0], 'Math::BigInt::Calc' )}, 'Math::BigInt' ) or $min_length > $min_length_limit
l!l&&r!l&&!r
1165
MC/DC100%2/2
$min_length < bless( {"sign" => "+","value" => bless( [0], 'Math::BigInt::Calc' )}, 'Math::BigInt' ) or $min_length > $min_length_limit
$min_length < bless( {"sign" => "+","value" => bless( [0], 'Math::BigInt::Calc' )}, 'Math::BigInt' ) $min_length > $min_length_limit
Truth table100%3/3
A or B
A= $min_length < bless( {"sign" => "+","value" => bless( [0], 'Math::BigInt::Calc' )}, 'Math::BigInt' )B= $min_length > $min_length_limit
ABresult
1X1
011
000
34  
35 # clean up blocklist:
36 # 1. all blocklist words should be lowercase
37 # 2. no words less than 3 chars
38 # 3. if some words contain chars that are not in the alphabet, remove those
39 65 my $alphabet_lower = quotemeta lc $alphabet;
40 @$blocklist =
41 31385 ▶ grep { length >= 3 && /^[$alphabet_lower]+$/ }
Condition66%2/3
length $_ >= {sign => '+', value => [3]} && /^[$alphabet_lower]+$/
!ll&&!rl&&r
055830827
MC/DC50%1/2
length $_ >= {sign => '+', value => [3]} && /^[$alphabet_lower]+$/
length $_ >= {sign => '+', value => [3]} /^[$alphabet_lower]+$/
Truth table66%2/3
A && B
A= length $_ >= {sign => '+', value => [3]}B= /^[$alphabet_lower]+$/
ABresult
0X0
100
111
42 65 map { lc } @$blocklist;
43  
44 65 $self->alphabet($self->shuffle($alphabet));
45 }
46  
47 # Encodes an array of unsigned integers into an ID
48 #
49 # These are the cases where encoding might fail:
50 # - One of the numbers passed is smaller than 0
51 # - An n-number of attempts has been made to re-generated the ID, where n is alphabet length + 1
52 #
53 # @param {array.<number>} numbers Non-negative integers to encode into an ID (or an arrayref)
54 # @returns {string} Generated ID
55  
56 111 ▶sub encode {
Subroutine encode: called CC=4 SCAR=13.9
Pod documented
57 111 my ($self, @numbers) = @_;
58  
59 # if no numbers passed, return an empty string
60 111 ▶ return '' unless @numbers;
Branch: 1 branch
BranchTrueFalseTotal
unless @numbers 1 110 111
61  
62 # Convert to array if arrayref provided
63 110 ▶ @numbers = @{$numbers[0]} if ref $numbers[0] eq 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
if ref $numbers[0] eq "ARRAY" 86 24 110
64  
65 110 ▶ croak "Encoding only supports non-negative numbers" if first { $_ < 0 } @numbers;
Subroutine __ANON__: called CC=1 SCAR=0.0
Branch: 1 branch
BranchTrueFalseTotal
if &first(sub { $_ < bless( {"sign" => "+","value" => bless( [0], 'Math::BigInt::Calc' )}, 'Math::BigInt' ); } , @numbers) 1 109 110
66  
67 109 return $self->_encode_numbers(0, @numbers);
68 }
69  
70 # Internal function that encodes an array of unsigned integers into an ID
71 #
72 # @param {number} increment An internal number used to modify the `offset` variable in order to re-generate the ID
73 # @param {array.<number>} numbers Non-negative integers to encode into an ID
74 # @returns {string} Generated ID
75  
76 122 ▶sub _encode_numbers {
Subroutine _encode_numbers: called CC=8 SCAR=20.8
77 122 my ($self, $increment, @numbers) = @_;
78  
79 # if increment is greater than alphabet length, we've reached max attempts
80 122 my $alphabet = $self->alphabet;
81 122 my $length = length $alphabet;
82 122 ▶ croak 'Reached max attempts to re-generate the ID' if $increment > $length;
Branch: 1 branch
BranchTrueFalseTotal
if $increment > $length 1 121 122
83  
84 # get a semi-random offset from input numbers
85 121 my $offset = @numbers;
86 121 for (0..$#numbers) {
87 412 $offset += ord(substr($alphabet, $numbers[$_] % $length, 1)) + $_;
88 }
89  
90 # if there is a non-zero `increment`, it's an internal attempt to re-generated the ID
91 121 $offset = ($offset + $increment) % $length;
92  
93 # re-arrange alphabet so that second-half goes in front of the first-half
94 121 $alphabet = substr($alphabet, $offset) . substr($alphabet, 0, $offset);
95  
96 # `prefix` is the first character in the generated ID, used for randomization
97 121 my $prefix = substr($alphabet, 0, 1);
98  
99 # reverse alphabet (otherwise for [0, x] `offset` and `separator` will be the same char)
100 121 $alphabet = reverse $alphabet;
101  
102 # final ID will always have the `prefix` character at the beginning
103 121 my @ret = ($prefix);
104  
105 # encode input array
106 121 for (0..$#numbers) {
107 # the first character of the alphabet is going to be reserved for the `separator`
108 412 push @ret, $self->to_id($numbers[$_], substr($alphabet, 1));
109  
110 # if not the last number
111 412 ▶ if ($_ < @numbers - 1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($_ < @numbers - bless( {"sign" => "+","value" => bless( [1], 'Math::BigInt::Calc' )}, 'Math::BigInt' )) 291 121 412
112 # `separator` character is used to isolate numbers within the ID
113 291 push @ret, substr($alphabet, 0, 1);
114  
115 # shuffle on every iteration
116 291 $alphabet = $self->shuffle($alphabet);
117 }
118 }
119  
120 # join all the parts to form an ID
121 121 my $id = join('', @ret);
122  
123 # handle `min_length` requirement, if the ID is too short
124 121 ▶ if ($self->min_length > length $id) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->min_length > length $id) 48 73 121
125 # append a separator
126 48 $id .= substr($alphabet, 0, 1);
127  
128 # keep appending `separator` + however much alphabet is needed
129 # for decoding: two separators next to each other is what tells us the rest are junk characters
130 48 ▶ while ($self->min_length - length $id > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->min_length - length($id) > bless( {"sign" => "+","value" => bless( [0], 'Math::BigInt::Calc' )}, 'Math::BigInt' )) 40 48 88
131 40 $alphabet = $self->shuffle($alphabet);
132 40 $id .= substr($alphabet, 0, min(length $alphabet, $self->min_length - length $id));
133 }
134 }
135  
136 # if ID has a blocked word anywhere, restart with a +1 increment
137 121 ▶ if ($self->is_blocked_id($id)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->is_blocked_id($id)) 13 108 121
138 13 $id = $self->_encode_numbers($increment + 1, @numbers);
139 }
140  
141 117 return $id;
142 }
143  
144 # Decodes an ID back into an array of unsigned integers
145 #
146 # These are the cases where the return value might be an empty array:
147 # - Empty ID / empty string
148 # - Non-alphabet character is found within ID
149 #
150 # @param {string} id Encoded ID
151 # @returns {array.<number>} Array of unsigned integers
152  
153 104 ▶sub decode {
Subroutine decode: called CC=10 SCAR=23.1
Pod documented
154 104 my ($self, $id) = @_;
155  
156 104 ▶ return if $id eq '';
Branch: 1 branch
BranchTrueFalseTotal
if $id eq "" 1 103 104
157 103 ▶ return unless defined wantarray;
Branch: 1 branch
BranchTrueFalseTotal
unless defined wantarray 0 103 103
158  
159 # if a character is not in the alphabet, return
160 103 my $alphabet = quotemeta $self->alphabet;
161 103 ▶ return if $id =~ /[^$alphabet]/;
Branch: 1 branch
BranchTrueFalseTotal
if $id =~ /[^$alphabet]/ 1 102 103
162  
163 # first character is always the `prefix`
164 102 my $prefix = substr($id, 0, 1);
165  
166 # `offset` is the semi-random position that was generated during encoding
167 102 my $offset = index $self->alphabet, $prefix;
168  
169 # re-arrange alphabet back into its original form
170 102 $alphabet = substr($self->alphabet, $offset) . substr($self->alphabet, 0, $offset);
171  
172 # reverse alphabet
173 102 $alphabet = reverse $alphabet;
174  
175 # now it's safe to remove the prefix character from ID, it's not needed anymore
176 102 $id = substr($id, 1);
177  
178 # decode
179 102 my @ret;
180 102 ▶ while (length $id) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $id) 402 72 474
181 402 my $separator = substr($alphabet, 0, 1);
182  
183 # we need the first part to the left of the separator to decode the number
184 402 my @chunks = split /\Q$separator\E/, $id, -1;
185 402 ▶ if (@chunks) {
Branch: 1 branch
BranchTrueFalseTotal
if (@chunks) 402 0 402
186 # if chunk is empty, we are done (the rest are junk characters)
187 402 ▶ return @ret if $chunks[0] eq '';
Branch: 1 branch
BranchTrueFalseTotal
if $chunks[0] eq "" 30 372 402
188  
189 # decode the number without using the `separator` character
190 372 push @ret, $self->to_number($chunks[0], substr($alphabet, 1));
191  
192 # if this ID has multiple numbers, shuffle the alphabet because that's what encoding function did
193 372 ▶ if (@chunks > 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (@chunks > bless( {"sign" => "+","value" => bless( [1], 'Math::BigInt::Calc' )}, 'Math::BigInt' )) 303 69 372
194 303 $alphabet = $self->shuffle($alphabet);
195 }
196 }
197  
198 # `id` is now going to be everything to the right of the `separator`
199 372 $id = join($separator, @chunks[1..$#chunks]);
200 }
201  
202 72 ▶ return wantarray ? @ret : @ret == 1 ? $ret[0] : \@ret;
Branch: 2 branches
BranchTrueFalseTotal
wantarray ? : 56 16 72
@ret == {sign => '+', value => ['1']} ? : 16 0 16
203 }
204  
205 # consistent shuffle (always produces the same result given the input)
206 699 ▶sub shuffle {
Subroutine shuffle: called CC=2 SCAR=6.9
Pod undocumented
207 699 my ($self, $alphabet) = @_;
208 699 my @chars = split '', $alphabet;
209  
210 699 ▶ for (my ($i, $j) = (0, @chars-1); $j>0; $i++, $j--) {
Branch: 1 branch
BranchTrueFalseTotal
if ($j > bless( {"sign" => "+","value" => bless( [0], 'Math::BigInt::Calc' )}, 'Math::BigInt' )) 41953 699 42652
211 41953 my $r = ($i * $j + ord($chars[$i]) + ord($chars[$j])) % @chars;
212 41953 @chars[$i,$r] = @chars[$r,$i];
213 }
214  
215 699 return join('', @chars);
216 }
217  
218 412 ▶sub to_id {
Subroutine to_id: called CC=2 SCAR=6.9
Pod undocumented
219 412 my ($self, $num, $alphabet) = @_;
220 412 my @id;
221 412 my $result = $num;
222 412 my $length = length $alphabet;
223  
224 412 ▶ do {
Branch: 1 branch
BranchTrueFalseTotal
$result > bless( {"sign" => "+","value" => bless( [0], 'Math::BigInt::Calc' )}, 'Math::BigInt' ) and do { do { unshift @id, substr($alphabet, $result % $length, bless( {"sign" => "+","value" => bless( [1], 'Math::BigInt::Calc' )}, 'Math::BigInt' )); $result = int $result / $length } } 147 412 559
225 559 unshift @id, substr($alphabet, $result % $length, 1);
226 559 $result = int($result / $length);
227 } while ($result > 0);
228  
229 412 return join('', @id);
230 }
231  
232 372 ▶sub to_number {
Subroutine to_number: called CC=1 SCAR=0.0
Pod undocumented
233 372 my ($self, $id, $alphabet) = @_;
234 372 ▶ reduce { $a * length($alphabet) + index($alphabet, $b) } 0, split '', $id;
Subroutine __ANON__: called CC=1 SCAR=0.0
235 }
236  
237 121 ▶sub is_blocked_id {
Subroutine is_blocked_id: called CC=10 SCAR=23.0
Pod undocumented
238 121 my ($self, $id) = @_;
239 121 $id = lc $id;
240  
241 121 foreach my $word (@{$self->blocklist}) {
242 # no point in checking words that are longer than the ID
243 55506 ▶ next unless length $word <= length $id;
Branch: 1 branch
BranchTrueFalseTotal
unless length $word <= length $id 20172 35334 55506
244 35334 ▶ if (length $id <= 3 || length $word <= 3) {
Branch: 3 branches
BranchTrueFalseTotal
if (length $id <= {sign => '+', value => [3]} or length $word <= {sign => '+', value => [3]}) { } 618 34716 35334
elsif ($word =~ /\d/) { } 21871 12845 34716
elsif ($id =~ /\Q$word\E/) { } 4 12841 12845
Condition100%3/3
length $id <= bless( {"sign" => "+","value" => bless( [3], 'Math::BigInt::Calc' )}, 'Math::BigInt' ) or length $word <= bless( {"sign" => "+","value" => bless( [3], 'Math::BigInt::Calc' )}, 'Math::BigInt' )
l!l&&r!l&&!r
960934716
MC/DC100%2/2
length $id <= bless( {"sign" => "+","value" => bless( [3], 'Math::BigInt::Calc' )}, 'Math::BigInt' ) or length $word <= bless( {"sign" => "+","value" => bless( [3], 'Math::BigInt::Calc' )}, 'Math::BigInt' )
length $id <= bless( {"sign" => "+","value" => bless( [3], 'Math::BigInt::Calc' )}, 'Math::BigInt' ) length $word <= bless( {"sign" => "+","value" => bless( [3], 'Math::BigInt::Calc' )}, 'Math::BigInt' )
Truth table100%3/3
A or B
A= length $id <= bless( {"sign" => "+","value" => bless( [3], 'Math::BigInt::Calc' )}, 'Math::BigInt' )B= length $word <= bless( {"sign" => "+","value" => bless( [3], 'Math::BigInt::Calc' )}, 'Math::BigInt' )
ABresult
1X1
011
000
245 # short words have to match completely; otherwise, too many matches
246 618 ▶ return 1 if $id eq $word;
Branch: 1 branch
BranchTrueFalseTotal
if $id eq $word 5 613 618
247 } elsif ($word =~ /\d/) {
248 # words with leet speak replacements are visible mostly on the ends of the ID
249 21871 ▶ return 1 if $id =~ /^\Q$word\E/ || $id =~ /\Q$word\E$/;
Branch: 1 branch
BranchTrueFalseTotal
if $id =~ /^\Q$word\E/ or $id =~ /\Q$word\E$/ 4 21867 21871
Condition100%3/3
$id =~ /^\Q$word\E/ or $id =~ /\Q$word\E$/
l!l&&r!l&&!r
3121867
MC/DC100%2/2
$id =~ /^\Q$word\E/ or $id =~ /\Q$word\E$/
$id =~ /^\Q$word\E/ $id =~ /\Q$word\E$/
Truth table100%3/3
A or B
A= $id =~ /^\Q$word\E/B= $id =~ /\Q$word\E$/
ABresult
1X1
011
000
250 } elsif ($id =~ /\Q$word\E/) {
251 # otherwise, check for blocked word anywhere in the string
252 4 return 1;
253 }
254 }
255  
256 108 return 0;
257 }
258  
259 4 1;
260 __END__
↑ Summary Sqids/Constants.pm »