blib/lib/PICA/Writer/Base.pm

filter: Statementstmt 88.4% 61 / 69 Branchbran 56.2% 18 / 32 Conditioncond 31.2% 5 / 16 MC/DCmcdc 11.1% 1 / 9 Subroutinesub 93.7% 15 / 16 Podpod 44.4% 4 / 9 totaltotal 68.8% 104 / 151 CC 25 SCAR 23.5
CC
25
Cov
72%
new
29.7
write_identifier
6.4
annotation
4.1
CRAP 10.5
« blib/lib/PICA/Patch.pm ↑ Summary blib/lib/PICA/Parser/XML.pm »
1 package PICA::Writer::Base;
2 16 ▶use v5.14.1;
Subroutine BEGIN: called CC=1 SCAR=0.0
3  
4 16 our $VERSION = '2.09';
5  
6 16 ▶use Scalar::Util qw(blessed openhandle reftype);
Subroutine BEGIN: called CC=1 SCAR=0.0
7 16 ▶use PICA::Schema qw(clean_pica);
Subroutine BEGIN: called CC=1 SCAR=0.0
8 16 ▶use Term::ANSIColor;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 16 ▶use Encode qw(decode);
Subroutine BEGIN: called CC=1 SCAR=0.0
10 16 ▶use Carp qw(croak);
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 sub new {
13 46 ▶ my $class = shift;
Subroutine new: called CC=11 SCAR=33.9
Pod documented
14 46 ▶ my (%options) = @_ % 2 ? (fh => @_) : @_;
Branch: 1 branch
BranchTrueFalseTotal
@_ % 2 ? : 0 46 46
15  
16 46 my $self = bless \%options, $class;
17  
18 46 ▶ my $fh = $self->{fh} // \*STDOUT;
Condition50%1/2
$self->{"fh"} // \*STDOUT
l!l
460
MC/DC0%0/1
$self->{"fh"} // \*STDOUT
$self->{"fh"}
Truth table50%1/2
A
A= $self->{"fh"}
Aresult
11
00
19 46 ▶ if (!ref $fh) {
Branch: 3 branches
BranchTrueFalseTotal
if (not ref $fh) { } 0 46 46
elsif (builtin::reftype($fh) eq 'SCALAR' and not builtin::blessed($fh)) { } 46 0 46
elsif (not &openhandle($fh) || builtin::blessed($fh) && $fh->can('print')) { } 0 0 0
20 0 ▶ if (open(my $handle, '>:encoding(UTF-8)', $fh)) {
Branch: 1 branch
BranchTrueFalseTotal
if (open my $handle, '>:encoding(UTF-8)', $fh) { } 0 0 0
21 0 $fh = $handle;
22 }
23 else {
24 0 croak "cannot open file for writing: $fh\n";
25 }
26 }
27 ▶ elsif (reftype $fh eq 'SCALAR' and !blessed $fh) {
Condition33%1/3
builtin::reftype($fh) eq "SCALAR" and not builtin::blessed($fh)
!ll&&!rl&&r
0046
MC/DC0%0/2
builtin::reftype($fh) eq "SCALAR" and not builtin::blessed($fh)
builtin::reftype($fh) eq "SCALAR" not builtin::blessed($fh)
Truth table33%1/3
A and not(B)
A= builtin::reftype($fh) eq "SCALAR"B= not builtin::blessed($fh)
ABresult
0X0
100
111
28 46 ▶ if (length $$fh) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $$fh) 0 46 46
29 0 $$fh = decode("UTF-8", $$fh);
30 }
31 46 ▶ open(my $handle, '>>:encoding(UTF-8)', $fh);
Subroutine BEGIN: called CC=1 SCAR=0.0
32 46 $fh = $handle;
33 }
34 ▶ elsif (!openhandle($fh) and !(blessed $fh && $fh->can('print'))) {
Condition0%0/6
&openhandle($fh) || builtin::blessed($fh) && $fh->can('print')
l!l&&r!l&&!r
000
builtin::blessed($fh) and $fh->can("print")
!ll&&!rl&&r
000
MC/DC0%0/3
&openhandle($fh) || builtin::blessed($fh) && $fh->can('print')
&openhandle($fh) builtin::blessed($fh) $fh->can("print")
Truth table0%0/4
A || B and C
A= &openhandle($fh)B= builtin::blessed($fh)C= $fh->can("print")
ABCresult
1XX1
0111
00X0
0100
35 0 croak 'expect filehandle or object with method print!';
36 }
37 46 $self->{fh} = $fh;
38  
39 46 $self;
40 }
41  
42 sub write {
43 46 ▶ my $self = shift;
Subroutine write: called CC=2 SCAR=6.9
Pod documented
44 46 $self->write_record($_) for @_;
45 46 $self;
46 }
47  
48 sub write_identifier {
49 55 ▶ my ($self, $field) = @_;
Subroutine write_identifier: called CC=6 SCAR=18.6
Pod undocumented
50  
51 55 my $fh = $self->{fh};
52 55 ▶ my %col = %{$self->{color} // {}};
Condition50%1/2
$self->{"color"} // {}
l!l
055
MC/DC0%0/1
$self->{"color"} // {}
$self->{"color"}
Truth table50%1/2
A
A= $self->{"color"}
Aresult
11
00
53  
54 55 ▶ $fh->print($col{tag} ? colored($field->[0], $col{tag}) : $field->[0]);
Branch: 1 branch
BranchTrueFalseTotal
$col{'tag'} ? : 0 55 55
55  
56 55 ▶ if ($field->[1] > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($field->[1] > 0) 2 53 55
57 2 my $occ = sprintf("%02d", $field->[1]);
58 $fh->print(($col{syntax} ? colored('/', $col{syntax}) : '/')
59 2 ▶ . ($col{occurrence} ? colored($occ, $col{occurrence}) : $occ));
Branch: 2 branches
BranchTrueFalseTotal
$col{'syntax'} ? : 0 2 2
$col{'occurrence'} ? : 0 2 2
60 }
61 }
62  
63 sub write_record {
64 43 ▶ my ($self, $record) = @_;
Subroutine write_record: called CC=4 SCAR=13.9
Pod documented
65 43 ▶ $record = clean_pica(
Branch: 1 branch
BranchTrueFalseTotal
$record = clean_pica($record, "allow_empty_subfields", 1, "ignore_empty_records", 1) or return 0 43 43
66 $record,
67 allow_empty_subfields => 1,
68 ignore_empty_records => 1
69 ) or return;
70 43 ▶ return unless @$record;
Branch: 1 branch
BranchTrueFalseTotal
unless @$record 3 40 43
71  
72 40 my $fh = $self->{fh};
73 40 $self->write_field($_) for @$record;
74 40 $fh->print($self->END_OF_RECORD);
75 }
76  
77 sub write_field {
78 55 ▶ my ($self, $field) = @_;
Subroutine write_field: called CC=2 SCAR=6.9
Pod undocumented
79  
80 55 $self->write_start_field($field);
81  
82 55 ▶ for (my $i = 3; $i < scalar @$field; $i += 2) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < scalar @$field) 58 55 113
83 58 $self->write_subfield($field->[$i - 1], $field->[$i]);
84 }
85  
86 55 $self->{fh}->print($self->END_OF_FIELD);
87 }
88  
89 sub annotation {
90 55 ▶ my ($self, $field) = @_;
Subroutine annotation: called CC=4 SCAR=14.2
Pod undocumented
91  
92 55 ▶ return unless $self->{annotate} // @$field % 2;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"annotate"} // @$field % 2 44 11 55
Condition66%2/3
$self->{"annotate"} // @$field % 2
l!l&&r!l&&!r
01144
MC/DC50%1/2
$self->{"annotate"} // @$field % 2
$self->{"annotate"} @$field % 2
Truth table66%2/3
A // B
A= $self->{"annotate"}B= @$field % 2
ABresult
1X1
011
000
93 11 ▶ return @$field % 2 ? $field->[$#$field] : " ";
Branch: 1 branch
BranchTrueFalseTotal
@$field % 2 ? : 11 0 11
94 }
95  
96 sub write_start_field {
97 0 ▶ my ($self, $field) = @_;
Subroutine write_start_field: not called CC=1 SCAR=6.9
Pod undocumented
98  
99 # ignore annotation by default
100 0 $self->write_identifier($field);
101 0 $self->{fh}->print(' ');
102 }
103  
104 sub write_subfield {
105 2 ▶ my ($self, $code, $value) = @_;
Subroutine write_subfield: called CC=1 SCAR=0.0
Pod undocumented
106 2 $self->{fh}->print($self->SUBFIELD_INDICATOR . $code . $value);
107 }
108  
109 sub end {
110 45 ▶ my $self = shift;
Subroutine end: called CC=2 SCAR=7.2
Pod documented
111 45 ▶ close $self->{fh} if $self->{fh} ne \*STDOUT;
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"fh"} ne \*STDOUT 45 0 45
112 }
113  
114 16 1;
115 __END__
« blib/lib/PICA/Patch.pm ↑ Summary blib/lib/PICA/Parser/XML.pm »