PICA/Parser/Base.pm

filter: Statementstmt 100% 53 / 53 Branchbran 80.0% 24 / 30 Conditioncond 90.9% 10 / 11 MC/DCmcdc 85.7% 6 / 7 Subroutinesub 100% 10 / 10 Podpod 100% 4 / 4 totaltotal 93.0% 107 / 115 CC 20 SCAR 18.8
CC
20
Cov
93%
new
11.1
next
7.1
_new
3.0
CRAP 6.6
« PICA/Data.pm ↑ Summary PICA/Writer/Patch.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.12';
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 121 ▶sub _new {
Subroutine _new: called CC=3 SCAR=11.1
12 121 my $class = shift;
13 121 ▶ my (%options) = @_ % 2 ? (fh => @_) : @_;
Branch: 1 branch
BranchTrueFalseTotal
@_ % 2 ? : 106 15 121
14  
15 bless {
16 strict => !!$options{strict},
17 fh => defined $options{fh} ? $options{fh} : \*STDIN,
18 annotate => $options{annotate},
19 121 ▶ counter => 0,
Branch: 1 branch
BranchTrueFalseTotal
defined $options{'fh'} ? : 121 0 121
20 }, $class;
21 }
22  
23 107 ▶sub new {
Subroutine new: called CC=11 SCAR=24.0
Pod documented
24 107 my $self = _new(@_);
25 107 my $input = $self->{fh};
26  
27 # check for file or filehandle
28 107 my $ishandle = eval {fileno($input);};
29 107 ▶ if (!$@ && defined $ishandle) {
Branch: 4 branches
BranchTrueFalseTotal
if (not $@ and defined $ishandle) { } 5 102 107
elsif (not $input =~ /\n/u and -e $input) { } 10 92 102
elsif (ref $input and builtin::reftype($input) eq 'SCALAR') { } 84 8 92
elsif ($input =~ /\n/u) { } 6 2 8
Condition100%3/3
not $@ and defined $ishandle
!ll&&!rl&&r
86165
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
30 5 $self->{reader} = $input;
31 }
32 ▶ elsif ($input !~ /\n/ and -e $input) {
Condition100%3/3
not $input =~ /\n/u and -e $input
!ll&&!rl&&r
68610
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
33 10 ▶ open($self->{reader}, "<:encoding(UTF-8)", $input)
Branch: 1 branch
BranchTrueFalseTotal
open $self->{"reader"}, "<:encoding(UTF-8)", $input or croak("Failed to read from file $input\n") 10 0 10
34 or croak "Failed to read from file $input\n";
35 }
36 ▶ elsif ((ref $input and reftype $input eq 'SCALAR')) {
Condition100%3/3
ref $input and builtin::reftype($input) eq "SCALAR"
!ll&&!rl&&r
6284
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
37 84 $input = encode('UTF-8', $$input);
38 84 ▶ 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") 84 0 84
39 or croak "Failed to read from string reference\n";
40 }
41 elsif ($input =~ /\n/) {
42 6 $input = encode('UTF-8', $input);
43 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") 6 0 6
44 or croak "Failed to read from string\n";
45 }
46 else {
47 2 croak "file or filehandle $input does not exists";
48 }
49  
50 105 $self;
51 }
52  
53 24 ▶sub count {
Subroutine count: called CC=1 SCAR=0.0
Pod documented
54 24 return $_[0]->{counter};
55 }
56  
57 146 ▶sub next {
Subroutine next: called CC=7 SCAR=19.6
Pod documented
58 146 my ($self) = @_;
59  
60 146 ▶ while (my $record = $self->_next_record) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $record = $self->_next_record) 133 17 150
61 133 ▶ next unless @$record;
Branch: 1 branch
BranchTrueFalseTotal
unless @$record 12 121 133
62 121 $self->{counter}++;
63  
64 # get last 003@ $0 as _id (TODO: _id for level 1 and 2?)
65 121 my $id;
66 121 ▶ if (my ($field) = grep {($_->[0] // '') =~ '003@'} @$record) {
Branch: 1 branch
BranchTrueFalseTotal
if (my($field) = grep({($_->[0] // "") =~ /003@/u;} @$record)) 53 68 121
Condition50%1/2
$_->[0] // ""
l!l
66830
MC/DC0%0/1
$_->[0] // ""
$_->[0]
Truth table50%1/2
A
A= $_->[0]
Aresult
11
00
67 53 ▶ for (my $i = 2; $i < @$field; $i += 2) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @$field) 53 0 53
68 53 ▶ if ($field->[$i] eq '0') {
Branch: 1 branch
BranchTrueFalseTotal
if ($field->[$i] eq 0) 53 0 53
69 53 $id = $field->[$i + 1];
70 53 last;
71 }
72 }
73 }
74  
75 121 my @fields = map {bless $_, 'PICA::Data::Field'} @$record;
76 121 return bless {record => \@fields, _id => $id}, 'PICA::Data';
77 }
78  
79 17 return;
80 }
81  
82 1 ▶sub all {
Subroutine all: called CC=2 SCAR=6.9
Pod documented
83 1 my ($self) = @_;
84 1 my @records;
85  
86 1 ▶ while (my $record = $self->next) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $record = $self->next) 2 1 3
87 2 push @records, $record;
88 }
89  
90 1 return \@records;
91 }
92  
93 16 1;
94 __END__
« PICA/Data.pm ↑ Summary PICA/Writer/Patch.pm »