CPAN/Meta/Merge.pm

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