CycloneDX/License.pm

filter: Statementstmt 82.5% 90 / 109 Branchbran 50.0% 21 / 42 Conditioncond 50.0% 10 / 20 MC/DCmcdc 16.6% 2 / 12 Subroutinesub 96.0% 24 / 25 Podpod 50.0% 1 / 2 totaltotal 70.4% 148 / 210 CC 29 SCAR 23.5
CC
29
Cov
71%
TO_JSON
24.5
__ANON__
12.4
_trigger_id
10.7
CRAP 10.5
« CycloneDX/Component/PerformanceMetric.pm ↑ Summary CycloneDX.pm »
1 package SBOM::CycloneDX::License;
2  
3 16 ▶use 5.010001;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 16 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 16 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 16 ▶use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 16 ▶use SBOM::CycloneDX::BomRef;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 16 ▶use SBOM::CycloneDX::Enum;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 16 ▶use SBOM::CycloneDX::List;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 16 ▶use SBOM::CycloneDX::License::Licensing;
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 16 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 16 ▶use List::Util qw(first);
Subroutine BEGIN: called CC=1 SCAR=0.0
15 16 ▶use Types::Standard qw(Str Enum InstanceOf);
Subroutine BEGIN: called CC=1 SCAR=0.0
16 16 ▶use Types::TypeTiny qw(ArrayLike);
Subroutine BEGIN: called CC=1 SCAR=0.0
17  
18 16 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
19 16 ▶use namespace::autoclean;
Subroutine BEGIN: called CC=1 SCAR=0.0
20  
21 16 ▶use constant DEBUG => $ENV{SBOM_DEBUG} || 0;
Subroutine BEGIN: called CC=2 SCAR=7.1
Condition50%1/2
$ENV{'SBOM_DEBUG'} || 0
l!l
016
MC/DC0%0/1
$ENV{'SBOM_DEBUG'} || 0
$ENV{'SBOM_DEBUG'}
Truth table50%1/2
A
A= $ENV{'SBOM_DEBUG'}
Aresult
11
00
22  
23 # TODO Incomplete
24  
25 22 ▶around BUILDARGS => sub {
Subroutine __ANON__: called CC=5 SCAR=25.2
26  
27 22 my ($orig, $class, @args) = @_;
28  
29 22 ▶ if (@args == 1 && defined $args[0] && !ref $args[0]) {
Branch: 1 branch
BranchTrueFalseTotal
if (@args == 1 and defined $args[0] and not ref $args[0]) 0 22 22
Condition33%2/6
@args == 1 and defined $args[0] and not ref $args[0]
!ll&&!rl&&r
2200
@args == 1 and defined $args[0]
!ll&&!rl&&r
2200
MC/DC0%0/3
@args == 1 and defined $args[0] and not ref $args[0]
@args == 1 defined $args[0] not ref $args[0]
Truth table25%1/4
A and B and not(C)
A= @args == 1B= defined $args[0]C= not ref $args[0]
ABCresult
0XX0
10X0
1100
1111
30  
31 0 my $license = $args[0];
32 0 $license =~ s/^\s+|\s+$//g;
33  
34 0 ▶ if ($license =~ /(\bWITH\b|\bOR\b|\bAND\b)/i) {
Branch: 1 branch
BranchTrueFalseTotal
if ($license =~ /(\bWITH\b|\bOR\b|\bAND\b)/i) { } 0 0 0
35 0 return {expression => $license};
36 }
37 else {
38 0 return {id => $license};
39 }
40 }
41  
42 22 return $class->$orig(@args);
43  
44 16 };
45  
46 16 extends 'SBOM::CycloneDX::Base';
47  
48 22 ▶sub BUILD {
Subroutine BUILD: called CC=3 SCAR=11.7
Pod undocumented
49 22 my ($self, $args) = @_;
50 22 ▶ Carp::croak('"id" and "name" cannot be used at the same time') if exists $args->{id} && exists $args->{name};
Branch: 1 branch
BranchTrueFalseTotal
if exists $args->{"id"} and exists $args->{"name"} 0 22 22
Condition66%2/3
exists $args->{"id"} and exists $args->{"name"}
!ll&&!rl&&r
4180
MC/DC0%0/2
exists $args->{"id"} and exists $args->{"name"}
exists $args->{"id"} exists $args->{"name"}
Truth table66%2/3
A and B
A= exists $args->{"id"}B= exists $args->{"name"}
ABresult
0X0
100
111
51 }
52  
53 has bom_ref => (
54 is => 'rw',
55 isa => InstanceOf ['SBOM::CycloneDX::BomRef'],
56 2 ▶ coerce => sub { ref($_[0]) ? $_[0] : SBOM::CycloneDX::BomRef->new($_[0]) }
Subroutine __ANON__: called CC=2 SCAR=7.6
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] ? : 0 2 2
57 16 );
58  
59 16 has id => (is => 'rw', isa => Str, trigger => 1);
60 16 has name => (is => 'rw', isa => Str);
61 16 has acknowledgement => (is => 'rw', isa => Enum [qw(declared concluded)]);
62 16 has text => (is => 'rw', isa => InstanceOf ['SBOM::CycloneDX::Attachment']);
63 16 has url => (is => 'rw', isa => Str, trigger => 1);
64 16 has expression => (is => 'rw', isa => Str); # TODO check SPDX expression
65  
66 has expression_details => (
67 is => 'rw',
68 isa => ArrayLike [InstanceOf ['SBOM::CycloneDX::License::ExpressionDetail']],
69 22 ▶ default => sub { SBOM::CycloneDX::List->new }
Subroutine __ANON__: called CC=1 SCAR=0.0
70 16 );
71  
72 has licensing => (
73 is => 'rw',
74 isa => InstanceOf ['SBOM::CycloneDX::License::Licensing'],
75 22 ▶ default => sub { SBOM::CycloneDX::License::Licensing->new }
Subroutine __ANON__: called CC=1 SCAR=0.0
76 16 );
77  
78 has properties => (
79 is => 'rw',
80 isa => ArrayLike [InstanceOf ['SBOM::CycloneDX::Property']],
81 22 ▶ default => sub { SBOM::CycloneDX::List->new }
Subroutine __ANON__: called CC=1 SCAR=0.0
82 16 );
83  
84  
85 18 ▶sub _trigger_id {
Subroutine _trigger_id: called CC=5 SCAR=23.7
86  
87 18 my ($self) = @_;
88  
89 18 ▶ if ($self->id && $self->id =~ /(\bWITH\b|\bOR\b|\bAND\b)/i) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->id and $self->id =~ /(\bWITH\b|\bOR\b|\bAND\b)/i) 0 18 18
Condition33%1/3
$self->id and $self->id =~ /(\bWITH\b|\bOR\b|\bAND\b)/i
!ll&&!rl&&r
0180
MC/DC0%0/2
$self->id and $self->id =~ /(\bWITH\b|\bOR\b|\bAND\b)/i
$self->id $self->id =~ /(\bWITH\b|\bOR\b|\bAND\b)/i
Truth table33%1/3
A and B
A= $self->idB= $self->id =~ /(\bWITH\b|\bOR\b|\bAND\b)/i
ABresult
0X0
100
111
90 0 DEBUG and say STDERR '-- Detected SPDX expression';
91 0 $self->expression($self->id);
92 0 $self->{id} = undef;
93 }
94  
95 18 ▶ if ($self->id && $self->id =~ /^(NOASSERTION|NONE)$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->id and $self->id =~ /^(NOASSERTION|NONE)$/) 0 18 18
Condition33%1/3
$self->id and $self->id =~ /^(NOASSERTION|NONE)$/
!ll&&!rl&&r
0180
MC/DC0%0/2
$self->id and $self->id =~ /^(NOASSERTION|NONE)$/
$self->id $self->id =~ /^(NOASSERTION|NONE)$/
Truth table33%1/3
A and B
A= $self->idB= $self->id =~ /^(NOASSERTION|NONE)$/
ABresult
0X0
100
111
96 0 DEBUG and say STDERR "-- Detected $1 license identifier (unset license identifier)";
97 0 $self->{id} = undef;
98 }
99  
100 }
101  
102  
103 0 ▶sub _trigger_url {
Subroutine _trigger_url: not called CC=2 SCAR=17.9
104 0 my ($self) = @_;
105 0 ▶ $self->url('https://opensource.org/license/' . $self->id) if $self->url eq '1';
Branch: 1 branch
BranchTrueFalseTotal
if $self->url eq 1 0 0 0
106 }
107  
108 53 ▶sub TO_JSON {
Subroutine TO_JSON: called CC=16 SCAR=32.0
Pod documented
109  
110 53 my $self = shift;
111  
112 53 my $json = {};
113  
114 53 ▶ if ($self->expression) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->expression) { } 0 53 53
115 0 $json->{expression} = $self->expression;
116 0 ▶ $json->{expressionDetails} = $self->expression_details if @{$self->expression_details};
Branch: 1 branch
BranchTrueFalseTotal
if @{$self->expression_details;} 0 0 0
117 0 ▶ $json->{acknowledgement} = $self->acknowledgement if $self->acknowledgement;
Branch: 1 branch
BranchTrueFalseTotal
if $self->acknowledgement 0 0 0
118 0 ▶ $json->{'bom-ref'} = $self->bom_ref if $self->bom_ref;
Branch: 1 branch
BranchTrueFalseTotal
if $self->bom_ref 0 0 0
119 0 ▶ $json->{licensing} = $self->licensing if %{$self->licensing->TO_JSON};
Branch: 1 branch
BranchTrueFalseTotal
if %{$self->licensing->TO_JSON;} 0 0 0
120 }
121 else {
122  
123 53 my $spdx_license = $self->id;
124 53 my $license_name = $self->name;
125  
126 53 ▶ if (defined $spdx_license and not first { $_ eq $spdx_license } @{SBOM::CycloneDX::Enum->SPDX_LICENSES()}) {
Subroutine __ANON__: called CC=1 SCAR=0.0
Branch: 1 branch
BranchTrueFalseTotal
if (defined $spdx_license and not &first(sub { $_ eq $spdx_license; } , @{"SBOM::CycloneDX::Enum"->SPDX_LICENSES;})) 22 31 53
Condition100%3/3
defined $spdx_license and not &first(sub { $_ eq $spdx_license; } , @{"SBOM::CycloneDX::Enum"->SPDX_LICENSES;})
!ll&&!rl&&r
102122
MC/DC100%2/2
defined $spdx_license and not &first(sub { $_ eq $spdx_license; } , @{"SBOM::CycloneDX::Enum"->SPDX_LICENSES;})
defined $spdx_license not &first(sub { $_ eq $spdx_license; } , @{"SBOM::CycloneDX::Enum"->SPDX_LICENSES;})
Truth table100%3/3
A and not(B)
A= defined $spdx_licenseB= not &first(sub { $_ eq $spdx_license; } , @{"SBOM::CycloneDX::Enum"->SPDX_LICENSES;})
ABresult
0X0
100
111
127 22 DEBUG and say STDERR "-- SPDX license not found ($spdx_license)";
128 }
129  
130 53 $json->{license} = {};
131  
132 53 ▶ $json->{license}->{id} = $spdx_license if $spdx_license;
Branch: 1 branch
BranchTrueFalseTotal
if $spdx_license 43 10 53
133 53 ▶ $json->{license}->{name} = $license_name if $license_name;
Branch: 1 branch
BranchTrueFalseTotal
if $license_name 6 47 53
134  
135 53 ▶ $json->{license}->{'bom-ref'} = $self->bom_ref if $self->bom_ref;
Branch: 1 branch
BranchTrueFalseTotal
if $self->bom_ref 6 47 53
136 53 ▶ $json->{license}->{acknowledgement} = $self->acknowledgement if $self->acknowledgement;
Branch: 1 branch
BranchTrueFalseTotal
if $self->acknowledgement 6 47 53
137 53 ▶ $json->{license}->{text} = $self->text if $self->text;
Branch: 1 branch
BranchTrueFalseTotal
if $self->text 6 47 53
138 53 ▶ $json->{license}->{url} = $self->url if $self->url;
Branch: 1 branch
BranchTrueFalseTotal
if $self->url 0 53 53
139 53 ▶ $json->{license}->{properties} = $self->properties if @{$self->properties};
Branch: 1 branch
BranchTrueFalseTotal
if @{$self->properties;} 0 53 53
140 53 ▶ $json->{license}->{licensing} = $self->licensing if %{$self->licensing->TO_JSON};
Branch: 1 branch
BranchTrueFalseTotal
if %{$self->licensing->TO_JSON;} 0 53 53
141  
142 }
143  
144 53 return $json;
145  
146 }
147  
148 16 1;
149  
150 =encoding utf-8
151  
152 =head1 NAME
153  
154 SBOM::CycloneDX::License - Specifies the details and attributes related to a software license
155  
156 =head1 SYNOPSIS
157  
158 # SPDX license
159  
160 $license = SBOM::CycloneDX::License->new(
161 id => 'Apache-2.0'
162 );
163  
164 # or
165  
166 $license = SBOM::CycloneDX::License->new('MIT');
167  
168 # SPDX license expression
169  
170 $license = SBOM::CycloneDX::License->new(
171 expression => 'MIT AND (LGPL-2.1-or-later OR BSD-3-Clause)'
172 );
173  
174 # or
175  
176 $license = SBOM::CycloneDX::License->new('MIT AND (LGPL-2.1-or-later OR BSD-3-Clause)');
177  
178 # Non-SPDX license
179  
180 $license = SBOM::CycloneDX::License->new(
181 name => 'Acme Software License'
182 );
183  
184  
185 =head1 DESCRIPTION
186  
187 L<SBOM::CycloneDX::License> specifies the details and attributes related to a software
188 license.
189  
190 It can either include a valid SPDX license identifier or a named license,
191 along with additional properties such as license acknowledgment, comprehensive
192 commercial licensing information, and the full text of the license.
193  
194 =head2 METHODS
195  
196 L<SBOM::CycloneDX::License> inherits all methods from L<SBOM::CycloneDX::Base>
197 and implements the following new ones.
198  
199 =over
200  
201 =item SBOM::CycloneDX::License->new( $id | $expression | %PARAMS )
202  
203 Properties:
204  
205 =over
206  
207 =item * C<bom_ref>, An identifier which can be used to reference the license
208 elsewhere in the BOM. Every bom-ref must be unique within the BOM.
209  
210 Value SHOULD not start with the BOM-Link intro C<urn:cdx:> to avoid conflicts with BOM-Links.
211  
212 =item * C<id>, A valid SPDX license identifier. If specified, this value must be
213 one of the enumeration of valid SPDX license identifiers defined in L<SBOM::CycloneDX::Enum> C<SPDX_LICENSES>.
214  
215 =item * C<expression>, A tuple of exactly one SPDX License Expression.
216  
217 Refer to L<https://spdx.org/specifications> for syntax requirements.
218  
219 =item * C<expression_details>, Details for parts of the C<expression>.
220  
221 See L<SBOM::CycloneDX::License::ExpressionDetail>
222  
223 =item * C<name>, The name of the license. This may include the name of a commercial
224 or proprietary license or an open source license that may not be defined by SPDX.
225  
226 =item * C<acknowledgement>,
227  
228 =item * C<text>, An way to include the textual content of a license.
229 See L<SBOM::CycloneDX::Attachment>
230  
231 =item * C<url>, The URL to the license file. If C<1> is provided, the license URL is
232 automatically generated.
233  
234 =item * C<licensing>, Declared licenses and concluded licenses represent two different
235 stages in the licensing process within software development. Declared licenses refer
236 to the initial intention of the software authors regarding the licensing terms
237 under which their code is released. On the other hand, concluded licenses are the
238 result of a comprehensive analysis of the project's codebase to identify and confirm
239 the actual licenses of the components used, which may differ from the initially
240 declared licenses. While declared licenses provide an upfront indication of the
241 licensing intentions, concluded licenses offer a more thorough understanding of
242 the actual licensing within a project, facilitating proper compliance and risk
243 management. Observed licenses are defined in C<$bom->evidence->licenses>. Observed
244 licenses form the evidence necessary to substantiate a concluded license.
245  
246 See L<SBOM::CycloneDX::License::Licensing>
247  
248  
249 =item * C<properties>, Provides the ability to document properties in a name-value
250 store. This provides flexibility to include data not officially supported in the
251 standard without having to use additional namespaces or create extensions.
252 Unlike key-value stores, properties support duplicate names, each potentially
253 having different values. Property names of interest to the general public are
254 encouraged to be registered in the CycloneDX Property Taxonomy. Formal
255 registration is optional. See L<SBOM::CycloneDX::Property>
256  
257 =back
258  
259 =item $license->bom_ref
260  
261 =item $license->id
262  
263 # SPDX license
264  
265 $license = SBOM::CycloneDX::License->new(
266 id => 'Apache-2.0'
267 );
268  
269 # or
270  
271 $license = SBOM::CycloneDX::License->new('MIT');
272  
273 =item $license->name
274  
275 =item $license->acknowledgement
276  
277 =item $license->text
278  
279 $license->text(SBOM::CycloneDX::Attachment(file => '/path/LICENSE.md'));
280  
281 =item $license->url
282  
283 =item $license->expression
284  
285 # SPDX license expression
286  
287 $license = SBOM::CycloneDX::License->new(
288 expression => 'MIT AND (LGPL-2.1-or-later OR BSD-3-Clause)'
289 );
290  
291 # or
292  
293 $license = SBOM::CycloneDX::License->new('MIT AND (LGPL-2.1-or-later OR BSD-3-Clause)');
294  
295 =item $license->expression_details
296  
297 =item $license->licensing
298  
299 $license->licensing->alt_ids(['acme', 'acme-license']);
300  
301  
302 $licensing = SBOM::CycloneDX::License::Licensing->new(
303 alt_ids => ['acme', 'acme-license'],
304 purchase_order => 'PO-12345',
305 license_types => ['appliance'],
306 );
307  
308 $license->licensing($licensing);
309  
310 =item $license->properties
311  
312 $license->properties->add(SBOM::CycloneDX::Property->new(name => 'foo', value => 'bar'));
313  
314 =back
315  
316 =head1 SUPPORT
317  
318 =head2 Bugs / Feature Requests
319  
320 Please report any bugs or feature requests through the issue tracker
321 at L<https://github.com/giterlizzi/perl-SBOM-CycloneDX/issues>.
322 You will be notified automatically of any progress on your issue.
323  
324 =head2 Source Code
325  
326 This is open source software. The code repository is available for
327 public review and contribution under the terms of the license.
328  
329 L<https://github.com/giterlizzi/perl-SBOM-CycloneDX>
330  
331 git clone https://github.com/giterlizzi/perl-SBOM-CycloneDX.git
332  
333  
334 =head1 AUTHOR
335  
336 =over 4
337  
338 =item * Giuseppe Di Terlizzi <gdt@cpan.org>
339  
340 =back
341  
342  
343 =head1 LICENSE AND COPYRIGHT
344  
345 This software is copyright (c) 2025-2026 by Giuseppe Di Terlizzi.
346  
347 This is free software; you can redistribute it and/or modify it under
348 the same terms as the Perl 5 programming language system itself.
349  
350 =cut
« CycloneDX/Component/PerformanceMetric.pm ↑ Summary CycloneDX.pm »