CPAN/Meta/Converter.pm

filter: Statementstmt 93.0% 401 / 431 Branchbran 80.7% 210 / 260 Conditioncond 64.2% 72 / 112 MC/DCmcdc 47.8% 33 / 69 Subroutinesub 95.7% 67 / 70 Podpod 100% 3 / 3 totaltotal 83.1% 786 / 945 CC 189 SCAR 20.6
CC
189
Cov
85%
_version_map
22.2
_downgrade_license
19.0
convert
16.2
CRAP 7.8
« CPAN/Meta/Prereqs.pm ↑ Summary CPAN/Meta/Merge.pm »
1 22 ▶use 5.008001;
Subroutine BEGIN: called CC=1 SCAR=0.0
2 22 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 22 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 package CPAN::Meta::Converter;
5  
6 22 our $VERSION = '2.150015';
7  
8 #pod =head1 SYNOPSIS
9 #pod
10 #pod my $struct = Parse::CPAN::Meta->load_file('META.json');
11 #pod my $cmc = CPAN::Meta::Converter->new( $struct );
12 #pod
13 #pod my $new_struct = $cmc->convert( version => "2" );
14 #pod
15 #pod =head1 DESCRIPTION
16 #pod
17 #pod This module converts CPAN Meta structures from one form to another. The
18 #pod primary use is to convert older structures to the most modern version of
19 #pod the specification, but other transformations may be implemented in the
20 #pod future as needed. (E.g. stripping all custom fields or stripping all
21 #pod optional fields.)
22 #pod
23 #pod =cut
24  
25 22 ▶use CPAN::Meta::Validator;
Subroutine BEGIN: called CC=1 SCAR=0.0
26 22 ▶use CPAN::Meta::Requirements 2.145;
Subroutine BEGIN: called CC=1 SCAR=0.0
27 22 ▶use Parse::CPAN::Meta 1.4400 ();
Subroutine BEGIN: called CC=1 SCAR=0.0
28  
29 # To help ExtUtils::MakeMaker bootstrap CPAN::Meta::Requirements on perls
30 # before 5.10, we fall back to the EUMM bundled compatibility version module if
31 # that's the only thing available. This shouldn't ever happen in a normal CPAN
32 # install of CPAN::Meta::Requirements, as version.pm will be picked up from
33 # prereqs and be available at runtime.
34  
35 22 ▶BEGIN {
Subroutine BEGIN: called CC=3 SCAR=12.7
36 22 ▶ eval "use version ()"; ## no critic
Subroutine BEGIN: called CC=1 SCAR=0.0
37 22 ▶ if ( my $err = $@ ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $err = $@) 0 22 22
38 0 ▶ eval "use ExtUtils::MakeMaker::version" or die $err; ## no critic
Branch: 1 branch
BranchTrueFalseTotal
eval "use ExtUtils::MakeMaker::version" or die $err 0 0 0
39 }
40 }
41  
42 # Perl 5.10.0 didn't have "is_qv" in version.pm
43 0 ▶*_is_qv = version->can('is_qv') ? sub { $_[0]->is_qv } : sub { exists $_[0]->{qv} };
Subroutine __ANON__: not called CC=1 SCAR=2.0
Subroutine __ANON__: not called CC=1 SCAR=2.0
Branch: 1 branch
BranchTrueFalseTotal
'version'->can('is_qv') ? : 22 0 22
44  
45 # We limit cloning to a maximum depth to bail out on circular data
46 # structures. While actual cycle detection might be technically better,
47 # we expect circularity in META data structures to be rare and generally
48 # the result of user error. Therefore, a depth counter is lower overhead.
49 22 our $DCLONE_MAXDEPTH = 1024;
50 22 our $_CLONE_DEPTH;
51  
52 21827 ▶sub _dclone {
Subroutine _dclone: called CC=9 SCAR=22.4
53 21827 my ( $ref ) = @_;
54 21827 ▶ return $ref unless my $reftype = ref $ref;
Branch: 1 branch
BranchTrueFalseTotal
unless my $reftype = ref $ref 16467 5360 21827
55  
56 5360 ▶ local $_CLONE_DEPTH = defined $_CLONE_DEPTH ? $_CLONE_DEPTH - 1 : $DCLONE_MAXDEPTH;
Branch: 1 branch
BranchTrueFalseTotal
defined $CPAN::Meta::Converter::_CLONE_DEPTH ? : 5021 339 5360
57 5360 ▶ die "Depth Limit $DCLONE_MAXDEPTH Exceeded" if $_CLONE_DEPTH == 0;
Branch: 1 branch
BranchTrueFalseTotal
if $CPAN::Meta::Converter::_CLONE_DEPTH == 0 0 5360 5360
58  
59 5360 ▶ return [ map { _dclone( $_ ) } @{$ref} ] if 'ARRAY' eq $reftype;
Branch: 1 branch
BranchTrueFalseTotal
if "ARRAY" eq $reftype 729 4631 5360
60 4631 ▶ return { map { $_ => _dclone( $ref->{$_} ) } keys %{$ref} } if 'HASH' eq $reftype;
Branch: 1 branch
BranchTrueFalseTotal
if "HASH" eq $reftype 4597 34 4631
61  
62 34 ▶ if ( 'SCALAR' eq $reftype ) {
Branch: 1 branch
BranchTrueFalseTotal
if ("SCALAR" eq $reftype) 0 34 34
63 0 my $new = _dclone(${$ref});
64 0 return \$new;
65 }
66  
67 # We can't know if TO_JSON gives us cloned data, so refs must recurse
68 34 ▶ if ( eval { $ref->can('TO_JSON') } ) {
Branch: 1 branch
BranchTrueFalseTotal
if (eval { do { $ref->can("TO_JSON") } }) 32 2 34
69 32 my $data = $ref->TO_JSON;
70 32 ▶ return ref $data ? _dclone( $data ) : $data;
Branch: 1 branch
BranchTrueFalseTotal
ref $data ? : 32 0 32
71 }
72  
73 # Just stringify everything else
74 2 return "$ref";
75 }
76  
77 22 my %known_specs = (
78 '2' => 'http://search.cpan.org/perldoc?CPAN::Meta::Spec',
79 '1.4' => 'http://module-build.sourceforge.net/META-spec-v1.4.html',
80 '1.3' => 'http://module-build.sourceforge.net/META-spec-v1.3.html',
81 '1.2' => 'http://module-build.sourceforge.net/META-spec-v1.2.html',
82 '1.1' => 'http://module-build.sourceforge.net/META-spec-v1.1.html',
83 '1.0' => 'http://module-build.sourceforge.net/META-spec-v1.0.html'
84 );
85  
86 22 my @spec_list = sort { $a <=> $b } keys %known_specs;
87 22 my ($LOWEST, $HIGHEST) = @spec_list[0,-1];
88  
89 #--------------------------------------------------------------------------#
90 # converters
91 #
92 # called as $converter->($element, $field_name, $full_meta, $to_version)
93 #
94 # defined return value used for field
95 # undef return value means field is skipped
96 #--------------------------------------------------------------------------#
97  
98 6870 ▶sub _keep { $_[0] }
Subroutine _keep: called CC=1 SCAR=0.0
99  
100 566 ▶sub _keep_or_one { defined($_[0]) ? $_[0] : 1 }
Subroutine _keep_or_one: called CC=2 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
defined $_[0] ? : 406 160 566
101  
102 0 ▶sub _keep_or_zero { defined($_[0]) ? $_[0] : 0 }
Subroutine _keep_or_zero: not called CC=2 SCAR=17.9
Branch: 1 branch
BranchTrueFalseTotal
defined $_[0] ? : 0 0 0
103  
104 461 ▶sub _keep_or_unknown { defined($_[0]) && length($_[0]) ? $_[0] : "unknown" }
Subroutine _keep_or_unknown: called CC=3 SCAR=11.0
Branch: 1 branch
BranchTrueFalseTotal
defined $_[0] && length $_[0] ? : 398 63 461
Condition100%3/3
defined $_[0] and length $_[0]
!ll&&!rl&&r
576398
MC/DC100%2/2
defined $_[0] and length $_[0]
defined $_[0] length $_[0]
Truth table100%3/3
A and B
A= defined $_[0]B= length $_[0]
ABresult
0X0
100
111
105  
106 523 ▶sub _generated_by {
Subroutine _generated_by: called CC=5 SCAR=16.2
107 523 my $gen = shift;
108 523 ▶ my $sig = __PACKAGE__ . " version " . (__PACKAGE__->VERSION || "<dev>");
Condition50%1/2
__PACKAGE__->VERSION || '<dev>'
l!l
5230
MC/DC0%0/1
__PACKAGE__->VERSION || '<dev>'
__PACKAGE__->VERSION
Truth table50%1/2
A
A= __PACKAGE__->VERSION
Aresult
11
00
109  
110 523 ▶ return $sig unless defined $gen and length $gen;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $gen and length $gen 5 518 523
Condition66%2/3
defined $gen and length $gen
!ll&&!rl&&r
50518
MC/DC50%1/2
defined $gen and length $gen
defined $gen length $gen
Truth table66%2/3
A and B
A= defined $genB= length $gen
ABresult
0X0
100
111
111 518 ▶ return $gen if $gen =~ /\Q$sig/;
Branch: 1 branch
BranchTrueFalseTotal
if $gen =~ /\Q$sig\E/ 287 231 518
112 231 return "$gen, $sig";
113 }
114  
115 910 ▶sub _listify { ! defined $_[0] ? undef : ref $_[0] eq 'ARRAY' ? $_[0] : [$_[0]] }
Subroutine _listify: called CC=3 SCAR=11.0
Branch: 2 branches
BranchTrueFalseTotal
!defined($_[0]) ? : 320 590 910
ref $_[0] eq 'ARRAY' ? : 542 48 590
116  
117 246 ▶sub _prefix_custom {
Subroutine _prefix_custom: called CC=1 SCAR=0.0
118 246 my $key = shift;
119 246 $key =~ s/^(?!x_) # Unless it already starts with x_
120 (?:x-?)? # Remove leading x- or x (if present)
121 /x_/ix; # and prepend x_
122 246 return $key;
123 }
124  
125 6 ▶sub _ucfirst_custom {
Subroutine _ucfirst_custom: called CC=2 SCAR=7.1
126 6 my $key = shift;
127 6 ▶ $key = ucfirst $key unless $key =~ /[A-Z]/;
Branch: 1 branch
BranchTrueFalseTotal
unless $key =~ /[A-Z]/ 0 6 6
128 6 return $key;
129 }
130  
131 6 ▶sub _no_prefix_ucfirst_custom {
Subroutine _no_prefix_ucfirst_custom: called CC=1 SCAR=0.0
132 6 my $key = shift;
133 6 $key =~ s/^x_//;
134 6 return _ucfirst_custom($key);
135 }
136  
137 541 ▶sub _change_meta_spec {
Subroutine _change_meta_spec: called CC=1 SCAR=0.0
138 541 my ($element, undef, undef, $version) = @_;
139 return {
140 version => $version,
141 541 url => $known_specs{$version},
142 };
143 }
144  
145 22 my @open_source = (
146 'perl',
147 'gpl',
148 'apache',
149 'artistic',
150 'artistic_2',
151 'lgpl',
152 'bsd',
153 'gpl',
154 'mit',
155 'mozilla',
156 'open_source',
157 );
158  
159 22 my %is_open_source = map {; $_ => 1 } @open_source;
160  
161 22 my @valid_licenses_1 = (
162 @open_source,
163 'unrestricted',
164 'restrictive',
165 'unknown',
166 );
167  
168 my %license_map_1 = (
169 22 ( map { $_ => $_ } @valid_licenses_1 ),
170 artistic2 => 'artistic_2',
171 );
172  
173 375 ▶sub _license_1 {
Subroutine _license_1: called CC=3 SCAR=11.3
174 375 my ($element) = @_;
175 375 ▶ return 'unknown' unless defined $element;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $element 24 351 375
176 351 ▶ if ( $license_map_1{lc $element} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($license_map_1{lc $element}) { } 351 0 351
177 351 return $license_map_1{lc $element};
178 }
179 else {
180 0 return 'unknown';
181 }
182 }
183  
184 22 my @valid_licenses_2 = qw(
185 agpl_3
186 apache_1_1
187 apache_2_0
188 artistic_1
189 artistic_2
190 bsd
191 freebsd
192 gfdl_1_2
193 gfdl_1_3
194 gpl_1
195 gpl_2
196 gpl_3
197 lgpl_2_1
198 lgpl_3_0
199 mit
200 mozilla_1_0
201 mozilla_1_1
202 openssl
203 perl_5
204 qpl_1_0
205 ssleay
206 sun
207 zlib
208 open_source
209 restricted
210 unrestricted
211 unknown
212 );
213  
214 # The "old" values were defined by Module::Build, and were often vague. I have
215 # made the decisions below based on reading Module::Build::API and how clearly
216 # it specifies the version of the license.
217 my %license_map_2 = (
218 22 (map { $_ => $_ } @valid_licenses_2),
219 apache => 'apache_2_0', # clearly stated as 2.0
220 artistic => 'artistic_1', # clearly stated as 1
221 artistic2 => 'artistic_2', # clearly stated as 2
222 gpl => 'open_source', # we don't know which GPL; punt
223 lgpl => 'open_source', # we don't know which LGPL; punt
224 mozilla => 'open_source', # we don't know which MPL; punt
225 perl => 'perl_5', # clearly Perl 5
226 restrictive => 'restricted',
227 );
228  
229 158 ▶sub _license_2 {
Subroutine _license_2: called CC=7 SCAR=19.7
230 158 my ($element) = @_;
231 158 ▶ return [ 'unknown' ] unless defined $element;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $element 20 138 158
232 138 ▶ $element = [ $element ] unless ref $element eq 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $element eq "ARRAY" 78 60 138
233 138 my @new_list;
234 138 for my $lic ( @$element ) {
235 143 ▶ next unless defined $lic;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $lic 0 143 143
236 143 ▶ if ( my $new = $license_map_2{lc $lic} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $new = $license_map_2{lc $lic}) 143 0 143
237 143 push @new_list, $new;
238 }
239 }
240 138 ▶ return @new_list ? \@new_list : [ 'unknown' ];
Branch: 1 branch
BranchTrueFalseTotal
@new_list ? : 138 0 138
241 }
242  
243 22 my %license_downgrade_map = qw(
244 agpl_3 open_source
245 apache_1_1 apache
246 apache_2_0 apache
247 artistic_1 artistic
248 artistic_2 artistic_2
249 bsd bsd
250 freebsd open_source
251 gfdl_1_2 open_source
252 gfdl_1_3 open_source
253 gpl_1 gpl
254 gpl_2 gpl
255 gpl_3 gpl
256 lgpl_2_1 lgpl
257 lgpl_3_0 lgpl
258 mit mit
259 mozilla_1_0 mozilla
260 mozilla_1_1 mozilla
261 openssl open_source
262 perl_5 perl
263 qpl_1_0 open_source
264 ssleay open_source
265 sun open_source
266 zlib open_source
267 open_source open_source
268 restricted restrictive
269 unrestricted unrestricted
270 unknown unknown
271 );
272  
273 33 ▶sub _downgrade_license {
Subroutine _downgrade_license: called CC=10 SCAR=29.4
274 33 my ($element) = @_;
275 33 ▶ if ( ! defined $element ) {
Branch: 3 branches
BranchTrueFalseTotal
if (not defined $element) { } 0 33 33
elsif (ref $element eq 'ARRAY') { } 33 0 33
elsif (not ref $element) { } 0 0 0
276 0 return "unknown";
277 }
278 elsif( ref $element eq 'ARRAY' ) {
279 33 ▶ if ( @$element > 1) {
Branch: 2 branches
BranchTrueFalseTotal
if (@$element > 1) { } 6 27 33
elsif (@$element == 1) { } 27 0 27
280 6 ▶ if (grep { !$is_open_source{ $license_downgrade_map{lc $_} || 'unknown' } } @$element) {
Branch: 1 branch
BranchTrueFalseTotal
if (grep {not $is_open_source{$license_downgrade_map{lc $_} or 'unknown'};} @$element) { } 0 6 6
Condition50%1/2
$license_downgrade_map{lc $_} || 'unknown'
l!l
120
MC/DC0%0/1
$license_downgrade_map{lc $_} || 'unknown'
$license_downgrade_map{lc $_}
Truth table50%1/2
A
A= $license_downgrade_map{lc $_}
Aresult
11
00
281 0 return 'unknown';
282 }
283 else {
284 6 return 'open_source';
285 }
286 }
287 elsif ( @$element == 1 ) {
288 27 ▶ return $license_downgrade_map{lc $element->[0]} || "unknown";
Condition100%2/2
$license_downgrade_map{lc $element->[0]} || 'unknown'
l!l
252
MC/DC100%1/1
$license_downgrade_map{lc $element->[0]} || 'unknown'
$license_downgrade_map{lc $element->[0]}
Truth table100%2/2
A
A= $license_downgrade_map{lc $element->[0]}
Aresult
11
00
289 }
290 }
291 elsif ( ! ref $element ) {
292 0 ▶ return $license_downgrade_map{lc $element} || "unknown";
Condition0%0/2
$license_downgrade_map{lc $element} || 'unknown'
l!l
00
MC/DC0%0/1
$license_downgrade_map{lc $element} || 'unknown'
$license_downgrade_map{lc $element}
Truth table0%0/2
A
A= $license_downgrade_map{lc $element}
Aresult
11
00
293 }
294 0 return "unknown";
295 }
296  
297 22 my $no_index_spec_1_2 = {
298 'file' => \&_listify,
299 'dir' => \&_listify,
300 'package' => \&_listify,
301 'namespace' => \&_listify,
302 };
303  
304 22 my $no_index_spec_1_3 = {
305 'file' => \&_listify,
306 'directory' => \&_listify,
307 'package' => \&_listify,
308 'namespace' => \&_listify,
309 };
310  
311 22 my $no_index_spec_2 = {
312 'file' => \&_listify,
313 'directory' => \&_listify,
314 'package' => \&_listify,
315 'namespace' => \&_listify,
316 ':custom' => \&_prefix_custom,
317 };
318  
319 94 ▶sub _no_index_1_2 {
Subroutine _no_index_1_2: called CC=7 SCAR=23.7
320 94 my (undef, undef, $meta) = @_;
321 94 ▶ my $no_index = $meta->{no_index} || $meta->{private};
Condition66%2/3
$meta->{'no_index'} || $meta->{'private'}
l!l&&r!l&&!r
21073
MC/DC50%1/2
$meta->{'no_index'} || $meta->{'private'}
$meta->{'no_index'} $meta->{'private'}
Truth table66%2/3
A || B
A= $meta->{'no_index'}B= $meta->{'private'}
ABresult
1X1
011
000
322 94 ▶ return unless $no_index;
Branch: 1 branch
BranchTrueFalseTotal
unless $no_index 73 21 94
323  
324 # cleanup wrong format
325 21 ▶ if ( ! ref $no_index ) {
Branch: 2 branches
BranchTrueFalseTotal
if (not ref $no_index) { } 0 21 21
elsif (ref $no_index eq 'ARRAY') { } 0 21 21
326 0 my $item = $no_index;
327 0 $no_index = { dir => [ $item ], file => [ $item ] };
328 }
329 elsif ( ref $no_index eq 'ARRAY' ) {
330 0 my $list = $no_index;
331 0 $no_index = { dir => [ @$list ], file => [ @$list ] };
332 }
333  
334 # common mistake: files -> file
335 21 ▶ if ( exists $no_index->{files} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $no_index->{"files"}) 0 21 21
336 0 $no_index->{file} = delete $no_index->{files};
337 }
338 # common mistake: modules -> module
339 21 ▶ if ( exists $no_index->{modules} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $no_index->{"modules"}) 0 21 21
340 0 $no_index->{module} = delete $no_index->{modules};
341 }
342 21 return _convert($no_index, $no_index_spec_1_2);
343 }
344  
345 367 ▶sub _no_index_directory {
Subroutine _no_index_directory: called CC=8 SCAR=22.9
346 367 my ($element, $key, $meta, $version) = @_;
347 367 ▶ return unless $element;
Branch: 1 branch
BranchTrueFalseTotal
unless $element 278 89 367
348  
349 # clean up wrong format
350 89 ▶ if ( ! ref $element ) {
Branch: 2 branches
BranchTrueFalseTotal
if (not ref $element) { } 0 89 89
elsif (ref $element eq 'ARRAY') { } 2 87 89
351 0 my $item = $element;
352 0 $element = { directory => [ $item ], file => [ $item ] };
353 }
354 elsif ( ref $element eq 'ARRAY' ) {
355 2 my $list = $element;
356 2 $element = { directory => [ @$list ], file => [ @$list ] };
357 }
358  
359 89 ▶ if ( exists $element->{dir} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $element->{"dir"}) 0 89 89
360 0 $element->{directory} = delete $element->{dir};
361 }
362 # common mistake: files -> file
363 89 ▶ if ( exists $element->{files} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $element->{"files"}) 0 89 89
364 0 $element->{file} = delete $element->{files};
365 }
366 # common mistake: modules -> module
367 89 ▶ if ( exists $element->{modules} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $element->{"modules"}) 0 89 89
368 0 $element->{module} = delete $element->{modules};
369 }
370 89 ▶ my $spec = $version == 2 ? $no_index_spec_2 : $no_index_spec_1_3;
Branch: 1 branch
BranchTrueFalseTotal
$version == 2 ? : 46 43 89
371 89 return _convert($element, $spec);
372 }
373  
374 16429 ▶sub _is_module_name {
Subroutine _is_module_name: called CC=3 SCAR=12.5
375 16429 my $mod = shift;
376 16429 ▶ return unless defined $mod && length $mod;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $mod and length $mod 0 16429 16429
Condition33%1/3
defined $mod and length $mod
!ll&&!rl&&r
0016429
MC/DC0%0/2
defined $mod and length $mod
defined $mod length $mod
Truth table33%1/3
A and B
A= defined $modB= length $mod
ABresult
0X0
100
111
377 16429 return $mod =~ m{^[A-Za-z][A-Za-z0-9_]*(?:::[A-Za-z0-9_]+)*$};
378 }
379  
380 1875 ▶sub _clean_version {
Subroutine _clean_version: called CC=6 SCAR=18.7
381 1875 my ($element) = @_;
382 1875 ▶ return 0 if ! defined $element;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $element 58 1817 1875
383  
384 1817 $element =~ s{^\s*}{};
385 1817 $element =~ s{\s*$}{};
386 1817 $element =~ s{^\.}{0.};
387  
388 1817 ▶ return 0 if ! length $element;
Branch: 1 branch
BranchTrueFalseTotal
unless length $element 0 1817 1817
389 1817 ▶ return 0 if ( $element eq 'undef' || $element eq '<undef>' );
Branch: 1 branch
BranchTrueFalseTotal
if $element eq "undef" or $element eq "<undef>" 0 1817 1817
Condition33%1/3
$element eq "undef" or $element eq "<undef>"
l!l&&r!l&&!r
001817
MC/DC0%0/2
$element eq "undef" or $element eq "<undef>"
$element eq "undef" $element eq "<undef>"
Truth table33%1/3
A or B
A= $element eq "undef"B= $element eq "<undef>"
ABresult
1X1
011
000
390  
391 1817 my $v = eval { version->new($element) };
392 # XXX check defined $v and not just $v because version objects leak memory
393 # in boolean context -- dagolden, 2012-02-03
394 1817 ▶ return defined $v ? $v->stringify : 0;
Branch: 1 branch
BranchTrueFalseTotal
defined $v ? : 1817 0 1817
395 }
396  
397 52 ▶sub _bad_version_hook {
Subroutine _bad_version_hook: called CC=2 SCAR=6.9
398 52 my ($v) = @_;
399 52 $v =~ s{^\s*}{};
400 52 $v =~ s{\s*$}{};
401 52 $v =~ s{[a-z]+$}{}; # strip trailing alphabetics
402 52 my $vobj = eval { version->new($v) };
403 52 ▶ return defined($vobj) ? $vobj : version->new(0); # or give up
Branch: 1 branch
BranchTrueFalseTotal
defined $vobj ? : 4 48 52
404 }
405  
406 2927 ▶sub _version_map {
Subroutine _version_map: called CC=14 SCAR=31.0
407 2927 my ($element) = @_;
408 2927 ▶ return unless defined $element;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $element 1609 1318 2927
409 1318 ▶ if ( ref $element eq 'HASH' ) {
Branch: 3 branches
BranchTrueFalseTotal
if (ref $element eq 'HASH') { } 1313 5 1318
elsif (ref $element eq 'ARRAY') { } 5 0 5
elsif (ref $element eq '' and length $element) { } 0 0 0
410 # XXX turn this into CPAN::Meta::Requirements with bad version hook
411 # and then turn it back into a hash
412 1313 my $new_map = CPAN::Meta::Requirements->new(
413 { bad_version_hook => \&_bad_version_hook } # punt
414 );
415 1313 ▶ while ( my ($k,$v) = each %$element ) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %$element) 8217 1313 9530
416 8217 ▶ next unless _is_module_name($k);
Branch: 1 branch
BranchTrueFalseTotal
unless _is_module_name($k) 5 8212 8217
417 8212 ▶ if ( !defined($v) || !length($v) || $v eq 'undef' || $v eq '<undef>' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $v && length $v or $v eq "undef" or $v eq "<undef>") 0 8212 8212
Condition33%3/9
not defined $v && length $v or $v eq "undef" or $v eq "<undef>"
l!l&&r!l&&!r
008212
not defined $v && length $v or $v eq "undef"
l!l&&r!l&&!r
008212
defined $v && length $v
!ll&&!rl&&r
008212
MC/DC0%0/4
not defined $v && length $v or $v eq "undef" or $v eq "<undef>"
defined $v length $v $v eq "undef" $v eq "<undef>"
Truth table20%1/5
not(A && B) or C or D
A= defined $vB= length $vC= $v eq "undef"D= $v eq "<undef>"
ABCDresult
0XXX1
10XX1
111X1
11011
11000
418 0 $v = 0;
419 }
420 # some weird, old META have bad yml with module => module
421 # so check if value is like a module name and not like a version
422 8212 ▶ if ( _is_module_name($v) && ! version::is_lax($v) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (_is_module_name($v) and not version::is_lax($v)) 12 8200 8212
Condition66%2/3
_is_module_name($v) and not version::is_lax($v)
!ll&&!rl&&r
8200012
MC/DC50%1/2
_is_module_name($v) and not version::is_lax($v)
_is_module_name($v) not version::is_lax($v)
Truth table66%2/3
A and not(B)
A= _is_module_name($v)B= not version::is_lax($v)
ABresult
0X0
100
111
423 12 $new_map->add_minimum($k => 0);
424 12 $new_map->add_minimum($v => 0);
425 }
426 8212 $new_map->add_string_requirement($k => $v);
427 }
428 1313 return $new_map->as_string_hash;
429 }
430 elsif ( ref $element eq 'ARRAY' ) {
431 5 my $hashref = { map { $_ => 0 } @$element };
432 5 return _version_map($hashref); # clean up any weird stuff
433 }
434 ▶ elsif ( ref $element eq '' && length $element ) {
Condition0%0/3
ref $element eq "" and length $element
!ll&&!rl&&r
000
MC/DC0%0/2
ref $element eq "" and length $element
ref $element eq "" length $element
Truth table0%0/3
A and B
A= ref $element eq ""B= length $element
ABresult
0X0
100
111
435 0 return { $element => 0 }
436 }
437 0 return;
438 }
439  
440 86 ▶sub _prereqs_from_1 {
Subroutine _prereqs_from_1: called CC=5 SCAR=16.1
441 86 my (undef, undef, $meta) = @_;
442 86 my $prereqs = {};
443 86 for my $phase ( qw/build configure/ ) {
444 172 my $key = "${phase}_requires";
445 $prereqs->{$phase}{requires} = _version_map($meta->{$key})
446 172 ▶ if $meta->{$key};
Branch: 1 branch
BranchTrueFalseTotal
if $meta->{$key} 56 116 172
447 }
448 86 for my $rel ( qw/requires recommends conflicts/ ) {
449 $prereqs->{runtime}{$rel} = _version_map($meta->{$rel})
450 258 ▶ if $meta->{$rel};
Branch: 1 branch
BranchTrueFalseTotal
if $meta->{$rel} 100 158 258
451 }
452 86 return $prereqs;
453 }
454  
455 22 my $prereqs_spec = {
456 configure => \&_prereqs_rel,
457 build => \&_prereqs_rel,
458 test => \&_prereqs_rel,
459 runtime => \&_prereqs_rel,
460 develop => \&_prereqs_rel,
461 ':custom' => \&_prefix_custom,
462 };
463  
464 22 my $relation_spec = {
465 requires => \&_version_map,
466 recommends => \&_version_map,
467 suggests => \&_version_map,
468 conflicts => \&_version_map,
469 ':custom' => \&_prefix_custom,
470 };
471  
472 107 ▶sub _cleanup_prereqs {
Subroutine _cleanup_prereqs: called CC=3 SCAR=11.0
473 107 my ($prereqs, $key, $meta, $to_version) = @_;
474 107 ▶ return unless $prereqs && ref $prereqs eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
unless $prereqs and ref $prereqs eq "HASH" 22 85 107
Condition66%2/3
$prereqs and ref $prereqs eq "HASH"
!ll&&!rl&&r
22085
MC/DC50%1/2
$prereqs and ref $prereqs eq "HASH"
$prereqs ref $prereqs eq "HASH"
Truth table66%2/3
A and B
A= $prereqsB= ref $prereqs eq "HASH"
ABresult
0X0
100
111
475 85 return _convert( $prereqs, $prereqs_spec, $to_version );
476 }
477  
478 425 ▶sub _prereqs_rel {
Subroutine _prereqs_rel: called CC=3 SCAR=11.0
479 425 my ($relation, $key, $meta, $to_version) = @_;
480 425 ▶ return unless $relation && ref $relation eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
unless $relation and ref $relation eq "HASH" 188 237 425
Condition66%2/3
$relation and ref $relation eq "HASH"
!ll&&!rl&&r
1880237
MC/DC50%1/2
$relation and ref $relation eq "HASH"
$relation ref $relation eq "HASH"
Truth table66%2/3
A and B
A= $relationB= ref $relation eq "HASH"
ABresult
0X0
100
111
481 237 return _convert( $relation, $relation_spec, $to_version );
482 }
483  
484  
485 22 ▶BEGIN {
Subroutine BEGIN: called CC=3 SCAR=11.0
486 22 my @old_prereqs = qw(
487 requires
488 configure_requires
489 recommends
490 conflicts
491 );
492  
493 22 for ( @old_prereqs ) {
494 88 my $sub = "_get_$_";
495 88 my ($phase,$type) = split qr/_/, $_;
496 88 ▶ if ( ! defined $type ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $type) 66 22 88
497 66 $type = $phase;
498 66 $phase = 'runtime';
499 }
500 22 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
501 88 ▶ *{$sub} = sub { _extract_prereqs($_[2]->{prereqs},$phase,$type) };
Subroutine __ANON__: called CC=1 SCAR=0.0
502 }
503 }
504  
505 33 ▶sub _get_build_requires {
Subroutine _get_build_requires: called CC=3 SCAR=11.0
506 33 my ($data, $key, $meta) = @_;
507  
508 33 ▶ my $test_h = _extract_prereqs($_[2]->{prereqs}, qw(test requires)) || {};
Condition100%2/2
_extract_prereqs($_[2]{'prereqs'}, ('test', 'requires')) || {}
l!l
2211
MC/DC100%1/1
_extract_prereqs($_[2]{'prereqs'}, ('test', 'requires')) || {}
_extract_prereqs($_[2]{'prereqs'}, ('test', 'requires'))
Truth table100%2/2
A
A= _extract_prereqs($_[2]{'prereqs'}, ('test', 'requires'))
Aresult
11
00
509 33 ▶ my $build_h = _extract_prereqs($_[2]->{prereqs}, qw(build requires)) || {};
Condition100%2/2
_extract_prereqs($_[2]{'prereqs'}, ('build', 'requires')) || {}
l!l
312
MC/DC100%1/1
_extract_prereqs($_[2]{'prereqs'}, ('build', 'requires')) || {}
_extract_prereqs($_[2]{'prereqs'}, ('build', 'requires'))
Truth table100%2/2
A
A= _extract_prereqs($_[2]{'prereqs'}, ('build', 'requires'))
Aresult
11
00
510  
511 33 my $test_req = CPAN::Meta::Requirements->from_string_hash($test_h);
512 33 my $build_req = CPAN::Meta::Requirements->from_string_hash($build_h);
513  
514 33 $test_req->add_requirements($build_req)->as_string_hash;
515 }
516  
517 318 ▶sub _extract_prereqs {
Subroutine _extract_prereqs: called CC=2 SCAR=7.1
518 318 my ($prereqs, $phase, $type) = @_;
519 318 ▶ return unless ref $prereqs eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $prereqs eq "HASH" 0 318 318
520 318 return scalar _version_map($prereqs->{$phase}{$type});
521 }
522  
523 33 ▶sub _downgrade_optional_features {
Subroutine _downgrade_optional_features: called CC=5 SCAR=16.1
524 33 my (undef, undef, $meta) = @_;
525 33 ▶ return unless exists $meta->{optional_features};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $meta->{"optional_features"} 9 24 33
526 24 my $origin = $meta->{optional_features};
527 24 my $features = {};
528 24 for my $name ( keys %$origin ) {
529 $features->{$name} = {
530 description => $origin->{$name}{description},
531 requires => _extract_prereqs($origin->{$name}{prereqs},'runtime','requires'),
532 configure_requires => _extract_prereqs($origin->{$name}{prereqs},'runtime','configure_requires'),
533 build_requires => _extract_prereqs($origin->{$name}{prereqs},'runtime','build_requires'),
534 recommends => _extract_prereqs($origin->{$name}{prereqs},'runtime','recommends'),
535 24 conflicts => _extract_prereqs($origin->{$name}{prereqs},'runtime','conflicts'),
536 };
537 24 for my $k (keys %{$features->{$name}} ) {
538 144 ▶ delete $features->{$name}{$k} unless defined $features->{$name}{$k};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $features->{$name}{$k} 96 48 144
539 }
540 }
541 24 return $features;
542 }
543  
544 76 ▶sub _upgrade_optional_features {
Subroutine _upgrade_optional_features: called CC=3 SCAR=11.0
545 76 my (undef, undef, $meta) = @_;
546 76 ▶ return unless exists $meta->{optional_features};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $meta->{"optional_features"} 72 4 76
547 4 my $origin = $meta->{optional_features};
548 4 my $features = {};
549 4 for my $name ( keys %$origin ) {
550 $features->{$name} = {
551 description => $origin->{$name}{description},
552 10 prereqs => _prereqs_from_1(undef, undef, $origin->{$name}),
553 };
554 10 delete $features->{$name}{prereqs}{configure};
555 }
556 4 return $features;
557 }
558  
559 22 my $optional_features_2_spec = {
560 description => \&_keep,
561 prereqs => \&_cleanup_prereqs,
562 ':custom' => \&_prefix_custom,
563 };
564  
565 25 ▶sub _feature_2 {
Subroutine _feature_2: called CC=3 SCAR=12.5
566 25 my ($element, $key, $meta, $to_version) = @_;
567 25 ▶ return unless $element && ref $element eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
unless $element and ref $element eq "HASH" 0 25 25
Condition33%1/3
$element and ref $element eq "HASH"
!ll&&!rl&&r
0025
MC/DC0%0/2
$element and ref $element eq "HASH"
$element ref $element eq "HASH"
Truth table33%1/3
A and B
A= $elementB= ref $element eq "HASH"
ABresult
0X0
100
111
568 25 _convert( $element, $optional_features_2_spec, $to_version );
569 }
570  
571 82 ▶sub _cleanup_optional_features_2 {
Subroutine _cleanup_optional_features_2: called CC=5 SCAR=16.2
572 82 my ($element, $key, $meta, $to_version) = @_;
573 82 ▶ return unless $element && ref $element eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
unless $element and ref $element eq "HASH" 57 25 82
Condition66%2/3
$element and ref $element eq "HASH"
!ll&&!rl&&r
57025
MC/DC50%1/2
$element and ref $element eq "HASH"
$element ref $element eq "HASH"
Truth table66%2/3
A and B
A= $elementB= ref $element eq "HASH"
ABresult
0X0
100
111
574 25 my $new_data = {};
575 25 for my $k ( keys %$element ) {
576 25 $new_data->{$k} = _feature_2( $element->{$k}, $k, $meta, $to_version );
577 }
578 25 ▶ return unless keys %$new_data;
Branch: 1 branch
BranchTrueFalseTotal
unless keys %$new_data 0 25 25
579 25 return $new_data;
580 }
581  
582 71 ▶sub _optional_features_1_4 {
Subroutine _optional_features_1_4: called CC=4 SCAR=13.9
583 71 my ($element) = @_;
584 71 ▶ return unless $element;
Branch: 1 branch
BranchTrueFalseTotal
unless $element 67 4 71
585 4 $element = _optional_features_as_map($element);
586 4 for my $name ( keys %$element ) {
587 12 for my $drop ( qw/requires_packages requires_os excluded_os/ ) {
588 36 delete $element->{$name}{$drop};
589 }
590 }
591 4 return $element;
592 }
593  
594 203 ▶sub _optional_features_as_map {
Subroutine _optional_features_as_map: called CC=4 SCAR=13.9
595 203 my ($element) = @_;
596 203 ▶ return unless $element;
Branch: 1 branch
BranchTrueFalseTotal
unless $element 155 48 203
597 48 ▶ if ( ref $element eq 'ARRAY' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $element eq "ARRAY") 4 44 48
598 4 my %map;
599 4 for my $feature ( @$element ) {
600 12 my (@parts) = %$feature;
601 12 $map{$parts[0]} = $parts[1];
602 }
603 4 $element = \%map;
604 }
605 48 return $element;
606 }
607  
608 901 ▶sub _is_urlish { defined $_[0] && $_[0] =~ m{\A[-+.a-z0-9]+:.+}i }
Subroutine _is_urlish: called CC=2 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
defined $_[0] and $_[0] =~ /\A[-+.a-z0-9]+:.+/i 468 433 901
609  
610 791 ▶sub _url_or_drop {
Subroutine _url_or_drop: called CC=2 SCAR=6.9
611 791 my ($element) = @_;
612 791 ▶ return $element if _is_urlish($element);
Branch: 1 branch
BranchTrueFalseTotal
if _is_urlish($element) 378 413 791
613 413 return;
614 }
615  
616 60 ▶sub _url_list {
Subroutine _url_list: called CC=3 SCAR=11.0
617 60 my ($element) = @_;
618 60 ▶ return unless $element;
Branch: 1 branch
BranchTrueFalseTotal
unless $element 30 30 60
619 30 $element = _listify( $element );
620 30 $element = [ grep { _is_urlish($_) } @$element ];
621 30 ▶ return unless @$element;
Branch: 1 branch
BranchTrueFalseTotal
unless @$element 0 30 30
622 30 return $element;
623 }
624  
625 461 ▶sub _author_list {
Subroutine _author_list: called CC=5 SCAR=16.8
626 461 my ($element) = @_;
627 461 ▶ return [ 'unknown' ] unless $element;
Branch: 1 branch
BranchTrueFalseTotal
unless $element 56 405 461
628 405 $element = _listify( $element );
629 405 ▶ $element = [ map { defined $_ && length $_ ? $_ : 'unknown' } @$element ];
Branch: 1 branch
BranchTrueFalseTotal
defined $_ && length $_ ? : 641 0 641
Condition33%1/3
defined $_ and length $_
!ll&&!rl&&r
00641
MC/DC0%0/2
defined $_ and length $_
defined $_ length $_
Truth table33%1/3
A and B
A= defined $_B= length $_
ABresult
0X0
100
111
630 405 ▶ return [ 'unknown' ] unless @$element;
Branch: 1 branch
BranchTrueFalseTotal
unless @$element 0 405 405
631 405 return $element;
632 }
633  
634 my $resource2_upgrade = {
635 37 ▶ license => sub { return _is_urlish($_[0]) ? _listify( $_[0] ) : undef },
Subroutine __ANON__: called CC=2 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
_is_urlish($_[0]) ? : 35 2 37
636 homepage => \&_url_or_drop,
637 37 ▶ bugtracker => sub {
Subroutine __ANON__: called CC=4 SCAR=15.2
638 37 my ($item) = @_;
639 37 ▶ return unless $item;
Branch: 1 branch
BranchTrueFalseTotal
unless $item 32 5 37
640 0 ▶ if ( $item =~ m{^mailto:(.*)$} ) { return { mailto => $1 } }
Branch: 2 branches
BranchTrueFalseTotal
if ($item =~ /^mailto:(.*)$/) { } 0 5 5
elsif (_is_urlish($item)) { } 5 0 5
641 5 elsif( _is_urlish($item) ) { return { web => $item } }
642 0 else { return }
643 },
644 37 ▶ repository => sub { return _is_urlish($_[0]) ? { url => $_[0] } : undef },
Subroutine __ANON__: called CC=2 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
_is_urlish($_[0]) ? : 19 18 37
645 22 ':custom' => \&_prefix_custom,
646 };
647  
648 76 ▶sub _upgrade_resources_2 {
Subroutine _upgrade_resources_2: called CC=2 SCAR=6.9
649 76 my (undef, undef, $meta, $version) = @_;
650 76 ▶ return unless exists $meta->{resources};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $meta->{"resources"} 39 37 76
651 37 return _convert($meta->{resources}, $resource2_upgrade);
652 }
653  
654 22 my $bugtracker2_spec = {
655 web => \&_url_or_drop,
656 mailto => \&_keep,
657 ':custom' => \&_prefix_custom,
658 };
659  
660 43 ▶sub _repo_type {
Subroutine _repo_type: called CC=5 SCAR=16.1
661 43 my ($element, $key, $meta, $to_version) = @_;
662 43 ▶ return $element if defined $element;
Branch: 1 branch
BranchTrueFalseTotal
if defined $element 25 18 43
663 18 ▶ return unless exists $meta->{url};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $meta->{"url"} 1 17 18
664 17 my $repo_url = $meta->{url};
665 17 for my $type ( qw/git svn/ ) {
666 33 ▶ return $type if $repo_url =~ m{\A$type};
Branch: 1 branch
BranchTrueFalseTotal
if $repo_url =~ /\A$type/ 16 17 33
667 }
668 1 return;
669 }
670  
671 22 my $repository2_spec = {
672 web => \&_url_or_drop,
673 url => \&_url_or_drop,
674 type => \&_repo_type,
675 ':custom' => \&_prefix_custom,
676 };
677  
678 my $resources2_cleanup = {
679 license => \&_url_list,
680 homepage => \&_url_or_drop,
681 60 ▶ bugtracker => sub { ref $_[0] ? _convert( $_[0], $bugtracker2_spec ) : undef },
Subroutine __ANON__: called CC=2 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] ? : 32 28 60
682 60 ▶ repository => sub { my $data = shift; ref $data ? _convert( $data, $repository2_spec ) : undef },
Subroutine __ANON__: called CC=2 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
ref $data ? : 43 17 60
683 22 ':custom' => \&_prefix_custom,
684 };
685  
686 82 ▶sub _cleanup_resources_2 {
Subroutine _cleanup_resources_2: called CC=3 SCAR=11.0
687 82 my ($resources, $key, $meta, $to_version) = @_;
688 82 ▶ return unless $resources && ref $resources eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
unless $resources and ref $resources eq "HASH" 22 60 82
Condition66%2/3
$resources and ref $resources eq "HASH"
!ll&&!rl&&r
22060
MC/DC50%1/2
$resources and ref $resources eq "HASH"
$resources ref $resources eq "HASH"
Truth table66%2/3
A and B
A= $resourcesB= ref $resources eq "HASH"
ABresult
0X0
100
111
689 60 return _convert($resources, $resources2_cleanup, $to_version);
690 }
691  
692 22 my $resource1_spec = {
693 license => \&_url_or_drop,
694 homepage => \&_url_or_drop,
695 bugtracker => \&_url_or_drop,
696 repository => \&_url_or_drop,
697 ':custom' => \&_keep,
698 };
699  
700 239 ▶sub _resources_1_3 {
Subroutine _resources_1_3: called CC=2 SCAR=6.9
701 239 my (undef, undef, $meta, $version) = @_;
702 239 ▶ return unless exists $meta->{resources};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $meta->{"resources"} 110 129 239
703 129 return _convert($meta->{resources}, $resource1_spec);
704 }
705  
706 22 *_resources_1_4 = *_resources_1_3;
707  
708 31 ▶sub _resources_1_2 {
Subroutine _resources_1_2: called CC=6 SCAR=18.0
709 31 my (undef, undef, $meta) = @_;
710 31 ▶ my $resources = $meta->{resources} || {};
Condition100%2/2
$meta->{'resources'} || {}
l!l
130
MC/DC100%1/1
$meta->{'resources'} || {}
$meta->{'resources'}
Truth table100%2/2
A
A= $meta->{'resources'}
Aresult
11
00
711 31 ▶ if ( $meta->{license_url} && ! $resources->{license} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($meta->{"license_url"} and not $resources->{"license"}) 1 30 31
Condition66%2/3
$meta->{"license_url"} and not $resources->{"license"}
!ll&&!rl&&r
3001
MC/DC50%1/2
$meta->{"license_url"} and not $resources->{"license"}
$meta->{"license_url"} not $resources->{"license"}
Truth table66%2/3
A and not(B)
A= $meta->{"license_url"}B= not $resources->{"license"}
ABresult
0X0
100
111
712 $resources->{license} = $meta->{license_url}
713 1 ▶ if _is_urlish($meta->{license_url});
Branch: 1 branch
BranchTrueFalseTotal
if _is_urlish($meta->{"license_url"}) 1 0 1
714 }
715 31 ▶ return unless keys %$resources;
Branch: 1 branch
BranchTrueFalseTotal
unless keys %$resources 29 2 31
716 2 return _convert($resources, $resource1_spec);
717 }
718  
719 my $resource_downgrade_spec = {
720 27 ▶ license => sub { return ref $_[0] ? $_[0]->[0] : $_[0] },
Subroutine __ANON__: called CC=2 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] ? : 25 2 27
721 homepage => \&_url_or_drop,
722 27 ▶ bugtracker => sub { return $_[0]->{web} },
Subroutine __ANON__: called CC=1 SCAR=0.0
723 27 ▶ repository => sub { return $_[0]->{url} || $_[0]->{web} },
Subroutine __ANON__: called CC=2 SCAR=7.2
Condition66%2/3
$_[0]{'url'} || $_[0]{'web'}
l!l&&r!l&&!r
1809
MC/DC50%1/2
$_[0]{'url'} || $_[0]{'web'}
$_[0]{'url'} $_[0]{'web'}
Truth table66%2/3
A || B
A= $_[0]{'url'}B= $_[0]{'web'}
ABresult
1X1
011
000
724 22 ':custom' => \&_no_prefix_ucfirst_custom,
725 };
726  
727 33 ▶sub _downgrade_resources {
Subroutine _downgrade_resources: called CC=2 SCAR=6.9
728 33 my (undef, undef, $meta, $version) = @_;
729 33 ▶ return unless exists $meta->{resources};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $meta->{"resources"} 6 27 33
730 27 return _convert($meta->{resources}, $resource_downgrade_spec);
731 }
732  
733 158 ▶sub _release_status {
Subroutine _release_status: called CC=3 SCAR=11.0
734 158 my ($element, undef, $meta) = @_;
735 158 ▶ return $element if $element && $element =~ m{\A(?:stable|testing|unstable)\z};
Branch: 1 branch
BranchTrueFalseTotal
if $element and $element =~ /\A(?:stable|testing|unstable)\z/ 54 104 158
Condition66%2/3
$element and $element =~ /\A(?:stable|testing|unstable)\z/
!ll&&!rl&&r
104054
MC/DC50%1/2
$element and $element =~ /\A(?:stable|testing|unstable)\z/
$element $element =~ /\A(?:stable|testing|unstable)\z/
Truth table66%2/3
A and B
A= $elementB= $element =~ /\A(?:stable|testing|unstable)\z/
ABresult
0X0
100
111
736 104 return _release_status_from_version(undef, undef, $meta);
737 }
738  
739 104 ▶sub _release_status_from_version {
Subroutine _release_status_from_version: called CC=3 SCAR=11.0
740 104 my (undef, undef, $meta) = @_;
741 104 ▶ my $version = $meta->{version} || '';
Condition100%2/2
$meta->{'version'} || ''
l!l
6737
MC/DC100%1/1
$meta->{'version'} || ''
$meta->{'version'}
Truth table100%2/2
A
A= $meta->{'version'}
Aresult
11
00
742 104 ▶ return ( $version =~ /_/ ) ? 'testing' : 'stable';
Branch: 1 branch
BranchTrueFalseTotal
$version =~ /_/ ? : 15 89 104
743 }
744  
745 22 my $provides_spec = {
746 file => \&_keep,
747 version => \&_keep,
748 };
749  
750 22 my $provides_spec_2 = {
751 file => \&_keep,
752 version => \&_keep,
753 ':custom' => \&_prefix_custom,
754 };
755  
756 461 ▶sub _provides {
Subroutine _provides: called CC=6 SCAR=17.9
757 461 my ($element, $key, $meta, $to_version) = @_;
758 461 ▶ return unless defined $element && ref $element eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
unless defined $element and ref $element eq "HASH" 319 142 461
Condition66%2/3
defined $element and ref $element eq "HASH"
!ll&&!rl&&r
3190142
MC/DC50%1/2
defined $element and ref $element eq "HASH"
defined $element ref $element eq "HASH"
Truth table66%2/3
A and B
A= defined $elementB= ref $element eq "HASH"
ABresult
0X0
100
111
759 142 ▶ my $spec = $to_version == 2 ? $provides_spec_2 : $provides_spec;
Branch: 1 branch
BranchTrueFalseTotal
$to_version == 2 ? : 66 76 142
760 142 my $new_data = {};
761 142 for my $k ( keys %$element ) {
762 1934 $new_data->{$k} = _convert($element->{$k}, $spec, $to_version);
763 $new_data->{$k}{version} = _clean_version($element->{$k}{version})
764 1934 ▶ if exists $element->{$k}{version};
Branch: 1 branch
BranchTrueFalseTotal
if exists $element->{$k}{"version"} 1875 59 1934
765 }
766 142 return $new_data;
767 }
768  
769 3287 ▶sub _convert {
Subroutine _convert: called CC=15 SCAR=27.2
770 3287 my ($data, $spec, $to_version, $is_fragment) = @_;
771  
772 3287 my $new_data = {};
773 3287 for my $key ( keys %$spec ) {
774 18679 ▶ next if $key eq ':custom' || $key eq ':drop';
Branch: 1 branch
BranchTrueFalseTotal
if $key eq ":custom" or $key eq ":drop" 2431 16248 18679
Condition100%3/3
$key eq ":custom" or $key eq ":drop"
l!l&&r!l&&!r
200342816248
MC/DC100%2/2
$key eq ":custom" or $key eq ":drop"
$key eq ":custom" $key eq ":drop"
Truth table100%3/3
A or B
A= $key eq ":custom"B= $key eq ":drop"
ABresult
1X1
011
000
775 16248 ▶ next unless my $fcn = $spec->{$key};
Branch: 1 branch
BranchTrueFalseTotal
unless my $fcn = $spec->{$key} 0 16248 16248
776 16248 ▶ if ( $is_fragment && $key eq 'generated_by' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($is_fragment and $key eq "generated_by") 43 16205 16248
Condition100%3/3
$is_fragment and $key eq "generated_by"
!ll&&!rl&&r
1555065543
MC/DC100%2/2
$is_fragment and $key eq "generated_by"
$is_fragment $key eq "generated_by"
Truth table100%3/3
A and B
A= $is_fragmentB= $key eq "generated_by"
ABresult
0X0
100
111
777 43 $fcn = \&_keep;
778 }
779 16248 ▶ die "spec for '$key' is not a coderef"
Branch: 1 branch
BranchTrueFalseTotal
unless ref $fcn and ref $fcn eq "CODE" 0 16248 16248
Condition33%1/3
ref $fcn and ref $fcn eq "CODE"
!ll&&!rl&&r
0016248
MC/DC0%0/2
ref $fcn and ref $fcn eq "CODE"
ref $fcn ref $fcn eq "CODE"
Truth table33%1/3
A and B
A= ref $fcnB= ref $fcn eq "CODE"
ABresult
0X0
100
111
780 unless ref $fcn && ref $fcn eq 'CODE';
781 16248 my $new_value = $fcn->($data->{$key}, $key, $data, $to_version);
782 16248 ▶ $new_data->{$key} = $new_value if defined $new_value;
Branch: 1 branch
BranchTrueFalseTotal
if defined $new_value 11239 5009 16248
783 }
784  
785 3287 my $drop_list = $spec->{':drop'};
786 3287 ▶ my $customizer = $spec->{':custom'} || \&_keep;
Condition100%2/2
$spec->{':custom'} || \&CPAN::Meta::Converter::_keep
l!l
20031284
MC/DC100%1/1
$spec->{':custom'} || \&CPAN::Meta::Converter::_keep
$spec->{':custom'}
Truth table100%2/2
A
A= $spec->{':custom'}
Aresult
11
00
787  
788 3287 for my $key ( keys %$data ) {
789 12008 ▶ next if $drop_list && grep { $key eq $_ } @$drop_list;
Branch: 1 branch
BranchTrueFalseTotal
if $drop_list and grep {$key eq $_;} @$drop_list 428 11580 12008
Condition100%3/3
$drop_list and grep {$key eq $_;} @$drop_list
!ll&&!rl&&r
68794701428
MC/DC100%2/2
$drop_list and grep {$key eq $_;} @$drop_list
$drop_list grep {$key eq $_;} @$drop_list
Truth table100%3/3
A and B
A= $drop_listB= grep {$key eq $_;} @$drop_list
ABresult
0X0
100
111
790 11580 ▶ next if exists $spec->{$key}; # we handled it
Branch: 1 branch
BranchTrueFalseTotal
if exists $spec->{$key} 10719 861 11580
791 861 $new_data->{ $customizer->($key) } = $data->{$key};
792 }
793  
794 3287 return $new_data;
795 }
796  
797 #--------------------------------------------------------------------------#
798 # define converters for each conversion
799 #--------------------------------------------------------------------------#
800  
801 # each converts from prior version
802 # special ":custom" field is used for keys not recognized in spec
803 22 my %up_convert = (
804 '2-from-1.4' => {
805 # PRIOR MANDATORY
806 'abstract' => \&_keep_or_unknown,
807 'author' => \&_author_list,
808 'generated_by' => \&_generated_by,
809 'license' => \&_license_2,
810 'meta-spec' => \&_change_meta_spec,
811 'name' => \&_keep,
812 'version' => \&_keep,
813 # CHANGED TO MANDATORY
814 'dynamic_config' => \&_keep_or_one,
815 # ADDED MANDATORY
816 'release_status' => \&_release_status,
817 # PRIOR OPTIONAL
818 'keywords' => \&_keep,
819 'no_index' => \&_no_index_directory,
820 'optional_features' => \&_upgrade_optional_features,
821 'provides' => \&_provides,
822 'resources' => \&_upgrade_resources_2,
823 # ADDED OPTIONAL
824 'description' => \&_keep,
825 'prereqs' => \&_prereqs_from_1,
826  
827 # drop these deprecated fields, but only after we convert
828 ':drop' => [ qw(
829 build_requires
830 configure_requires
831 conflicts
832 distribution_type
833 license_url
834 private
835 recommends
836 requires
837 ) ],
838  
839 # other random keys need x_ prefixing
840 ':custom' => \&_prefix_custom,
841 },
842 '1.4-from-1.3' => {
843 # PRIOR MANDATORY
844 'abstract' => \&_keep_or_unknown,
845 'author' => \&_author_list,
846 'generated_by' => \&_generated_by,
847 'license' => \&_license_1,
848 'meta-spec' => \&_change_meta_spec,
849 'name' => \&_keep,
850 'version' => \&_keep,
851 # PRIOR OPTIONAL
852 'build_requires' => \&_version_map,
853 'conflicts' => \&_version_map,
854 'distribution_type' => \&_keep,
855 'dynamic_config' => \&_keep_or_one,
856 'keywords' => \&_keep,
857 'no_index' => \&_no_index_directory,
858 'optional_features' => \&_optional_features_1_4,
859 'provides' => \&_provides,
860 'recommends' => \&_version_map,
861 'requires' => \&_version_map,
862 'resources' => \&_resources_1_4,
863 # ADDED OPTIONAL
864 'configure_requires' => \&_keep,
865  
866 # drop these deprecated fields, but only after we convert
867 ':drop' => [ qw(
868 license_url
869 private
870 )],
871  
872 # other random keys are OK if already valid
873 ':custom' => \&_keep
874 },
875 '1.3-from-1.2' => {
876 # PRIOR MANDATORY
877 'abstract' => \&_keep_or_unknown,
878 'author' => \&_author_list,
879 'generated_by' => \&_generated_by,
880 'license' => \&_license_1,
881 'meta-spec' => \&_change_meta_spec,
882 'name' => \&_keep,
883 'version' => \&_keep,
884 # PRIOR OPTIONAL
885 'build_requires' => \&_version_map,
886 'conflicts' => \&_version_map,
887 'distribution_type' => \&_keep,
888 'dynamic_config' => \&_keep_or_one,
889 'keywords' => \&_keep,
890 'no_index' => \&_no_index_directory,
891 'optional_features' => \&_optional_features_as_map,
892 'provides' => \&_provides,
893 'recommends' => \&_version_map,
894 'requires' => \&_version_map,
895 'resources' => \&_resources_1_3,
896  
897 # drop these deprecated fields, but only after we convert
898 ':drop' => [ qw(
899 license_url
900 private
901 )],
902  
903 # other random keys are OK if already valid
904 ':custom' => \&_keep
905 },
906 '1.2-from-1.1' => {
907 # PRIOR MANDATORY
908 'version' => \&_keep,
909 # CHANGED TO MANDATORY
910 'license' => \&_license_1,
911 'name' => \&_keep,
912 'generated_by' => \&_generated_by,
913 # ADDED MANDATORY
914 'abstract' => \&_keep_or_unknown,
915 'author' => \&_author_list,
916 'meta-spec' => \&_change_meta_spec,
917 # PRIOR OPTIONAL
918 'build_requires' => \&_version_map,
919 'conflicts' => \&_version_map,
920 'distribution_type' => \&_keep,
921 'dynamic_config' => \&_keep_or_one,
922 'recommends' => \&_version_map,
923 'requires' => \&_version_map,
924 # ADDED OPTIONAL
925 'keywords' => \&_keep,
926 'no_index' => \&_no_index_1_2,
927 'optional_features' => \&_optional_features_as_map,
928 'provides' => \&_provides,
929 'resources' => \&_resources_1_2,
930  
931 # drop these deprecated fields, but only after we convert
932 ':drop' => [ qw(
933 license_url
934 private
935 )],
936  
937 # other random keys are OK if already valid
938 ':custom' => \&_keep
939 },
940 '1.1-from-1.0' => {
941 # CHANGED TO MANDATORY
942 'version' => \&_keep,
943 # IMPLIED MANDATORY
944 'name' => \&_keep,
945 # PRIOR OPTIONAL
946 'build_requires' => \&_version_map,
947 'conflicts' => \&_version_map,
948 'distribution_type' => \&_keep,
949 'dynamic_config' => \&_keep_or_one,
950 'generated_by' => \&_generated_by,
951 'license' => \&_license_1,
952 'recommends' => \&_version_map,
953 'requires' => \&_version_map,
954 # ADDED OPTIONAL
955 'license_url' => \&_url_or_drop,
956 'private' => \&_keep,
957  
958 # other random keys are OK if already valid
959 ':custom' => \&_keep
960 },
961 );
962  
963 22 my %down_convert = (
964 '1.4-from-2' => {
965 # MANDATORY
966 'abstract' => \&_keep_or_unknown,
967 'author' => \&_author_list,
968 'generated_by' => \&_generated_by,
969 'license' => \&_downgrade_license,
970 'meta-spec' => \&_change_meta_spec,
971 'name' => \&_keep,
972 'version' => \&_keep,
973 # OPTIONAL
974 'build_requires' => \&_get_build_requires,
975 'configure_requires' => \&_get_configure_requires,
976 'conflicts' => \&_get_conflicts,
977 'distribution_type' => \&_keep,
978 'dynamic_config' => \&_keep_or_one,
979 'keywords' => \&_keep,
980 'no_index' => \&_no_index_directory,
981 'optional_features' => \&_downgrade_optional_features,
982 'provides' => \&_provides,
983 'recommends' => \&_get_recommends,
984 'requires' => \&_get_requires,
985 'resources' => \&_downgrade_resources,
986  
987 # drop these unsupported fields (after conversion)
988 ':drop' => [ qw(
989 description
990 prereqs
991 release_status
992 )],
993  
994 # custom keys will be left unchanged
995 ':custom' => \&_keep
996 },
997 '1.3-from-1.4' => {
998 # MANDATORY
999 'abstract' => \&_keep_or_unknown,
1000 'author' => \&_author_list,
1001 'generated_by' => \&_generated_by,
1002 'license' => \&_license_1,
1003 'meta-spec' => \&_change_meta_spec,
1004 'name' => \&_keep,
1005 'version' => \&_keep,
1006 # OPTIONAL
1007 'build_requires' => \&_version_map,
1008 'conflicts' => \&_version_map,
1009 'distribution_type' => \&_keep,
1010 'dynamic_config' => \&_keep_or_one,
1011 'keywords' => \&_keep,
1012 'no_index' => \&_no_index_directory,
1013 'optional_features' => \&_optional_features_as_map,
1014 'provides' => \&_provides,
1015 'recommends' => \&_version_map,
1016 'requires' => \&_version_map,
1017 'resources' => \&_resources_1_3,
1018  
1019 # drop these unsupported fields, but only after we convert
1020 ':drop' => [ qw(
1021 configure_requires
1022 )],
1023  
1024 # other random keys are OK if already valid
1025 ':custom' => \&_keep,
1026 },
1027 '1.2-from-1.3' => {
1028 # MANDATORY
1029 'abstract' => \&_keep_or_unknown,
1030 'author' => \&_author_list,
1031 'generated_by' => \&_generated_by,
1032 'license' => \&_license_1,
1033 'meta-spec' => \&_change_meta_spec,
1034 'name' => \&_keep,
1035 'version' => \&_keep,
1036 # OPTIONAL
1037 'build_requires' => \&_version_map,
1038 'conflicts' => \&_version_map,
1039 'distribution_type' => \&_keep,
1040 'dynamic_config' => \&_keep_or_one,
1041 'keywords' => \&_keep,
1042 'no_index' => \&_no_index_1_2,
1043 'optional_features' => \&_optional_features_as_map,
1044 'provides' => \&_provides,
1045 'recommends' => \&_version_map,
1046 'requires' => \&_version_map,
1047 'resources' => \&_resources_1_3,
1048  
1049 # other random keys are OK if already valid
1050 ':custom' => \&_keep,
1051 },
1052 '1.1-from-1.2' => {
1053 # MANDATORY
1054 'version' => \&_keep,
1055 # IMPLIED MANDATORY
1056 'name' => \&_keep,
1057 'meta-spec' => \&_change_meta_spec,
1058 # OPTIONAL
1059 'build_requires' => \&_version_map,
1060 'conflicts' => \&_version_map,
1061 'distribution_type' => \&_keep,
1062 'dynamic_config' => \&_keep_or_one,
1063 'generated_by' => \&_generated_by,
1064 'license' => \&_license_1,
1065 'private' => \&_keep,
1066 'recommends' => \&_version_map,
1067 'requires' => \&_version_map,
1068  
1069 # drop unsupported fields
1070 ':drop' => [ qw(
1071 abstract
1072 author
1073 provides
1074 no_index
1075 keywords
1076 resources
1077 )],
1078  
1079 # other random keys are OK if already valid
1080 ':custom' => \&_keep,
1081 },
1082 '1.0-from-1.1' => {
1083 # IMPLIED MANDATORY
1084 'name' => \&_keep,
1085 'meta-spec' => \&_change_meta_spec,
1086 'version' => \&_keep,
1087 # PRIOR OPTIONAL
1088 'build_requires' => \&_version_map,
1089 'conflicts' => \&_version_map,
1090 'distribution_type' => \&_keep,
1091 'dynamic_config' => \&_keep_or_one,
1092 'generated_by' => \&_generated_by,
1093 'license' => \&_license_1,
1094 'recommends' => \&_version_map,
1095 'requires' => \&_version_map,
1096  
1097 # other random keys are OK if already valid
1098 ':custom' => \&_keep,
1099 },
1100 );
1101  
1102 22 my %cleanup = (
1103 '2' => {
1104 # PRIOR MANDATORY
1105 'abstract' => \&_keep_or_unknown,
1106 'author' => \&_author_list,
1107 'generated_by' => \&_generated_by,
1108 'license' => \&_license_2,
1109 'meta-spec' => \&_change_meta_spec,
1110 'name' => \&_keep,
1111 'version' => \&_keep,
1112 # CHANGED TO MANDATORY
1113 'dynamic_config' => \&_keep_or_one,
1114 # ADDED MANDATORY
1115 'release_status' => \&_release_status,
1116 # PRIOR OPTIONAL
1117 'keywords' => \&_keep,
1118 'no_index' => \&_no_index_directory,
1119 'optional_features' => \&_cleanup_optional_features_2,
1120 'provides' => \&_provides,
1121 'resources' => \&_cleanup_resources_2,
1122 # ADDED OPTIONAL
1123 'description' => \&_keep,
1124 'prereqs' => \&_cleanup_prereqs,
1125  
1126 # drop these deprecated fields, but only after we convert
1127 ':drop' => [ qw(
1128 build_requires
1129 configure_requires
1130 conflicts
1131 distribution_type
1132 license_url
1133 private
1134 recommends
1135 requires
1136 ) ],
1137  
1138 # other random keys need x_ prefixing
1139 ':custom' => \&_prefix_custom,
1140 },
1141 '1.4' => {
1142 # PRIOR MANDATORY
1143 'abstract' => \&_keep_or_unknown,
1144 'author' => \&_author_list,
1145 'generated_by' => \&_generated_by,
1146 'license' => \&_license_1,
1147 'meta-spec' => \&_change_meta_spec,
1148 'name' => \&_keep,
1149 'version' => \&_keep,
1150 # PRIOR OPTIONAL
1151 'build_requires' => \&_version_map,
1152 'conflicts' => \&_version_map,
1153 'distribution_type' => \&_keep,
1154 'dynamic_config' => \&_keep_or_one,
1155 'keywords' => \&_keep,
1156 'no_index' => \&_no_index_directory,
1157 'optional_features' => \&_optional_features_1_4,
1158 'provides' => \&_provides,
1159 'recommends' => \&_version_map,
1160 'requires' => \&_version_map,
1161 'resources' => \&_resources_1_4,
1162 # ADDED OPTIONAL
1163 'configure_requires' => \&_keep,
1164  
1165 # other random keys are OK if already valid
1166 ':custom' => \&_keep
1167 },
1168 '1.3' => {
1169 # PRIOR MANDATORY
1170 'abstract' => \&_keep_or_unknown,
1171 'author' => \&_author_list,
1172 'generated_by' => \&_generated_by,
1173 'license' => \&_license_1,
1174 'meta-spec' => \&_change_meta_spec,
1175 'name' => \&_keep,
1176 'version' => \&_keep,
1177 # PRIOR OPTIONAL
1178 'build_requires' => \&_version_map,
1179 'conflicts' => \&_version_map,
1180 'distribution_type' => \&_keep,
1181 'dynamic_config' => \&_keep_or_one,
1182 'keywords' => \&_keep,
1183 'no_index' => \&_no_index_directory,
1184 'optional_features' => \&_optional_features_as_map,
1185 'provides' => \&_provides,
1186 'recommends' => \&_version_map,
1187 'requires' => \&_version_map,
1188 'resources' => \&_resources_1_3,
1189  
1190 # other random keys are OK if already valid
1191 ':custom' => \&_keep
1192 },
1193 '1.2' => {
1194 # PRIOR MANDATORY
1195 'version' => \&_keep,
1196 # CHANGED TO MANDATORY
1197 'license' => \&_license_1,
1198 'name' => \&_keep,
1199 'generated_by' => \&_generated_by,
1200 # ADDED MANDATORY
1201 'abstract' => \&_keep_or_unknown,
1202 'author' => \&_author_list,
1203 'meta-spec' => \&_change_meta_spec,
1204 # PRIOR OPTIONAL
1205 'build_requires' => \&_version_map,
1206 'conflicts' => \&_version_map,
1207 'distribution_type' => \&_keep,
1208 'dynamic_config' => \&_keep_or_one,
1209 'recommends' => \&_version_map,
1210 'requires' => \&_version_map,
1211 # ADDED OPTIONAL
1212 'keywords' => \&_keep,
1213 'no_index' => \&_no_index_1_2,
1214 'optional_features' => \&_optional_features_as_map,
1215 'provides' => \&_provides,
1216 'resources' => \&_resources_1_2,
1217  
1218 # other random keys are OK if already valid
1219 ':custom' => \&_keep
1220 },
1221 '1.1' => {
1222 # CHANGED TO MANDATORY
1223 'version' => \&_keep,
1224 # IMPLIED MANDATORY
1225 'name' => \&_keep,
1226 'meta-spec' => \&_change_meta_spec,
1227 # PRIOR OPTIONAL
1228 'build_requires' => \&_version_map,
1229 'conflicts' => \&_version_map,
1230 'distribution_type' => \&_keep,
1231 'dynamic_config' => \&_keep_or_one,
1232 'generated_by' => \&_generated_by,
1233 'license' => \&_license_1,
1234 'recommends' => \&_version_map,
1235 'requires' => \&_version_map,
1236 # ADDED OPTIONAL
1237 'license_url' => \&_url_or_drop,
1238 'private' => \&_keep,
1239  
1240 # other random keys are OK if already valid
1241 ':custom' => \&_keep
1242 },
1243 '1.0' => {
1244 # IMPLIED MANDATORY
1245 'name' => \&_keep,
1246 'meta-spec' => \&_change_meta_spec,
1247 'version' => \&_keep,
1248 # IMPLIED OPTIONAL
1249 'build_requires' => \&_version_map,
1250 'conflicts' => \&_version_map,
1251 'distribution_type' => \&_keep,
1252 'dynamic_config' => \&_keep_or_one,
1253 'generated_by' => \&_generated_by,
1254 'license' => \&_license_1,
1255 'recommends' => \&_version_map,
1256 'requires' => \&_version_map,
1257  
1258 # other random keys are OK if already valid
1259 ':custom' => \&_keep,
1260 },
1261 );
1262  
1263 # for a given field in a spec version, what fields will it feed
1264 # into in the *latest* spec (i.e. v2); meta-spec omitted because
1265 # we always expect a meta-spec to be generated
1266 22 my %fragments_generate = (
1267 '2' => {
1268 'abstract' => 'abstract',
1269 'author' => 'author',
1270 'generated_by' => 'generated_by',
1271 'license' => 'license',
1272 'name' => 'name',
1273 'version' => 'version',
1274 'dynamic_config' => 'dynamic_config',
1275 'release_status' => 'release_status',
1276 'keywords' => 'keywords',
1277 'no_index' => 'no_index',
1278 'optional_features' => 'optional_features',
1279 'provides' => 'provides',
1280 'resources' => 'resources',
1281 'description' => 'description',
1282 'prereqs' => 'prereqs',
1283 },
1284 '1.4' => {
1285 'abstract' => 'abstract',
1286 'author' => 'author',
1287 'generated_by' => 'generated_by',
1288 'license' => 'license',
1289 'name' => 'name',
1290 'version' => 'version',
1291 'build_requires' => 'prereqs',
1292 'conflicts' => 'prereqs',
1293 'distribution_type' => 'distribution_type',
1294 'dynamic_config' => 'dynamic_config',
1295 'keywords' => 'keywords',
1296 'no_index' => 'no_index',
1297 'optional_features' => 'optional_features',
1298 'provides' => 'provides',
1299 'recommends' => 'prereqs',
1300 'requires' => 'prereqs',
1301 'resources' => 'resources',
1302 'configure_requires' => 'prereqs',
1303 },
1304 );
1305 # this is not quite true but will work well enough
1306 # as 1.4 is a superset of earlier ones
1307 22 $fragments_generate{$_} = $fragments_generate{'1.4'} for qw/1.3 1.2 1.1 1.0/;
1308  
1309 #--------------------------------------------------------------------------#
1310 # Code
1311 #--------------------------------------------------------------------------#
1312  
1313 #pod =method new
1314 #pod
1315 #pod my $cmc = CPAN::Meta::Converter->new( $struct );
1316 #pod
1317 #pod The constructor should be passed a valid metadata structure but invalid
1318 #pod structures are accepted. If no meta-spec version is provided, version 1.0 will
1319 #pod be assumed.
1320 #pod
1321 #pod Optionally, you can provide a C<default_version> argument after C<$struct>:
1322 #pod
1323 #pod my $cmc = CPAN::Meta::Converter->new( $struct, default_version => "1.4" );
1324 #pod
1325 #pod This is only needed when converting a metadata fragment that does not include a
1326 #pod C<meta-spec> field.
1327 #pod
1328 #pod =cut
1329  
1330 272 ▶sub new {
Subroutine new: called CC=1 SCAR=0.0
Pod documented
1331 272 my ($class,$data,%args) = @_;
1332  
1333 # create an attributes hash
1334 my $self = {
1335 'data' => $data,
1336 272 'spec' => _extract_spec_version($data, $args{default_version}),
1337 };
1338  
1339 # create the object
1340 272 return bless $self, $class;
1341 }
1342  
1343 516 ▶sub _extract_spec_version {
Subroutine _extract_spec_version: called CC=13 SCAR=25.9
1344 516 my ($data, $default) = @_;
1345 516 my $spec = $data->{'meta-spec'};
1346  
1347 # is meta-spec there and valid?
1348 516 ▶ return( $default || "1.0" ) unless defined $spec && ref $spec eq 'HASH'; # before meta-spec?
Branch: 1 branch
BranchTrueFalseTotal
unless defined $spec and ref $spec eq "HASH" 62 454 516
Condition100%5/5
defined $spec and ref $spec eq "HASH"
!ll&&!rl&&r
593454
$default || '1.0'
l!l
2339
MC/DC100%2/2
defined $spec and ref $spec eq "HASH"
defined $spec ref $spec eq "HASH"
MC/DC100%1/1
$default || '1.0'
$default
Truth table100%3/3
A and B
A= defined $specB= ref $spec eq "HASH"
ABresult
0X0
100
111
Truth table100%2/2
A
A= $default
Aresult
11
00
1349  
1350 # does the version key look like a valid version?
1351 454 my $v = $spec->{version};
1352 454 ▶ if ( defined $v && $v =~ /^\d+(?:\.\d+)?$/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $v and $v =~ /^\d+(?:\.\d+)?$/) 448 6 454
Condition66%2/3
defined $v and $v =~ /^\d+(?:\.\d+)?$/
!ll&&!rl&&r
06448
MC/DC50%1/2
defined $v and $v =~ /^\d+(?:\.\d+)?$/
defined $v $v =~ /^\d+(?:\.\d+)?$/
Truth table66%2/3
A and B
A= defined $vB= $v =~ /^\d+(?:\.\d+)?$/
ABresult
0X0
100
111
1353 448 ▶ return $v if defined $v && grep { $v eq $_ } keys %known_specs; # known spec
Branch: 1 branch
BranchTrueFalseTotal
if defined $v and grep {$v eq $_;} keys %known_specs 432 16 448
Condition66%2/3
defined $v and grep {$v eq $_;} keys %known_specs
!ll&&!rl&&r
016432
MC/DC50%1/2
defined $v and grep {$v eq $_;} keys %known_specs
defined $v grep {$v eq $_;} keys %known_specs
Truth table66%2/3
A and B
A= defined $vB= grep {$v eq $_;} keys %known_specs
ABresult
0X0
100
111
1354 16 ▶ return $v+0 if defined $v && grep { $v == $_ } keys %known_specs; # 2.0 => 2
Branch: 1 branch
BranchTrueFalseTotal
if defined $v and grep {$v == $_;} keys %known_specs 11 5 16
Condition66%2/3
defined $v and grep {$v == $_;} keys %known_specs
!ll&&!rl&&r
0511
MC/DC50%1/2
defined $v and grep {$v == $_;} keys %known_specs
defined $v grep {$v == $_;} keys %known_specs
Truth table66%2/3
A and B
A= defined $vB= grep {$v == $_;} keys %known_specs
ABresult
0X0
100
111
1355 }
1356  
1357 # otherwise, use heuristics: look for 1.x vs 2.0 fields
1358 11 ▶ return "2" if exists $data->{prereqs};
Branch: 1 branch
BranchTrueFalseTotal
if exists $data->{"prereqs"} 5 6 11
1359 6 ▶ return "1.4" if exists $data->{configure_requires};
Branch: 1 branch
BranchTrueFalseTotal
if exists $data->{"configure_requires"} 0 6 6
1360 6 ▶ return( $default || "1.2" ); # when meta-spec was first defined
Condition50%1/2
$default || '1.2'
l!l
06
MC/DC0%0/1
$default || '1.2'
$default
Truth table50%1/2
A
A= $default
Aresult
11
00
1361 }
1362  
1363 #pod =method convert
1364 #pod
1365 #pod my $new_struct = $cmc->convert( version => "2" );
1366 #pod
1367 #pod Returns a new hash reference with the metadata converted to a different form.
1368 #pod C<convert> will die if any conversion/standardization still results in an
1369 #pod invalid structure.
1370 #pod
1371 #pod Valid parameters include:
1372 #pod
1373 #pod =over
1374 #pod
1375 #pod =item *
1376 #pod
1377 #pod C<version> -- Indicates the desired specification version (e.g. "1.0", "1.1" ... "1.4", "2").
1378 #pod Defaults to the latest version of the CPAN Meta Spec.
1379 #pod
1380 #pod =back
1381 #pod
1382 #pod Conversion proceeds through each version in turn. For example, a version 1.2
1383 #pod structure might be converted to 1.3 then 1.4 then finally to version 2. The
1384 #pod conversion process attempts to clean-up simple errors and standardize data.
1385 #pod For example, if C<author> is given as a scalar, it will converted to an array
1386 #pod reference containing the item. (Converting a structure to its own version will
1387 #pod also clean-up and standardize.)
1388 #pod
1389 #pod When data are cleaned and standardized, missing or invalid fields will be
1390 #pod replaced with sensible defaults when possible. This may be lossy or imprecise.
1391 #pod For example, some badly structured META.yml files on CPAN have prerequisite
1392 #pod modules listed as both keys and values:
1393 #pod
1394 #pod requires => { 'Foo::Bar' => 'Bam::Baz' }
1395 #pod
1396 #pod These would be split and each converted to a prerequisite with a minimum
1397 #pod version of zero.
1398 #pod
1399 #pod When some mandatory fields are missing or invalid, the conversion will attempt
1400 #pod to provide a sensible default or will fill them with a value of 'unknown'. For
1401 #pod example a missing or unrecognized C<license> field will result in a C<license>
1402 #pod field of 'unknown'. Fields that may get an 'unknown' include:
1403 #pod
1404 #pod =for :list
1405 #pod * abstract
1406 #pod * author
1407 #pod * license
1408 #pod
1409 #pod =cut
1410  
1411 273 ▶sub convert {
Subroutine convert: called CC=16 SCAR=27.8
Pod documented
1412 273 my ($self, %args) = @_;
1413 273 my $args = { %args };
1414  
1415 273 ▶ my $new_version = $args->{version} || $HIGHEST;
Condition33%1/3
$args->{'version'} || $HIGHEST
l!l&&r!l&&!r
27300
MC/DC0%0/2
$args->{'version'} || $HIGHEST
$args->{'version'} $HIGHEST
Truth table33%1/3
A || B
A= $args->{'version'}B= $HIGHEST
ABresult
1X1
011
000
1416 273 my $is_fragment = $args->{is_fragment};
1417  
1418 273 my ($old_version) = $self->{spec};
1419 273 my $converted = _dclone($self->{data});
1420  
1421 273 ▶ if ( $old_version == $new_version ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($old_version == $new_version) { } 92 181 273
elsif ($old_version > $new_version) { } 81 100 181
1422 92 $converted = _convert( $converted, $cleanup{$old_version}, $old_version, $is_fragment );
1423 92 ▶ unless ( $args->{is_fragment} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($args->{"is_fragment"}) 61 31 92
1424 61 my $cmv = CPAN::Meta::Validator->new( $converted );
1425 61 ▶ unless ( $cmv->is_valid ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($cmv->is_valid) 0 61 61
1426 0 my $errs = join("\n", $cmv->errors);
1427 0 die "Failed to clean-up $old_version metadata. Errors:\n$errs\n";
1428 }
1429 }
1430 92 return $converted;
1431 }
1432 elsif ( $old_version > $new_version ) {
1433 81 my @vers = sort { $b <=> $a } keys %known_specs;
1434 81 for my $i ( 0 .. $#vers-1 ) {
1435 346 ▶ next if $vers[$i] > $old_version;
Branch: 1 branch
BranchTrueFalseTotal
if $vers[$i] > $old_version 80 266 346
1436 266 ▶ last if $vers[$i+1] < $new_version;
Branch: 1 branch
BranchTrueFalseTotal
if $vers[$i + 1] < $new_version 37 229 266
1437 229 my $spec_string = "$vers[$i+1]-from-$vers[$i]";
1438 229 $converted = _convert( $converted, $down_convert{$spec_string}, $vers[$i+1], $is_fragment );
1439 229 ▶ unless ( $args->{is_fragment} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($args->{"is_fragment"}) 229 0 229
1440 229 my $cmv = CPAN::Meta::Validator->new( $converted );
1441 229 ▶ unless ( $cmv->is_valid ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($cmv->is_valid) 5 224 229
1442 5 my $errs = join("\n", $cmv->errors);
1443 5 die "Failed to downconvert metadata to $vers[$i+1]. Errors:\n$errs\n";
1444 }
1445 }
1446 }
1447 76 return $converted;
1448 }
1449 else {
1450 100 my @vers = sort { $a <=> $b } keys %known_specs;
1451 100 for my $i ( 0 .. $#vers-1 ) {
1452 490 ▶ next if $vers[$i] < $old_version;
Branch: 1 branch
BranchTrueFalseTotal
if $vers[$i] < $old_version 225 265 490
1453 265 ▶ last if $vers[$i+1] > $new_version;
Branch: 1 branch
BranchTrueFalseTotal
if $vers[$i + 1] > $new_version 20 245 265
1454 245 my $spec_string = "$vers[$i+1]-from-$vers[$i]";
1455 245 $converted = _convert( $converted, $up_convert{$spec_string}, $vers[$i+1], $is_fragment );
1456 245 ▶ unless ( $args->{is_fragment} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($args->{"is_fragment"}) 233 12 245
1457 233 my $cmv = CPAN::Meta::Validator->new( $converted );
1458 233 ▶ unless ( $cmv->is_valid ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($cmv->is_valid) 5 228 233
1459 5 my $errs = join("\n", $cmv->errors);
1460 5 die "Failed to upconvert metadata to $vers[$i+1]. Errors:\n$errs\n";
1461 }
1462 }
1463 }
1464 95 return $converted;
1465 }
1466 }
1467  
1468 #pod =method upgrade_fragment
1469 #pod
1470 #pod my $new_struct = $cmc->upgrade_fragment;
1471 #pod
1472 #pod Returns a new hash reference with the metadata converted to the latest version
1473 #pod of the CPAN Meta Spec. No validation is done on the result -- you must
1474 #pod validate after merging fragments into a complete metadata document.
1475 #pod
1476 #pod Available since version 2.141170.
1477 #pod
1478 #pod =cut
1479  
1480 36 ▶sub upgrade_fragment {
Subroutine upgrade_fragment: called CC=5 SCAR=16.1
Pod documented
1481 36 my ($self) = @_;
1482 36 my ($old_version) = $self->{spec};
1483 my %expected =
1484 136 map {; $_ => 1 }
1485 156 grep { defined }
1486 156 map { $fragments_generate{$old_version}{$_} }
1487 36 keys %{ $self->{data} };
1488 36 my $converted = $self->convert( version => $HIGHEST, is_fragment => 1 );
1489 36 for my $key ( keys %$converted ) {
1490 295 ▶ next if $key =~ /^x_/i || $key eq 'meta-spec';
Branch: 1 branch
BranchTrueFalseTotal
if $key =~ /^x_/i or $key eq "meta-spec" 41 254 295
Condition100%3/3
$key =~ /^x_/i or $key eq "meta-spec"
l!l&&r!l&&!r
536254
MC/DC100%2/2
$key =~ /^x_/i or $key eq "meta-spec"
$key =~ /^x_/i $key eq "meta-spec"
Truth table100%3/3
A or B
A= $key =~ /^x_/iB= $key eq "meta-spec"
ABresult
1X1
011
000
1491 254 ▶ delete $converted->{$key} unless $expected{$key};
Branch: 1 branch
BranchTrueFalseTotal
unless $expected{$key} 122 132 254
1492 }
1493 36 return $converted;
1494 }
1495  
1496 22 1;
1497  
1498 # ABSTRACT: Convert CPAN distribution metadata structures
1499  
1500 =pod
1501  
1502 =encoding UTF-8
1503  
1504 =head1 NAME
1505  
1506 CPAN::Meta::Converter - Convert CPAN distribution metadata structures
1507  
1508 =head1 VERSION
1509  
1510 version 2.150015
1511  
1512 =head1 SYNOPSIS
1513  
1514 my $struct = Parse::CPAN::Meta->load_file('META.json');
1515 my $cmc = CPAN::Meta::Converter->new( $struct );
1516  
1517 my $new_struct = $cmc->convert( version => "2" );
1518  
1519 =head1 DESCRIPTION
1520  
1521 This module converts CPAN Meta structures from one form to another. The
1522 primary use is to convert older structures to the most modern version of
1523 the specification, but other transformations may be implemented in the
1524 future as needed. (E.g. stripping all custom fields or stripping all
1525 optional fields.)
1526  
1527 =head1 METHODS
1528  
1529 =head2 new
1530  
1531 my $cmc = CPAN::Meta::Converter->new( $struct );
1532  
1533 The constructor should be passed a valid metadata structure but invalid
1534 structures are accepted. If no meta-spec version is provided, version 1.0 will
1535 be assumed.
1536  
1537 Optionally, you can provide a C<default_version> argument after C<$struct>:
1538  
1539 my $cmc = CPAN::Meta::Converter->new( $struct, default_version => "1.4" );
1540  
1541 This is only needed when converting a metadata fragment that does not include a
1542 C<meta-spec> field.
1543  
1544 =head2 convert
1545  
1546 my $new_struct = $cmc->convert( version => "2" );
1547  
1548 Returns a new hash reference with the metadata converted to a different form.
1549 C<convert> will die if any conversion/standardization still results in an
1550 invalid structure.
1551  
1552 Valid parameters include:
1553  
1554 =over
1555  
1556 =item *
1557  
1558 C<version> -- Indicates the desired specification version (e.g. "1.0", "1.1" ... "1.4", "2").
1559 Defaults to the latest version of the CPAN Meta Spec.
1560  
1561 =back
1562  
1563 Conversion proceeds through each version in turn. For example, a version 1.2
1564 structure might be converted to 1.3 then 1.4 then finally to version 2. The
1565 conversion process attempts to clean-up simple errors and standardize data.
1566 For example, if C<author> is given as a scalar, it will converted to an array
1567 reference containing the item. (Converting a structure to its own version will
1568 also clean-up and standardize.)
1569  
1570 When data are cleaned and standardized, missing or invalid fields will be
1571 replaced with sensible defaults when possible. This may be lossy or imprecise.
1572 For example, some badly structured META.yml files on CPAN have prerequisite
1573 modules listed as both keys and values:
1574  
1575 requires => { 'Foo::Bar' => 'Bam::Baz' }
1576  
1577 These would be split and each converted to a prerequisite with a minimum
1578 version of zero.
1579  
1580 When some mandatory fields are missing or invalid, the conversion will attempt
1581 to provide a sensible default or will fill them with a value of 'unknown'. For
1582 example a missing or unrecognized C<license> field will result in a C<license>
1583 field of 'unknown'. Fields that may get an 'unknown' include:
1584  
1585 =over 4
1586  
1587 =item *
1588  
1589 abstract
1590  
1591 =item *
1592  
1593 author
1594  
1595 =item *
1596  
1597 license
1598  
1599 =back
1600  
1601 =head2 upgrade_fragment
1602  
1603 my $new_struct = $cmc->upgrade_fragment;
1604  
1605 Returns a new hash reference with the metadata converted to the latest version
1606 of the CPAN Meta Spec. No validation is done on the result -- you must
1607 validate after merging fragments into a complete metadata document.
1608  
1609 Available since version 2.141170.
1610  
1611 =head1 BUGS
1612  
1613 Please report any bugs or feature using the CPAN Request Tracker.
1614 Bugs can be submitted through the web interface at
1615 L<http://rt.cpan.org/Dist/Display.html?Queue=CPAN-Meta>
1616  
1617 When submitting a bug or request, please include a test-file or a patch to an
1618 existing test-file that illustrates the bug or desired feature.
1619  
1620 =head1 AUTHORS
1621  
1622 =over 4
1623  
1624 =item *
1625  
1626 David Golden <dagolden@cpan.org>
1627  
1628 =item *
1629  
1630 Ricardo Signes <cpan@semiotic.systems>
1631  
1632 =item *
1633  
1634 Adam Kennedy <adamk@cpan.org>
1635  
1636 =back
1637  
1638 =head1 COPYRIGHT AND LICENSE
1639  
1640 This software is copyright (c) 2010 by David Golden, Ricardo Signes, Adam Kennedy and Contributors.
1641  
1642 This is free software; you can redistribute it and/or modify it under
1643 the same terms as the Perl 5 programming language system itself.
1644  
1645 =cut
1646  
1647 __END__
« CPAN/Meta/Prereqs.pm ↑ Summary CPAN/Meta/Merge.pm »