PackageURL.pm

filter: Statementstmt 95.4% 125 / 131 Branchbran 91.6% 55 / 60 Conditioncond 70.0% 7 / 10 MC/DCmcdc 50.0% 3 / 6 Subroutinesub 87.5% 21 / 24 Podpod 92.8% 13 / 14 totaltotal 91.4% 224 / 245 CC 37 SCAR 24.1
CC
37
Cov
93%
new
22.0
from_string
11.4
to_string
6.0
CRAP 11.2
↑ Summary PackageURL/Util.pm »
1 package URI::PackageURL;
2  
3 5 ▶use feature ':5.10';
Subroutine BEGIN: called CC=1 SCAR=0.0
4 5 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 5 ▶use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 5 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 5 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 5 ▶use Exporter qw(import);
Subroutine BEGIN: called CC=1 SCAR=0.0
10 5 ▶use URI::PackageURL::Util qw(purl_to_urls);
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 5 ▶use overload '""' => 'to_string', fallback => 1;
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 5 our $VERSION = '2.00';
15 5 our @EXPORT = qw(encode_purl decode_purl);
16  
17 5 my $PURL_REGEXP = qr{^pkg:[A-Za-z\\.\\-\\+][A-Za-z0-9\\.\\-\\+]*/.+};
18  
19 sub new {
20  
21 61 ▶ my ($class, %params) = @_;
Subroutine new: called CC=22 SCAR=30.9
Pod documented
22  
23 61 ▶ my $scheme = delete $params{scheme} // 'pkg';
Condition100%2/2
delete $params{"scheme"} // "pkg"
l!l
1447
MC/DC100%1/1
delete $params{"scheme"} // "pkg"
delete $params{"scheme"}
Truth table100%2/2
A
A= delete $params{"scheme"}
Aresult
11
00
24 61 ▶ my $type = delete $params{type} or Carp::croak "Invalid PackageURL: 'type' component is required";
Branch: 1 branch
BranchTrueFalseTotal
my $type = delete $params{"type"} or Carp::croak("Invalid PackageURL: 'type' component is required") 2 59 61
25 59 my $namespace = delete $params{namespace};
26 59 ▶ my $name = delete $params{name} or Carp::croak "Invalid PackageURL: 'name' component is required";
Branch: 1 branch
BranchTrueFalseTotal
my $name = delete $params{"name"} or Carp::croak("Invalid PackageURL: 'name' component is required") 4 55 59
27 55 my $version = delete $params{version};
28 55 ▶ my $qualifiers = delete $params{qualifiers} // {};
Condition100%2/2
delete $params{"qualifiers"} // {}
l!l
2035
MC/DC100%1/1
delete $params{"qualifiers"} // {}
delete $params{"qualifiers"}
Truth table100%2/2
A
A= delete $params{"qualifiers"}
Aresult
11
00
29 55 my $subpath = delete $params{subpath};
30  
31 55 $type = lc $type;
32  
33 55 ▶ if (grep { $_ eq $type } qw(alpm apk bitbucket composer deb github gitlab hex npm oci pypi)) {
Branch: 1 branch
BranchTrueFalseTotal
if (grep {$_ eq $type;} "alpm", "apk", "bitbucket", "composer", "deb", "github", "gitlab", "hex", "npm", "oci", "pypi") 13 42 55
34 13 $name = lc $name;
35 }
36  
37 55 ▶ if ($namespace) {
Branch: 1 branch
BranchTrueFalseTotal
if ($namespace) 36 19 55
38 36 ▶ if (grep { $_ eq $type } qw(alpm apk bitbucket composer deb github gitlab golang hex rpm)) {
Branch: 1 branch
BranchTrueFalseTotal
if (grep {$_ eq $type;} "alpm", "apk", "bitbucket", "composer", "deb", "github", "gitlab", "golang", "hex", "rpm") 12 24 36
39 12 $namespace = lc $namespace;
40 }
41 }
42  
43 55 foreach my $qualifier (keys %{$qualifiers}) {
44 31 ▶ Carp::croak "Invalid PackageURL: '$qualifier' is not a valid qualifier" if ($qualifier =~ /\s/);
Branch: 1 branch
BranchTrueFalseTotal
if $qualifier =~ /\s/ 1 30 31
45 }
46  
47 54 ▶ $name =~ s/_/-/g if $type eq 'pypi';
Branch: 1 branch
BranchTrueFalseTotal
if $type eq "pypi" 2 52 54
48 54 ▶ $name =~ s/::/-/g if $type eq 'cpan';
Branch: 1 branch
BranchTrueFalseTotal
if $type eq "cpan" 3 51 54
49  
50 54 ▶ if ($type eq 'swift') {
Branch: 1 branch
BranchTrueFalseTotal
if ($type eq "swift") 3 51 54
51 3 ▶ Carp::croak "Invalid PackageURL: Swift 'version' is required" unless defined $version;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $version 1 2 3
52 2 ▶ Carp::croak "Invalid PackageURL: Swift 'namespace' is required" unless defined $namespace;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $namespace 1 1 2
53 }
54  
55 52 ▶ if ($type eq 'cran') {
Branch: 1 branch
BranchTrueFalseTotal
if ($type eq "cran") 2 50 52
56 2 ▶ Carp::croak "Invalid PackageURL: Cran 'version' is required" unless defined $version;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $version 1 1 2
57 }
58  
59 51 ▶ if ($type eq 'conan') {
Branch: 1 branch
BranchTrueFalseTotal
if ($type eq "conan") 4 47 51
60  
61 4 ▶ if ($namespace && $namespace ne '') {
Branch: 1 branch
BranchTrueFalseTotal
if ($namespace and $namespace ne '') { } 2 2 4
Condition66%2/3
$namespace and $namespace ne ""
!ll&&!rl&&r
202
MC/DC50%1/2
$namespace and $namespace ne ""
$namespace $namespace ne ""
Truth table66%2/3
A and B
A= $namespaceB= $namespace ne ""
ABresult
0X0
100
111
62 2 ▶ if (!defined $qualifiers->{channel}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $qualifiers->{"channel"}) 1 1 2
63 1 Carp::croak "Invalid PackageURL: Conan 'channel' qualifier does not exist for namespace '$namespace'";
64 }
65 }
66 else {
67 2 ▶ if (defined $qualifiers->{channel}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $qualifiers->{"channel"}) 1 1 2
68 1 Carp::croak "Invalid PackageURL: Conan 'namespace' does not exist for channel '$qualifiers->{channel}'";
69 }
70 }
71  
72 }
73  
74 49 my $self = {
75 scheme => $scheme,
76 type => $type,
77 namespace => $namespace,
78 name => $name,
79 version => $version,
80 qualifiers => $qualifiers,
81 subpath => $subpath
82 };
83  
84 49 return bless $self, $class;
85  
86 }
87  
88 0 ▶sub scheme { shift->{scheme} }
Subroutine scheme: not called CC=1 SCAR=6.9
Pod documented
89 63 ▶sub type { shift->{type} }
Subroutine type: called CC=1 SCAR=0.0
Pod documented
90 78 ▶sub namespace { shift->{namespace} }
Subroutine namespace: called CC=1 SCAR=0.0
Pod documented
91 53 ▶sub name { shift->{name} }
Subroutine name: called CC=1 SCAR=0.0
Pod documented
92 86 ▶sub version { shift->{version} }
Subroutine version: called CC=1 SCAR=0.0
Pod documented
93 56 ▶sub qualifiers { shift->{qualifiers} }
Subroutine qualifiers: called CC=1 SCAR=0.0
Pod documented
94 43 ▶sub subpath { shift->{subpath} }
Subroutine subpath: called CC=1 SCAR=0.0
Pod documented
95  
96 sub encode_purl {
97 1 ▶ return URI::PackageURL->new(@_)->to_string;
Subroutine encode_purl: called CC=1 SCAR=0.0
Pod documented
98 }
99  
100 sub decode_purl {
101 4 ▶ return URI::PackageURL->from_string(shift);
Subroutine decode_purl: called CC=1 SCAR=0.0
Pod documented
102 }
103  
104 sub from_string {
105  
106 14 ▶ my ($class, $string) = @_;
Subroutine from_string: called CC=11 SCAR=24.4
Pod undocumented
107  
108 14 ▶ if ($string !~ /$PURL_REGEXP/) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($string =~ /$PURL_REGEXP/) 0 14 14
109 0 Carp::croak 'Malformed PackageURL string';
110 }
111  
112 14 my %components = ();
113  
114  
115 # Split the purl string once from right on '#'
116 # The left side is the remainder
117 # Strip the right side from leading and trailing '/'
118 # Split this on '/'
119 # Discard any empty string segment from that split
120 # Discard any '.' or '..' segment from that split
121 # Percent-decode each segment
122 # UTF-8-decode each segment if needed in your programming language
123 # Join segments back with a '/'
124 # This is the subpath
125  
126 14 my @s1 = split('#', $string);
127  
128 14 ▶ if ($s1[1]) {
Branch: 1 branch
BranchTrueFalseTotal
if ($s1[1]) 1 13 14
129 1 $s1[1] =~ s{(^\/|\/$)}{};
130 1 ▶ my @subpath = map { _url_decode($_) } grep { $_ ne '' && $_ ne '.' && $_ ne '..' } split /\//, $s1[1];
Branch: 1 branch
BranchTrueFalseTotal
$_ ne "" and $_ ne "." and $_ ne ".." 3 0 3
Condition33%1/3
$_ ne "" and $_ ne "."
!ll&&!rl&&r
003
MC/DC0%0/2
$_ ne "" and $_ ne "."
$_ ne "" $_ ne "."
Truth table33%1/3
A and B
A= $_ ne ""B= $_ ne "."
ABresult
0X0
100
111
131 1 $components{subpath} = join '/', @subpath;
132 }
133  
134 # Split the remainder once from right on '?'
135 # The left side is the remainder
136 # The right side is the qualifiers string
137 # Split the qualifiers on '&'. Each part is a key=value pair
138 # For each pair, split the key=value once from left on '=':
139 # The key is the lowercase left side
140 # The value is the percent-decoded right side
141 # UTF-8-decode the value if needed in your programming language
142 # Discard any key/value pairs where the value is empty
143 # If the key is checksums, split the value on ',' to create a list of checksums
144 # This list of key/value is the qualifiers object
145  
146 14 my @s2 = split(/\?/, $s1[0]);
147  
148 14 ▶ if ($s2[1]) {
Branch: 1 branch
BranchTrueFalseTotal
if ($s2[1]) 4 10 14
149  
150 4 my @qualifiers = split('&', $s2[1]);
151  
152 4 foreach my $qualifier (@qualifiers) {
153  
154 5 my ($key, $value) = split('=', $qualifier);
155 5 $value = _url_decode($value);
156  
157 5 ▶ if ($key eq 'checksums') {
Branch: 1 branch
BranchTrueFalseTotal
if ($key eq "checksums") 0 5 5
158 0 $value = [split(',', $value)];
159 }
160  
161 5 $components{qualifiers}->{$key} = $value;
162  
163 }
164  
165 }
166  
167  
168 # Split the remainder once from left on ':'
169 # The left side lowercased is the scheme
170 # The right side is the remainder
171  
172 14 my @s3 = split(':', $s2[0], 2);
173 14 $components{scheme} = lc $s3[0];
174  
175  
176 # Strip the remainder from leading and trailing '/'
177 # Split this once from left on '/'
178 # The left side lowercased is the type
179 # The right side is the remainder
180  
181 14 $s3[1] =~ s{(^\/|\/$)}{};
182 14 my @s4 = split('/', $s3[1], 2);
183 14 $components{type} = lc $s4[0];
184  
185  
186 # Split the remainder once from right on '@'
187 # The left side is the remainder
188 # Percent-decode the right side. This is the version.
189 # UTF-8-decode the version if needed in your programming language
190 # This is the version
191  
192 14 my @s5 = split('@', $s4[1]);
193 14 ▶ $components{version} = _url_decode($s5[1]) if ($s5[1]);
Branch: 1 branch
BranchTrueFalseTotal
if $s5[1] 14 0 14
194  
195  
196 # Split the remainder once from right on '/'
197 # The left side is the remainder
198 # Percent-decode the right side. This is the name
199 # UTF-8-decode this name if needed in your programming language
200 # Apply type-specific normalization to the name if needed
201 # This is the name
202  
203 14 my @s6 = split('/', $s5[0], 2);
204 14 ▶ $components{name} = (scalar @s6 > 1) ? _url_decode($s6[1]) : _url_decode($s6[0]);
Branch: 1 branch
BranchTrueFalseTotal
scalar @s6 > 1 ? : 10 4 14
205  
206  
207 # Split the remainder on '/'
208 # Discard any empty segment from that split
209 # Percent-decode each segment
210 # UTF-8-decode the each segment if needed in your programming language
211 # Apply type-specific normalization to each segment if needed
212 # Join segments back with a '/'
213 # This is the namespace
214  
215 14 ▶ if (scalar @s6 > 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @s6 > 1) 10 4 14
216 10 my @s7 = split('/', $s6[0]);
217 10 $components{namespace} = join '/', map { _url_decode($_) } @s7;
218 }
219  
220 14 return $class->new(%components);
221  
222 }
223  
224 sub to_string {
225  
226 39 ▶ my $self = shift;
Subroutine to_string: called CC=6 SCAR=17.9
Pod documented
227  
228 39 my @purl = ('pkg', ':', $self->type, '/');
229  
230 # Namespace
231 39 ▶ if ($self->namespace) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->namespace) 28 11 39
232  
233 28 my @ns = map { _url_encode($_) } split(/\//, $self->namespace);
234 28 push @purl, (join('/', @ns), '/');
235  
236 }
237  
238 # Name
239 39 push @purl, _url_encode($self->name);
240  
241 # Version
242 39 ▶ push @purl, ('@', _url_encode($self->version)) if ($self->version);
Branch: 1 branch
BranchTrueFalseTotal
if $self->version 34 5 39
243  
244 # Qualifiers
245 39 ▶ if (my $qualifiers = $self->qualifiers) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $qualifiers = $self->qualifiers) 39 0 39
246  
247 39 my @qualifiers = map { sprintf('%s=%s', $_, _url_encode($qualifiers->{$_})) } sort keys %{$qualifiers};
248 39 ▶ push @purl, ('?', join('&', @qualifiers)) if (@qualifiers);
Branch: 1 branch
BranchTrueFalseTotal
if @qualifiers 16 23 39
249  
250 }
251  
252 # Subpath
253 39 ▶ push @purl, ('#', $self->subpath) if ($self->subpath);
Branch: 1 branch
BranchTrueFalseTotal
if $self->subpath 3 36 39
254  
255 39 return join '', @purl;
256  
257 }
258  
259 sub to_urls {
260 0 ▶ purl_to_urls(shift);
Subroutine to_urls: not called CC=1 SCAR=6.9
Pod documented
261 }
262  
263 sub TO_JSON {
264  
265 0 ▶ my $self = shift;
Subroutine TO_JSON: not called CC=1 SCAR=6.9
Pod documented
266  
267 return {
268 0 type => $self->type,
269 name => $self->name,
270 version => $self->version,
271 namespace => $self->namespace,
272 qualifiers => $self->qualifiers,
273 subpath => $self->subpath,
274 };
275  
276 }
277  
278 sub _url_encode {
279  
280 129 ▶ my $string = shift;
Subroutine _url_encode: called CC=1 SCAR=0.0
281  
282 # RFC-3986 (but exclude "/" and ":")
283 129 $string =~ s/([^A-Za-z0-9\-._~\/:])/sprintf '%%%02X', ord $1/ge;
284 129 return $string;
285  
286 }
287  
288 sub _url_decode {
289 46 ▶ my $string = shift;
Subroutine _url_decode: called CC=1 SCAR=0.0
290 46 $string =~ s/%([0-9a-fA-F]{2})/chr hex $1/ge;
291 46 return $string;
292 }
293  
294 5 1;
295  
296 __END__
↑ Summary PackageURL/Util.pm »