blib/lib/JSON/Conditional.pm

filter: Statementstmt 96.3% 104 / 108 Branchbran 70.2% 52 / 74 Conditioncond 61.1% 22 / 36 MC/DCmcdc 26.0% 6 / 23 Subroutinesub 85.7% 12 / 14 Podpod 0.0% 0 / 8 totaltotal 74.5% 196 / 263 CC 57 SCAR 29.0
CC
57
Cov
82%
expressions
33.7
loops
18.1
itterate
8.3
CRAP 18.2
↑ Summary
1 package JSON::Conditional;
2 9 ▶use 5.006; use strict; use warnings; our $VERSION = '0.01';
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
3 9 ▶use JSON; use Clone qw/clone/;
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 9 our $JSON;
6  
7 BEGIN {
8 9 ▶ $JSON = JSON->new->allow_blessed->convert_blessed;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 }
10  
11 sub new {
12 8 ▶ bless ($_[1] || {}), $_[0];
Subroutine new: called CC=2 SCAR=7.6
Pod undocumented
Condition50%1/2
$_[1] || {}
l!l
08
MC/DC0%0/1
$_[1] || {}
$_[1]
Truth table50%1/2
A
A= $_[1]
Aresult
11
00
13 }
14  
15 sub encode {
16 0 ▶ $JSON->encode($_[1]);
Subroutine encode: not called CC=1 SCAR=6.9
Pod undocumented
17 }
18  
19 sub decode {
20 0 ▶ $JSON->decode($_[1]);
Subroutine decode: not called CC=1 SCAR=6.9
Pod undocumented
21 }
22  
23 sub compile {
24 8 ▶ my ($self, $json, $params, $return_struct) = @_;
Subroutine compile: called CC=3 SCAR=11.0
Pod undocumented
25
26 8 ▶ $json = $JSON->decode($json) unless ref $json;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $json 8 0 8
27  
28 8 $json = $self->itterate($json, $params);
29  
30 8 ▶ return $return_struct ? $json : $JSON->encode($json);
Branch: 1 branch
BranchTrueFalseTotal
$return_struct ? : 7 1 8
31 }
32  
33 sub itterate {
34 77 ▶ my ($self, $json, $params) = @_;
Subroutine itterate: called CC=8 SCAR=21.2
Pod undocumented
35  
36 77 my $ref = ref $json;
37  
38 77 ▶ if ($ref eq 'HASH') {
Branch: 2 branches
BranchTrueFalseTotal
if ($ref eq 'HASH') { } 36 41 77
elsif ($ref eq 'ARRAY') { } 5 36 41
39 36 $json = $self->loops(
40 $self->conditionals($json, $params),
41 $params
42 );
43
44 36 for my $key ( keys %{$json} ) {
45 55 my $value = $self->itterate($json->{$key}, $params);
46 $value eq 'compiled_null'
47 ? delete $json->{$key}
48 55 ▶ : do {
Branch: 1 branch
BranchTrueFalseTotal
$value eq 'compiled_null' ? : 0 55 55
49 55 $json->{$key} = $value;
50 };
51 }
52  
53 36 ▶ return keys %{$json} ? $json : 'compiled_null';
Branch: 1 branch
BranchTrueFalseTotal
keys %{$json;} ? : 36 0 36
54 } elsif ($ref eq 'ARRAY') {
55 5 my $i = 0;
56 5 for my $item (@{ $json }) {
57 14 my $value = $self->itterate($item, $params);
58 $value eq 'compiled_null'
59 14 ▶ ? do {
Branch: 1 branch
BranchTrueFalseTotal
$value eq 'compiled_null' ? : 0 14 14
60 0 splice @{$json}, $i, 1;
61 }
62 : $i++;
63 }
64 }
65  
66 41 return $json;
67 }
68  
69 sub loops {
70 36 ▶ my ($self, $json, $params) = @_;
Subroutine loops: called CC=17 SCAR=29.0
Pod undocumented
71  
72 my %loops = map {
73 36 ($_ => delete $json->{$_})
74 } qw/for/;
75  
76 36 ▶ if ($loops{for}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($loops{"for"}) 5 31 36
77 5 my $key = delete $loops{for}{key};
78 5 ▶ die "no key defined for loop" unless defined $key;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $key 0 5 5
79 5 ▶ if ($loops{for}{each}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($loops{"for"}{"each"}) 3 2 5
80 3 my @each = ();
81 3 my $map = delete $loops{for}{each};
82 die "param $key must be an arrayref"
83 3 ▶ unless (ref($params->{$key}) || "") eq 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $params->{$key} || "") eq "ARRAY" 0 3 3
Condition50%1/2
ref $params->{$key} || ''
l!l
30
MC/DC0%0/1
ref $params->{$key} || ''
ref $params->{$key}
Truth table50%1/2
A
A= ref $params->{$key}
Aresult
11
00
84 3 for (@{$params->{$key}}) {
85 12 my $jsn = $self->conditionals(clone($loops{for}), $_);
86 12 ▶ push @each, $jsn if scalar keys %{$jsn};
Branch: 1 branch
BranchTrueFalseTotal
if scalar keys %{$jsn;} 12 0 12
87 }
88 3 ▶ $json->{$map} = \@each if scalar @each;
Branch: 1 branch
BranchTrueFalseTotal
if scalar @each 3 0 3
89 }
90 5 ▶ if ($loops{for}{keys}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($loops{"for"}{"keys"}) 2 3 5
91 2 my %keys = ();
92 2 my $map = delete $loops{for}{keys};
93 die "param $key muse be an hashref"
94 2 ▶ unless (ref($params->{$key}) || "") eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $params->{$key} || "") eq "HASH" 0 2 2
Condition50%1/2
ref $params->{$key} || ''
l!l
20
MC/DC0%0/1
ref $params->{$key} || ''
ref $params->{$key}
Truth table50%1/2
A
A= ref $params->{$key}
Aresult
11
00
95 2 for my $k (keys %{$params->{$key}}) {
96 my $jsn = $self->conditionals(
97 clone($loops{for}),
98 8 $params->{$key}->{$k}
99 );
100 8 ▶ $keys{$k} = $jsn if scalar keys %{$jsn};
Branch: 1 branch
BranchTrueFalseTotal
if scalar keys %{$jsn;} 8 0 8
101 }
102 2 ▶ if (scalar %keys) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar %keys) 2 0 2
103 $map =~ m/^1$/ ? do {
104 1 for my $k (keys %keys) {
105 4 $json->{$k} = $keys{$k};
106 }
107 2 ▶ } : do {
Branch: 1 branch
BranchTrueFalseTotal
$map =~ /^1$/ ? : 1 1 2
108 1 $json->{$map} = \%keys;
109 }
110 }
111 }
112 }
113  
114 36 return $json;
115 }
116  
117 sub conditionals {
118 56 ▶ my ($self, $json, $params) = @_;
Subroutine conditionals: called CC=8 SCAR=20.9
Pod undocumented
119  
120 my %keywords = map {
121 56 ($_ => delete $json->{$_})
122 } qw/if elsif else/;
123  
124 56 ▶ if ($keywords{if}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($keywords{"if"}) 23 33 56
125 23 my ($expression) = $self->expressions($keywords{if}, $params);
126 23 ▶ unless ($expression) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($expression) 15 8 23
127 15 ▶ if ($keywords{elsif}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($keywords{"elsif"}) 15 0 15
128 15 ($expression) = $self->expressions($keywords{elsif}, $params);
129 }
130 15 ▶ unless ($expression) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($expression) 6 9 15
131 6 ▶ if ($keywords{else}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($keywords{"else"}) 6 0 6
132 6 ($expression) = $keywords{else}->{then};
133 }
134 }
135 }
136 23 ▶ if ($expression) {
Branch: 1 branch
BranchTrueFalseTotal
if ($expression) 23 0 23
137 23 $json->{$_} = $expression->{$_} for ( keys %{$expression} );
138 }
139 }
140
141 56 return $json;
142 }
143  
144 sub expressions {
145 42 ▶ my ($self, $keyword, $params) = @_;
Subroutine expressions: called CC=24 SCAR=35.2
Pod undocumented
146  
147 42 my $success = 0;
148
149 $success = exists $params->{$keyword->{key}}
150 42 ▶ if defined $keyword->{exists};
Branch: 1 branch
BranchTrueFalseTotal
if defined $keyword->{"exists"} 0 42 42
151  
152 42 my $key = $params->{$keyword->{key}};
153 42 ▶ if (defined $key) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $key) 42 0 42
154 $success = $key =~ m/$keyword->{m}/
155 42 ▶ if !$success && defined $keyword->{m};
Branch: 1 branch
BranchTrueFalseTotal
if not $success and defined $keyword->{"m"} 34 8 42
Condition66%2/3
not $success and defined $keyword->{"m"}
!ll&&!rl&&r
0834
MC/DC50%1/2
not $success and defined $keyword->{"m"}
not $success defined $keyword->{"m"}
Truth table66%2/3
not(A) and B
A= not $successB= defined $keyword->{"m"}
ABresult
0X0
100
111
156 $success = $key !~ m/$keyword->{nm}/
157 42 ▶ if !$success && defined $keyword->{nm};
Branch: 1 branch
BranchTrueFalseTotal
if not $success and defined $keyword->{"nm"} 0 42 42
Condition66%2/3
not $success and defined $keyword->{"nm"}
!ll&&!rl&&r
13290
MC/DC0%0/2
not $success and defined $keyword->{"nm"}
not $success defined $keyword->{"nm"}
Truth table66%2/3
not(A) and B
A= not $successB= defined $keyword->{"nm"}
ABresult
0X0
100
111
158 $success = $key eq $keyword->{eq}
159 42 ▶ if !$success && defined $keyword->{eq};
Branch: 1 branch
BranchTrueFalseTotal
if not $success and defined $keyword->{"eq"} 2 40 42
Condition100%3/3
not $success and defined $keyword->{"eq"}
!ll&&!rl&&r
13272
MC/DC100%2/2
not $success and defined $keyword->{"eq"}
not $success defined $keyword->{"eq"}
Truth table100%3/3
not(A) and B
A= not $successB= defined $keyword->{"eq"}
ABresult
0X0
100
111
160 $success = $key ne $keyword->{ne}
161 42 ▶ if !$success && defined $keyword->{ne};
Branch: 1 branch
BranchTrueFalseTotal
if not $success and defined $keyword->{"ne"} 6 36 42
Condition100%3/3
not $success and defined $keyword->{"ne"}
!ll&&!rl&&r
15216
MC/DC100%2/2
not $success and defined $keyword->{"ne"}
not $success defined $keyword->{"ne"}
Truth table100%3/3
not(A) and B
A= not $successB= defined $keyword->{"ne"}
ABresult
0X0
100
111
162 $success = $key > $keyword->{gt}
163 42 ▶ if !$success && defined $keyword->{gt};
Branch: 1 branch
BranchTrueFalseTotal
if not $success and defined $keyword->{"gt"} 0 42 42
Condition66%2/3
not $success and defined $keyword->{"gt"}
!ll&&!rl&&r
17250
MC/DC0%0/2
not $success and defined $keyword->{"gt"}
not $success defined $keyword->{"gt"}
Truth table66%2/3
not(A) and B
A= not $successB= defined $keyword->{"gt"}
ABresult
0X0
100
111
164 $success = $key < $keyword->{lt}
165 if !$success && defined $keyword->{lt}
166 42 ▶ }
Branch: 1 branch
BranchTrueFalseTotal
if not $success and defined $keyword->{"lt"} 0 42 42
Condition66%2/3
not $success and defined $keyword->{"lt"}
!ll&&!rl&&r
17250
MC/DC0%0/2
not $success and defined $keyword->{"lt"}
not $success defined $keyword->{"lt"}
Truth table66%2/3
not(A) and B
A= not $successB= defined $keyword->{"lt"}
ABresult
0X0
100
111
167  
168 ($success, $keyword) = $self->expressions($keyword->{or}, $params)
169 42 ▶ if ($keyword->{or} && !$success);
Branch: 1 branch
BranchTrueFalseTotal
if $keyword->{"or"} and not $success 0 42 42
Condition33%1/3
$keyword->{"or"} and not $success
!ll&&!rl&&r
4200
MC/DC0%0/2
$keyword->{"or"} and not $success
$keyword->{"or"} not $success
Truth table33%1/3
A and not(B)
A= $keyword->{"or"}B= not $success
ABresult
0X0
100
111
170 ($success, $keyword) = $self->expressions($keyword->{and}, $params)
171 42 ▶ if ($keyword->{and} && $success);
Branch: 1 branch
BranchTrueFalseTotal
if $keyword->{"and"} and $success 0 42 42
Condition33%1/3
$keyword->{"and"} and $success
!ll&&!rl&&r
4200
MC/DC0%0/2
$keyword->{"and"} and $success
$keyword->{"and"} $success
Truth table33%1/3
A and B
A= $keyword->{"and"}B= $success
ABresult
0X0
100
111
172 42 ▶ if ($keyword->{elsif} && !$success) {
Branch: 1 branch
BranchTrueFalseTotal
if ($keyword->{"elsif"} and not $success) 4 38 42
Condition66%2/3
$keyword->{"elsif"} and not $success
!ll&&!rl&&r
3804
MC/DC50%1/2
$keyword->{"elsif"} and not $success
$keyword->{"elsif"} not $success
Truth table66%2/3
A and not(B)
A= $keyword->{"elsif"}B= not $success
ABresult
0X0
100
111
173 4 $keyword = $keyword->{elsif};
174 4 ($success, $keyword) = $self->expressions($keyword, $params);
175 }
176 ($success, $keyword) = ($keyword->{else}->{then}, $keyword->{else})
177 42 ▶ if ($keyword->{else} && !$success);
Branch: 1 branch
BranchTrueFalseTotal
if $keyword->{"else"} and not $success 0 42 42
Condition33%1/3
$keyword->{"else"} and not $success
!ll&&!rl&&r
4200
MC/DC0%0/2
$keyword->{"else"} and not $success
$keyword->{"else"} not $success
Truth table33%1/3
A and not(B)
A= $keyword->{"else"}B= not $success
ABresult
0X0
100
111
178  
179 42 ▶ return (($success ? $keyword->{then} : 0), $keyword);
Branch: 1 branch
BranchTrueFalseTotal
$success ? : 21 21 42
180 }
181  
182 9 1;
183  
184 __END__
↑ Summary