blib/lib/Catmandu/Fix/Condition/exists.pm

filter: Statementstmt 94.1% 32 / 34 Branchbran 50.0% 2 / 4 Conditioncond 33.3% 1 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 100% 6 / 6 Podpod 0.0% 0 / 1 totaltotal 82.0% 41 / 50 CC 4 SCAR 9.5
CC
4
Cov
85%
__ANON__
4.6
emit
1.0
BEGIN
1.0
CRAP 2.6
« blib/lib/Catmandu/Fix/paste.pm ↑ Summary blib/lib/Catmandu/Store/Hash/Bag.pm »
1 package Catmandu::Fix::Condition::exists;
2  
3 14 ▶use Catmandu::Sane;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 14 our $VERSION = '1.0201_02';
6  
7 14 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 14 ▶use namespace::clean;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 14 ▶use Catmandu::Fix::Has;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 14 with 'Catmandu::Fix::Condition';
12  
13 14 has path => (fix_arg => 1);
14  
15 sub emit {
16 44 ▶ my ($self, $fixer, $label) = @_;
Subroutine emit: called CC=1 SCAR=0.1
Pod undocumented
17 44 my $path = $fixer->split_path($self->path);
18 44 my $key = pop @$path;
19 44 my $str_key = $fixer->emit_string($key);
20  
21 my $perl = $fixer->emit_walk_path(
22 $fixer->var,
23 $path,
24 sub {
25 44 ▶ my $var = shift;
Subroutine __ANON__: called CC=4 SCAR=15.2
26 44 my $perl = "if (";
27 44 ▶ if ($key eq '$first' || $key eq '$last') {
Branch: 2 branches
BranchTrueFalseTotal
if ($key eq '$first' or $key eq '$last') { } 0 44 44
elsif ($key =~ /^\d+$/) { } 0 44 44
Condition33%1/3
$key eq "\$first" or $key eq "\$last"
l!l&&r!l&&!r
0044
MC/DC0%0/2
$key eq "\$first" or $key eq "\$last"
$key eq "\$first" $key eq "\$last"
Truth table33%1/3
A or B
A= $key eq "\$first"B= $key eq "\$last"
ABresult
1X1
011
000
28 0 $perl .= "is_array_ref(${var}) && \@{${var}}";
29 }
30 elsif ($key =~ /^\d+$/) {
31 0 $perl
32 .= "is_hash_ref(${var}) && exists(${var}->{${str_key}}) || is_array_ref(${var}) && \@{${var}} > ${key}";
33 }
34 else {
35 44 $perl
36 .= "is_hash_ref(${var}) && exists(${var}->{${str_key}})";
37 }
38 44 $perl .= ") {";
39  
40 44 $perl .= $fixer->emit_fixes($self->pass_fixes);
41  
42 44 $perl .= "last $label;";
43 44 $perl .= "}";
44 44 $perl;
45 }
46 44 );
47  
48 44 $perl .= $fixer->emit_fixes($self->fail_fixes);
49  
50 44 $perl;
51 }
52  
53 14 1;
54  
55 __END__
56  
57 =pod
58  
59 =head1 NAME
60  
61 Catmandu::Fix::Condition::exists - only execute fixes if the path exists
62  
63 =head1 SYNOPSIS
64  
65 # uppercase the value of field 'foo' if the field 'oogly' exists
66 if exists(oogly)
67 upcase(foo) # foo => 'BAR'
68 end
69 # inverted
70 unless exists(oogly)
71 upcase(foo) # foo => 'bar'
72 end
73  
74 =head1 SEE ALSO
75  
76 L<Catmandu::Fix>
77  
78 =cut
« blib/lib/Catmandu/Fix/paste.pm ↑ Summary blib/lib/Catmandu/Store/Hash/Bag.pm »