blib/lib/PICA/Parser/Base.pm

filter: Statementstmt 100% 49 / 49 Branchbran 78.5% 22 / 28 Conditioncond 90.9% 10 / 11 MC/DCmcdc 85.7% 6 / 7 Subroutinesub 100% 9 / 9 Podpod 100% 2 / 2 totaltotal 92.4% 98 / 106 CC 19 SCAR 18.7
CC
19
Cov
92%
new
11.1
next
7.1
_new
3.0
CRAP 6.5
« blib/lib/PICA/Data.pm ↑ Summary blib/lib/PICA/Error.pm »
1 package PICA::Parser::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 PICA::Data::Field;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 16 ▶use Carp qw(croak);
Subroutine BEGIN: called CC=1 SCAR=0.0
8 16 ▶use Scalar::Util qw(reftype);
Subroutine BEGIN: called CC=1 SCAR=0.0
9 16 ▶use Encode qw(encode);
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 sub _new {
12 112 ▶ my $class = shift;
Subroutine _new: called CC=3 SCAR=11.1
13 112 ▶ my (%options) = @_ % 2 ? (fh => @_) : @_;
Branch: 1 branch
BranchTrueFalseTotal
@_ % 2 ? : 97 15 112
14  
15 bless {
16 strict => !!$options{strict},
17 fh => defined $options{fh} ? $options{fh} : \*STDIN,
18 annotate => $options{annotate}
19 112 ▶ }, $class;
Branch: 1 branch
BranchTrueFalseTotal
defined $options{'fh'} ? : 112 0 112
20 }
21  
22 sub new {
23 98 ▶ my $self = _new(@_);
Subroutine new: called CC=11 SCAR=24.0
Pod documented
24 98 my $input = $self->{fh};
25  
26 # check for file or filehandle
27 98 my $ishandle = eval {fileno($input);};
28 98 ▶ if (!$@ && defined $ishandle) {
Branch: 4 branches
BranchTrueFalseTotal
if (not $@ and defined $ishandle) { } 4 94 98
elsif (not $input =~ /\n/u and -e $input) { } 8 86 94
elsif (ref $input and builtin::reftype($input) eq 'SCALAR') { } 78 8 86
elsif ($input =~ /\n/u) { } 6 2 8
Condition100%3/3
not $@ and defined $ishandle
!ll&&!rl&&r
80144
MC/DC100%2/2
not $@ and defined $ishandle
not $@ defined $ishandle
Truth table100%3/3
not(A) and B
A= not $@B= defined $ishandle
ABresult
0X0
100
111
29 4 $self->{reader} = $input;
30 }
31 ▶ elsif ($input !~ /\n/ and -e $input) {
Condition100%3/3
not $input =~ /\n/u and -e $input
!ll&&!rl&&r
6808
MC/DC100%2/2
not $input =~ /\n/u and -e $input
not $input =~ /\n/u -e $input
Truth table100%3/3
not(A) and B
A= not $input =~ /\n/uB= -e $input
ABresult
0X0
100
111
32 8 ▶ open($self->{reader}, "<:encoding(UTF-8)", $input)
Subroutine BEGIN: called CC=1 SCAR=0.0
Branch: 1 branch
BranchTrueFalseTotal
open $self->{"reader"}, "<:encoding(UTF-8)", $input or croak("Failed to read from file $input\n") 0 8 8
33 or croak "Failed to read from file $input\n";
34 }
35 ▶ elsif ((ref $input and reftype $input eq 'SCALAR')) {
Condition100%3/3
ref $input and builtin::reftype($input) eq "SCALAR"
!ll&&!rl&&r
6278
MC/DC100%2/2
ref $input and builtin::reftype($input) eq "SCALAR"
ref $input builtin::reftype($input) eq "SCALAR"
Truth table100%3/3
A and B
A= ref $inputB= builtin::reftype($input) eq "SCALAR"
ABresult
0X0
100
111
36 78 $input = encode('UTF-8', $$input);
37 78 ▶ open($self->{reader}, "<:encoding(UTF-8)", \$input)
Branch: 1 branch
BranchTrueFalseTotal
open $self->{"reader"}, "<:encoding(UTF-8)", \$input or croak("Failed to read from string reference\n") 0 78 78
38 or croak "Failed to read from string reference\n";
39 }
40 elsif ($input =~ /\n/) {
41 6 $input = encode('UTF-8', $input);
42 6 ▶ open($self->{reader}, "<:encoding(UTF-8)", \$input)
Branch: 1 branch
BranchTrueFalseTotal
open $self->{"reader"}, "<:encoding(UTF-8)", \$input or croak("Failed to read from string\n") 0 6 6
43 or croak "Failed to read from string\n";
44 }
45 else {
46 2 croak "file or filehandle $input does not exists";
47 }
48  
49 96 $self;
50 }
51  
52 sub next {
53 133 ▶ my ($self) = @_;
Subroutine next: called CC=7 SCAR=19.6
Pod documented
54  
55 133 ▶ while (my $record = $self->_next_record) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $record = $self->_next_record) 121 15 136
56 121 ▶ next unless @$record;
Branch: 1 branch
BranchTrueFalseTotal
unless @$record 9 112 121
57  
58 # get last 003@ $0 as _id (TODO: _id for level 1 and 2?)
59 112 my $id;
60 112 ▶ if (my ($field) = grep {($_->[0] // '') =~ '003@'} @$record) {
Branch: 1 branch
BranchTrueFalseTotal
if (my($field) = grep({($_->[0] // "") =~ /003@/u;} @$record)) 46 66 112
Condition50%1/2
$_->[0] // ""
l!l
66040
MC/DC0%0/1
$_->[0] // ""
$_->[0]
Truth table50%1/2
A
A= $_->[0]
Aresult
11
00
61 46 ▶ for (my $i = 2; $i < @$field; $i += 2) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @$field) 46 0 46
62 46 ▶ if ($field->[$i] eq '0') {
Branch: 1 branch
BranchTrueFalseTotal
if ($field->[$i] eq 0) 46 0 46
63 46 $id = $field->[$i + 1];
64 46 last;
65 }
66 }
67 }
68  
69 112 my @fields = map {bless $_, 'PICA::Data::Field'} @$record;
70 112 return bless {record => \@fields, _id => $id}, 'PICA::Data';
71 }
72  
73 15 return;
74 }
75  
76 16 1;
77 __END__
« blib/lib/PICA/Data.pm ↑ Summary blib/lib/PICA/Error.pm »