Utils/StrongParameters.pm

filter: Statementstmt 93.5% 130 / 139 Branchbran 80.0% 56 / 70 Conditioncond 78.5% 33 / 42 MC/DCmcdc 68.4% 13 / 19 Subroutinesub 93.3% 14 / 15 Podpod 0.0% 0 / 5 totaltotal 84.8% 246 / 290 CC 61 SCAR 29.0
CC
61
Cov
87%
_parse_data
32.6
_parse_formlike
19.1
flatten_array_value
6.0
CRAP 18.2
↑ Summary Plugin/StrongParameters.pm »
1 package Catalyst::Utils::StrongParameters;
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  
9 2 has context => (is=>'ro', required=>1);
10 2 has _namespace => (is=>'rw', required=>0, isa=>'ArrayRef', predicate=>'has_namespace', init_arg=>'namespace');
11 2 has _flatten_array_value => (is=>'ro', required=>1, init_arg=>'flatten_array_value');
12 2 has _current => (is=>'rw', required=>0, init_arg=>undef);
13 2 has _required => (is=>'rw', required=>0, init_arg=>undef);
14 2 has _src => (is=>'ro', required=>1, init_arg=>'src');
15  
16 2 ▶sub namespace {
Subroutine namespace: called CC=2 SCAR=7.1
Pod undocumented
17 2 my ($self, $arg) = @_;
18 2 ▶ $self->_namespace($arg) if defined($arg);
Branch: 1 branch
BranchTrueFalseTotal
if defined $arg 2 0 2
19 2 return $self;
20 }
21 0 ▶sub flatten_array_value {
Subroutine flatten_array_value: not called CC=2 SCAR=17.9
Pod undocumented
22 0 my ($self, $arg) = @_;
23 0 ▶ $self->_flatten_array_value($arg) if defined($arg);
Branch: 1 branch
BranchTrueFalseTotal
if defined $arg 0 0 0
24 0 return $self;
25 }
26  
27 10 ▶sub permitted {
Subroutine permitted: called CC=4 SCAR=13.9
Pod undocumented
28 10 my ($self, @proto) = @_;
29 10 ▶ my $namespace = $self->_namespace ||[];
Condition100%2/2
$self->_namespace || []
l!l
28
MC/DC100%1/1
$self->_namespace || []
$self->_namespace
Truth table100%2/2
A
A= $self->_namespace
Aresult
11
00
30 10 $self->_required(0);
31  
32 10 ▶ if(ref $proto[0]) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $proto[0]) 4 6 10
33 4 my $namespace_affix = shift @proto;
34 4 $namespace = [ @$namespace, @$namespace_affix ];
35 }
36  
37 10 my $context = dclone($self->context);
38 10 my $parsed = $self->_parse($context, $namespace, [@proto]);
39 10 ▶ my $current = $self->_current ||+{};
Condition100%2/2
$self->_current || {}
l!l
28
MC/DC100%1/1
$self->_current || {}
$self->_current
Truth table100%2/2
A
A= $self->_current
Aresult
11
00
40 10 $current = Catalyst::Utils::merge_hashes($current, $parsed);
41 10 $self->_current($current);
42  
43 10 return $self;
44 }
45  
46 4 ▶sub required {
Subroutine required: called CC=4 SCAR=14.7
Pod undocumented
47 4 my ($self, @proto) = @_;
48 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
49 4 $self->_required(1);
50  
51 4 ▶ if(ref $proto[0]) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $proto[0]) 0 4 4
52 0 my $namespace_affix = shift @proto;
53 0 $namespace = [ @$namespace, @$namespace_affix ];
54 }
55  
56 4 my $context = dclone($self->context);
57 4 my $parsed = $self->_parse($context, $namespace, [@proto]);
58 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
59 2 $current = Catalyst::Utils::merge_hashes($current, $parsed);
60 2 $self->_current($current);
61  
62 2 return $self;
63 }
64  
65 6 ▶sub to_hash {
Subroutine to_hash: called CC=2 SCAR=7.1
Pod undocumented
66 6 my $self = shift;
67 6 ▶ return %{ $self->_current || +{} };
Condition50%1/2
$self->_current || {}
l!l
60
MC/DC0%0/1
$self->_current || {}
$self->_current
Truth table50%1/2
A
A= $self->_current
Aresult
11
00
68 }
69  
70 25 ▶sub _sorted {
Subroutine _sorted: called CC=3 SCAR=11.0
71 25 ▶ return 1 if $a eq '';
Branch: 1 branch
BranchTrueFalseTotal
if $a eq "" 2 23 25
72 23 ▶ return -1 if $b eq '';
Branch: 1 branch
BranchTrueFalseTotal
if $b eq "" 4 19 23
73 19 return $a <=> $b;
74 }
75  
76 112 ▶sub _normalize_array_value {
Subroutine _normalize_array_value: called CC=4 SCAR=13.9
77 112 my ($self, $value) = @_;
78 112 ▶ return $value unless $self->_flatten_array_value;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->_flatten_array_value 50 62 112
79 62 ▶ return ((ref($value)||'') eq 'ARRAY') ? $value->[-1] : $value;
Branch: 1 branch
BranchTrueFalseTotal
(ref $value || '') eq 'ARRAY' ? : 2 60 62
Condition100%2/2
ref $value || ''
l!l
260
MC/DC100%1/1
ref $value || ''
ref $value
Truth table100%2/2
A
A= ref $value
Aresult
11
00
80 }
81  
82 14 ▶sub _parse {
Subroutine _parse: called CC=2 SCAR=6.9
83 14 my ($self, @args) = @_;
84 14 ▶ return $self->_src eq 'data' ? $self->_parse_data(@args) : $self->_parse_formlike(@args);
Branch: 1 branch
BranchTrueFalseTotal
$self->_src eq 'data' ? : 2 12 14
85 }
86  
87 56 ▶sub _parse_formlike {
Subroutine _parse_formlike: called CC=19 SCAR=29.5
88 56 my ($self, $context, $ns, $rules) = @_;
89 56 my $current = +{};
90 56 ▶ while(@{$rules}) {
Branch: 1 branch
BranchTrueFalseTotal
if (@{$rules;}) 130 54 184
91 130 my $rule = shift @{$rules};
92 130 ▶ if(ref($rule)||'' eq 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $rule or !1) { } 24 106 130
Condition100%2/2
ref $rule or
l!l
24106
MC/DC100%1/1
ref $rule or
ref $rule
Truth table100%2/2
A
A= ref $rule
Aresult
11
00
93 24 my ($local_ns, $rules) = %$rule;
94 24 my $key = join('.', @$ns, $local_ns);
95 24 my %indexes = ();
96 24 foreach my $context_field (keys %$context) {
97 698 my ($i, $under) = ($context_field =~m/^\Q$key\E\[(\d*)\]\.?(.*)$/);
98 698 ▶ next unless defined $i;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $i 636 62 698
99 62 $indexes{$i} = $under;
100 }
101 24 foreach my $index(sort _sorted keys %indexes) {
102 27 my $cloned_rules = dclone($rules); # each iteration in the loop needs its own copy of the rules;
103 27 ▶ $cloned_rules = [''] unless @$cloned_rules; # to handle the bare array case
Branch: 1 branch
BranchTrueFalseTotal
unless @$cloned_rules 14 13 27
104 27 my $value = $self->_parse_formlike( $context, [@$ns, "${local_ns}[$index]"], $cloned_rules);
105 ## I don't think these are missing params, just a row with invalid fields
106 27 ▶ next if( (ref($value)||'') eq 'HASH') && !%$value;
Branch: 1 branch
BranchTrueFalseTotal
if (ref $value || "") eq "HASH" and not %$value 1 26 27
Condition100%5/5
(ref $value || "") eq "HASH" and not %$value
!ll&&!rl&&r
14121
ref $value || ''
l!l
1314
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
107 26 push @{$current->{$local_ns}}, $value;
108 }
109 } else {
110 106 ▶ if((ref($rules->[0])||'') eq 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if ((ref $rules->[0] || '') eq 'ARRAY') { } 17 89 106
Condition100%2/2
ref $rules->[0] || ''
l!l
2878
MC/DC100%1/1
ref $rules->[0] || ''
ref $rules->[0]
Truth table100%2/2
A
A= ref $rules->[0]
Aresult
11
00
111 17 my $value = $self->_parse_formlike( $context, [@$ns, $rule], shift(@$rules) );
112 17 ▶ next unless %$value; # For 'permitted';
Branch: 1 branch
BranchTrueFalseTotal
unless %$value 7 10 17
113 10 $current->{$rule} = $value;
114 } else {
115 89 ▶ if($rule eq '') {
Branch: 1 branch
BranchTrueFalseTotal
if ($rule eq '') { } 14 75 89
116 14 my $key = join('.', @$ns);
117 14 ▶ unless(defined $context->{$key}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $context->{$key}) 0 14 14
118 0 ▶ $self->_required ? Catalyst::Exception::MissingParameter->throw(param=>$key) : next;
Branch: 1 branch
BranchTrueFalseTotal
$self->_required ? : 0 0 0
119 }
120 14 $current = $self->_normalize_array_value($context->{$key});
121 } else {
122 75 my $key = join('.', @$ns, $rule);
123 75 ▶ unless(defined $context->{$key}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $context->{$key}) 27 48 75
124 27 ▶ $self->_required ? Catalyst::Exception::MissingParameter->throw(param=>$key) : next;
Branch: 1 branch
BranchTrueFalseTotal
$self->_required ? : 2 25 27
125 }
126 48 $current->{$rule} = $self->_normalize_array_value($context->{$key});
127 }
128 }
129 }
130 }
131 54 return $current;
132 }
133  
134 38 ▶sub _parse_data {
Subroutine _parse_data: called CC=28 SCAR=34.8
135 38 my ($self, $context, $ns, $rules) = @_;
136 38 my $current = +{};
137 38 ▶ MAIN: while(@{$rules}) {
Branch: 1 branch
BranchTrueFalseTotal
if (@{$rules;}) 98 38 136
138 98 my $rule = shift @{$rules};
139 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
140 20 my ($local_ns, $rules) = %$rule;
141 20 my $value = $context;
142 20 foreach my $pointer (@$ns, $local_ns) {
143 30 ▶ if(exists($value->{$pointer})) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $value->{$pointer}) { } 20 10 30
144 20 $value = $value->{$pointer};
145 } else {
146 10 ▶ $self->_required ? Catalyst::Exception::MissingParameter->throw(param=>join('.', (@$ns, $local_ns))) : next MAIN;
Branch: 1 branch
BranchTrueFalseTotal
$self->_required ? : 0 10 10
147 }
148 }
149  
150 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
151 10 my @gathered = ();
152 10 foreach my $item (@$value) {
153 25 my $cloned_rules = dclone($rules); # each iteration in the loop needs its own copy of the rules;
154 25 ▶ $cloned_rules = [''] unless @$cloned_rules; # to handle the bare array case
Branch: 1 branch
BranchTrueFalseTotal
unless @$cloned_rules 14 11 25
155 25 my $value = $self->_parse_data($item, [], $cloned_rules);
156 ## I don't think these are missing params, just a row with invalid fields
157 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
158 24 push @gathered, $value;
159 }
160 10 $current->{$local_ns} = \@gathered;
161 } else {
162 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
163 11 my $value = $self->_parse_data( $context, [@$ns, $rule], shift(@$rules) );
164 11 ▶ next unless %$value; # For 'permitted';
Branch: 1 branch
BranchTrueFalseTotal
unless %$value 5 6 11
165 6 $current->{$rule} = $value;
166 } else {
167 67 ▶ if($rule eq '') {
Branch: 1 branch
BranchTrueFalseTotal
if ($rule eq '') { } 14 53 67
168 14 my $value = $context;
169 14 foreach my $pointer (@$ns) {
170 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
171 0 $value = $value->{$pointer};
172 } else {
173 0 ▶ $self->_required ? Catalyst::Exception::MissingParameter->throw(param=>join('.', (@$ns, $rule))) : next MAIN;
Branch: 1 branch
BranchTrueFalseTotal
$self->_required ? : 0 0 0
174 }
175 }
176 14 $current = $self->_normalize_array_value($value);
177 } else {
178 53 my $value = $context;
179 53 foreach my $pointer (@$ns, $rule) {
180 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
181 74 $value = $value->{$pointer};
182 } else {
183 17 ▶ $self->_required ? Catalyst::Exception::MissingParameter->throw(param=>join('.', (@$ns, $rule))) : next MAIN;
Branch: 1 branch
BranchTrueFalseTotal
$self->_required ? : 0 17 17
184 }
185 }
186 36 $current->{$rule} = $self->_normalize_array_value($value);
187 }
188 }
189 }
190 }
191 38 return $current;
192 }
193  
194  
195 2 __PACKAGE__->meta->make_immutable;
196  
197 2 1;
198  
199 =head1 NAME
200  
201 Catalyst::Utils::StrongParameters - Enforce structural rules on your body and data parameters
202  
203 =head1 SYNOPSIS
204  
205 =head1 DESCRIPTION
206  
207 See L<Catalyst::TraitFor::Request::StrongParameters> for usage. These are just utility classes
208 and not likely useful for end user unless you are rolling your own parsing or something. All
209 the publically useful docs are there.
210  
211 =head1 ATTRIBUTES
212  
213 This role defines the following attributes:
214  
215 TBD
216  
217 =head1 METHODS
218  
219 This role defines the following methods:
220  
221 TBD
222  
223 =head1 AUTHOR
224  
225 See L<Catalyst::TraitFor::Request::StrongParameters>
226  
227 =head1 SEE ALSO
228  
229 L<Catalyst>, L<Catalyst::Request>
230  
231 =head1 COPYRIGHT & LICENSE
232  
233 See L<Catalyst::TraitFor::Request::StrongParameters>
234  
235 =cut
↑ Summary Plugin/StrongParameters.pm »