CPAN/Meta/Merge.pm

filter: Statementstmt 84.2% 112 / 133 Branchbran 66.6% 28 / 42 Conditioncond 56.5% 13 / 23 MC/DCmcdc 33.3% 4 / 12 Subroutinesub 95.4% 21 / 22 Podpod 100% 2 / 2 totaltotal 76.9% 180 / 234 CC 37 SCAR 17.4
CC
37
Cov
77%
_improvise
20.0
_optional_features
7.0
_is_identical
7.0
CRAP 5.7
« CPAN/Meta/Converter.pm ↑ Summary CPAN/Meta.pm »
1 2 ▶use 5.008001;
Subroutine BEGIN: called CC=1 SCAR=0.0
2 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 package CPAN::Meta::Merge;
6  
7 2 our $VERSION = '2.150015';
8  
9 2 ▶use Carp qw/croak/;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 2 ▶use Scalar::Util qw/blessed/;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 2 ▶use CPAN::Meta::Converter 2.141170;
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 34 ▶sub _is_identical {
Subroutine _is_identical: called CC=5 SCAR=19.4
14 34 my ($left, $right) = @_;
15 return
16 34 ▶ (not defined $left and not defined $right)
Condition50%6/12
!(defined $left || defined $right) || defined $left && defined $right && $left eq $right
l!l&&r!l&&!r
0295
defined $left || defined $right
l!l&&r!l&&!r
3400
defined $left and defined $right and $left eq $right
!ll&&!rl&&r
0529
defined $left and defined $right
!ll&&!rl&&r
0034
MC/DC20%1/5
!(defined $left || defined $right) || defined $left && defined $right && $left eq $right
defined $left defined $right defined $left defined $right $left eq $right
Truth table22%2/9
not(A || B) || C and D and E
A= defined $leftB= defined $rightC= defined $leftD= defined $rightE= $left eq $right
ABCDEresult
00XXX1
1X1111
011111
1X0XX0
1X10X0
1X1100
010XX0
0110X0
011100
17 # if either of these are references, we compare the serialized value
18 || (defined $left and defined $right and $left eq $right);
19 }
20  
21 29 ▶sub _identical {
Subroutine _identical: called CC=2 SCAR=6.9
22 29 my ($left, $right, $path) = @_;
23 29 ▶ croak sprintf "Can't merge attribute %s: '%s' does not equal '%s'", join('.', @{$path}), $left, $right
Branch: 1 branch
BranchTrueFalseTotal
unless _is_identical($left, $right) 1 28 29
24 unless _is_identical($left, $right);
25 28 return $left;
26 }
27  
28 48 ▶sub _merge {
Subroutine _merge: called CC=5 SCAR=18.7
29 48 my ($current, $next, $mergers, $path) = @_;
30 48 for my $key (keys %{$next}) {
31 202 ▶ if (not exists $current->{$key}) {
Branch: 3 branches
BranchTrueFalseTotal
if (not exists $current->{$key}) { } 131 71 202
elsif (my $merger = $mergers->{$key}) { } 71 0 71
elsif ($merger = $mergers->{':default'}) { } 0 0 0
32 131 $current->{$key} = $next->{$key};
33 }
34 elsif (my $merger = $mergers->{$key}) {
35 71 $current->{$key} = $merger->($current->{$key}, $next->{$key}, [ @{$path}, $key ]);
36 }
37 elsif ($merger = $mergers->{':default'}) {
38 0 $current->{$key} = $merger->($current->{$key}, $next->{$key}, [ @{$path}, $key ]);
39 }
40 else {
41 0 croak sprintf "Can't merge unknown attribute '%s'", join '.', @{$path}, $key;
42 }
43 }
44 41 return $current;
45 }
46  
47 10 ▶sub _uniq {
Subroutine _uniq: called CC=1 SCAR=0.0
48 10 my %seen = ();
49 10 return grep { not $seen{$_}++ } @_;
50 }
51  
52 8 ▶sub _set_addition {
Subroutine _set_addition: called CC=1 SCAR=0.0
53 8 my ($left, $right) = @_;
54 8 return [ +_uniq(@{$left}, @{$right}) ];
55 }
56  
57 7 ▶sub _uniq_map {
Subroutine _uniq_map: called CC=6 SCAR=17.9
58 7 my ($left, $right, $path) = @_;
59 7 for my $key (keys %{$right}) {
60 8 ▶ if (not exists $left->{$key}) {
Branch: 3 branches
BranchTrueFalseTotal
if (not exists $left->{$key}) { } 3 5 8
elsif (_is_identical($left->{$key}, $right->{$key})) { } 1 4 5
elsif (ref $left->{$key} eq 'HASH' and ref $right->{$key} eq 'HASH') { } 2 2 4
61 3 $left->{$key} = $right->{$key};
62 }
63 # identical strings or references are merged identically
64 elsif (_is_identical($left->{$key}, $right->{$key})) {
65 1 1; # do nothing - keep left
66 }
67 ▶ elsif (ref $left->{$key} eq 'HASH' and ref $right->{$key} eq 'HASH') {
Condition66%2/3
ref $left->{$key} eq "HASH" and ref $right->{$key} eq "HASH"
!ll&&!rl&&r
202
MC/DC50%1/2
ref $left->{$key} eq "HASH" and ref $right->{$key} eq "HASH"
ref $left->{$key} eq "HASH" ref $right->{$key} eq "HASH"
Truth table66%2/3
A and B
A= ref $left->{$key} eq "HASH"B= ref $right->{$key} eq "HASH"
ABresult
0X0
100
111
68 2 $left->{$key} = _uniq_map($left->{$key}, $right->{$key}, [ @{$path}, $key ]);
69 }
70 else {
71 2 croak 'Duplication of element ' . join '.', @{$path}, $key;
72 }
73 }
74 4 return $left;
75 }
76  
77 0 ▶sub _improvise {
Subroutine _improvise: not called CC=4 SCAR=30.0
78 0 my ($left, $right, $path) = @_;
79 0 my ($name) = reverse @{$path};
80 0 ▶ if ($name =~ /^x_/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($name =~ /^x_/) 0 0 0
81 0 ▶ if (ref($left) eq 'ARRAY') {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $left eq 'ARRAY') { } 0 0 0
elsif (ref $left eq 'HASH') { } 0 0 0
82 0 return _set_addition($left, $right, $path);
83 }
84 elsif (ref($left) eq 'HASH') {
85 0 return _uniq_map($left, $right, $path);
86 }
87 else {
88 0 return _identical($left, $right, $path);
89 }
90 }
91 0 croak sprintf "Can't merge '%s'", join '.', @{$path};
92 }
93  
94 4 ▶sub _optional_features {
Subroutine _optional_features: called CC=7 SCAR=19.5
95 4 my ($left, $right, $path) = @_;
96  
97 4 for my $key (keys %{$right}) {
98 4 ▶ if (not exists $left->{$key}) {
Branch: 1 branch
BranchTrueFalseTotal
if (not exists $left->{$key}) { } 1 3 4
99 1 $left->{$key} = $right->{$key};
100 }
101 else {
102 3 for my $subkey (keys %{ $right->{$key} }) {
103 5 ▶ next if $subkey eq 'prereqs';
Branch: 1 branch
BranchTrueFalseTotal
if $subkey eq "prereqs" 2 3 5
104 3 ▶ if (not exists $left->{$key}{$subkey}) {
Branch: 1 branch
BranchTrueFalseTotal
if (not exists $left->{$key}{$subkey}) { } 0 3 3
105 0 $left->{$key}{$subkey} = $right->{$key}{$subkey};
106 }
107 else {
108 Carp::croak "Cannot merge two optional_features named '$key' with different '$subkey' values"
109 2 ▶ if do { no warnings 'uninitialized'; $left->{$key}{$subkey} ne $right->{$key}{$subkey} };
Subroutine BEGIN: called CC=1 SCAR=0.0
Branch: 1 branch
BranchTrueFalseTotal
if do { BEGIN {${^WARNING_BITS} = "\x55\x55\x55\x55\x55\x55\x55\x55\x55\x55\x51\x55\x55\x55\x55\x55\x55\x55\x55\x55\x55\x55"} $left->{$key}{$subkey} ne $right->{$key}{$subkey} } 2 1 3
110 }
111 }
112  
113 1 require CPAN::Meta::Prereqs;
114 $left->{$key}{prereqs} =
115 CPAN::Meta::Prereqs->new($left->{$key}{prereqs})
116 1 ->with_merged_prereqs(CPAN::Meta::Prereqs->new($right->{$key}{prereqs}))
117 ->as_string_hash;
118 }
119 }
120 2 return $left;
121 }
122  
123  
124 my %default = (
125 abstract => \&_identical,
126 author => \&_set_addition,
127 2 ▶ dynamic_config => sub {
Subroutine __ANON__: called CC=2 SCAR=8.1
128 2 my ($left, $right) = @_;
129 2 ▶ return $left || $right;
Condition33%1/3
$left || $right
l!l&&r!l&&!r
020
MC/DC0%0/2
$left || $right
$left $right
Truth table33%1/3
A || B
A= $leftB= $right
ABresult
1X1
011
000
130 },
131 2 ▶ generated_by => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
132 2 my ($left, $right) = @_;
133 2 return join ', ', _uniq(split(/, /, $left), split(/, /, $right));
134 },
135 license => \&_set_addition,
136 'meta-spec' => {
137 version => \&_identical,
138 url => \&_identical
139 },
140 name => \&_identical,
141 release_status => \&_identical,
142 version => \&_identical,
143 description => \&_identical,
144 keywords => \&_set_addition,
145 8 no_index => { map { ($_ => \&_set_addition) } qw/file directory package namespace/ },
146 optional_features => \&_optional_features,
147 2 ▶ prereqs => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
148 2 require CPAN::Meta::Prereqs;
149 2 my ($left, $right) = map { CPAN::Meta::Prereqs->new($_) } @_[0,1];
150 2 return $left->with_merged_prereqs($right)->as_string_hash;
151 },
152 2 provides => \&_uniq_map,
153 resources => {
154 license => \&_set_addition,
155 homepage => \&_identical,
156 bugtracker => \&_uniq_map,
157 repository => \&_uniq_map,
158 ':default' => \&_improvise,
159 },
160 ':default' => \&_improvise,
161 );
162  
163 4 ▶sub new {
Subroutine new: called CC=5 SCAR=17.1
Pod documented
164 4 my ($class, %arguments) = @_;
165 4 ▶ croak 'default version required' if not exists $arguments{default_version};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $arguments{"default_version"} 0 4 4
166 4 my %mapping = %default;
167 4 ▶ my %extra = %{ $arguments{extra_mappings} || {} };
Condition100%2/2
$arguments{'extra_mappings'} || {}
l!l
22
MC/DC100%1/1
$arguments{'extra_mappings'} || {}
$arguments{'extra_mappings'}
Truth table100%2/2
A
A= $arguments{'extra_mappings'}
Aresult
11
00
168 4 for my $key (keys %extra) {
169 3 ▶ if (ref($mapping{$key}) eq 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $mapping{$key} eq 'HASH') { } 0 3 3
170 0 $mapping{$key} = { %{ $mapping{$key} }, %{ $extra{$key} } };
171 }
172 else {
173 3 $mapping{$key} = $extra{$key};
174 }
175 }
176 return bless {
177 default_version => $arguments{default_version},
178 4 mapping => _coerce_mapping(\%mapping, []),
179 }, $class;
180 }
181  
182 2 my %coderef_for = (
183 set_addition => \&_set_addition,
184 uniq_map => \&_uniq_map,
185 identical => \&_identical,
186 improvise => \&_improvise,
187 improvize => \&_improvise, # [sic] for backwards compatibility
188 );
189  
190 17 ▶sub _coerce_mapping {
Subroutine _coerce_mapping: called CC=5 SCAR=16.2
191 17 my ($orig, $map_path) = @_;
192 17 my %ret;
193 17 for my $key (keys %{$orig}) {
194 116 my $value = $orig->{$key};
195 116 ▶ if (ref($orig->{$key}) eq 'CODE') {
Branch: 3 branches
BranchTrueFalseTotal
if (ref $orig->{$key} eq 'CODE') { } 100 16 116
elsif (ref $value eq 'HASH') { } 13 3 16
elsif ($coderef_for{$value}) { } 3 0 3
196 100 $ret{$key} = $value;
197 }
198 elsif (ref($value) eq 'HASH') {
199 13 my $mapping = _coerce_mapping($value, [ @{$map_path}, $key ]);
200 17 ▶ $ret{$key} = sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
201 17 my ($left, $right, $path) = @_;
202 17 return _merge($left, $right, $mapping, [ @{$path} ]);
203 13 };
204 }
205 elsif ($coderef_for{$value}) {
206 3 $ret{$key} = $coderef_for{$value};
207 }
208 else {
209 0 croak "Don't know what to do with " . join '.', @{$map_path}, $key;
210 }
211 }
212 17 return \%ret;
213 }
214  
215 15 ▶sub merge {
Subroutine merge: called CC=5 SCAR=16.4
Pod documented
216 15 my ($self, @items) = @_;
217 15 my $current = {};
218 15 for my $next (@items) {
219 31 ▶ if ( blessed($next) && $next->isa('CPAN::Meta') ) {
Branch: 2 branches
BranchTrueFalseTotal
if (builtin::blessed($next) and $next->isa('CPAN::Meta')) { } 1 30 31
elsif (ref $next eq 'HASH') { } 30 0 30
Condition66%2/3
builtin::blessed($next) and $next->isa("CPAN::Meta")
!ll&&!rl&&r
3001
MC/DC50%1/2
builtin::blessed($next) and $next->isa("CPAN::Meta")
builtin::blessed($next) $next->isa("CPAN::Meta")
Truth table66%2/3
A and B
A= builtin::blessed($next)B= $next->isa("CPAN::Meta")
ABresult
0X0
100
111
220 1 $next = $next->as_struct;
221 }
222 elsif ( ref($next) eq 'HASH' ) {
223 my $cmc = CPAN::Meta::Converter->new(
224 $next, default_version => $self->{default_version}
225 30 );
226 30 $next = $cmc->upgrade_fragment;
227 }
228 else {
229 0 croak "Don't know how to merge '$next'";
230 }
231 31 $current = _merge($current, $next, $self->{mapping}, []);
232 }
233 9 return $current;
234 }
235  
236 2 1;
237  
238 # ABSTRACT: Merging CPAN Meta fragments
239  
240  
241 # vim: ts=2 sts=2 sw=2 et :
242  
243 __END__
« CPAN/Meta/Converter.pm ↑ Summary CPAN/Meta.pm »