blib/lib/URI/Template.pm

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