blib/lib/License/SPDX.pm

filter: Statementstmt 100% 90 / 90 Branchbran 100% 28 / 28 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 23 / 23 Podpod 100% 9 / 9 totaltotal 100% 150 / 150 CC 15 SCAR 10.0
CC
15
Cov
100%
__ANON__
5.0
__ANON__
5.0
check_exception
4.0
CRAP 2.7
↑ Summary
1 package License::SPDX;
2  
3 10 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 10 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 10 ▶use Class::Utils qw(set_params);
Subroutine BEGIN: called CC=1 SCAR=0.0
7 10 ▶use Cpanel::JSON::XS;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 10 ▶use Error::Pure qw(err);
Subroutine BEGIN: called CC=1 SCAR=0.0
9 10 ▶use File::Share ':all';
Subroutine BEGIN: called CC=1 SCAR=0.0
10 10 ▶use List::Util qw(first);
Subroutine BEGIN: called CC=1 SCAR=0.0
11 10 ▶use Perl6::Slurp qw(slurp);
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 10 our $VERSION = 0.05;
14  
15 # Constructor.
16 sub new {
17 22 ▶ my ($class, @params) = @_;
Subroutine new: called CC=1 SCAR=0.0
Pod documented
18  
19 # Create object.
20 22 my $self = bless {}, $class;
21  
22 # Process parameters.
23 22 set_params($self, @params);
24  
25 # Load all SPDX licenses.
26 22 open my $data_fh, '<', dist_dir('License-SPDX').'/licenses.json';
27 22 my $data = slurp($data_fh);
28 22 $self->{'licenses'} = Cpanel::JSON::XS->new->ascii->pretty->allow_nonref->decode($data);
29  
30 # Load all SPDX exceptions.
31 22 open my $data_exc_fh, '<', dist_dir('License-SPDX').'/exceptions.json';
32 22 my $data_exc = slurp($data_exc_fh);
33 22 $self->{'exceptions'} = Cpanel::JSON::XS->new->ascii->pretty->allow_nonref->decode($data_exc);
34  
35 22 return $self;
36 }
37  
38 sub check_exception {
39 7 ▶ my ($self, $check_string, $opts_hr) = @_;
Subroutine check_exception: called CC=4 SCAR=13.9
Pod documented
40  
41 7 ▶ if (! defined $opts_hr) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $opts_hr) 2 5 7
42 2 $opts_hr = {};
43 }
44 7 ▶ if (! exists $opts_hr->{'check_type'}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $opts_hr->{"check_type"}) 2 5 7
45 2 $opts_hr->{'check_type'} = 'id';
46 }
47  
48 my $check_cb = sub {
49 235 ▶ my $exception_hr = shift;
Subroutine __ANON__: called CC=5 SCAR=16.1
50 235 ▶ if ($opts_hr->{'check_type'} eq 'id') {
Branch: 2 branches
BranchTrueFalseTotal
if ($opts_hr->{'check_type'} eq 'id') { } 156 79 235
elsif ($opts_hr->{'check_type'} eq 'name') { } 78 1 79
51 156 ▶ if ($check_string eq $exception_hr->{'licenseExceptionId'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($check_string eq $exception_hr->{"licenseExceptionId"}) 2 154 156
52 2 return 1;
53 }
54 } elsif ($opts_hr->{'check_type'} eq 'name') {
55 78 ▶ if ($check_string eq $exception_hr->{'name'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($check_string eq $exception_hr->{"name"}) 1 77 78
56 1 return 1;
57 }
58 } else {
59 1 err "Check type '$opts_hr->{'check_type'}' doesn't supported.";
60 }
61 7 };
62  
63 7 ▶ if (first { $check_cb->($_); } @{$self->{'exceptions'}->{'exceptions'}}) {
Subroutine __ANON__: called CC=1 SCAR=0.0
Branch: 1 branch
BranchTrueFalseTotal
if (&first(sub { &$check_cb($_); } , @{$$self{'exceptions'}{'exceptions'};})) { } 3 3 6
64 3 return 1;
65 } else {
66 3 return 0;
67 }
68 }
69  
70 sub check_license {
71 7 ▶ my ($self, $check_string, $opts_hr) = @_;
Subroutine check_license: called CC=4 SCAR=13.9
Pod documented
72  
73 7 ▶ if (! defined $opts_hr) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $opts_hr) 2 5 7
74 2 $opts_hr = {};
75 }
76 7 ▶ if (! exists $opts_hr->{'check_type'}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $opts_hr->{"check_type"}) 2 5 7
77 2 $opts_hr->{'check_type'} = 'id';
78 }
79  
80 my $check_cb = sub {
81 2611 ▶ my $license_hr = shift;
Subroutine __ANON__: called CC=5 SCAR=16.1
82 2611 ▶ if ($opts_hr->{'check_type'} eq 'id') {
Branch: 2 branches
BranchTrueFalseTotal
if ($opts_hr->{'check_type'} eq 'id') { } 1740 871 2611
elsif ($opts_hr->{'check_type'} eq 'name') { } 870 1 871
83 1740 ▶ if ($check_string eq $license_hr->{'licenseId'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($check_string eq $license_hr->{"licenseId"}) 2 1738 1740
84 2 return 1;
85 }
86 } elsif ($opts_hr->{'check_type'} eq 'name') {
87 870 ▶ if ($check_string eq $license_hr->{'name'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($check_string eq $license_hr->{"name"}) 1 869 870
88 1 return 1;
89 }
90 } else {
91 1 err "Check type '$opts_hr->{'check_type'}' doesn't supported.";
92 }
93 7 };
94  
95 7 ▶ if (first { $check_cb->($_); } @{$self->{'licenses'}->{'licenses'}}) {
Subroutine __ANON__: called CC=1 SCAR=0.0
Branch: 1 branch
BranchTrueFalseTotal
if (&first(sub { &$check_cb($_); } , @{$$self{'licenses'}{'licenses'};})) { } 3 3 6
96 3 return 1;
97 } else {
98 3 return 0;
99 }
100 }
101  
102 sub exception {
103 2 ▶ my ($self, $exception_id) = @_;
Subroutine exception: called CC=1 SCAR=0.0
Pod documented
104  
105 2 ▶ return first { $_->{'licenseExceptionId'} eq $exception_id } @{$self->{'exceptions'}->{'exceptions'}};
Subroutine __ANON__: called CC=1 SCAR=0.0
106 }
107  
108 sub exceptions {
109 1 ▶ my $self = shift;
Subroutine exceptions: called CC=1 SCAR=0.0
Pod documented
110  
111 1 return @{$self->{'exceptions'}->{'exceptions'}};
112 }
113  
114 sub license {
115 2 ▶ my ($self, $license_id) = @_;
Subroutine license: called CC=1 SCAR=0.0
Pod documented
116  
117 2 ▶ return first { $_->{'licenseId'} eq $license_id } @{$self->{'licenses'}->{'licenses'}};
Subroutine __ANON__: called CC=1 SCAR=0.0
118 }
119  
120 sub licenses {
121 1 ▶ my $self = shift;
Subroutine licenses: called CC=1 SCAR=0.0
Pod documented
122  
123 1 return @{$self->{'licenses'}->{'licenses'}};
124 }
125  
126 sub spdx_release_date {
127 1 ▶ my $self = shift;
Subroutine spdx_release_date: called CC=1 SCAR=0.0
Pod documented
128  
129 1 return $self->{'licenses'}->{'releaseDate'};
130 }
131  
132 sub spdx_version {
133 1 ▶ my $self = shift;
Subroutine spdx_version: called CC=1 SCAR=0.0
Pod documented
134  
135 1 return $self->{'licenses'}->{'licenseListVersion'};
136 }
137  
138 10 1;
139  
140 __END__
↑ Summary