blib/lib/Data/Clean.pm

filter: Statementstmt 87.2% 158 / 181 Branchbran 61.1% 33 / 54 Conditioncond 52.1% 12 / 23 MC/DCmcdc 30.7% 4 / 13 Subroutinesub 80.7% 21 / 26 Podpod 20.0% 3 / 15 totaltotal 74.0% 231 / 312 CC 45 SCAR 29.6
CC
45
Cov
79%
_generate_cleanser_code
40.1
__ANON__
9.6
new
4.0
CRAP 19.2
↑ Summary
1 package Data::Clean;
2  
3 2 ▶use 5.010001;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 2 ▶use Log::ger;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 2 our $AUTHORITY = 'cpan:PERLANCAR'; # AUTHORITY
9 2 our $DATE = '2022-08-28'; # DATE
10 2 our $DIST = 'Data-Clean'; # DIST
11 2 our $VERSION = '0.508'; # VERSION
12  
13 11 ▶sub new {
Subroutine new: called CC=4 SCAR=13.9
Pod documented
14 11 my ($class, %opts) = @_;
15 11 my $self = bless {_opts=>\%opts}, $class;
16 11 log_trace("Cleanser options: %s", \%opts);
17  
18 11 my $cd = $self->_generate_cleanser_code;
19 9 for my $mod (keys %{ $cd->{modules} }) {
20 18 (my $mod_pm = "$mod.pm") =~ s!::!/!g;
21 18 require $mod_pm;
22 }
23 9 $self->{_cd} = $cd;
24 9 $self->{_code} = eval $cd->{src}; ## no critic: BuiltinFunctions::ProhibitStringyEval
25 {
26 9 ▶ last unless $cd->{clone_func} =~ /(.+)::(.+)/;
Branch: 1 branch
BranchTrueFalseTotal
unless $cd->{"clone_func"} =~ /(.+)::(.+)/ 0 9 9
27 9 (my $mod_pm = "$1.pm") =~ s!::!/!g;
28 9 require $mod_pm;
29 }
30 9 ▶ die "Can't generate code: $@" if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 9 9
31  
32 9 $self;
33 }
34  
35 1 ▶sub command_call_method {
Subroutine command_call_method: called CC=2 SCAR=7.6
Pod undocumented
36 1 my ($self, $cd, $args) = @_;
37 1 my $mn = $args->[0];
38 1 ▶ die "Invalid method name syntax" unless $mn =~ /\A\w+\z/;
Branch: 1 branch
BranchTrueFalseTotal
unless $mn =~ /\A\w+\z/ 1 0 1
39 0 return "{{var}} = {{var}}->$mn; \$ref = ref({{var}})";
40 }
41  
42 2 ▶sub command_call_func {
Subroutine command_call_func: called CC=2 SCAR=6.9
Pod undocumented
43 2 my ($self, $cd, $args) = @_;
44 2 my $fn = $args->[0];
45 2 ▶ die "Invalid func name syntax" unless $fn =~ /\A\w+(::\w+)*\z/;
Branch: 1 branch
BranchTrueFalseTotal
unless $fn =~ /\A\w+(::\w+)*\z/ 1 1 2
46 1 return "{{var}} = $fn({{var}}); \$ref = ref({{var}})";
47 }
48  
49 0 ▶sub command_one_or_zero {
Subroutine command_one_or_zero: not called CC=1 SCAR=6.9
Pod undocumented
50 0 my ($self, $cd, $args) = @_;
51 0 return "{{var}} = {{var}} ? 1:0; \$ref = ''";
52 }
53  
54 0 ▶sub command_deref_scalar_one_or_zero {
Subroutine command_deref_scalar_one_or_zero: not called CC=1 SCAR=6.9
Pod undocumented
55 0 my ($self, $cd, $args) = @_;
56 0 return "{{var}} = \${ {{var}} } ? 1:0; \$ref = ''";
57 }
58  
59 0 ▶sub command_deref_scalar {
Subroutine command_deref_scalar: not called CC=1 SCAR=6.9
Pod undocumented
60 0 my ($self, $cd, $args) = @_;
61 0 return '{{var}} = ${ {{var}} }; $ref = ref({{var}})';
62 }
63  
64 0 ▶sub command_stringify {
Subroutine command_stringify: not called CC=1 SCAR=6.9
Pod undocumented
65 0 my ($self, $cd, $args) = @_;
66 0 return '{{var}} = "{{var}}"; $ref = ""';
67 }
68  
69 2 ▶sub command_replace_with_ref {
Subroutine command_replace_with_ref: called CC=1 SCAR=0.0
Pod undocumented
70 2 my ($self, $cd, $args) = @_;
71 2 return '{{var}} = $ref; $ref = ""';
72 }
73  
74 3 ▶sub command_replace_with_str {
Subroutine command_replace_with_str: called CC=1 SCAR=0.0
Pod undocumented
75 3 require String::PerlQuote;
76  
77 3 my ($self, $cd, $args) = @_;
78 3 return "{{var}} = ".String::PerlQuote::double_quote($args->[0]).'; $ref=""';
79 }
80  
81 1 ▶sub command_unbless {
Subroutine command_unbless: called CC=1 SCAR=0.0
Pod undocumented
82 1 my ($self, $cd, $args) = @_;
83  
84 1 return join(
85 "",
86 'my $reftype = Scalar::Util::reftype({{var}}); ',
87 '{{var}} = $reftype eq "HASH" ? {%{ {{var}} }} :',
88 ' $reftype eq "ARRAY" ? [@{ {{var}} }] :',
89 ' $reftype eq "SCALAR" ? \(my $copy = ${ {{var}} }) :',
90 ' $reftype eq "CODE" ? sub { goto &{ {{var}} } } :',
91 '(die "Cannot unbless object with type $ref")',
92 );
93 }
94  
95 2 ▶sub command_clone {
Subroutine command_clone: called CC=2 SCAR=7.1
Pod undocumented
96 2 my ($self, $cd, $args) = @_;
97  
98 2 ▶ my $limit = $args->[0] // 1;
Condition50%1/2
$args->[0] // 1
l!l
20
MC/DC0%0/1
$args->[0] // 1
$args->[0]
Truth table50%1/2
A
A= $args->[0]
Aresult
11
00
99 2 return join(
100 "",
101 "if (++\$ctr_circ <= $limit) { ",
102 "{{var}} = $cd->{clone_func}({{var}}); redo ",
103 "} else { ",
104 "{{var}} = 'CIRCULAR'; \$ref = '' }",
105 );
106 }
107  
108 1 ▶sub command_unbless_ffc_inlined {
Subroutine command_unbless_ffc_inlined: called CC=2 SCAR=7.1
Pod undocumented
109 1 my ($self, $cd, $args) = @_;
110  
111 # code taken from Function::Fallback::CoreOrPP 0.07
112 1 ▶ $cd->{subs}{unbless} //= <<'EOC';
Condition50%1/2
$cd->{"subs"}{"unbless"} //= " my \$ref = shift;\n\n my \$r = ref(\$ref);\n # not a reference\n return \$ref unless \$r;\n\n # return if not a blessed ref\n my (\$r2, \$r3) = \"\$ref\" =~ /(.+)=(.+?)\\(/\n or return \$ref;\n\n if (\$r3 eq 'HASH') {\n return { %\$ref };\n } elsif (\$r3 eq 'ARRAY') {\n return [ \@\$ref ];\n } elsif (\$r3 eq 'SCALAR') {\n return \\( my \$copy = \${\$ref} );\n } else {\n die \"Can't handle \$ref\";\n }\n"
l!l
01
MC/DC0%0/1
$cd->{"subs"}{"unbless"} //= " my \$ref = shift;\n\n my \$r = ref(\$ref);\n # not a reference\n return \$ref unless \$r;\n\n # return if not a blessed ref\n my (\$r2, \$r3) = \"\$ref\" =~ /(.+)=(.+?)\\(/\n or return \$ref;\n\n if (\$r3 eq 'HASH') {\n return { %\$ref };\n } elsif (\$r3 eq 'ARRAY') {\n return [ \@\$ref ];\n } elsif (\$r3 eq 'SCALAR') {\n return \\( my \$copy = \${\$ref} );\n } else {\n die \"Can't handle \$ref\";\n }\n"
$cd->{"subs"}{"unbless"}
Truth table50%1/2
A
A= $cd->{"subs"}{"unbless"}
Aresult
11
00
113 my $ref = shift;
114  
115 my $r = ref($ref);
116 # not a reference
117 return $ref unless $r;
118  
119 # return if not a blessed ref
120 my ($r2, $r3) = "$ref" =~ /(.+)=(.+?)\(/
121 or return $ref;
122  
123 if ($r3 eq 'HASH') {
124 return { %$ref };
125 } elsif ($r3 eq 'ARRAY') {
126 return [ @$ref ];
127 } elsif ($r3 eq 'SCALAR') {
128 return \( my $copy = ${$ref} );
129 } else {
130 die "Can't handle $ref";
131 }
132 EOC
133  
134 1 "{{var}} = \$sub_unbless->({{var}}); \$ref = ref({{var}})";
135 }
136  
137 # test
138 0 ▶sub command_die {
Subroutine command_die: not called CC=1 SCAR=6.9
Pod undocumented
139 0 my ($self, $cd, $args) = @_;
140 0 return "die";
141 }
142  
143 11 ▶sub _generate_cleanser_code {
Subroutine _generate_cleanser_code: called CC=30 SCAR=36.9
144 11 my $self = shift;
145 11 my $opts = $self->{_opts};
146  
147 # compilation data, a structure that will be passed around between routines
148 # during the generation of cleanser code.
149 my $cd = {
150 modules => {}, # key = module name, val = version
151 11 clone_func => $self->{_opts}{'!clone_func'},
152 code => '',
153 subs => {},
154 };
155  
156 11 ▶ $cd->{modules}{'Scalar::Util'} //= 0;
Condition50%1/2
$cd->{"modules"}{"Scalar::Util"} //= 0
l!l
011
MC/DC0%0/1
$cd->{"modules"}{"Scalar::Util"} //= 0
$cd->{"modules"}{"Scalar::Util"}
Truth table50%1/2
A
A= $cd->{"modules"}{"Scalar::Util"}
Aresult
11
00
157 11 ▶ $cd->{modules}{'Data::Dmp'} //= 0 if $opts->{'!debug'};
Branch: 1 branch
BranchTrueFalseTotal
if $opts->{"!debug"} 0 11 11
Condition0%0/2
$cd->{"modules"}{"Data::Dmp"} //= 0
l!l
00
MC/DC0%0/1
$cd->{"modules"}{"Data::Dmp"} //= 0
$cd->{"modules"}{"Data::Dmp"}
Truth table0%0/2
A
A= $cd->{"modules"}{"Data::Dmp"}
Aresult
11
00
158  
159 11 ▶ if (!$cd->{clone_func}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($cd->{"clone_func"}) 11 0 11
160 $cd->{clone_func} = $ENV{PERL_DATA_CLEAN_CLONE_FUNC} //
161 11 ▶ 'Clone::PP::clone';
Condition50%1/2
$ENV{"PERL_DATA_CLEAN_CLONE_FUNC"} // "Clone::PP::clone"
l!l
011
MC/DC0%0/1
$ENV{"PERL_DATA_CLEAN_CLONE_FUNC"} // "Clone::PP::clone"
$ENV{"PERL_DATA_CLEAN_CLONE_FUNC"}
Truth table50%1/2
A
A= $ENV{"PERL_DATA_CLEAN_CLONE_FUNC"}
Aresult
11
00
162 }
163 {
164 11 ▶ last unless $cd->{clone_func} =~ /(.+)::(.+)/;
Branch: 1 branch
BranchTrueFalseTotal
unless $cd->{"clone_func"} =~ /(.+)::(.+)/ 0 11 11
165 11 ▶ $cd->{modules}{$1} //= 0;
Condition50%1/2
$cd->{"modules"}{$1} //= 0
l!l
011
MC/DC0%0/1
$cd->{"modules"}{$1} //= 0
$cd->{"modules"}{$1}
Truth table50%1/2
A
A= $cd->{"modules"}{$1}
Aresult
11
00
166 }
167  
168 11 my (@code, @stmts_ary, @stmts_hash, @stmts_main);
169  
170 11 my $n = 0;
171 30 ▶ my $add_stmt = sub {
Subroutine __ANON__: called CC=9 SCAR=22.6
172 30 my $which = shift;
173 30 ▶ if ($which eq 'if' || $which eq 'new_if') {
Branch: 1 branch
BranchTrueFalseTotal
if ($which eq 'if' or $which eq 'new_if') { } 28 2 30
Condition100%3/3
$which eq "if" or $which eq "new_if"
l!l&&r!l&&!r
16122
MC/DC100%2/2
$which eq "if" or $which eq "new_if"
$which eq "if" $which eq "new_if"
Truth table100%3/3
A or B
A= $which eq "if"B= $which eq "new_if"
ABresult
1X1
011
000
174 28 my ($cond0, $act0) = @_;
175 28 for ([\@stmts_ary, '$e', 'ary'],
176 [\@stmts_hash, '$h->{$k}', 'hash'],
177 [\@stmts_main, '$_', 'main']) {
178 84 my $act = $act0 ; $act =~ s/\Q{{var}}\E/$_->[1]/g;
179 84 my $cond = $cond0; $cond =~ s/\Q{{var}}\E/$_->[1]/g;
180 0 ▶ if ($opts->{'!debug'}) { unless (@{ $_->[0] }) { push @{ $_->[0] }, ' print "DEBUG:'.$_->[2].' cleaner: val=", Data::Dmp::dmp_ellipsis('.$_->[1].'), ", ref=$ref\n"; '."\n" } }
Branch: 2 branches
BranchTrueFalseTotal
if ($opts->{"!debug"}) 0 84 84
unless (@{$_->[0];}) 0 0 0
181 84 ▶ push @{ $_->[0] }, " ".($n && $which ne 'new_if' ? "els":"")."if ($cond) { $act }\n";
Branch: 1 branch
BranchTrueFalseTotal
$n && $which ne 'new_if' ? : 30 54 84
Condition100%3/3
$n and $which ne "new_if"
!ll&&!rl&&r
272730
MC/DC100%2/2
$n and $which ne "new_if"
$n $which ne "new_if"
Truth table100%3/3
A and B
A= $nB= $which ne "new_if"
ABresult
0X0
100
111
182 }
183 28 $n++;
184 } else {
185 2 my ($stmt0) = @_;
186 2 for ([\@stmts_ary, '$e', 'ary'],
187 [\@stmts_hash, '$h->{$k}', 'hash'],
188 [\@stmts_main, '$_', 'main']) {
189 6 my $stmt = $stmt0; $stmt =~ s/\Q{{var}}\E/$_->[1]/g;
190 6 push @{ $_->[0] }, " $stmt;\n";
191 }
192 }
193 11 };
194 16 ▶ my $add_if = sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
195 16 $add_stmt->('if', @_);
196 11 };
197 12 ▶ my $add_new_if = sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
198 12 $add_stmt->('new_if', @_);
199 11 };
200 10 ▶ my $add_if_ref = sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
201 10 my ($ref, $act0) = @_;
202 10 $add_if->("\$ref eq '$ref'", $act0);
203 11 };
204 7 ▶ my $add_new_if_ref = sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
205 7 my ($ref, $act0) = @_;
206 7 $add_new_if->("\$ref eq '$ref'", $act0);
207 11 };
208  
209 # catch circular references
210 11 my $circ = $opts->{-circular};
211 11 ▶ if ($circ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($circ) 3 8 11
212 3 my $meth = "command_$circ->[0]";
213 3 ▶ die "Can't handle command $circ->[0] for option '-circular'" unless $self->can($meth);
Branch: 1 branch
BranchTrueFalseTotal
unless $self->can($meth) 0 3 3
214 3 my @args = @$circ; shift @args;
215 3 my $act = $self->$meth($cd, \@args);
216 0 ▶ if ($opts->{'!debug'}) { $add_stmt->('stmt', 'print "DEBUG: main cleaner: ref=$ref, " . {{var}} . "\n"'); }
Branch: 1 branch
BranchTrueFalseTotal
if ($opts->{"!debug"}) 0 3 3
217 3 $add_new_if->('$ref && $refs{ {{var}} }++', $act);
218 }
219  
220 # catch object of specified classes (e.g. DateTime, etc)
221 11 for my $on (grep {/\A\w*(::\w+)*\z/} sort keys %$opts) {
222 3 my $o = $opts->{$on};
223 3 ▶ next unless $o;
Branch: 1 branch
BranchTrueFalseTotal
unless $o 0 3 3
224 3 my $meth = "command_$o->[0]";
225 3 ▶ die "Can't handle command $o->[0] for option '$on'" unless $self->can($meth);
Branch: 1 branch
BranchTrueFalseTotal
unless $self->can($meth) 0 3 3
226 3 my @args = @$o; shift @args;
227 3 my $act = $self->$meth($cd, \@args);
228 3 $add_if_ref->($on, $act);
229 }
230  
231 # catch general object not caught by previous
232 11 for my $p ([-obj => 'Scalar::Util::blessed({{var}})']) {
233 11 my $o = $opts->{$p->[0]};
234 11 ▶ next unless $o;
Branch: 1 branch
BranchTrueFalseTotal
unless $o 5 6 11
235 6 my $meth = "command_$o->[0]";
236 6 ▶ die "Can't handle command $o->[0] for option '$p->[0]'" unless $self->can($meth);
Branch: 1 branch
BranchTrueFalseTotal
unless $self->can($meth) 0 6 6
237 6 my @args = @$o; shift @args;
238 6 $add_if->($p->[1], $self->$meth($cd, \@args));
239 }
240  
241 # recurse array and hash
242 9 ▶ if ($opts->{'!recurse_obj'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opts->{'!recurse_obj'}) { } 2 7 9
243 2 $add_stmt->('stmt', 'my $reftype=Scalar::Util::reftype({{var}})//""');
244 2 $add_new_if->('$reftype eq "ARRAY"', '$process_array->({{var}})');
245 2 $add_if->('$reftype eq "HASH"' , '$process_hash->({{var}})');
246 } else {
247 7 $add_new_if_ref->("ARRAY", '$process_array->({{var}})');
248 7 $add_if_ref->("HASH" , '$process_hash->({{var}})');
249 }
250  
251 # lastly, catch any reference left
252 9 for my $p ([-ref => '$ref']) {
253 9 my $o = $opts->{$p->[0]};
254 9 ▶ next unless $o;
Branch: 1 branch
BranchTrueFalseTotal
unless $o 9 0 9
255 0 my $meth = "command_$o->[0]";
256 0 ▶ die "Can't handle command $o->[0] for option '$p->[0]'" unless $self->can($meth);
Branch: 1 branch
BranchTrueFalseTotal
unless $self->can($meth) 0 0 0
257 0 my @args = @$o; shift @args;
258 0 $add_if->($p->[1], $self->$meth($cd, \@args));
259 }
260  
261 9 push @code, 'sub {'."\n";
262  
263 9 for (sort keys %{$cd->{subs}}) {
264 1 push @code, "state \$sub_$_ = sub { ".$cd->{subs}{$_}." };\n";
265 }
266  
267 9 push @code, 'my $data = shift;'."\n";
268 9 ▶ push @code, 'state %refs;'."\n" if $circ;
Branch: 1 branch
BranchTrueFalseTotal
if $circ 3 6 9
269 9 ▶ push @code, 'state $ctr_circ;'."\n" if $circ;
Branch: 1 branch
BranchTrueFalseTotal
if $circ 3 6 9
270 9 push @code, 'state $process_array;'."\n";
271 9 push @code, 'state $process_hash;'."\n";
272 9 push @code, (
273 'if (!$process_array) { $process_array = sub { my $a = shift; for my $e (@$a) { ',
274 'my $ref=ref($e);'."\n",
275 join("", @stmts_ary).'} } }'."\n"
276 );
277 9 push @code, (
278 'if (!$process_hash) { $process_hash = sub { my $h = shift; for my $k (keys %$h) { ',
279 'my $ref=ref($h->{$k});'."\n",
280 join("", @stmts_hash).'} } }'."\n"
281 );
282 9 ▶ push @code, '%refs = (); $ctr_circ=0;'."\n" if $circ;
Branch: 1 branch
BranchTrueFalseTotal
if $circ 3 6 9
283 9 push @code, (
284 'for ($data) { ',
285 'my $ref=ref($_);'."\n",
286 join("", @stmts_main).'}'."\n"
287 );
288 9 ▶ push @code, 'print "DEBUG: main cleaner: result: ", Data::Dmp::dmp_ellipsis($data), "\n";'."\n" if $opts->{'!debug'};
Branch: 1 branch
BranchTrueFalseTotal
if $opts->{"!debug"} 0 9 9
289 9 push @code, '$data'."\n";
290 9 push @code, '}'."\n";
291  
292 9 my $code = join("", @code).";";
293  
294 9 ▶ if ($ENV{LOG_CLEANSER_CODE} && log_is_trace()) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ENV{"LOG_CLEANSER_CODE"} and log_is_trace()) 0 9 9
Condition33%1/3
$ENV{"LOG_CLEANSER_CODE"} and log_is_trace()
!ll&&!rl&&r
900
MC/DC0%0/2
$ENV{"LOG_CLEANSER_CODE"} and log_is_trace()
$ENV{"LOG_CLEANSER_CODE"} log_is_trace()
Truth table33%1/3
A and B
A= $ENV{"LOG_CLEANSER_CODE"}B= log_is_trace()
ABresult
0X0
100
111
295 0 require String::LineNumber;
296 log_trace("Cleanser code:\n%s",
297 0 ▶ $ENV{LINENUM} // 1 ?
Branch: 1 branch
BranchTrueFalseTotal
$ENV{'LINENUM'} // 1 ? : 0 0 0
Condition0%0/2
$ENV{"LINENUM"} // 1
l!l
00
MC/DC0%0/1
$ENV{"LINENUM"} // 1
$ENV{"LINENUM"}
Truth table0%0/2
A
A= $ENV{"LINENUM"}
Aresult
11
00
298 String::LineNumber::linenum($code) : $code);
299 }
300  
301 9 $cd->{src} = $code;
302  
303 9 $cd;
304 }
305  
306 11 ▶sub clean_in_place {
Subroutine clean_in_place: called CC=1 SCAR=0.0
Pod documented
307 11 my ($self, $data) = @_;
308  
309 11 $self->{_code}->($data);
310 }
311  
312 4 ▶sub clone_and_clean {
Subroutine clone_and_clean: called CC=1 SCAR=0.0
Pod documented
313 2 ▶ no strict 'refs'; ## no critic: TestingAndDebugging::ProhibitNoStrict
Subroutine BEGIN: called CC=1 SCAR=0.0
314  
315 4 my ($self, $data) = @_;
316 4 my $clone = &{$self->{_cd}{clone_func}}($data);
317 4 $self->clean_in_place($clone);
318 }
319  
320 2 1;
321 # ABSTRACT: Clean data structure
322  
323 __END__
↑ Summary