Utils/StructuredParameters.pm

filter: Statementstmt 93.0% 160 / 172 Branchbran 79.0% 68 / 86 Conditioncond 71.7% 38 / 53 MC/DCmcdc 56.0% 14 / 25 Subroutinesub 89.4% 17 / 19 Podpod 0.0% 0 / 8 totaltotal 81.8% 297 / 363 CC 75 SCAR 30.5
CC
75
Cov
86%
_parse_data
34.1
_parse_formlike
29.5
flatten_array_value
6.0
CRAP 21.0
↑ Summary Plugin/StructuredParameters.pm »
1 package Catalyst::Utils::StructuredParameters;
2  
3 2 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 2 ▶use Storable qw(dclone);
Subroutine BEGIN: called CC=1 SCAR=0.0
5 2 ▶use Catalyst::Utils;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 2 ▶use Catalyst::Exception::MissingParameter;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 2 ▶use Catalyst::Exception::InvalidArrayPointer;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 2 ▶use Catalyst::Exception::InvalidArrayLength;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 2 our $MAX_ARRAY_DEPTH = 1000;
11  
12 2 has context => (is=>'ro', required=>1);
13 2 has _namespace => (is=>'rw', required=>0, isa=>'ArrayRef', predicate=>'has_namespace', init_arg=>'namespace');
14 2 has _flatten_array_value => (is=>'rw', required=>1, init_arg=>'flatten_array_value');
15 2 has _current => (is=>'rw', required=>0, init_arg=>undef);
16 2 has _required => (is=>'rw', required=>0, init_arg=>undef);
17 2 has _src => (is=>'ro', required=>1, init_arg=>'src');
18 2 has _max_array_depth => (is=>'ro', required=>1, init_arg=>'max_array_depth', default=>$MAX_ARRAY_DEPTH);
19  
20 2 ▶sub namespace {
Subroutine namespace: called CC=2 SCAR=7.1
Pod undocumented
21 2 my ($self, $arg) = @_;
22 2 ▶ $self->_namespace($arg) if defined($arg);
Branch: 1 branch
BranchTrueFalseTotal
if defined $arg 2 0 2
23 2 return $self;
24 }
25  
26 0 ▶sub flatten_array_value {
Subroutine flatten_array_value: not called CC=2 SCAR=17.9
Pod undocumented
27 0 my ($self, $arg) = @_;
28 0 ▶ $self->_flatten_array_value($arg) if defined($arg);
Branch: 1 branch
BranchTrueFalseTotal
if defined $arg 0 0 0
29 0 return $self;
30 }
31  
32 37 ▶sub max_array_depth {
Subroutine max_array_depth: called CC=2 SCAR=7.1
Pod undocumented
33 37 my ($self, $arg) = @_;
34 37 ▶ $self->_max_array_depth($arg) if defined($arg);
Branch: 1 branch
BranchTrueFalseTotal
if defined $arg 0 37 37
35 37 return $self;
36 }
37  
38 13 ▶sub permitted {
Subroutine permitted: called CC=4 SCAR=13.9
Pod undocumented
39 13 my ($self, @proto) = @_;
40 13 ▶ my $namespace = $self->_namespace ||[];
Condition100%2/2
$self->_namespace || []
l!l
211
MC/DC100%1/1
$self->_namespace || []
$self->_namespace
Truth table100%2/2
A
A= $self->_namespace
Aresult
11
00
41 13 $self->_required(0);
42  
43 13 ▶ if(ref $proto[0]) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $proto[0]) 7 6 13
44 7 my $namespace_affix = shift @proto;
45 7 $namespace = [ @$namespace, @$namespace_affix ];
46 }
47  
48 13 my $context = dclone($self->context);
49 13 my $parsed = $self->_parse($context, $namespace, [@proto]);
50 13 ▶ my $current = $self->_current ||+{};
Condition100%2/2
$self->_current || {}
l!l
211
MC/DC100%1/1
$self->_current || {}
$self->_current
Truth table100%2/2
A
A= $self->_current
Aresult
11
00
51 13 $current = Catalyst::Utils::merge_hashes($current, $parsed);
52 13 $self->_current($current);
53  
54 13 return $self;
55 }
56  
57 4 ▶sub required {
Subroutine required: called CC=4 SCAR=14.7
Pod undocumented
58 4 my ($self, @proto) = @_;
59 4 ▶ my $namespace = $self->_namespace ||[];
Condition50%1/2
$self->_namespace || []
l!l
04
MC/DC0%0/1
$self->_namespace || []
$self->_namespace
Truth table50%1/2
A
A= $self->_namespace
Aresult
11
00
60 4 $self->_required(1);
61  
62 4 ▶ if(ref $proto[0]) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $proto[0]) 0 4 4
63 0 my $namespace_affix = shift @proto;
64 0 $namespace = [ @$namespace, @$namespace_affix ];
65 }
66  
67 4 my $context = dclone($self->context);
68 4 my $parsed = $self->_parse($context, $namespace, [@proto]);
69 2 ▶ my $current = $self->_current ||+{};
Condition50%1/2
$self->_current || {}
l!l
20
MC/DC0%0/1
$self->_current || {}
$self->_current
Truth table50%1/2
A
A= $self->_current
Aresult
11
00
70 2 $current = Catalyst::Utils::merge_hashes($current, $parsed);
71 2 $self->_current($current);
72  
73 2 return $self;
74 }
75  
76 9 ▶sub to_hash {
Subroutine to_hash: called CC=2 SCAR=7.1
Pod undocumented
77 9 my $self = shift;
78 9 ▶ return %{ $self->_current || +{} };
Condition50%1/2
$self->_current || {}
l!l
90
MC/DC0%0/1
$self->_current || {}
$self->_current
Truth table50%1/2
A
A= $self->_current
Aresult
11
00
79 }
80  
81 0 ▶sub keys {
Subroutine keys: not called CC=2 SCAR=17.9
Pod undocumented
82 0 my $self = shift;
83 0 ▶ return CORE::keys %{ $self->_current || +{} };
Condition0%0/2
$self->_current || {}
l!l
00
MC/DC0%0/1
$self->_current || {}
$self->_current
Truth table0%0/2
A
A= $self->_current
Aresult
11
00
84 }
85  
86 1 ▶sub get {
Subroutine get: called CC=2 SCAR=7.1
Pod undocumented
87 1 my $self = shift;
88 1 ▶ return @{ $self->_current || +{} }{@_};
Condition50%1/2
$self->_current || {}
l!l
10
MC/DC0%0/1
$self->_current || {}
$self->_current
Truth table50%1/2
A
A= $self->_current
Aresult
11
00
89 }
90  
91 32 ▶sub _sorted {
Subroutine _sorted: called CC=3 SCAR=11.0
92 32 ▶ return 1 if $a eq '';
Branch: 1 branch
BranchTrueFalseTotal
if $a eq "" 3 29 32
93 29 ▶ return -1 if $b eq '';
Branch: 1 branch
BranchTrueFalseTotal
if $b eq "" 8 21 29
94 21 return $a <=> $b;
95 }
96  
97 121 ▶sub _normalize_array_value {
Subroutine _normalize_array_value: called CC=4 SCAR=13.9
98 121 my ($self, $value) = @_;
99 121 ▶ return $value unless $self->_flatten_array_value;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->_flatten_array_value 58 63 121
100 63 ▶ return ((ref($value)||'') eq 'ARRAY') ? $value->[-1] : $value;
Branch: 1 branch
BranchTrueFalseTotal
(ref $value || '') eq 'ARRAY' ? : 2 61 63
Condition100%2/2
ref $value || ''
l!l
261
MC/DC100%1/1
ref $value || ''
ref $value
Truth table100%2/2
A
A= ref $value
Aresult
11
00
101 }
102  
103 17 ▶sub _parse {
Subroutine _parse: called CC=2 SCAR=6.9
104 17 my ($self, @args) = @_;
105 17 ▶ return $self->_src eq 'data' ? $self->_parse_data(@args) : $self->_parse_formlike(@args);
Branch: 1 branch
BranchTrueFalseTotal
$self->_src eq 'data' ? : 2 15 17
106 }
107  
108 71 ▶sub _parse_formlike {
Subroutine _parse_formlike: called CC=29 SCAR=33.9
109 71 my ($self, $context, $ns, $rules) = @_;
110  
111 71 my $current = +{};
112 71 ▶ while(@{$rules}) {
Branch: 1 branch
BranchTrueFalseTotal
if (@{$rules;}) 147 69 216
113 147 my $rule = shift @{$rules};
114 147 ▶ if(ref($rule)||'' eq 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $rule or !1) { } 27 120 147
Condition100%2/2
ref $rule or
l!l
27120
MC/DC100%1/1
ref $rule or
ref $rule
Truth table100%2/2
A
A= ref $rule
Aresult
11
00
115 27 my ($local_ns, $rules) = %$rule;
116 27 my $key = join('.', @$ns, $local_ns);
117 27 my %indexes = ();
118 27 foreach my $context_field (CORE::keys %$context) {
119 717 my ($i, $under) = ($context_field =~m/^\Q$key\E\[(\d*)\]\.?(.*)$/);
120 717 ▶ next unless defined $i;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $i 645 72 717
121 72 $indexes{$i} = $under;
122 }
123  
124 27 my $found_array_depth = scalar CORE::keys %indexes;
125 27 ▶ Catalyst::Exception::InvalidArrayLength->throw(
Branch: 1 branch
BranchTrueFalseTotal
if $found_array_depth > $self->max_array_depth 0 27 27
126 pointer=>$local_ns,
127 max=>$self->max_array_depth,
128 attempted=>$found_array_depth
129 ) if $found_array_depth > $self->max_array_depth;
130  
131 27 foreach my $index(sort _sorted CORE::keys %indexes) {
132 36 my $cloned_rules = dclone($rules); # each iteration in the loop needs its own copy of the rules;
133 36 ▶ $cloned_rules = [''] unless @$cloned_rules; # to handle the bare array case
Branch: 1 branch
BranchTrueFalseTotal
unless @$cloned_rules 15 21 36
134 36 my $old = $self->_flatten_array_value;
135 36 ▶ $self->_flatten_array_value(0) if $index eq '';
Branch: 1 branch
BranchTrueFalseTotal
if $index eq "" 6 30 36
136 36 my $value = $self->_parse_formlike( $context, [@$ns, "${local_ns}[$index]"], $cloned_rules);
137 36 ▶ if(($index eq '') && $old) {
Branch: 1 branch
BranchTrueFalseTotal
if ($index eq '' and $old) { } 6 30 36
Condition66%2/3
$index eq "" and $old
!ll&&!rl&&r
3006
MC/DC50%1/2
$index eq "" and $old
$index eq "" $old
Truth table66%2/3
A and B
A= $index eq ""B= $old
ABresult
0X0
100
111
138 6 $self->_flatten_array_value($old);
139 6 ▶ if( (ref($value)||'') eq 'ARRAY') {
Branch: 2 branches
BranchTrueFalseTotal
if ((ref $value || '') eq 'ARRAY') { } 1 5 6
elsif ((ref $value || '') eq 'HASH') { } 5 0 5
Condition50%1/2
ref $value || ''
l!l
60
MC/DC0%0/1
ref $value || ''
ref $value
Truth table50%1/2
A
A= ref $value
Aresult
11
00
140 1 push @{$current->{$local_ns}}, $_ for @$value;
141 ▶ } elsif((ref($value)||'') eq 'HASH') {
Condition50%1/2
ref $value || ''
l!l
50
MC/DC0%0/1
ref $value || ''
ref $value
Truth table50%1/2
A
A= ref $value
Aresult
11
00
142 5 ▶ if(ref $value->{$indexes{$index}}) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $$value{$indexes{$index}}) { } 2 3 5
143 2 my @values = map { +{ $indexes{$index} => $_ } } @{ $value->{$indexes{$index}} };
144 2 push @{$current->{$local_ns}}, @values;
145 } else {
146 3 push @{$current->{$local_ns}}, $value;
147 }
148 }
149 } else {
150 ## I don't think these are missing params, just a row with invalid fields
151 30 ▶ next if( (ref($value)||'') eq 'HASH') && !%$value;
Branch: 1 branch
BranchTrueFalseTotal
if (ref $value || "") eq "HASH" and not %$value 1 29 30
Condition100%5/5
(ref $value || "") eq "HASH" and not %$value
!ll&&!rl&&r
14151
ref $value || ''
l!l
1614
MC/DC100%2/2
(ref $value || "") eq "HASH" and not %$value
ref $value not %$value
Truth table100%3/3
A and not(B)
A= ref $valueB= not %$value
ABresult
0X0
100
111
152 29 push @{$current->{$local_ns}}, $value;
153 }
154 }
155 } else {
156 120 ▶ if((ref($rules->[0])||'') eq 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if ((ref $rules->[0] || '') eq 'ARRAY') { } 20 100 120
Condition100%2/2
ref $rules->[0] || ''
l!l
3189
MC/DC100%1/1
ref $rules->[0] || ''
ref $rules->[0]
Truth table100%2/2
A
A= ref $rules->[0]
Aresult
11
00
157 20 my $value = $self->_parse_formlike( $context, [@$ns, $rule], shift(@$rules) );
158 20 ▶ next unless %$value; # For 'permitted';
Branch: 1 branch
BranchTrueFalseTotal
unless %$value 7 13 20
159 13 $current->{$rule} = $value;
160 } else {
161 100 ▶ if($rule eq '') {
Branch: 1 branch
BranchTrueFalseTotal
if ($rule eq '') { } 15 85 100
162 15 my $key = join('.', @$ns);
163 15 ▶ unless(defined $context->{$key}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $context->{$key}) 0 15 15
164 0 ▶ $self->_required ? Catalyst::Exception::MissingParameter->throw(param=>$key) : next;
Branch: 1 branch
BranchTrueFalseTotal
$self->_required ? : 0 0 0
165 }
166 15 $current = $self->_normalize_array_value($context->{$key});
167 } else {
168 85 my $key = join('.', @$ns, $rule);
169 85 ▶ unless(defined $context->{$key}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $context->{$key}) 29 56 85
170 29 ▶ $self->_required ? Catalyst::Exception::MissingParameter->throw(param=>$key) : next;
Branch: 1 branch
BranchTrueFalseTotal
$self->_required ? : 2 27 29
171 }
172 56 $current->{$rule} = $self->_normalize_array_value($context->{$key});
173 }
174 }
175 }
176 }
177 69 return $current;
178 }
179  
180 38 ▶sub _parse_data {
Subroutine _parse_data: called CC=29 SCAR=35.3
181 38 my ($self, $context, $ns, $rules) = @_;
182 38 my $current = +{};
183 38 ▶ MAIN: while(@{$rules}) {
Branch: 1 branch
BranchTrueFalseTotal
if (@{$rules;}) 98 38 136
184 98 my $rule = shift @{$rules};
185 98 ▶ if(ref($rule)||'' eq 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $rule or !1) { } 20 78 98
Condition100%2/2
ref $rule or
l!l
2078
MC/DC100%1/1
ref $rule or
ref $rule
Truth table100%2/2
A
A= ref $rule
Aresult
11
00
186 20 my ($local_ns, $rules) = %$rule;
187 20 my $value = $context;
188 20 foreach my $pointer (@$ns, $local_ns) {
189 30 ▶ if(exists($value->{$pointer})) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $value->{$pointer}) { } 20 10 30
190 20 $value = $value->{$pointer};
191 } else {
192 10 ▶ $self->_required ? Catalyst::Exception::MissingParameter->throw(param=>join('.', (@$ns, $local_ns))) : next MAIN;
Branch: 1 branch
BranchTrueFalseTotal
$self->_required ? : 0 10 10
193 }
194 }
195  
196 10 ▶ Catalyst::Exception::InvalidArrayPointer->throw(pointer=>join('.', (@$ns, $local_ns))) unless (ref($value)||'') eq 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $value || "") eq "ARRAY" 0 10 10
Condition50%1/2
ref $value || ''
l!l
100
MC/DC0%0/1
ref $value || ''
ref $value
Truth table50%1/2
A
A= ref $value
Aresult
11
00
197  
198 10 my $found_array_depth = scalar @$value;
199 10 ▶ Catalyst::Exception::InvalidArrayLength->throw(
Branch: 1 branch
BranchTrueFalseTotal
if $found_array_depth > $self->max_array_depth 0 10 10
200 pointer=>$local_ns,
201 max=>$self->max_array_depth,
202 attempted=>$found_array_depth
203 ) if $found_array_depth > $self->max_array_depth;
204
205 10 my @gathered = ();
206  
207 10 foreach my $item (@$value) {
208 25 my $cloned_rules = dclone($rules); # each iteration in the loop needs its own copy of the rules;
209 25 ▶ $cloned_rules = [''] unless @$cloned_rules; # to handle the bare array case
Branch: 1 branch
BranchTrueFalseTotal
unless @$cloned_rules 14 11 25
210 25 my $value = $self->_parse_data($item, [], $cloned_rules);
211 ## I don't think these are missing params, just a row with invalid fields
212 25 ▶ next if( (ref($value)||'') eq 'HASH') && !%$value;
Branch: 1 branch
BranchTrueFalseTotal
if (ref $value || "") eq "HASH" and not %$value 1 24 25
Condition100%5/5
(ref $value || "") eq "HASH" and not %$value
!ll&&!rl&&r
14101
ref $value || ''
l!l
1114
MC/DC100%2/2
(ref $value || "") eq "HASH" and not %$value
ref $value not %$value
Truth table100%3/3
A and not(B)
A= ref $valueB= not %$value
ABresult
0X0
100
111
213 24 push @gathered, $value;
214 }
215 10 $current->{$local_ns} = \@gathered;
216 } else {
217 78 ▶ if((ref($rules->[0])||'') eq 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if ((ref $rules->[0] || '') eq 'ARRAY') { } 11 67 78
Condition100%2/2
ref $rules->[0] || ''
l!l
1365
MC/DC100%1/1
ref $rules->[0] || ''
ref $rules->[0]
Truth table100%2/2
A
A= ref $rules->[0]
Aresult
11
00
218 11 my $value = $self->_parse_data( $context, [@$ns, $rule], shift(@$rules) );
219 11 ▶ next unless %$value; # For 'permitted';
Branch: 1 branch
BranchTrueFalseTotal
unless %$value 5 6 11
220 6 $current->{$rule} = $value;
221 } else {
222 67 ▶ if($rule eq '') {
Branch: 1 branch
BranchTrueFalseTotal
if ($rule eq '') { } 14 53 67
223 14 my $value = $context;
224 14 foreach my $pointer (@$ns) {
225 0 ▶ if(((ref($value)||'') eq 'HASH') && exists($value->{$pointer})) {
Branch: 1 branch
BranchTrueFalseTotal
if ((ref $value || '') eq 'HASH' and exists $value->{$pointer}) { } 0 0 0
Condition0%0/5
(ref $value || "") eq "HASH" and exists $value->{$pointer}
!ll&&!rl&&r
000
ref $value || ''
l!l
00
MC/DC0%0/2
(ref $value || "") eq "HASH" and exists $value->{$pointer}
ref $value exists $value->{$pointer}
Truth table0%0/3
A and B
A= ref $valueB= exists $value->{$pointer}
ABresult
0X0
100
111
226 0 $value = $value->{$pointer};
227 } else {
228 0 ▶ $self->_required ? Catalyst::Exception::MissingParameter->throw(param=>join('.', (@$ns, $rule))) : next MAIN;
Branch: 1 branch
BranchTrueFalseTotal
$self->_required ? : 0 0 0
229 }
230 }
231 14 $current = $self->_normalize_array_value($value);
232 } else {
233 53 my $value = $context;
234 53 foreach my $pointer (@$ns, $rule) {
235 91 ▶ if(((ref($value)||'') eq 'HASH') && exists($value->{$pointer})) {
Branch: 1 branch
BranchTrueFalseTotal
if ((ref $value || '') eq 'HASH' and exists $value->{$pointer}) { } 74 17 91
Condition100%5/5
(ref $value || "") eq "HASH" and exists $value->{$pointer}
!ll&&!rl&&r
12574
ref $value || ''
l!l
7912
MC/DC100%2/2
(ref $value || "") eq "HASH" and exists $value->{$pointer}
ref $value exists $value->{$pointer}
Truth table100%3/3
A and B
A= ref $valueB= exists $value->{$pointer}
ABresult
0X0
100
111
236 74 $value = $value->{$pointer};
237 } else {
238 17 ▶ $self->_required ? Catalyst::Exception::MissingParameter->throw(param=>join('.', (@$ns, $rule))) : next MAIN;
Branch: 1 branch
BranchTrueFalseTotal
$self->_required ? : 0 17 17
239 }
240 }
241 36 $current->{$rule} = $self->_normalize_array_value($value);
242 }
243 }
244 }
245 }
246 38 return $current;
247 }
248  
249  
250 2 __PACKAGE__->meta->make_immutable;
251  
252 2 1;
253  
254 =head1 NAME
255  
256 Catalyst::Utils::StructuredParameters - Enforce structural rules on your body and data parameters
257  
258 =head1 SYNOPSIS
259  
260 =head1 DESCRIPTION
261  
262 See L<Catalyst::TraitFor::Request::StructuredParameters> for usage. These are just utility classes
263 and not likely useful for end user unless you are rolling your own parsing or something. All
264 the publically useful docs are there.
265  
266 =head1 ATTRIBUTES
267  
268 This role defines the following attributes:
269  
270 TBD
271  
272 =head1 METHODS
273  
274 This role defines the following methods:
275  
276 TBD
277  
278 =head1 AUTHOR
279  
280 See L<Catalyst::TraitFor::Request::StructuredParameters>
281  
282 =head1 SEE ALSO
283  
284 L<Catalyst>, L<Catalyst::Request>
285  
286 =head1 COPYRIGHT & LICENSE
287  
288 See L<Catalyst::TraitFor::Request::StructuredParameters>
289  
290 =cut
↑ Summary Plugin/StructuredParameters.pm »