ExtDirect/Util.pm

filter: Statementstmt 86.1% 106 / 123 Branchbran 84.3% 54 / 64 Conditioncond 95.0% 19 / 20 MC/DCmcdc 72.7% 8 / 11 Subroutinesub 92.8% 13 / 14 Podpod 0.0% 0 / 5 totaltotal 84.3% 200 / 237 CC 42 SCAR 28.2
CC
42
Cov
86%
parse_attribute
22.0
decode_metadata
20.0
parse_global_flags
19.1
CRAP 16.7
↑ Summary ExtDirect/Test/Pkg/JuiceBar.pm »
1 package RPC::ExtDirect::Util;
2  
3 33 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 33 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 33 ▶no warnings 'uninitialized'; ## no critic
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 33 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 33 ▶use JSON;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 33 ▶use base 'Exporter';
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 33 our @EXPORT_OK = qw/
13 clean_error_message
14 get_caller_info
15 parse_global_flags
16 /;
17  
18 ### EXPORTED PUBLIC PACKAGE SUBROUTINE ###
19 #
20 # Clean croak() and die() messages of file/line information
21 #
22  
23 sub clean_error_message {
24 13 ▶ my ($msg) = @_;
Subroutine clean_error_message: called CC=1 SCAR=0.0
Pod undocumented
25  
26 13 $msg =~ s/
27 (?<![,]) \s
28 at
29 .*?
30 line \s \d+(, \s <DATA> \s line \s \d+)? \.? \n*
31 (?:\s*eval \s \{...\} \s called \s at \s .*? line \s \d+ \n*)?
32 //msx;
33  
34 13 return $msg;
35 }
36  
37 ### EXPORTED PUBLIC PACKAGE SUBROUTINE ###
38 #
39 # Return formatted call stack part to use in exceptions
40 #
41  
42 sub get_caller_info {
43 11 ▶ my ($depth) = @_;
Subroutine get_caller_info: called CC=1 SCAR=0.0
Pod undocumented
44
45 11 my ($package, $sub) = (caller $depth)[3] =~ / \A (.*) :: (.*?) \z /xms;
46
47 11 return $package . '->' . $sub;
48 }
49  
50 ### EXPORTED PUBLIC PACKAGE SUBROUTINE ###
51 #
52 # Fetch the values of the (deprecated) global flags into an object,
53 # giving a warning when they're used
54 #
55  
56 sub parse_global_flags {
57 175 ▶ my ($flags, $obj) = @_;
Subroutine parse_global_flags: called CC=18 SCAR=29.5
Pod undocumented
58
59 175 my $caller_pkg = caller;
60
61 175 for my $flag ( @$flags ) {
62 2333 my $package = $flag->{package};
63 2333 my $var = $flag->{var};
64 2333 my $type = $flag->{type};
65 2333 my $fields = $flag->{setter};
66 2333 my $default = $flag->{default};
67
68 2333 my $have_default = exists $flag->{default};
69 2333 my $full_var = $package . '::' . $var;
70
71 2333 my ($value, $have_value);
72
73 {
74 33 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
75 33 ▶ no warnings 'once';
Subroutine BEGIN: called CC=1 SCAR=0.0
76
77 2333 ▶ if ( $type eq 'scalar' ) {
Branch: 3 branches
BranchTrueFalseTotal
if ($type eq 'scalar') { } 2161 172 2333
elsif ($type eq 'hash') { } 169 3 172
elsif ($type eq 'array') { } 3 0 3
78 2161 $have_value = defined ${ $full_var };
79 2161 ▶ $value = $have_value ? ${ $full_var } : $default;
Branch: 1 branch
BranchTrueFalseTotal
$have_value ? : 4 2157 2161
80 }
81 elsif ( $type eq 'hash' ) {
82 169 $have_value = %{ $full_var };
83 169 ▶ $value = $have_value ? { %{ $full_var } }
Branch: 2 branches
BranchTrueFalseTotal
$have_value ? : 1 168 169
'HASH' eq ref $default ? : 1 167 168
84 : 'HASH' eq ref $default ? { %$default }
85 : undef
86 ;
87 }
88 elsif ( $type eq 'array' ) {
89 3 $have_value = @{ $full_var };
90 3 ▶ $value = $have_value ? [ @{ $full_var } ]
Branch: 2 branches
BranchTrueFalseTotal
$have_value ? : 1 2 3
'ARRAY' eq ref $default ? : 1 1 2
91 : 'ARRAY' eq ref $default ? [ @$default ]
92 : undef
93 ;
94 }
95 else {
96 0 die "Unknown global variable type: '$type'"; # Debug mostly
97 }
98 }
99
100 2333 ▶ if ( $have_value ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($have_value) 6 2327 2333
101 6 my $warning = <<"END";
102  
103 The package global variable $full_var is deprecated
104 and is going to be removed in the next RPC::ExtDirect version.
105 END
106
107 6 ▶ if ( 'ARRAY' eq ref $fields ) {
Branch: 1 branch
BranchTrueFalseTotal
if ('ARRAY' eq ref $fields) { } 0 6 6
108
109 0 my $tpl = <<"END";
110 Use $caller_pkg instance with the following config options instead:
111 %s
112  
113 my \$config = $caller_pkg->new(
114 %s
115 );
116  
117 END
118 0 my $w1 = join ', ', map { "`$_`" } @$fields;
119 0 my $w2 = join "\n", map { "\t\t$_ => ..." } @$fields;
120
121 0 $warning .= sprintf $tpl, $w1, $w2;
122 }
123 else {
124 6 $warning .= <<"END";
125 Use the `$fields` config option with the $caller_pkg
126 instance instead:
127  
128 my \$config = $caller_pkg->new(
129 $fields => ...
130 );
131
132 END
133 }
134
135 6 warn $warning;
136 }
137  
138 2333 ▶ croak "Can't resolve the field name for var $full_var"
Branch: 1 branch
BranchTrueFalseTotal
unless $fields 0 2333 2333
139 unless $fields;
140
141 2333 ▶ $fields = [ $fields ] unless 'ARRAY' eq ref $fields;
Branch: 1 branch
BranchTrueFalseTotal
unless "ARRAY" eq ref $fields 2333 0 2333
142
143 2333 for my $field ( @$fields ) {
144 2333 my $predicate = "has_$field";
145
146 2333 ▶ $obj->$field($value)
Branch: 1 branch
BranchTrueFalseTotal
if $have_value or $have_default and not $obj->$predicate 10 2323 2333
Condition83%5/6
$have_value or $have_default and not $obj->$predicate
l!l&&r!l&&!r
652322
$have_default and not $obj->$predicate
!ll&&!rl&&r
232304
MC/DC0%0/3
$have_value or $have_default and not $obj->$predicate
$have_value $have_default not $obj->$predicate
Truth table50%2/4
A or B and not(C)
A= $have_valueB= $have_defaultC= not $obj->$predicate
ABCresult
1XX1
0111
00X0
0100
147 if $have_value || ($have_default && !$obj->$predicate());
148 }
149 }
150 }
151  
152 ### NON-EXPORTED PUBLIC PACKAGE SUBROUTINE ###
153 #
154 # Parse ExtDirect attribute, perform sanity checks and return
155 # the attribute hashref
156 #
157  
158 sub parse_attribute {
159 357 ▶ my ($package, $symbol, $referent, $attr, $data, $phase, $file, $line)
Subroutine parse_attribute: called CC=22 SCAR=30.9
Pod undocumented
160 = @_;
161  
162 357 ▶ croak "Method attribute is not ExtDirect at $file line $line"
Branch: 1 branch
BranchTrueFalseTotal
unless $attr eq "ExtDirect" 1 356 357
163 unless $attr eq 'ExtDirect';
164  
165 # Attribute::Handlers automagically parses the data into arrayref
166 # *if* it is parseable Perl (which it should be). If not, the data
167 # is going to be a garbled string which is kaput for us. However,
168 # an *empty* string means the bare attribute was used with no
169 # parameters, which is strange but is not an error.
170 356 ▶ croak "Malformed ExtDirect attribute '$data' at $file line $line"
Branch: 1 branch
BranchTrueFalseTotal
if $data ne "" and "ARRAY" ne ref $data 1 355 356
Condition100%3/3
$data ne "" and "ARRAY" ne ref $data
!ll&&!rl&&r
193361
MC/DC100%2/2
$data ne "" and "ARRAY" ne ref $data
$data ne "" "ARRAY" ne ref $data
Truth table100%3/3
A and B
A= $data ne ""B= "ARRAY" ne ref $data
ABresult
0X0
100
111
171 if $data ne '' && 'ARRAY' ne ref $data;
172  
173 33 ▶ my $symbol_name = eval { no strict 'refs'; *{$symbol}{NAME} };
Subroutine BEGIN: called CC=1 SCAR=0.0
174 355 ▶ croak "Can't resolve symbol '$symbol' for package '$package' ".
Branch: 1 branch
BranchTrueFalseTotal
if $@ 1 354 355
175 "at $file line $line: $@"
176 if $@;
177
178 # Attribute may be empty, means no argument checking
179 354 ▶ $data ||= [];
Condition100%2/2
$data ||= []
l!l
33519
MC/DC100%1/1
$data ||= []
$data
Truth table100%2/2
A
A= $data
Aresult
11
00
180  
181 # Calling convention attributes are mutually exclusive
182 354 my @calling_convention;
183  
184 my %attr;
185
186 # Compatibility form (n, ...), where n stands for (len => n)
187 354 ▶ if ( $data->[0] =~ / \A \d+ \z /xms ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($data->[0] =~ / \A \d+ \z /msx) 193 161 354
188 193 $attr{len} = shift @$data;
189 193 push @calling_convention, 'len';
190 }
191  
192 354 ▶ while ( @$data ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@$data) 314 338 652
193 314 my $param_def = shift @$data;
194
195 # len means ordered (by position) arguments
196 314 ▶ if ( $param_def =~ / \A len \z /xms ) {
Branch: 6 branches
BranchTrueFalseTotal
if ($param_def =~ / \A len \z /msx) { } 10 304 314
elsif ($param_def =~ / \A formHandler \z /msx) { } 48 256 304
elsif ($param_def =~ / \A pollHandler \z /msx) { } 18 238 256
elsif ($param_def =~ / \A params \z /imsx) { } 63 175 238
elsif ($param_def =~ / \A (before|instead|after) \z /imsx) { } 111 64 175
elsif ($param_def =~ / \A strict \z /imsx) { } 6 58 64
197 10 $attr{len} = shift @$data;
198  
199 croak "ExtDirect attribute 'len' should be followed ".
200 "by a number of ordered arguments at file $file ".
201 "line $line"
202 10 ▶ unless $attr{len} =~ / \A \d+ \z /xms;
Branch: 1 branch
BranchTrueFalseTotal
unless $attr{"len"} =~ / \A \d+ \z /msx 2 8 10
203
204 8 push @calling_convention, 'len';
205 }
206  
207 # formHandler means exactly that, a handler for form requests
208 elsif ( $param_def =~ / \A formHandler \z /xms ) {
209 48 $attr{formHandler} = 1;
210 48 push @calling_convention, 'formHandler';
211 }
212  
213 # pollHandlers are used with EventProvider
214 elsif ( $param_def =~ / \A pollHandler \z /xms ) {
215 18 $attr{pollHandler} = 1;
216 18 push @calling_convention, 'pollHandler';
217 }
218
219 # named arguments for the method
220 elsif ( $param_def =~ / \A params \z /ixms ) {
221 63 my $arg_names = shift @$data;
222  
223 63 ▶ croak "ExtDirect attribute 'params' must be followed by ".
Branch: 1 branch
BranchTrueFalseTotal
if ref $arg_names ne "ARRAY" 2 61 63
224 "arrayref at $file line $line"
225 if ref $arg_names ne 'ARRAY';
226  
227 # Copy the names
228 61 $attr{params} = [ @{ $arg_names } ];
229  
230 61 push @calling_convention, 'params';
231 }
232  
233 # Hooks
234 elsif ( $param_def =~ / \A (before|instead|after) \z /ixms ) {
235 111 my $type = $1;
236 111 my $code = shift @$data;
237  
238 111 ▶ croak "ExtDirect attribute '$type' must be followed by coderef, ".
Branch: 1 branch
BranchTrueFalseTotal
if defined $code and $code ne "NONE" and "CODE" ne ref $code 3 108 111
Condition100%6/6
defined $code and $code ne "NONE" and "CODE" ne ref $code
!ll&&!rl&&r
22863
defined $code and $code ne "NONE"
!ll&&!rl&&r
71589
MC/DC100%3/3
defined $code and $code ne "NONE" and "CODE" ne ref $code
defined $code $code ne "NONE" "CODE" ne ref $code
Truth table100%4/4
A and B and C
A= defined $codeB= $code ne "NONE"C= "CODE" ne ref $code
ABCresult
0XX0
10X0
1100
1111
239 "undef, or 'NONE' at $file line $line"
240 if defined $code && $code ne 'NONE' && 'CODE' ne ref $code;
241
242 108 $attr{ $type } = $code;
243 }
244  
245 # Strict is a boolean attribute, but let's be flexible about it
246 elsif ( $param_def =~ / \A strict \z /ixms ) {
247 6 $attr{strict} = !!(shift @$data);
248 }
249
250 # Assume a generic foo => 'bar' attribute and fall through
251 else {
252 58 $attr{ $param_def } = shift @$data;
253 }
254  
255 # There should be at most one calling convention attribute defined,
256 # but we don't care how many exactly if more than one
257 307 ▶ croak sprintf "ExtDirect attributes '%s' and '%s' are ".
Branch: 1 branch
BranchTrueFalseTotal
if @calling_convention > 1 9 298 307
258 "mutually exclusive at file $file line $line",
259 @calling_convention
260 if @calling_convention > 1;
261 };
262  
263 # strict should only be defined for named methods
264 croak "ExtDirect attribute 'strict' should be used with 'params' ".
265 "for named Methods at file $file line $line"
266 338 ▶ if exists $attr{strict} && !defined $attr{params};
Branch: 1 branch
BranchTrueFalseTotal
if exists $attr{"strict"} and not defined $attr{"params"} 4 334 338
Condition100%3/3
exists $attr{"strict"} and not defined $attr{"params"}
!ll&&!rl&&r
33224
MC/DC100%2/2
exists $attr{"strict"} and not defined $attr{"params"}
exists $attr{"strict"} not defined $attr{"params"}
Truth table100%3/3
A and not(B)
A= exists $attr{"strict"}B= not defined $attr{"params"}
ABresult
0X0
100
111
267
268 return {
269 334 package => $package,
270 method => $symbol_name,
271 %attr,
272 };
273 }
274  
275 ### NON-EXPORTED PUBLIC PACKAGE SUBROUTINE ###
276 #
277 # Decode metadata sent by the client. This function changes
278 # the passed hashref in situ (so has side effects).
279 #
280  
281 sub decode_metadata {
282 # This is a bit hacky but will do
283 0 ▶ my ($self, $keywords) = @_;
Subroutine decode_metadata: not called CC=4 SCAR=30.0
Pod undocumented
284  
285 0 my $meta_encoded = $keywords->{metadata};
286  
287 0 ▶ if ( defined $meta_encoded ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $meta_encoded) 0 0 0
288 # Whoever sends *multiple* metadata fields is going to regret it.
289 0 ▶ my $meta_json = 'ARRAY' eq ref $meta_encoded ? pop @$meta_encoded
Branch: 1 branch
BranchTrueFalseTotal
'ARRAY' eq ref $meta_encoded ? : 0 0 0
290 : $meta_encoded
291 ;
292  
293 0 local $@;
294 0 $keywords->{metadata} = eval { JSON::from_json($meta_json) };
295  
296 0 ▶ if ( $@ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 0 0
297 0 my $error = clean_error_message($@);
298 0 $self->set_error("Invalid metadata: $error");
299 }
300 }
301 }
302  
303 33 1;
304  
↑ Summary ExtDirect/Test/Pkg/JuiceBar.pm »