VersionRange/Constraint.pm

filter: Statementstmt 98.4% 64 / 65 Branchbran 100% 20 / 20 Conditioncond 80.0% 4 / 5 MC/DCmcdc 66.6% 2 / 3 Subroutinesub 94.1% 16 / 17 Podpod 100% 7 / 7 totaltotal 96.5% 113 / 117 CC 13 SCAR 11.1
CC
13
Cov
98%
from_string
6.0
_percent_decode
5.0
to_string
3.0
CRAP 3.0
« VersionRange.pm ↑ Summary VersionRange/Version.pm »
1 package URI::VersionRange::Constraint;
2  
3 15 ▶use feature ':5.10';
Subroutine BEGIN: called CC=1 SCAR=0.0
4 15 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 15 ▶use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 15 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 15 ▶use Carp ();
Subroutine BEGIN: called CC=1 SCAR=0.0
9 15 ▶use Exporter qw(import);
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 15 ▶use overload '""' => 'to_string', fallback => 1;
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 15 ▶use URI::VersionRange::Version;
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 15 our $VERSION = '2.26';
16  
17 15 our %COMPARATOR = (
18 '=' => 'equal',
19 '<' => 'less than',
20 '<=' => 'less than or equal',
21 '>' => 'greater than',
22 '>=' => 'greater than or equal',
23 );
24  
25 15 my $PERCENT_ENCODE_REGEX = qr{[><=!*|% \x80-\xFF]};
26  
27 8296 ▶sub new {
Subroutine new: called CC=2 SCAR=6.9
Pod documented
28  
29 8296 my ($class, %params) = @_;
30  
31 8296 ▶ my $comparator = delete $params{comparator} // '=';
Condition100%2/2
delete $params{"comparator"} // "="
l!l
82906
MC/DC100%1/1
delete $params{"comparator"} // "="
delete $params{"comparator"}
Truth table100%2/2
A
A= delete $params{"comparator"}
Aresult
11
00
32 8296 my $version = delete $params{version};
33  
34 8296 my $self = {comparator => $comparator, version => $version};
35  
36 8296 return bless $self, $class;
37 }
38  
39 22258 ▶sub version { shift->{version} }
Subroutine version: called CC=1 SCAR=0.0
Pod documented
40 21306 ▶sub comparator { shift->{comparator} }
Subroutine comparator: called CC=1 SCAR=0.0
Pod documented
41  
42 8300 ▶sub from_string {
Subroutine from_string: called CC=6 SCAR=17.9
Pod documented
43  
44 8300 my ($class, $string) = @_;
45  
46 8300 ▶ Carp::croak 'Invalid constraint: empty version' unless defined $string && length $string;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $string and length $string 1 8299 8300
Condition66%2/3
defined $string and length $string
!ll&&!rl&&r
018299
MC/DC50%1/2
defined $string and length $string
defined $string length $string
Truth table66%2/3
A and B
A= defined $stringB= length $string
ABresult
0X0
100
111
47  
48 # - For each <version-constraint>:
49 # - Determine if the <version-constraint> starts with one of the two comparators:
50 # - If it starts with ">=", then the comparator is ">=".
51 # - If it starts with "<=", then the comparator is "<=".
52 # - If it starts with "!=", then the comparator is "!=".
53 # - If it starts with "<", then the comparator is "<".
54 # - If it starts with ">", then the comparator is ">".
55 # - Remove the comparator from <version-constraint> string start. The remaining string is the version.
56 # - Otherwise the version is the full <version-constraint> string (which implies an equality comparator of "=")
57 # - Tools should validate and report an error if the version is empty.
58 # - If the version contains a percent "%" character, apply URL quoting rules to unquote this string.
59  
60 8299 ▶ if ($string =~ /^(>=|<=|!=|<|>|=)(.*)/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($string =~ /^(>=|<=|!=|<|>|=)(.*)/) 1056 7243 8299
61 1056 my ($comparator, $version) = ($1, $2);
62 1056 $version =~ s/^\s+//;
63 1056 ▶ Carp::croak 'Invalid constraint: empty version' unless length $version;
Branch: 1 branch
BranchTrueFalseTotal
unless length $version 1 1055 1056
64 1055 return $class->new(comparator => $comparator, version => _percent_decode($version));
65 }
66  
67 7243 ▶ return $class->new(comparator => '*') if ($string eq '*');
Branch: 1 branch
BranchTrueFalseTotal
if $string eq "*" 24 7219 7243
68  
69 7219 $string =~ s/^\s+//;
70 7219 return $class->new(comparator => '=', version => _percent_decode($string));
71  
72 }
73  
74 9396 ▶sub to_string {
Subroutine to_string: called CC=3 SCAR=11.0
Pod documented
75  
76 9396 my $self = shift;
77  
78 9396 ▶ return '*' if $self->comparator eq '*';
Branch: 1 branch
BranchTrueFalseTotal
if $self->comparator eq "*" 5 9391 9396
79  
80 9391 ▶ return _percent_encode($self->version) if $self->comparator eq '=';
Branch: 1 branch
BranchTrueFalseTotal
if $self->comparator eq "=" 7335 2056 9391
81  
82 2056 return join '', $self->comparator, _percent_encode($self->version);
83  
84 }
85  
86 0 ▶sub to_human_string { sprintf '%s %s', $COMPARATOR{$_[0]->comparator}, $_[0]->version }
Subroutine to_human_string: not called CC=1 SCAR=6.9
Pod documented
87  
88 3 ▶sub TO_JSON { {version => $_[0]->version, comparator => $_[0]->comparator} }
Subroutine TO_JSON: called CC=1 SCAR=0.0
Pod documented
89  
90 9419 ▶sub _percent_encode {
Subroutine _percent_encode: called CC=1 SCAR=0.0
91  
92 9419 my $string = shift;
93 9419 utf8::encode($string);
94 9419 $string =~ s/($PERCENT_ENCODE_REGEX)/sprintf '%%%02X', ord $1/ge;
95 9419 return $string;
96  
97 }
98  
99 8274 ▶sub _percent_decode {
Subroutine _percent_decode: called CC=5 SCAR=16.1
100  
101 8274 my $string = shift;
102 8274 ▶ return $string unless $string =~ /%/;
Branch: 1 branch
BranchTrueFalseTotal
unless $string =~ /%/ 8240 34 8274
103  
104 34 ▶ Carp::croak "Invalid percent-encoding in version '$string'" if $string =~ /%(?![0-9A-F]{2})/;
Branch: 1 branch
BranchTrueFalseTotal
if $string =~ /%(?![0-9A-F]{2})/ 5 29 34
105  
106 29 my $version = $string;
107 29 $version =~ s/%([0-9A-F]{2})/chr hex $1/ge;
108  
109 29 ▶ Carp::croak "Invalid UTF-8 encoding in version '$string'" unless utf8::decode($version);
Branch: 1 branch
BranchTrueFalseTotal
unless utf8::decode($version) 1 28 29
110 28 ▶ Carp::croak "Non-canonical percent-encoding in version '$string'" if _percent_encode($version) ne $string;
Branch: 1 branch
BranchTrueFalseTotal
if _percent_encode($version) ne $string 5 23 28
111  
112 23 return $version;
113  
114 }
115  
116  
117 15 1;
118  
119 __END__
« VersionRange.pm ↑ Summary VersionRange/Version.pm »