blib/lib/GDPR/IAB/TCFv2/BitField.pm

filter: Statementstmt 97.3% 73 / 75 Branchbran 73.6% 28 / 38 Conditioncond 50.0% 1 / 2 MC/DCmcdc 0.0% 0 / 1 Subroutinesub 100% 11 / 11 Podpod 80.0% 4 / 5 totaltotal 88.6% 117 / 132 CC 21 SCAR 19.2
CC
21
Cov
89%
TO_JSON
11.3
Parse
8.5
contains
3.0
CRAP 6.8
« blib/lib/GDPR/IAB/TCFv2/BitUtils.pm ↑ Summary blib/lib/GDPR/IAB/TCFv2/PublisherRestrictions.pm »
1 package GDPR::IAB::TCFv2::BitField 0.530;
2 73 ▶use v5.12;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 73 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 73 ▶use integer;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 73 ▶use bytes;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 73 ▶use GDPR::IAB::TCFv2::BitUtils qw<is_set>;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 73 ▶use Carp qw<croak>;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 20952 ▶sub Parse {
Subroutine Parse: called CC=8 SCAR=21.4
Pod undocumented
11 20952 my ($klass, %args) = @_;
12  
13 20952 ▶ croak "missing 'data'" unless defined $args{data};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $args{"data"} 0 20952 20952
14 20952 ▶ croak "missing 'data_size'" unless defined $args{data_size};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $args{"data_size"} 0 20952 20952
15 20952 ▶ croak "missing 'max_id'" unless defined $args{max_id};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $args{"max_id"} 0 20952 20952
16  
17 20952 ▶ croak "missing 'options'" unless defined $args{options};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $args{"options"} 0 20952 20952
18 20952 ▶ croak "missing 'options.json'" unless defined $args{options}->{json};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $args{"options"}{"json"} 0 20952 20952
19  
20 20952 my $data = $args{data};
21 20952 my $data_size = $args{data_size};
22 20952 ▶ my $offset = $args{offset} // 0;
Condition50%1/2
$args{"offset"} // 0
l!l
209520
MC/DC0%0/1
$args{"offset"} // 0
$args{"offset"}
Truth table50%1/2
A
A= $args{"offset"}
Aresult
11
00
23 20952 my $max_id = $args{max_id};
24 20952 my $options = $args{options};
25  
26 20952 ▶ croak
Branch: 1 branch
BranchTrueFalseTotal
if $offset + $max_id > $data_size << 3 2 20950 20952
27 2 "a BitField for $max_id bits requires a consent string of at least @{[ $offset + $max_id ]} bits. This consent string only had @{[ $data_size << 3 ]} bits"
28 if $offset + $max_id > ($data_size << 3);
29  
30 20950 my $self = {data => $data, offset => $offset, max_id => $max_id, options => $options,};
31  
32 20950 bless $self, $klass;
33  
34 20950 return ($self, $offset + $max_id);
35 }
36  
37 5 ▶sub max_id {
Subroutine max_id: called CC=1 SCAR=0.0
Pod documented
38 5 my $self = shift;
39  
40 5 return $self->{max_id};
41 }
42  
43 16780 ▶sub contains {
Subroutine contains: called CC=3 SCAR=11.0
Pod documented
44 16780 my ($self, $id) = @_;
45  
46 16780 ▶ croak "invalid vendor id $id: must be positive integer bigger than 0" if $id < 1;
Branch: 1 branch
BranchTrueFalseTotal
if $id < 1 0 16780 16780
47  
48 16780 ▶ return if $id > $self->{max_id};
Branch: 1 branch
BranchTrueFalseTotal
if $id > $self->{"max_id"} 1975 14805 16780
49  
50 14805 return is_set($self->{data}, $self->{offset} + $id - 1);
51 }
52  
53 30 ▶sub all {
Subroutine all: called CC=2 SCAR=6.9
Pod documented
54 30 my $self = shift;
55  
56 30 my $bitstring = unpack("B*", substr($self->{data}, $self->{offset} >> 3));
57 30 $bitstring = substr($bitstring, $self->{offset} & 7, $self->{max_id});
58  
59 30 my @set_bits;
60 30 ▶ while ($bitstring =~ /1/g) {
Branch: 1 branch
BranchTrueFalseTotal
if ($bitstring =~ /1/dg) 732 30 762
61 732 push @set_bits, pos($bitstring);
62 }
63  
64 30 return \@set_bits;
65 }
66  
67 1949 ▶sub TO_JSON {
Subroutine TO_JSON: called CC=11 SCAR=24.2
Pod documented
68 1949 my ($self, $filter_id) = @_;
69  
70 1949 ▶ if (defined $filter_id) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $filter_id) 20 1929 1949
71 20 my $val = $self->contains($filter_id);
72  
73 20 ▶ if (!!$self->{options}->{json}->{compact}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (not $self->{"options"}{"json"}{"compact"}) 0 20 20
74 0 ▶ return $val ? [$filter_id] : [];
Branch: 1 branch
BranchTrueFalseTotal
$val ? : 0 0 0
75 }
76  
77 20 my ($false, $true) = @{$self->{options}->{json}->{boolean_values}};
78 20 ▶ my $bool_val = $val ? $true : $false;
Branch: 1 branch
BranchTrueFalseTotal
$val ? : 14 6 20
79  
80 20 ▶ if (!!$self->{options}->{json}->{verbose}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (not $self->{"options"}{"json"}{"verbose"}) 0 20 20
81 0 return {$filter_id => $bool_val};
82 }
83  
84 20 ▶ return $val ? {$filter_id => $true} : {};
Branch: 1 branch
BranchTrueFalseTotal
$val ? : 14 6 20
85 }
86  
87 1929 ▶ if (!!$self->{options}->{json}->{compact}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (not $self->{"options"}{"json"}{"compact"}) 30 1899 1929
88 30 return $self->all;
89 }
90  
91 1899 my $bitstring = unpack("B*", substr($self->{data}, $self->{offset} >> 3));
92 1899 $bitstring = substr($bitstring, $self->{offset} & 7, $self->{max_id});
93  
94 1899 my ($false, $true) = @{$self->{options}->{json}->{boolean_values}};
95  
96 1899 ▶ if (!!$self->{options}->{json}->{verbose}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (not $self->{"options"}{"json"}{"verbose"}) 2 1897 1899
97 2 my %map = map { $_ => $false } 1 .. $self->{max_id};
98 2 ▶ while ($bitstring =~ /1/g) {
Branch: 1 branch
BranchTrueFalseTotal
if ($bitstring =~ /1/dg) 68 2 70
99 68 $map{pos($bitstring)} = $true;
100 }
101 2 return \%map;
102 }
103  
104 1897 my %map;
105 1897 ▶ while ($bitstring =~ /1/g) {
Branch: 1 branch
BranchTrueFalseTotal
if ($bitstring =~ /1/dg) 978402 1897 980299
106 978402 $map{pos($bitstring)} = $true;
107 }
108 1897 return \%map;
109 }
110  
111 73 1;
112 __END__
« blib/lib/GDPR/IAB/TCFv2/BitUtils.pm ↑ Summary blib/lib/GDPR/IAB/TCFv2/PublisherRestrictions.pm »