SSVC/Base.pm

filter: Statementstmt 82.6% 81 / 98 Branchbran 50.0% 3 / 6 Conditioncond 0.0% 0 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 80.7% 21 / 26 Podpod 0.0% 0 / 14 totaltotal 70.4% 105 / 149 CC 7 SCAR 9.3
CC
7
Cov
79%
to_vector_string
12.0
normalize_decision_points
3.0
decision_points_to_vector
3.0
CRAP 2.5
« SSVC/CISA.pm ↑ Summary SSVC/CISA/BOD2604.pm »
1 package SSVC::Base;
2  
3 8 ▶use feature ':5.10';
Subroutine BEGIN: called CC=1 SCAR=0.0
4 8 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 8 ▶use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 8 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 8 ▶use Time::Piece;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 8 ▶use Carp ();
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 8 ▶use overload '""' => 'to_vector_string', fallback => 1;
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 8 ▶use constant METADATA => +{};
Subroutine BEGIN: called CC=1 SCAR=0.0
14 8 ▶use constant DECISION_POINTS => +{};
Subroutine BEGIN: called CC=1 SCAR=0.0
15 8 ▶use constant DECISION_TREE => +{};
Subroutine BEGIN: called CC=1 SCAR=0.0
16 8 ▶use constant DECISION_PATH => +[];
Subroutine BEGIN: called CC=1 SCAR=0.0
17 8 ▶use constant VECTOR => +[];
Subroutine BEGIN: called CC=1 SCAR=0.0
18  
19 sub new {
20  
21 487 ▶ my ($class, %params) = @_;
Subroutine new: called CC=1 SCAR=0.0
Pod undocumented
22  
23 487 my %decision_points = $class->normalize_decision_points(%params);
24 487 my %vector = $class->decision_points_to_vector(%decision_points);
25 487 my $decision = undef;
26  
27 487 my $self = {decision_points => \%decision_points, vector => \%vector, decision => $decision};
28  
29 487 return bless $self, $class;
30  
31 }
32  
33 145 ▶sub vector { shift->{vector} }
Subroutine vector: called CC=1 SCAR=0.0
Pod undocumented
34 0 ▶sub decision_points { shift->{decision_points} }
Subroutine decision_points: not called CC=1 SCAR=6.9
Pod undocumented
35  
36 sub compute {
37  
38 43 ▶ my ($self) = @_;
Subroutine compute: called CC=1 SCAR=0.0
Pod undocumented
39  
40 43 my $key = join ':', map { $self->vector->{$_} } @{$self->DECISION_PATH};
41 43 return $self->DECISION_TREE->{$key};
42  
43 }
44  
45 sub vector_value {
46 7999 ▶ my ($self, $name) = @_;
Subroutine vector_value: called CC=1 SCAR=0.0
Pod undocumented
47 7999 return $self->{vector}->{$name};
48 }
49  
50 sub decision_point_value {
51 0 ▶ my ($self, $name) = @_;
Subroutine decision_point_value: not called CC=1 SCAR=6.9
Pod undocumented
52 0 return $self->{decision_points}->{$name};
53 }
54  
55 sub decision_point_enum {
56 3157 ▶ my ($self, $name) = @_;
Subroutine decision_point_enum: called CC=1 SCAR=0.0
Pod undocumented
57 3157 my @values = @{$self->DECISION_POINTS->{$name}->{values}};
58 3157 return [@values[grep { $_ % 2 } 0 .. @values]];
59 }
60  
61 sub decision_point_labels {
62 1915 ▶ my ($self, $name) = @_;
Subroutine decision_point_labels: called CC=1 SCAR=0.0
Pod undocumented
63 1915 my %labels = @{$self->DECISION_POINTS->{$name}->{values}};
64 1915 return \%labels;
65 }
66  
67 sub decision_point_codes {
68 3157 ▶ my ($self, $name) = @_;
Subroutine decision_point_codes: called CC=1 SCAR=0.0
Pod undocumented
69 3157 my %codes = reverse @{$self->DECISION_POINTS->{$name}->{values}};
70 3157 return \%codes;
71 }
72  
73 sub decision_point_info {
74 0 ▶ my ($self, $name) = @_;
Subroutine decision_point_info: not called CC=1 SCAR=6.9
Pod undocumented
75 0 my $dp = $self->DECISION_POINTS->{$name};
76  
77 return {
78 vector_name => $dp->{vector_name},
79 label => $dp->{label},
80 definition => $dp->{definition},
81 0 enum => $self->decision_point_enum($name),
82 labels => $self->decision_point_labels($name),
83 codes => $self->decision_point_codes($name),
84 };
85 }
86  
87 sub decision_points_to_vector {
88  
89 487 ▶ my ($self, %decision_points) = @_;
Subroutine decision_points_to_vector: called CC=3 SCAR=11.0
Pod undocumented
90  
91 487 my %output = ();
92  
93 487 foreach (keys %{$self->DECISION_POINTS}) {
94 4100 ▶ next unless $decision_points{$_};
Branch: 1 branch
BranchTrueFalseTotal
unless $decision_points{$_} 943 3157 4100
95 3157 my $vector_name = $self->DECISION_POINTS->{$_}->{vector_name};
96 3157 $output{$vector_name} = $self->decision_point_codes($_)->{$decision_points{$_}};
97 }
98  
99 487 return %output;
100  
101 }
102  
103 sub normalize_decision_points {
104  
105 487 ▶ my ($self, %params) = @_;
Subroutine normalize_decision_points: called CC=3 SCAR=11.1
Pod undocumented
106  
107 487 my %normalized = ();
108  
109 487 foreach my $dp (keys %params) {
110  
111 3157 my @allowed = @{$self->decision_point_enum($dp)};
112  
113 3157 ▶ unless (grep { lc $params{$dp} eq $_ } @allowed) {
Branch: 1 branch
BranchTrueFalseTotal
unless (grep {lc $params{$dp} eq $_;} @allowed) 0 3157 3157
114 0 Carp::croak "Unknown value for '$dp' (allowed: " . join(', ', @allowed) . ")";
115 }
116  
117 3157 $normalized{$dp} = lc $params{$dp};
118 }
119  
120 487 return %normalized;
121  
122 }
123  
124  
125 sub to_vector_string {
126  
127 0 ▶ my $self = shift;
Subroutine to_vector_string: not called CC=3 SCAR=24.8
Pod undocumented
128 0 my $prefix = 'SSVC';
129  
130 0 ▶ if (defined $self->METADATA->{key}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->METADATA->{"key"}) 0 0 0
131 0 $prefix = uc $self->METADATA->{key};
132 }
133  
134 0 my $now = Time::Piece->new->to_gmtime->datetime;
135 0 ▶ my @vectors = (map { join ':', $_, $self->{vector}->{$_} || Carp::croak 'Missing metric' } @{$self->VECTOR});
Condition0%0/3
$self->{'vector'}{$_} || Carp::croak('Missing metric')
l!l&&r!l&&!r
000
MC/DC0%0/2
$self->{'vector'}{$_} || Carp::croak('Missing metric')
$self->{'vector'}{$_} Carp::croak('Missing metric')
Truth table0%0/3
A || B
A= $self->{'vector'}{$_}B= Carp::croak('Missing metric')
ABresult
1X1
011
000
136  
137 0 return join '/', "${prefix}v1", @vectors, "${now}.000Z";
138  
139 }
140  
141 0 ▶sub TO_JSON { {} }
Subroutine TO_JSON: not called CC=1 SCAR=6.9
Pod undocumented
142  
143 8 1;
144  
145 __END__
« SSVC/CISA.pm ↑ Summary SSVC/CISA/BOD2604.pm »