Diff/Filter.pm

filter: Statementstmt 98.1% 104 / 106 Branchbran 86.2% 69 / 80 Conditioncond 91.1% 62 / 68 MC/DCmcdc 87.1% 34 / 39 Subroutinesub 94.4% 17 / 18 Podpod 0.0% 0 / 7 totaltotal 89.9% 286 / 318 CC 68 SCAR 21.1
CC
68
Cov
93%
filter
11.7
_deep_value_filter
11.4
source_filter
10.0
CRAP 8.3
« Diff/Role/Common.pm ↑ Summary Diff/Source.pm »
1 package DBIx::Class::Schema::Diff::Filter;
2 4 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 4 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 # Further filters diff data produced by DBIx::Class::Schema::Diff
6  
7 4 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 4 with 'DBIx::Class::Schema::Diff::Role::Common';
9  
10 4 ▶use Types::Standard qw(:all);
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 86 ▶has 'mode', is => 'ro', isa => Enum[qw(limit ignore)], default => sub{'limit'};
Subroutine __ANON__: called CC=1 SCAR=0.0
13 0 ▶has 'match', is => 'ro', isa => Maybe[InstanceOf['Hash::Layout']], default => sub{undef};
Subroutine __ANON__: not called CC=1 SCAR=1.2
14  
15 4 has 'events', is => 'ro', coerce => \&_coerce_list_hash,
16 isa => Maybe[Map[Enum[qw(added changed deleted)],Bool]];
17  
18 4 has 'source_events', is => 'ro', coerce => \&_coerce_list_hash,
19 isa => Maybe[Map[Enum[qw(added changed deleted)],Bool]];
20  
21 has 'empty_match', is => 'ro', lazy => 1, default => sub {
22 86 ▶ my $self = shift;
Subroutine __ANON__: called CC=2 SCAR=6.9
23 86 ▶ return (scalar(keys %{$self->match->Data}) > 0) ? 0 : 1;
Branch: 1 branch
BranchTrueFalseTotal
scalar keys %{$self->match->Data;} > 0 ? : 81 5 86
24 4 }, init_arg => undef, isa => Bool;
25  
26 105 ▶has 'matched_paths', is => 'ro', init_arg => undef, default => sub {[]};
Subroutine __ANON__: called CC=1 SCAR=0.0
27  
28 sub filter {
29 105 ▶ my ($self, $diff) = @_;
Subroutine filter: called CC=11 SCAR=24.6
Pod undocumented
30 105 ▶ return undef unless ($diff);
Branch: 1 branch
BranchTrueFalseTotal
unless $diff 0 105 105
31
32 105 my $newd = {};
33 105 for my $s_name (keys %$diff) {
34 606 my $h = $diff->{$s_name};
35 next if (
36 $self->skip_source($s_name)
37 || $self->_is_skip( source_events => $h->{_event})
38 606 ▶ );
Branch: 1 branch
BranchTrueFalseTotal
if $self->skip_source($s_name) or $self->_is_skip("source_events", $h->{"_event"}) 164 442 606
Condition100%3/3
$self->skip_source($s_name) or $self->_is_skip("source_events", $h->{"_event"})
l!l&&r!l&&!r
14321442
MC/DC100%2/2
$self->skip_source($s_name) or $self->_is_skip("source_events", $h->{"_event"})
$self->skip_source($s_name) $self->_is_skip("source_events", $h->{"_event"})
Truth table100%3/3
A or B
A= $self->skip_source($s_name)B= $self->_is_skip("source_events", $h->{"_event"})
ABresult
1X1
011
000
39
40 442 $newd->{$s_name} = $self->source_filter( $s_name => $h );
41 442 ▶ delete $newd->{$s_name} unless (defined $newd->{$s_name});
Branch: 1 branch
BranchTrueFalseTotal
unless defined $newd->{$s_name} 0 442 442
42  
43 # Strip if the event is 'changed' but the diff data has been stripped
44 delete $newd->{$s_name} if (
45 $newd->{$s_name} &&
46 $newd->{$s_name}{_event} &&
47 $newd->{$s_name}{_event} eq 'changed' &&
48 442 ▶ scalar(keys %{$newd->{$s_name}}) == 1
Branch: 1 branch
BranchTrueFalseTotal
if $newd->{$s_name} and $newd->{$s_name}{"_event"} and $newd->{$s_name}{"_event"} eq "changed" and scalar keys %{$$newd{$s_name};} == 1 90 352 442
Condition66%6/9
$newd->{$s_name} and $newd->{$s_name}{"_event"} and $newd->{$s_name}{"_event"} eq "changed" and scalar keys %{$$newd{$s_name};} == 1
!ll&&!rl&&r
7827490
$newd->{$s_name} and $newd->{$s_name}{"_event"} and $newd->{$s_name}{"_event"} eq "changed"
!ll&&!rl&&r
078364
$newd->{$s_name} and $newd->{$s_name}{"_event"}
!ll&&!rl&&r
00442
MC/DC50%2/4
$newd->{$s_name} and $newd->{$s_name}{"_event"} and $newd->{$s_name}{"_event"} eq "changed" and scalar keys %{$$newd{$s_name};} == 1
$newd->{$s_name} $newd->{$s_name}{"_event"} $newd->{$s_name}{"_event"} eq "changed" scalar keys %{$$newd{$s_name};} == 1
Truth table60%3/5
A and B and C and D
A= $newd->{$s_name}B= $newd->{$s_name}{"_event"}C= $newd->{$s_name}{"_event"} eq "changed"D= scalar keys %{$$newd{$s_name};} == 1
ABCDresult
0XXX0
10XX0
110X0
11100
11111
49 );
50 }
51
52 105 ▶ return scalar(keys %$newd) > 0 ? $newd : undef;
Branch: 1 branch
BranchTrueFalseTotal
scalar keys %$newd > 0 ? : 105 0 105
53 }
54  
55  
56 sub source_filter {
57 442 ▶ my ($self, $s_name, $diff) = @_;
Subroutine source_filter: called CC=10 SCAR=23.1
Pod undocumented
58 442 ▶ return undef unless ($diff);
Branch: 1 branch
BranchTrueFalseTotal
unless $diff 0 442 442
59
60 442 my $newd = {};
61 442 for my $type (keys %$diff) {
62 1023 ▶ next if ($type ne '_event' && $self->skip_type($s_name => $type));
Branch: 1 branch
BranchTrueFalseTotal
if $type ne "_event" and $self->skip_type($s_name, $type) 168 855 1023
Condition100%3/3
$type ne "_event" and $self->skip_type($s_name, $type)
!ll&&!rl&&r
442413168
MC/DC100%2/2
$type ne "_event" and $self->skip_type($s_name, $type)
$type ne "_event" $self->skip_type($s_name, $type)
Truth table100%3/3
A and B
A= $type ne "_event"B= $self->skip_type($s_name, $type)
ABresult
0X0
100
111
63 855 my $val = $diff->{$type};
64 855 ▶ if($type eq 'columns' || $type eq 'relationships' || $type eq 'constraints') {
Branch: 1 branch
BranchTrueFalseTotal
if ($type eq 'columns' or $type eq 'relationships' or $type eq 'constraints') { } 280 575 855
Condition100%6/6
$type eq "columns" or $type eq "relationships" or $type eq "constraints"
l!l&&r!l&&!r
153127575
$type eq "columns" or $type eq "relationships"
l!l&&r!l&&!r
8370702
MC/DC100%3/3
$type eq "columns" or $type eq "relationships" or $type eq "constraints"
$type eq "columns" $type eq "relationships" $type eq "constraints"
Truth table100%4/4
A or B or C
A= $type eq "columns"B= $type eq "relationships"C= $type eq "constraints"
ABCresult
1XX1
01X1
0011
0000
65 280 $newd->{$type} = $self->_info_filter( $type, $s_name => $val );
66 280 ▶ delete $newd->{$type} unless (defined $newd->{$type});
Branch: 1 branch
BranchTrueFalseTotal
unless defined $newd->{$type} 48 232 280
67 }
68 else {
69 575 $newd->{$type} = $val
70 }
71 }
72
73 442 ▶ return (scalar(keys %$newd) > 0) ? $newd : undef;
Branch: 1 branch
BranchTrueFalseTotal
scalar keys %$newd > 0 ? : 442 0 442
74 }
75  
76 sub _info_filter {
77 280 ▶ my ($self, $type, $s_name, $items) = @_;
Subroutine _info_filter: called CC=10 SCAR=23.0
78 280 ▶ return undef unless ($items);
Branch: 1 branch
BranchTrueFalseTotal
unless $items 0 280 280
79  
80 280 my $new_items = {};
81  
82 280 for my $name (keys %$items) {
83 next if (
84 $self->_is_skip( 'events' => $items->{$name}{_event})
85 474 ▶ || $self->skip_type_id($s_name, $type => $name )
Branch: 1 branch
BranchTrueFalseTotal
if $self->_is_skip("events", $items->{$name}{"_event"}) or $self->skip_type_id($s_name, $type, $name) 93 381 474
Condition100%3/3
$self->_is_skip("events", $items->{$name}{"_event"}) or $self->skip_type_id($s_name, $type, $name)
l!l&&r!l&&!r
687381
MC/DC100%2/2
$self->_is_skip("events", $items->{$name}{"_event"}) or $self->skip_type_id($s_name, $type, $name)
$self->_is_skip("events", $items->{$name}{"_event"}) $self->skip_type_id($s_name, $type, $name)
Truth table100%3/3
A or B
A= $self->_is_skip("events", $items->{$name}{"_event"})B= $self->skip_type_id($s_name, $type, $name)
ABresult
1X1
011
000
86 );
87  
88 381 ▶ if($items->{$name}{_event} eq 'changed') {
Branch: 1 branch
BranchTrueFalseTotal
if ($items->{$name}{'_event'} eq 'changed') { } 195 186 381
89
90 195 my $check = $self->test_path($s_name, $type, $name);
91 195 ▶ if($check && ref($check) eq 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if ($check and ref $check eq 'HASH') { } 47 148 195
Condition100%3/3
$check and ref $check eq "HASH"
!ll&&!rl&&r
1361247
MC/DC100%2/2
$check and ref $check eq "HASH"
$check ref $check eq "HASH"
Truth table100%3/3
A and B
A= $checkB= ref $check eq "HASH"
ABresult
0X0
100
111
92 my $new_diff = $self->_deep_value_filter(
93 47 ▶ $items->{$name}{diff}, $s_name, $type, $name
Branch: 1 branch
BranchTrueFalseTotal
my $new_diff = $self->_deep_value_filter($items->{$name}{"diff"}, $s_name, $type, $name) or next 23 24 47
94 ) or next;
95
96 24 $new_items->{$name} = {
97 _event => 'changed',
98 diff => $new_diff
99 };
100 }
101 else {
102 # Allow through as-is:
103 148 $new_items->{$name} = $items->{$name};# if ($check);
104 }
105 }
106 else {
107 # Allow through as-is:
108 186 $new_items->{$name} = $items->{$name};
109 #if($self->match->lookup_leaf_path($s_name, $type, $name));
110 }
111 }
112
113 280 ▶ return scalar(keys %$new_items) > 0 ? $new_items : undef;
Branch: 1 branch
BranchTrueFalseTotal
scalar keys %$new_items > 0 ? : 232 48 280
114 }
115  
116 sub _deep_value_filter {
117 59 ▶ my ($self, $hash, @path) = @_;
Subroutine _deep_value_filter: called CC=11 SCAR=24.3
118
119 59 my $new_hash = {};
120 59 for my $k (keys %$hash) {
121 59 my $val = $hash->{$k};
122 59 my $set = $self->test_path(@path,$k);
123
124 59 ▶ if($set) {
Branch: 1 branch
BranchTrueFalseTotal
if ($set) { } 37 22 59
125 37 ▶ if($val && ref($val) eq 'HASH' && ref($set) eq 'HASH' && scalar(keys %$val) > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($val and ref $val eq 'HASH' and ref $set eq 'HASH' and scalar keys %$val > 0) { } 12 25 37
Condition77%7/9
$val and ref $val eq "HASH" and ref $set eq "HASH" and scalar keys %$val > 0
!ll&&!rl&&r
25012
$val and ref $val eq "HASH" and ref $set eq "HASH"
!ll&&!rl&&r
16912
$val and ref $val eq "HASH"
!ll&&!rl&&r
01621
MC/DC50%2/4
$val and ref $val eq "HASH" and ref $set eq "HASH" and scalar keys %$val > 0
$val ref $val eq "HASH" ref $set eq "HASH" scalar keys %$val > 0
Truth table60%3/5
A and B and C and D
A= $valB= ref $val eq "HASH"C= ref $set eq "HASH"D= scalar keys %$val > 0
ABCDresult
0XXX0
10XX0
110X0
11100
11111
126 12 $new_hash->{$k} = $self->_deep_value_filter($val,@path,$k);
127 12 ▶ delete $new_hash->{$k} unless (defined $new_hash->{$k});
Branch: 1 branch
BranchTrueFalseTotal
unless defined $new_hash->{$k} 0 12 12
128 }
129 else {
130 25 ▶ next if ($self->mode eq 'ignore');
Branch: 1 branch
BranchTrueFalseTotal
if $self->mode eq "ignore" 1 24 25
131 24 $new_hash->{$k} = $val;
132 }
133 }
134 else {
135 22 ▶ next if ($self->mode eq 'limit');
Branch: 1 branch
BranchTrueFalseTotal
if $self->mode eq "limit" 22 0 22
136 0 $new_hash->{$k} = $val;
137 }
138 }
139
140 59 ▶ return scalar(keys %$new_hash) > 0 ? $new_hash : undef;
Branch: 1 branch
BranchTrueFalseTotal
scalar keys %$new_hash > 0 ? : 36 23 59
141 }
142  
143  
144 sub _is_skip {
145 937 ▶ my ($self, $meth, $key) = @_;
Subroutine _is_skip: called CC=4 SCAR=13.9
146 937 my $h = $self->$meth;
147 937 ▶ $self->mode eq 'limit' ? $h && ! $h->{$key} : $h && $h->{$key};
Branch: 1 branch
BranchTrueFalseTotal
$self->mode eq 'limit' ? : 714 223 937
Condition100%6/6
$h and not $h->{$key}
!ll&&!rl&&r
70617
$h and $h->{$key}
!ll&&!rl&&r
1841920
MC/DC100%2/2
$h and not $h->{$key}
$h not $h->{$key}
MC/DC100%2/2
$h and $h->{$key}
$h $h->{$key}
Truth table100%3/3
A and not(B)
A= $hB= not $h->{$key}
ABresult
0X0
100
111
Truth table100%3/3
A and B
A= $hB= $h->{$key}
ABresult
0X0
100
111
148 }
149  
150  
151 sub skip_source {
152 606 ▶ my ($self, $s_name) = @_;
Subroutine skip_source: called CC=8 SCAR=20.8
Pod undocumented
153 606 ▶ my $HL = $self->match or return 0;
Branch: 1 branch
BranchTrueFalseTotal
my $HL = $self->match or return 0 0 606 606
154 606 ▶ my $set = $self->test_path($s_name) || 0;
Condition100%2/2
$self->test_path($s_name) || 0
l!l
355251
MC/DC100%1/1
$self->test_path($s_name) || 0
$self->test_path($s_name)
Truth table100%2/2
A
A= $self->test_path($s_name)
Aresult
11
00
155
156 606 ▶ if($self->mode eq 'limit') {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->mode eq 'limit') { } 489 117 606
157 489 ▶ return 0 if ($self->empty_match);
Branch: 1 branch
BranchTrueFalseTotal
if $self->empty_match 40 449 489
158 449 ▶ return $set ? 0 : 1;
Branch: 1 branch
BranchTrueFalseTotal
$set ? : 310 139 449
159 }
160 else {
161 117 ▶ return $set && ! ref($set) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$set && !ref($set) ? : 4 113 117
Condition100%3/3
$set and not ref $set
!ll&&!rl&&r
72414
MC/DC100%2/2
$set and not ref $set
$set not ref $set
Truth table100%3/3
A and not(B)
A= $setB= not ref $set
ABresult
0X0
100
111
162 }
163 }
164  
165 sub skip_type {
166 581 ▶ my ($self, $s_name, $type) = @_;
Subroutine skip_type: called CC=8 SCAR=20.8
Pod undocumented
167 581 ▶ my $HL = $self->match or return 0;
Branch: 1 branch
BranchTrueFalseTotal
my $HL = $self->match or return 0 0 581 581
168 581 my $set = $self->test_path($s_name,$type);
169
170 581 ▶ if($self->mode eq 'limit') {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->mode eq 'limit') { } 466 115 581
171 466 ▶ return 0 if ($self->empty_match);
Branch: 1 branch
BranchTrueFalseTotal
if $self->empty_match 44 422 466
172 # If this source/type is set, OR if the entire source is included:
173 422 ▶ return $set || $self->test_leaf_path($s_name) ? 0 : 1;
Branch: 1 branch
BranchTrueFalseTotal
$set || $self->test_leaf_path($s_name) ? : 270 152 422
Condition100%3/3
$set or $self->test_leaf_path($s_name)
l!l&&r!l&&!r
22050152
MC/DC100%2/2
$set or $self->test_leaf_path($s_name)
$set $self->test_leaf_path($s_name)
Truth table100%3/3
A or B
A= $setB= $self->test_leaf_path($s_name)
ABresult
1X1
011
000
174 }
175 else {
176 115 ▶ return $set && ! ref($set) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$set && !ref($set) ? : 16 99 115
Condition100%3/3
$set and not ref $set
!ll&&!rl&&r
732616
MC/DC100%2/2
$set and not ref $set
$set not ref $set
Truth table100%3/3
A and not(B)
A= $setB= not ref $set
ABresult
0X0
100
111
177 }
178 }
179  
180 sub skip_type_id {
181 468 ▶ my ($self, $s_name, $type, $id) = @_;
Subroutine skip_type_id: called CC=9 SCAR=22.0
Pod undocumented
182  
183 468 ▶ my $HL = $self->match or return 0;
Branch: 1 branch
BranchTrueFalseTotal
my $HL = $self->match or return 0 0 468 468
184 468 my $set = $self->test_path($s_name,$type,$id);
185
186 468 ▶ if($self->mode eq 'limit') {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->mode eq 'limit') { } 364 104 468
187 364 ▶ return 0 if ($self->empty_match);
Branch: 1 branch
BranchTrueFalseTotal
if $self->empty_match 52 312 364
188 # If this source/type is set, OR if the entire source or source/type is included:
189 312 ▶ return $set
Branch: 1 branch
BranchTrueFalseTotal
$set || $self->test_leaf_path($s_name) || $self->test_leaf_path($s_name, $type) ? : 236 76 312
Condition100%6/6
$set or $self->test_leaf_path($s_name) or $self->test_leaf_path($s_name, $type)
l!l&&r!l&&!r
13410276
$set or $self->test_leaf_path($s_name)
l!l&&r!l&&!r
8648178
MC/DC100%3/3
$set or $self->test_leaf_path($s_name) or $self->test_leaf_path($s_name, $type)
$set $self->test_leaf_path($s_name) $self->test_leaf_path($s_name, $type)
Truth table100%4/4
A or B or C
A= $setB= $self->test_leaf_path($s_name)C= $self->test_leaf_path($s_name, $type)
ABCresult
1XX1
01X1
0011
0000
190 || $self->test_leaf_path($s_name)
191 || $self->test_leaf_path($s_name,$type) ? 0 : 1;
192 }
193 else {
194 104 ▶ return $set && ! ref($set) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$set && !ref($set) ? : 11 93 104
Condition100%3/3
$set and not ref $set
!ll&&!rl&&r
90311
MC/DC100%2/2
$set and not ref $set
$set not ref $set
Truth table100%3/3
A and not(B)
A= $setB= not ref $set
ABresult
0X0
100
111
195 }
196 }
197  
198 sub test_path {
199 1909 ▶ my ($self, @path) = @_;
Subroutine test_path: called CC=2 SCAR=6.9
Pod undocumented
200 1909 ▶ return $self->test_leaf_path(@path) || $self->match->lookup_path(@path);
Condition100%3/3
$self->test_leaf_path(@path) || $self->match->lookup_path(@path)
l!l&&r!l&&!r
2245891096
MC/DC100%2/2
$self->test_leaf_path(@path) || $self->match->lookup_path(@path)
$self->test_leaf_path(@path) $self->match->lookup_path(@path)
Truth table100%3/3
A || B
A= $self->test_leaf_path(@path)B= $self->match->lookup_path(@path)
ABresult
1X1
011
000
201 }
202  
203 sub test_leaf_path {
204 2515 ▶ my ($self, @path) = @_;
Subroutine test_leaf_path: called CC=3 SCAR=11.0
Pod undocumented
205 2515 my $ret = $self->match->lookup_leaf_path(@path);
206 2515 ▶ push @{$self->matched_paths}, \@path if (
Branch: 1 branch
BranchTrueFalseTotal
if $ret and not ref $ret 424 2091 2515
Condition66%2/3
$ret and not ref $ret
!ll&&!rl&&r
20910424
MC/DC50%1/2
$ret and not ref $ret
$ret not ref $ret
Truth table66%2/3
A and not(B)
A= $retB= not ref $ret
ABresult
0X0
100
111
207 $ret
208 # We don't want to record the path as "matched" for empty HashRef {} leafs
209 && ! ref $ret
210 );
211 2515 return $ret;
212 }
213  
214 4 1;
215  
216  
217 __END__
« Diff/Role/Common.pm ↑ Summary Diff/Source.pm »