blib/lib/URI/Template.pm

filter: Statementstmt 57.8% 100 / 173 Branchbran 32.6% 34 / 104 Conditioncond 23.5% 4 / 17 MC/DCmcdc 9.0% 1 / 11 Subroutinesub 80.0% 16 / 20 Podpod 100% 6 / 6 totaltotal 48.6% 161 / 331 CC 63 SCAR 37.8
CC
63
Cov
47%
_tostring_query
132.0
_tostring_semi
90.0
_tostring_path
90.0
CRAP 43.8
↑ Summary
1 package URI::Template;
2  
3 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 3 our $VERSION = '0.23';
7  
8 3 ▶use URI;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 3 ▶use URI::Escape ();
Subroutine BEGIN: called CC=1 SCAR=0.0
10 3 ▶use Unicode::Normalize ();
Subroutine BEGIN: called CC=1 SCAR=0.0
11 3 ▶use overload '""' => \&template;
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 3 my $RESERVED = q(:/?#\[\]\@!\$\&'\(\)\*\+,;=);
14 3 my %TOSTRING = (
15 '' => \&_tostring,
16 '+' => \&_tostring,
17 '#' => \&_tostring,
18 ';' => \&_tostring_semi,
19 '?' => \&_tostring_query,
20 '&' => \&_tostring_query,
21 '/' => \&_tostring_path,
22 '.' => \&_tostring_path,
23 );
24  
25 sub new {
26 8 ▶ my $class = shift;
Subroutine new: called CC=2 SCAR=6.9
Pod documented
27 8 my $templ = shift;
28 8 ▶ $templ = '' unless defined $templ;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $templ 2 6 8
29 8 my $self = bless { template => $templ, _vars => {} } => $class;
30  
31 8 $self->_study;
32  
33 8 return $self;
34 }
35  
36 sub _quote {
37 14 ▶ my ( $val, $safe ) = @_;
Subroutine _quote: called CC=4 SCAR=17.9
38 14 ▶ $safe ||= '';
Condition50%1/2
$safe ||= ""
l!l
014
MC/DC0%0/1
$safe ||= ""
$safe
Truth table50%1/2
A
A= $safe
Aresult
11
00
39 14 my $unsafe = '^A-Za-z0-9\-\._' . $safe;
40  
41 ## Where RESERVED are allowed to pass-through, so are
42 ## already-pct-encoded values
43 14 ▶ if( $safe ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($safe) 0 14 14
44 0 my (@chunks) = split(/(%[0-9A-Fa-f]{2})/, $val);
45  
46 # even chunks are not %xx, odd chunks are
47 return join '',
48 0 ▶ map { $_ % 2
Branch: 1 branch
BranchTrueFalseTotal
$_ % 2 ? : 0 0 0
49 ? $chunks[$_]
50 : URI::Escape::uri_escape_utf8( Unicode::Normalize::NFKC($chunks[$_]), $unsafe ) } 0..$#chunks;
51  
52 }
53  
54 # try to mirror python's urllib quote
55 14 return URI::Escape::uri_escape_utf8( Unicode::Normalize::NFKC( $val ),
56 $unsafe );
57 }
58  
59 sub _tostring {
60 14 ▶ my ( $var, $value, $exp ) = @_;
Subroutine _tostring: called CC=6 SCAR=26.2
61 14 my $safe = $exp->{ safe };
62  
63 14 ▶ if ( ref $value eq 'ARRAY' ) {
Branch: 3 branches
BranchTrueFalseTotal
if (ref $value eq 'ARRAY') { } 0 14 14
elsif (ref $value eq 'HASH') { } 0 14 14
elsif (defined $value) { } 14 0 14
64 0 return join( ',', map { _quote( $_, $safe ) } @$value );
65 }
66 elsif ( ref $value eq 'HASH' ) {
67 return join(
68 ',',
69 map {
70 0 _quote( $_, $safe )
71 . ( $var->{ explode } ? '=' : ',' )
72 0 ▶ . _quote( $value->{ $_ }, $safe )
Branch: 1 branch
BranchTrueFalseTotal
$var->{'explode'} ? : 0 0 0
73 } sort keys %$value
74 );
75 }
76 elsif ( defined $value ) {
77 return _quote(
78 14 ▶ substr( $value, 0, $var->{ prefix } || length( $value ) ),
Condition33%1/3
$var->{'prefix'} || length $value
l!l&&r!l&&!r
0140
MC/DC0%0/2
$var->{'prefix'} || length $value
$var->{'prefix'} length $value
Truth table33%1/3
A || B
A= $var->{'prefix'}B= length $value
ABresult
1X1
011
000
79 $safe );
80 }
81  
82 0 return;
83 }
84  
85 sub _tostring_semi {
86 0 ▶ my ( $var, $value, $exp ) = @_;
Subroutine _tostring_semi: not called CC=9 SCAR=45.0
87 0 my $safe = $exp->{ safe };
88 0 my $join = $exp->{ op };
89 0 ▶ $join = '&' if $exp->{ op } eq '?';
Branch: 1 branch
BranchTrueFalseTotal
if $exp->{"op"} eq "?" 0 0 0
90  
91 0 ▶ if ( ref $value eq 'ARRAY' ) {
Branch: 3 branches
BranchTrueFalseTotal
if (ref $value eq 'ARRAY') { } 0 0 0
elsif (ref $value eq 'HASH') { } 0 0 0
elsif (defined $value) { } 0 0 0
92 0 ▶ if ( $var->{ explode } ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($var->{'explode'}) { } 0 0 0
93 return join( $join,
94 0 map { $var->{ name } . '=' . _quote( $_, $safe ) } @$value );
95 }
96 else {
97 return $var->{ name } . '='
98 0 . join( ',', map { _quote( $_, $safe ) } @$value );
99 }
100 }
101 elsif ( ref $value eq 'HASH' ) {
102 0 ▶ if ( $var->{ explode } ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($var->{'explode'}) { } 0 0 0
103 return join(
104 $join,
105 map {
106 0 _quote( $_, $safe ) . '='
107 0 . _quote( $value->{ $_ }, $safe )
108 } sort keys %$value
109 );
110 }
111 else {
112 return $var->{ name } . '=' . join(
113 ',',
114 map {
115 0 _quote( $_, $safe ) . ','
116 0 . _quote( $value->{ $_ }, $safe )
117 } sort keys %$value
118 );
119 }
120 }
121 elsif ( defined $value ) {
122 0 ▶ return $var->{ name } unless length( $value );
Branch: 1 branch
BranchTrueFalseTotal
unless length $value 0 0 0
123 return
124 $var->{ name } . '='
125 . _quote(
126 0 ▶ substr( $value, 0, $var->{ prefix } || length( $value ) ),
Condition0%0/3
$var->{'prefix'} || length $value
l!l&&r!l&&!r
000
MC/DC0%0/2
$var->{'prefix'} || length $value
$var->{'prefix'} length $value
Truth table0%0/3
A || B
A= $var->{'prefix'}B= length $value
ABresult
1X1
011
000
127 $safe );
128 }
129  
130 0 return;
131 }
132  
133 sub _tostring_query {
134 0 ▶ my ( $var, $value, $exp ) = @_;
Subroutine _tostring_query: not called CC=11 SCAR=48.8
135 0 my $safe = $exp->{ safe };
136 0 my $join = $exp->{ op };
137 0 ▶ $join = '&' if $exp->{ op } =~ /[?&]/;
Branch: 1 branch
BranchTrueFalseTotal
if $exp->{"op"} =~ /[?&]/ 0 0 0
138  
139 0 ▶ if ( ref $value eq 'ARRAY' ) {
Branch: 3 branches
BranchTrueFalseTotal
if (ref $value eq 'ARRAY') { } 0 0 0
elsif (ref $value eq 'HASH') { } 0 0 0
elsif (defined $value) { } 0 0 0
140 0 ▶ return if !@$value;
Branch: 1 branch
BranchTrueFalseTotal
unless @$value 0 0 0
141 0 ▶ if ( $var->{ explode } ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($var->{'explode'}) { } 0 0 0
142 return join( $join,
143 0 map { $var->{ name } . '=' . _quote( $_, $safe ) } @$value );
144 }
145 else {
146 return $var->{ name } . '='
147 0 . join( ',', map { _quote( $_, $safe ) } @$value );
148 }
149 }
150 elsif ( ref $value eq 'HASH' ) {
151 0 ▶ return if !keys %$value;
Branch: 1 branch
BranchTrueFalseTotal
unless keys %$value 0 0 0
152 0 ▶ if ( $var->{ explode } ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($var->{'explode'}) { } 0 0 0
153 return join(
154 $join,
155 map {
156 0 _quote( $_, $safe ) . '='
157 0 . _quote( $value->{ $_ }, $safe )
158 } sort keys %$value
159 );
160 }
161 else {
162 return $var->{ name } . '=' . join(
163 ',',
164 map {
165 0 _quote( $_, $safe ) . ','
166 0 . _quote( $value->{ $_ }, $safe )
167 } sort keys %$value
168 );
169 }
170 }
171 elsif ( defined $value ) {
172 0 ▶ return $var->{ name } . '=' unless length( $value );
Branch: 1 branch
BranchTrueFalseTotal
unless length $value 0 0 0
173 return
174 $var->{ name } . '='
175 . _quote(
176 0 ▶ substr( $value, 0, $var->{ prefix } || length( $value ) ),
Condition0%0/3
$var->{'prefix'} || length $value
l!l&&r!l&&!r
000
MC/DC0%0/2
$var->{'prefix'} || length $value
$var->{'prefix'} length $value
Truth table0%0/3
A || B
A= $var->{'prefix'}B= length $value
ABresult
1X1
011
000
177 $safe );
178 }
179 }
180  
181 sub _tostring_path {
182 0 ▶ my ( $var, $value, $exp ) = @_;
Subroutine _tostring_path: not called CC=9 SCAR=45.0
183 0 my $safe = $exp->{ safe };
184 0 my $join = $exp->{ op };
185  
186 0 ▶ if ( ref $value eq 'ARRAY' ) {
Branch: 3 branches
BranchTrueFalseTotal
if (ref $value eq 'ARRAY') { } 0 0 0
elsif (ref $value eq 'HASH') { } 0 0 0
elsif (defined $value) { } 0 0 0
187 0 ▶ return unless @$value;
Branch: 1 branch
BranchTrueFalseTotal
unless @$value 0 0 0
188 return join(
189 ( $var->{ explode } ? $join : ',' ),
190 0 ▶ map { _quote( $_, $safe ) } @$value
Branch: 1 branch
BranchTrueFalseTotal
$var->{'explode'} ? : 0 0 0
191 );
192 }
193 elsif ( ref $value eq 'HASH' ) {
194 return join(
195 ( $var->{ explode } ? $join : ',' ),
196 map {
197 0 ▶ _quote( $_, $safe )
Branch: 1 branch
BranchTrueFalseTotal
$var->{'explode'} ? : 0 0 0
198 . ( $var->{ explode } ? '=' : ',' )
199 0 ▶ . _quote( $value->{ $_ }, $safe )
Branch: 1 branch
BranchTrueFalseTotal
$var->{'explode'} ? : 0 0 0
200 } sort keys %$value
201 );
202 }
203 elsif ( defined $value ) {
204 return _quote(
205 0 ▶ substr( $value, 0, $var->{ prefix } || length( $value ) ),
Condition0%0/3
$var->{'prefix'} || length $value
l!l&&r!l&&!r
000
MC/DC0%0/2
$var->{'prefix'} || length $value
$var->{'prefix'} length $value
Truth table0%0/3
A || B
A= $var->{'prefix'}B= length $value
ABresult
1X1
011
000
206 $safe );
207 }
208  
209 0 return;
210 }
211  
212 sub _study {
213 10 ▶ my ( $self ) = @_;
Subroutine _study: called CC=4 SCAR=13.9
214 10 ▶ my @hunks = grep { defined && length } split /(\{.+?\})/, $self->template;
Branch: 1 branch
BranchTrueFalseTotal
defined $_ and length $_ 34 0 34
215 10 my $pos = 1;
216 10 for ( @hunks ) {
217 34 ▶ next unless /^\{(.+?)\}$/;
Branch: 1 branch
BranchTrueFalseTotal
unless /^\{(.+?)\}$/ 20 14 34
218 14 $_ = $self->_compile_expansion( $1, $pos++ );
219 }
220 10 $self->{ studied } = \@hunks;
221 }
222  
223 sub _compile_expansion {
224 14 ▶ my ( $self, $str, $pos ) = @_;
Subroutine _compile_expansion: called CC=14 SCAR=31.0
225  
226 14 my %exp = ( op => '', vars => [], str => $str );
227 14 ▶ if ( $str =~ /^([+#.\/;?&|!\@])(.+)/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($str =~ m[^([+#./;?&|!\@])(.+)]) 0 14 14
228 0 $exp{ op } = $1;
229 0 $exp{ str } = $2;
230 }
231  
232 14 ▶ $exp{ safe } = $RESERVED if $exp{ op } =~ m{[+#]};
Branch: 1 branch
BranchTrueFalseTotal
if $exp{"op"} =~ /[+#]/ 0 14 14
233  
234 14 for my $varspec ( split( ',', delete $exp{ str } ) ) {
235 14 my %var = ( name => $varspec );
236 14 ▶ if ( $varspec =~ /=/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($varspec =~ /=/) 0 14 14
237 0 @var{ 'name', 'default' } = split( /=/, $varspec, 2 );
238 }
239 14 ▶ if ( $var{ name } =~ s{\*$}{} ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($var{'name'} =~ s/\*$//) { } 0 14 14
elsif ($var{'name'} =~ /:/) { } 0 14 14
240 0 $var{ explode } = 1;
241 }
242 elsif ( $var{ name } =~ /:/ ) {
243 0 @var{ 'name', 'prefix' } = split( /:/, $var{ name }, 2 );
244 0 ▶ if ( $var{ prefix } =~ m{[^0-9]} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($var{"prefix"} =~ /[^0-9]/) 0 0 0
245 0 die 'Non-numeric prefix specified';
246 }
247 }
248  
249 # remove "optional" flag (for opensearch compatibility)
250 14 $var{ name } =~ s{\?$}{};
251 14 $self->{ _vars }->{ $var{ name } } = $pos;
252  
253 14 push @{ $exp{ vars } }, \%var;
254 }
255  
256 14 my $join = $exp{ op };
257 14 my $start = $exp{ op };
258  
259 14 ▶ if ( $exp{ op } eq '+' ) {
Branch: 5 branches
BranchTrueFalseTotal
if ($exp{'op'} eq '+') { } 0 14 14
elsif ($exp{'op'} eq '#') { } 0 14 14
elsif ($exp{'op'} eq '?') { } 0 14 14
elsif ($exp{'op'} eq '&') { } 0 14 14
elsif ($exp{'op'} eq '') { } 14 0 14
260 0 $start = '';
261 0 $join = ',';
262 }
263 elsif ( $exp{ op } eq '#' ) {
264 0 $join = ',';
265 }
266 elsif ( $exp{ op } eq '?' ) {
267 0 $join = '&';
268 }
269 elsif ( $exp{ op } eq '&' ) {
270 0 $join = '&';
271 }
272 elsif ( $exp{ op } eq '' ) {
273 14 $join = ',';
274 }
275  
276 14 ▶ if ( !exists $TOSTRING{ $exp{ op } } ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $TOSTRING{$exp{"op"}}) 0 14 14
277 0 die 'Invalid operation "' . $exp{ op } . '"';
278 }
279  
280 return sub {
281 16 ▶ my $variables = shift;
Subroutine __ANON__: called CC=7 SCAR=19.5
282  
283 16 my @return;
284 16 for my $var ( @{ $exp{ vars } } ) {
285 16 my $value;
286 16 ▶ if ( exists $variables->{ $var->{ name } } ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $$variables{$var->{"name"}}) 14 2 16
287 14 $value = $variables->{ $var->{ name } };
288 }
289 16 ▶ $value = $var->{ default } if !defined $value;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $value 2 14 16
290  
291 16 ▶ next unless defined $value;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $value 2 14 16
292  
293 14 my $expand = $TOSTRING{ $exp{ op } }->( $var, $value, \%exp );
294  
295 14 ▶ push @return, $expand if defined $expand;
Branch: 1 branch
BranchTrueFalseTotal
if defined $expand 14 0 14
296 }
297  
298 16 ▶ return $start . join( $join, @return ) if @return;
Branch: 1 branch
BranchTrueFalseTotal
if @return 14 2 16
299 2 return '';
300 14 };
301 }
302  
303 sub template {
304 17 ▶ my $self = shift;
Subroutine template: called CC=3 SCAR=11.0
Pod documented
305 17 my $templ = shift;
306  
307 # Update template
308 17 ▶ if ( defined $templ && $templ ne $self->{ template } ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $templ and $templ ne $self->{"template"}) 2 15 17
Condition66%2/3
defined $templ and $templ ne $self->{"template"}
!ll&&!rl&&r
1502
MC/DC50%1/2
defined $templ and $templ ne $self->{"template"}
defined $templ $templ ne $self->{"template"}
Truth table66%2/3
A and B
A= defined $templB= $templ ne $self->{"template"}
ABresult
0X0
100
111
309 2 $self->{ template } = $templ;
310 2 $self->{ _vars } = {};
311 2 $self->_study;
312 2 return $self;
313 }
314  
315 15 return $self->{ template };
316 }
317  
318 sub variables {
319 4 ▶ my @vars = sort {$_[ 0 ]->{ _vars }->{ $a } <=> $_[ 0 ]->{ _vars }->{ $b } } keys %{ $_[ 0 ]->{ _vars } };
Subroutine variables: called CC=1 SCAR=0.0
Pod documented
320 4 return @vars;
321 }
322  
323 sub expansions {
324 0 ▶ my $self = shift;
Subroutine expansions: not called CC=1 SCAR=6.9
Pod documented
325 0 return grep { ref } @{ $self->{ studied } };
326 }
327  
328 sub process {
329 10 ▶ my $self = shift;
Subroutine process: called CC=1 SCAR=0.0
Pod documented
330 10 return URI->new( $self->process_to_string( @_ ) );
331 }
332  
333 sub process_to_string {
334 11 ▶ my $self = shift;
Subroutine process_to_string: called CC=4 SCAR=13.9
Pod documented
335 11 ▶ my $arg = @_ == 1 ? $_[ 0 ] : { @_ };
Branch: 1 branch
BranchTrueFalseTotal
@_ == 1 ? : 0 11 11
336 11 my $str = '';
337  
338 11 for my $hunk ( @{ $self->{ studied } } ) {
339 40 ▶ if ( !ref $hunk ) { $str .= $hunk; next; }
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $hunk) 24 16 40
340  
341 16 $str .= $hunk->( $arg );
342 }
343  
344 11 return $str;
345 }
346  
347 3 1;
348  
349 __END__
↑ Summary