lib/Web/ComposableRequest/Util.pm

filter: Statementstmt 97.4% 190 / 195 Branchbran 72.8% 51 / 70 Conditioncond 62.0% 18 / 29 MC/DCmcdc 38.8% 7 / 18 Subroutinesub 100% 41 / 41 Podpod 100% 20 / 20 totaltotal 87.6% 327 / 373 CC 50 SCAR 17.0
CC
50
Cov
88%
__ANON__
15.8
merge_attributes
9.8
compose_class
7.1
CRAP 5.5
« lib/Web/ComposableRequest/Role/L10N.pm ↑ Summary lib/Web/ComposableRequest.pm »
1 package Web::ComposableRequest::Util;
2  
3 1 ▶use strictures;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use parent 'Exporter::Tiny';
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 1 ▶use Digest::MD5 qw( md5 md5_hex );
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use Encode qw( decode );
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use English qw( -no_match_vars );
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use List::Util qw( first );
Subroutine BEGIN: called CC=1 SCAR=0.0
10 1 ▶use Scalar::Util qw( blessed );
Subroutine BEGIN: called CC=1 SCAR=0.0
11 1 ▶use Subclass::Of;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 1 ▶use Sys::Hostname qw( hostname );
Subroutine BEGIN: called CC=1 SCAR=0.0
13 1 ▶use URI::Escape qw( );
Subroutine BEGIN: called CC=1 SCAR=0.0
14 1 ▶use URI::http;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 1 ▶use URI::https;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 1 ▶use Web::ComposableRequest::Constants qw( EXCEPTION_CLASS LANG );
Subroutine BEGIN: called CC=1 SCAR=0.0
17  
18 1 our @EXPORT_OK = qw( add_config_role base64_decode_ns base64_encode_ns bson64id
19 bson64id_time compose_class decode_array decode_hash
20 extract_lang first_char is_arrayref is_hashref is_member
21 list_config_roles merge_attributes new_uri trim thread_id
22 throw uri_escape );
23  
24 1 my $bson_id_count = 0;
25 1 my $bson_prev_time = 0;
26 1 my $class_stash = {};
27 1 my @config_roles = ();
28 1 my $host_id = substr md5( hostname ), 0, 3;
29 1 my $reserved = q(;/?:@&=+$,[]);
30 1 my $mark = q(-_.!~*'()); #'; emacs
31 1 my $unreserved = "A-Za-z0-9\Q${mark}\E%\#";
32 1 my $uric = quotemeta( $reserved ) . '\p{isAlpha}' . $unreserved;
33  
34 # Private functions
35 9 ▶my $_base64_char_set = sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
36 9 return [ 0 .. 9, 'A' .. 'Z', '_', 'a' .. 'z', '~', '+' ];
37 1 };
38  
39 2 ▶my $_index64 = sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
40 2 return [ qw(XX XX XX XX XX XX XX XX XX XX XX XX XX XX XX XX
41 XX XX XX XX XX XX XX XX XX XX XX XX XX XX XX XX
42 XX XX XX XX XX XX XX XX XX XX XX 64 XX XX XX XX
43 0 1 2 3 4 5 6 7 8 9 XX XX XX XX XX XX
44 XX 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
45 25 26 27 28 29 30 31 32 33 34 35 XX XX XX XX 36
46 XX 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51
47 52 53 54 55 56 57 58 59 60 61 62 XX XX XX 63 XX
48  
49 XX XX XX XX XX XX XX XX XX XX XX XX XX XX XX XX
50 XX XX XX XX XX XX XX XX XX XX XX XX XX XX XX XX
51 XX XX XX XX XX XX XX XX XX XX XX XX XX XX XX XX
52 XX XX XX XX XX XX XX XX XX XX XX XX XX XX XX XX
53 XX XX XX XX XX XX XX XX XX XX XX XX XX XX XX XX
54 XX XX XX XX XX XX XX XX XX XX XX XX XX XX XX XX
55 XX XX XX XX XX XX XX XX XX XX XX XX XX XX XX XX
56 XX XX XX XX XX XX XX XX XX XX XX XX XX XX XX XX) ];
57 1 };
58  
59 2 ▶my $_base64_decode_ns = sub {
Subroutine __ANON__: called CC=13 SCAR=27.6
60 2 ▶ my $x = shift; defined $x or return; my @x = split q(), $x;
Branch: 1 branch
BranchTrueFalseTotal
defined $x or return 2 0 2
61  
62 2 my $index = $_index64->(); my $j = 0; my $k = 0;
63  
64 2 my $len = length $x; my $pad = 64; my @y = ();
65  
66 ROUND: {
67 2 ▶ while ($j < $len) {
Branch: 1 branch
BranchTrueFalseTotal
if ($j < $len) 7 1 8
68 7 my @c = (); my $i = 0;
69  
70 7 ▶ while ($i < 4) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < 4) 28 5 33
71 28 my $uc = $index->[ ord $x[ $j++ ] ];
72  
73 28 ▶ $uc ne 'XX' and $c[ $i++ ] = 0 + $uc; $j == $len or next;
Branch: 2 branches
BranchTrueFalseTotal
$uc ne "XX" and $c[$i++] = 0 + $uc 28 0 28
$j == $len or next 2 26 28
74  
75 2 ▶ if ($i < 4) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < 4) 0 2 2
76 0 ▶ $i < 2 and last ROUND; $i == 2 and $c[ 2 ] = $pad; $c[ 3 ] = $pad;
Branch: 2 branches
BranchTrueFalseTotal
$i < 2 and last ROUND 0 0 0
$i == 2 and $c[2] = $pad 0 0 0
77 }
78  
79 2 last;
80 }
81  
82 7 ▶ ($c[ 0 ] == $pad || $c[ 1 ] == $pad) and last;
Branch: 1 branch
BranchTrueFalseTotal
$c[0] == $pad || $c[1] == $pad and last 0 7 7
Condition33%1/3
$c[0] == $pad or $c[1] == $pad
l!l&&r!l&&!r
007
MC/DC0%0/2
$c[0] == $pad or $c[1] == $pad
$c[0] == $pad $c[1] == $pad
Truth table33%1/3
A or B
A= $c[0] == $padB= $c[1] == $pad
ABresult
1X1
011
000
83 7 $y[ $k++ ] = ( $c[ 0 ] << 2) | (($c[ 1 ] & 0x30) >> 4);
84 7 ▶ $c[ 2 ] == $pad and last;
Branch: 1 branch
BranchTrueFalseTotal
$c[2] == $pad and last 1 6 7
85 6 $y[ $k++ ] = (($c[ 1 ] & 0x0F) << 4) | (($c[ 2 ] & 0x3C) >> 2);
86 6 ▶ $c[ 3 ] == $pad and last;
Branch: 1 branch
BranchTrueFalseTotal
$c[3] == $pad and last 0 6 6
87 6 $y[ $k++ ] = (($c[ 2 ] & 0x03) << 6) | $c[ 3 ];
88 }
89 }
90  
91 2 return join q(), map { chr $_ } @y;
92 1 };
93  
94 9 ▶my $_base64_encode_ns = sub {
Subroutine __ANON__: called CC=6 SCAR=18.1
95 9 ▶ my $x = shift; defined $x or return; my @x = split q(), $x;
Branch: 1 branch
BranchTrueFalseTotal
defined $x or return 9 0 9
96  
97 9 my $basis = $_base64_char_set->(); my $len = length $x; my @y = ();
98  
99 9 ▶ for (my $i = 0, my $j = 0; $len > 0; $len -= 3, $i += 3) {
Branch: 1 branch
BranchTrueFalseTotal
if ($len > 0) 42 9 51
100 42 ▶ my $c1 = ord $x[ $i ]; my $c2 = $len > 1 ? ord $x[ $i + 1 ] : 0;
Branch: 1 branch
BranchTrueFalseTotal
$len > 1 ? : 41 1 42
101  
102 42 $y[ $j++ ] = $basis->[ $c1 >> 2 ];
103 42 $y[ $j++ ] = $basis->[ (($c1 & 0x3) << 4) | (($c2 & 0xF0) >> 4) ];
104  
105 42 ▶ if ($len > 2) {
Branch: 2 branches
BranchTrueFalseTotal
if ($len > 2) { } 41 1 42
elsif ($len == 2) { } 0 1 1
106 41 my $c3 = ord $x[ $i + 2 ];
107  
108 41 $y[ $j++ ] = $basis->[ (($c2 & 0xF) << 2) | (($c3 & 0xC0) >> 6) ];
109 41 $y[ $j++ ] = $basis->[ $c3 & 0x3F ];
110 }
111 elsif ($len == 2) {
112 0 $y[ $j++ ] = $basis->[ ($c2 & 0xF) << 2 ];
113 0 $y[ $j++ ] = $basis->[ 64 ];
114 }
115 else { # len == 1
116 1 $y[ $j++ ] = $basis->[ 64 ];
117 1 $y[ $j++ ] = $basis->[ 64 ];
118 }
119 }
120  
121 9 return join q(), @y;
122 1 };
123  
124 8 ▶my $_bsonid_inc = sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
125 8 my $now = shift; $bson_id_count++;
126  
127 8 ▶ $now > $bson_prev_time and $bson_id_count = 0; $bson_prev_time = $now;
Branch: 1 branch
BranchTrueFalseTotal
$now > $bson_prev_time and $bson_id_count = 0 2 6 8
128  
129 8 return (pack 'n', thread_id() % 0xFFFF ).(pack 'n', $bson_id_count % 0xFFFF);
130 1 };
131  
132 8 ▶my $_bsonid_time = sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
133 8 my $now = shift;
134  
135 8 return (substr pack( 'N', $now >> 32 ), 2, 2).(pack 'N', $now % 0xFFFFFFFF);
136 1 };
137  
138 8 ▶my $_bson_id = sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
139 8 my $now = time; my $pid = pack 'n', $PID % 0xFFFF;
140  
141 8 return $_bsonid_time->( $now ).$host_id.$pid.$_bsonid_inc->( $now );
142 1 };
143  
144 # Exported functions
145 4 ▶sub add_config_role ($) {
Subroutine add_config_role: called CC=1 SCAR=0.0
Pod documented
146 4 my $role = shift; return push @config_roles, $role;
147 }
148  
149 1 ▶sub base64_decode_ns ($) {
Subroutine base64_decode_ns: called CC=1 SCAR=0.0
Pod documented
150 1 return $_base64_decode_ns->( $_[ 0 ] );
151 }
152  
153 1 ▶sub base64_encode_ns (;$) {
Subroutine base64_encode_ns: called CC=1 SCAR=0.0
Pod documented
154 1 return $_base64_encode_ns->( $_[ 0 ] );
155 }
156  
157 8 ▶sub bson64id (;$) {
Subroutine bson64id: called CC=1 SCAR=0.0
Pod documented
158 8 return $_base64_encode_ns->( $_bson_id->() );
159 }
160  
161 1 ▶sub bson64id_time ($) {
Subroutine bson64id_time: called CC=1 SCAR=0.0
Pod documented
162 1 return unpack 'N', substr $_base64_decode_ns->( $_[ 0 ] ), 2, 4;
163 }
164  
165 5 ▶sub compose_class ($$;@) {
Subroutine compose_class: called CC=7 SCAR=19.6
Pod documented
166 5 my ($base, $params, %options) = @_;
167  
168 5 ▶ my @params = keys %{ $params // {} }; @params > 0 or return $base;
Branch: 1 branch
BranchTrueFalseTotal
@params > 0 or return $base 4 1 5
Condition50%1/2
$params // {}
l!l
50
MC/DC0%0/1
$params // {}
$params
Truth table50%1/2
A
A= $params
Aresult
11
00
169  
170 4 my $class = "${base}::".(substr md5_hex( join q(), @params ), 0, 8);
171  
172 4 ▶ exists $class_stash->{ $class } and return $class_stash->{ $class };
Branch: 1 branch
BranchTrueFalseTotal
exists $class_stash->{$class} and return $class_stash->{$class} 3 1 4
173  
174 1 ▶ my $is = $options{is} // 'ro'; my @attrs;
Condition50%1/2
$options{"is"} // "ro"
l!l
10
MC/DC0%0/1
$options{"is"} // "ro"
$options{"is"}
Truth table50%1/2
A
A= $options{"is"}
Aresult
11
00
175  
176 1 for my $name (@params) {
177 1 my ($type, $default) = @{ $params->{ $name } };
178 1 my $props = [ is => $is, isa => $type ];
179  
180 1 ▶ defined $default and push @{ $props }, 'default', $default;
Branch: 1 branch
BranchTrueFalseTotal
defined $default and push @{$props;}, "default", $default 1 0 1
181 1 push @attrs, $name, $props;
182 }
183  
184 1 return $class_stash->{ $class } = subclass_of
185 ( $base, -package => $class, -has => [ @attrs ] );
186 }
187  
188 7 ▶sub decode_array ($$) {
Subroutine decode_array: called CC=4 SCAR=13.9
Pod documented
189 7 my ($enc, $param) = @_;
190  
191 7 ▶ (not defined $param->[ 0 ] or blessed $param->[ 0 ]) and return;
Branch: 1 branch
BranchTrueFalseTotal
!defined($param->[0]) || builtin::blessed($param->[0]) and return 5 2 7
Condition66%2/3
not defined $param->[0] or builtin::blessed($param->[0])
l!l&&r!l&&!r
502
MC/DC50%1/2
not defined $param->[0] or builtin::blessed($param->[0])
not defined $param->[0] builtin::blessed($param->[0])
Truth table66%2/3
not(A) or B
A= not defined $param->[0]B= builtin::blessed($param->[0])
ABresult
1X1
011
000
192  
193 2 ▶ for (my $i = 0, my $len = @{ $param }; $i < $len; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $len) 6 2 8
194 6 $param->[ $i ] = decode( $enc, $param->[ $i ] );
195 }
196  
197 2 return;
198 }
199  
200 9 ▶sub decode_hash ($$) {
Subroutine decode_hash: called CC=3 SCAR=11.0
Pod documented
201 9 my ($enc, $param) = @_; my @keys = keys %{ $param };
202  
203 9 for my $k (@keys) {
204 12 my $v = delete $param->{ $k };
205  
206 $param->{ decode( $enc, $k ) }
207 12 ▶ = is_arrayref( $v ) ? [ map { decode( $enc, $_ ) } @{ $v } ]
Branch: 1 branch
BranchTrueFalseTotal
&is_arrayref($v) ? : 1 11 12
208 : decode( $enc, $v );
209 }
210  
211 9 return;
212 }
213  
214 3 ▶sub extract_lang ($) {
Subroutine extract_lang: called CC=2 SCAR=7.2
Pod documented
215 3 ▶ my $v = shift; return $v ? (split m{ _ }mx, $v)[ 0 ] : LANG;
Branch: 1 branch
BranchTrueFalseTotal
$v ? : 3 0 3
216 }
217  
218 12 ▶sub first_char ($) {
Subroutine first_char: called CC=1 SCAR=0.0
Pod documented
219 12 return substr $_[ 0 ], 0, 1;
220 }
221  
222 207 ▶sub is_arrayref (;$) {
Subroutine is_arrayref: called CC=3 SCAR=11.0
Pod documented
223 207 ▶ return $_[ 0 ] && ref $_[ 0 ] eq 'ARRAY' ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$_[0] && ref $_[0] eq 'ARRAY' ? : 29 178 207
Condition100%3/3
$_[0] and ref $_[0] eq "ARRAY"
!ll&&!rl&&r
1616229
MC/DC100%2/2
$_[0] and ref $_[0] eq "ARRAY"
$_[0] ref $_[0] eq "ARRAY"
Truth table100%3/3
A and B
A= $_[0]B= ref $_[0] eq "ARRAY"
ABresult
0X0
100
111
224 }
225  
226 52 ▶sub is_hashref (;$) {
Subroutine is_hashref: called CC=3 SCAR=11.0
Pod documented
227 52 ▶ return $_[ 0 ] && ref $_[ 0 ] eq 'HASH' ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$_[0] && ref $_[0] eq 'HASH' ? : 12 40 52
Condition100%3/3
$_[0] and ref $_[0] eq "HASH"
!ll&&!rl&&r
43612
MC/DC100%2/2
$_[0] and ref $_[0] eq "HASH"
$_[0] ref $_[0] eq "HASH"
Truth table100%3/3
A and B
A= $_[0]B= ref $_[0] eq "HASH"
ABresult
0X0
100
111
228 }
229  
230 47 ▶sub is_member (;@) {
Subroutine is_member: called CC=4 SCAR=13.9
Pod documented
231 47 ▶ my ($candidate, @args) = @_; $candidate or return;
Branch: 1 branch
BranchTrueFalseTotal
$candidate or return 47 0 47
232  
233 47 ▶ is_arrayref $args[ 0 ] and @args = @{ $args[ 0 ] };
Branch: 1 branch
BranchTrueFalseTotal
&is_arrayref($args[0]) and @args = @{$args[0];} 7 40 47
234  
235 47 ▶ return (first { $_ eq $candidate } @args) ? 1 : 0;
Subroutine __ANON__: called CC=1 SCAR=0.0
Branch: 1 branch
BranchTrueFalseTotal
&first(sub { $_ eq $candidate; } , @args) ? : 9 38 47
236 }
237  
238 2 ▶sub list_config_roles () {
Subroutine list_config_roles: called CC=1 SCAR=0.0
Pod documented
239 2 return @config_roles;
240 }
241  
242 4 ▶sub merge_attributes ($@) {
Subroutine merge_attributes: called CC=9 SCAR=22.8
Pod documented
243 4 my ($dest, @args) = @_;
244  
245 4 ▶ my $attr = is_arrayref( $args[ -1 ] ) ? pop @args : [];
Branch: 1 branch
BranchTrueFalseTotal
&is_arrayref($args[-1]) ? : 4 0 4
246  
247 4 ▶ for my $k (grep { not exists $dest->{ $_ } or not defined $dest->{ $_ } }
Condition33%1/3
exists $dest->{$_} && defined $dest->{$_}
!ll&&!rl&&r
3800
MC/DC0%0/2
exists $dest->{$_} && defined $dest->{$_}
exists $dest->{$_} defined $dest->{$_}
Truth table33%1/3
A && B
A= exists $dest->{$_}B= defined $dest->{$_}
ABresult
0X0
100
111
248 4 @{ $attr }) {
249 38 my $i = 0; my $v;
250  
251 38 ▶ while (not defined $v and defined( my $src = $args[ $i++ ] )) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $v and defined(my $src = $args[$i++])) 40 38 78
Condition100%3/3
not defined $v and defined(my $src = $args[$i++])
!ll&&!rl&&r
112740
MC/DC100%2/2
not defined $v and defined(my $src = $args[$i++])
not defined $v defined(my $src = $args[$i++])
Truth table100%3/3
not(A) and B
A= not defined $vB= defined(my $src = $args[$i++])
ABresult
0X0
100
111
252 40 my $class = blessed $src;
253  
254 40 ▶ $v = $class ? ($src->can( $k ) ? $src->$k() : undef) : $src->{ $k };
Branch: 2 branches
BranchTrueFalseTotal
$class ? : 0 40 40
$src->can($k) ? : 0 0 0
255 }
256  
257 38 ▶ defined $v and $dest->{ $k } = $v;
Branch: 1 branch
BranchTrueFalseTotal
defined $v and $dest->{$k} = $v 11 27 38
258 }
259  
260 4 return $dest;
261 }
262  
263 8 ▶sub new_uri ($$) {
Subroutine new_uri: called CC=1 SCAR=0.0
Pod documented
264 8 my $v = uri_escape( $_[ 1 ] ); return bless \$v, 'URI::'.$_[ 0 ];
265 }
266  
267 8 ▶sub thread_id () {
Subroutine thread_id: called CC=2 SCAR=7.6
Pod documented
268 8 ▶ return exists $INC{ 'threads.pm' } ? threads->tid() : 0;
Branch: 1 branch
BranchTrueFalseTotal
exists $INC{'threads.pm'} ? : 0 8 8
269 }
270  
271 1 ▶sub throw (;@) {
Subroutine throw: called CC=1 SCAR=0.0
Pod documented
272 1 EXCEPTION_CLASS->throw( @_ );
273 }
274  
275 6 ▶sub trim (;$$) {
Subroutine trim: called CC=3 SCAR=11.3
Pod documented
276 6 ▶ my $chs = $_[ 1 ] // " \t"; (my $v = $_[ 0 ] // q()) =~ s{ \A [$chs]+ }{}mx;
Condition50%2/4
$_[1] // " \t"
l!l
06
$_[0] // ""
l!l
60
MC/DC0%0/1
$_[1] // " \t"
$_[1]
MC/DC0%0/1
$_[0] // ""
$_[0]
Truth table50%1/2
A
A= $_[1]
Aresult
11
00
Truth table50%1/2
A
A= $_[0]
Aresult
11
00
277  
278 6 chomp $v; $v =~ s{ [$chs]+ \z }{}mx; return $v;
279 }
280  
281 10 ▶sub uri_escape ($;$) {
Subroutine uri_escape: called CC=2 SCAR=7.1
Pod documented
282 10 ▶ my ($v, $pattern) = @_; $pattern //= $uric;
Condition33%1/3
$pattern //= $uric
l!l&&r!l&&!r
0100
MC/DC0%0/2
$pattern //= $uric
$pattern $uric
Truth table33%1/3
A //= B
A= $patternB= $uric
ABresult
1X1
011
000
283  
284 10 $v =~ s{([^$pattern])}{ URI::Escape::uri_escape_utf8($1) }ego;
285 10 utf8::downgrade( $v );
286 10 return $v;
287 }
288  
289 1 1;
290  
291 __END__
« lib/Web/ComposableRequest/Role/L10N.pm ↑ Summary lib/Web/ComposableRequest.pm »