Fix/pica_add.pm

filter: Statementstmt 100% 62 / 62 Branchbran 75.0% 18 / 24 Conditioncond 53.8% 7 / 13 MC/DCmcdc 28.5% 2 / 7 Subroutinesub 88.8% 8 / 9 Podpod n/a totaltotal 84.3% 97 / 115 CC 21 SCAR 22.9
CC
21
Cov
88%
__ANON__
15.2
_build_fixer
7.2
BEGIN
1.0
CRAP 9.8
« Fix/pica_update.pm ↑ Summary Fix/pica_remove.pm »
1 package Catmandu::Fix::pica_add;
2
3 2 ▶use Catmandu::Sane;
Subroutine BEGIN: called CC=1 SCAR=0.0
4
5 2 our $VERSION = '1.18';
6
7 2 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 2 ▶use Catmandu::Util::Path qw(as_path);
Subroutine BEGIN: called CC=1 SCAR=0.0
9 2 ▶use Catmandu::Fix::Has;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 2 ▶use PICA::Path;
Subroutine BEGIN: called CC=1 SCAR=0.0
11
12 2 has path => ( fix_arg => 1 );
13 has pica_path => (
14 fix_arg => 1,
15 8 ▶ coerce => sub { PICA::Path->new( $_[0] ) }
Subroutine __ANON__: called CC=1 SCAR=0.0
16 2 );
17 2 has record => ( fix_opt => 1 );
18 2 has force_new => ( fix_opt => 1 );
19
20 2 with 'Catmandu::Fix::Builder';
21
22 8 ▶sub _build_fixer {
Subroutine _build_fixer: called CC=7 SCAR=19.7
23 8 my ($self) = @_;
24
25 8 my $value_getter = as_path( $self->path )->getter;
26 8 ▶ my $record_path = as_path( $self->record // 'record' );
Condition50%1/2
$self->record // "record"
l!l
08
MC/DC0%0/1
$self->record // "record"
$self->record
Truth table50%1/2
A
A= $self->record
Aresult
11
00
27 8 my $record_getter = $record_path->getter;
28 8 my $pica_path = $self->pica_path;
29 8 my $force_new = $self->force_new;
30
31 0 ▶ return sub { }
Subroutine __ANON__: not called CC=1 SCAR=0.0
32 8 ▶ unless defined $pica_path->fields && defined $pica_path->subfields;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $pica_path->fields and defined $pica_path->subfields 0 8 8
Condition33%1/3
defined $pica_path->fields and defined $pica_path->subfields
!ll&&!rl&&r
008
MC/DC0%0/2
defined $pica_path->fields and defined $pica_path->subfields
defined $pica_path->fields defined $pica_path->subfields
Truth table33%1/3
A and B
A= defined $pica_path->fieldsB= defined $pica_path->subfields
ABresult
0X0
100
111
33
34 8 my @fixed_field;
35 8 ▶ @fixed_field = ( $pica_path->fields, $pica_path->occurrences )
Branch: 1 branch
BranchTrueFalseTotal
if $pica_path->fields =~ qr/^[0-9A-Z\@]{4}$/ and ($pica_path->occurrences // "") =~ qr/^[0-9]*$/ 8 0 8
Condition60%3/5
$pica_path->fields =~ qr/^[0-9A-Z\@]{4}$/ and ($pica_path->occurrences // "") =~ qr/^[0-9]*$/
!ll&&!rl&&r
008
$pica_path->occurrences // ""
l!l
17
MC/DC50%1/2
$pica_path->fields =~ qr/^[0-9A-Z\@]{4}$/ and ($pica_path->occurrences // "") =~ qr/^[0-9]*$/
$pica_path->fields =~ qr/^[0-9A-Z\@]{4}$/ $pica_path->occurrences
Truth table66%2/3
A and B
A= $pica_path->fields =~ qr/^[0-9A-Z\@]{4}$/B= $pica_path->occurrences
ABresult
0X0
100
111
36 if ( $pica_path->fields =~ qr{^[0-9A-Z@]{4}$}
37 && ( $pica_path->occurrences // '' ) =~ qr{^[0-9]*$} );
38
39 8 my $subfields = $pica_path->subfields;
40
41 8 ▶ sub {
Subroutine __ANON__: called CC=15 SCAR=27.2
42 8 my ($data) = @_;
43 my @values =
44 8 ▶ map { ref $_ eq 'ARRAY' ? @$_ : $_ } @{ $value_getter->($data) };
Branch: 1 branch
BranchTrueFalseTotal
ref $_ eq 'ARRAY' ? : 1 7 8
45 8 ▶ return $data unless @values;
Branch: 1 branch
BranchTrueFalseTotal
unless @values 0 8 8
46
47 8 my $records = $record_getter->($data);
48 8 ▶ if ( !@$records ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@$records) 1 7 8
49 1 my $record = [];
50 1 $records = [$record];
51 1 $record_path->creator($record)->($data);
52 }
53
54 8 for my $record (@$records) {
55
56 my $fields =
57 8 ▶ [ $force_new ? () : grep { $pica_path->match_field($_) }
Branch: 1 branch
BranchTrueFalseTotal
$force_new ? : 1 7 8
58 @$record ];
59
60 8 ▶ if (@$fields) {
Branch: 2 branches
BranchTrueFalseTotal
if (@$fields) { } 1 7 8
elsif (@fixed_field) { } 7 0 7
61 1 my @sf_codes = split '', $subfields;
62 1 my @sf_values = @values;
63
64 1 foreach my $f (@$fields) {
65 1 ▶ my $annotation = @$f % 2 ? pop @$f : undef;
Branch: 1 branch
BranchTrueFalseTotal
@$f % 2 ? : 0 1 1
66
67 1 ▶ for (
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @sf_codes and $i < @sf_values) 1 1 2
Condition66%2/3
$i < @sf_codes and $i < @sf_values
!ll&&!rl&&r
101
MC/DC50%1/2
$i < @sf_codes and $i < @sf_values
$i < @sf_codes $i < @sf_values
Truth table66%2/3
A and B
A= $i < @sf_codesB= $i < @sf_values
ABresult
0X0
100
111
68 my $i = 0 ;
69 $i < @sf_codes && $i < @sf_values ;
70 $i++
71 )
72 {
73 1 push @$f, $sf_codes[$i], $sf_values[$i];
74 }
75
76 1 ▶ push @$f, $annotation if defined $annotation;
Branch: 1 branch
BranchTrueFalseTotal
if defined $annotation 0 1 1
77 }
78 }
79 elsif (@fixed_field) {
80 7 my $field = [@fixed_field];
81
82 7 my $i = 0;
83 7 foreach ( split '', $subfields ) {
84 8 push @$field, $_, $values[ $i++ ];
85 8 ▶ last if $i >= @values;
Branch: 1 branch
BranchTrueFalseTotal
if $i >= @values 7 1 8
86 }
87
88 7 push @$record, $field;
89 }
90 }
91
92 8 $data;
93 }
94 8 }
95
96 2 1;
97 __END__
« Fix/pica_update.pm ↑ Summary Fix/pica_remove.pm »