lib/CPAN/Audit/Version.pm

filter: Statementstmt 96.1% 50 / 52 Branchbran 92.8% 13 / 14 Conditioncond 100% 3 / 3 MC/DCmcdc 100% 2 / 2 Subroutinesub 100% 15 / 15 Podpod 100% 3 / 3 totaltotal 96.6% 86 / 89 CC 11 SCAR 14.2
CC
11
Cov
96%
in_range
9.0
affected_versions
3.0
BEGIN
1.0
CRAP 4.1
« lib/CPAN/Audit/Query.pm ↑ Summary lib/CPAN/Audit/Discover/Cpanfile.pm »
1 package CPAN::Audit::Version;
2 15 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 15 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 15 ▶use version;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 15 our $VERSION = "1.002";
7  
8 =encoding utf8
9  
10 =head1 NAME
11  
12 CPAN::Audit::Version - the infrastructure to compare versions and version ranges
13  
14 =head1 SYNOPSIS
15  
16 use CPAN::Audit::Version;
17  
18 my $cav = CPAN::Audit::Version->new;
19  
20 $cav->in_range( $version, $range );
21  
22 =head1 DESCRIPTION
23  
24 =head2 Class methods
25  
26 =over 4
27  
28 =item * new
29  
30 Create a new object. This ignores all arguments.
31  
32 =cut
33  
34 2 ▶sub new {
Subroutine new: called CC=1 SCAR=0.0
Pod documented
35 2 my $class = shift;
36  
37 2 my $self = {};
38 2 bless $self, $class;
39  
40 2 return $self;
41 }
42  
43 =back
44  
45 =head2 Instance methods
46  
47 =over 4
48  
49 =item * affected_versions( ARRAY_REF, RANGE )
50  
51 Given an array reference of versions, return a list of all of the
52 versions in ARRAY_REF that are in RANGE. This is really a filter
53 on ARRAY_REF using the values for which C<in_range> returns true.
54  
55 my @matching = $cav->affected_versions( \@versions, $range );
56  
57 =cut
58  
59 15 ▶BEGIN {
Subroutine BEGIN: called CC=1 SCAR=0.0
60 15 ▶use version;
Subroutine BEGIN: called CC=1 SCAR=0.0
61 my $ops = {
62 43544 ▶ '<' => sub { $_[0] < 0 },
Subroutine __ANON__: called CC=1 SCAR=0.0
63 166 ▶ '<=' => sub { $_[0] <= 0 },
Subroutine __ANON__: called CC=1 SCAR=0.0
64 11324 ▶ '==' => sub { $_[0] == 0 },
Subroutine __ANON__: called CC=1 SCAR=0.0
65 8960 ▶ '>' => sub { $_[0] > 0 },
Subroutine __ANON__: called CC=1 SCAR=0.0
66 60127 ▶ '>=' => sub { $_[0] >= 0 },
Subroutine __ANON__: called CC=1 SCAR=0.0
67 2 ▶ '!=' => sub { $_[0] != 0 },
Subroutine __ANON__: called CC=1 SCAR=0.0
68 15 };
69  
70 1 ▶sub affected_versions {
Subroutine affected_versions: called CC=3 SCAR=11.0
Pod documented
71 1 my( $self, $available_versions, $range ) = @_;
72  
73 1 my @affected_versions;
74 1 foreach my $version (@$available_versions) {
75 3 ▶ if ( $self->in_range( $version, $range ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->in_range($version, $range)) 2 1 3
76 2 push @affected_versions, $version;
77 }
78 }
79  
80 1 return @affected_versions;
81 }
82  
83 =item * in_range( VERSION, RANGE )
84  
85 Returns true if VERSION is contained in RANGE, and false otherwise.
86 VERSION is any sort of Perl, such as C<1.23> or C<1.2.3>. The RANGE
87 is a comma-separated list of range specifications using the comparators
88 C<< < >>, C<< <= >>, C<< == >>, C<< > >>, C<< >= >>, or C<< != >>. For
89 example, C<< >=1.23,<1.45 >>, C<< ==1.23 >>, or C<< >1.23 >>.
90  
91 my $version = 5.67;
92 my $range = '>=5,<6'; # so, all the versions in 5.x
93  
94 if( $cav->in_range( $version, $range ) ) {
95 say "$version is within $range";
96 }
97 else {
98 say "$version is not within $range";
99 }
100  
101 =cut
102  
103 119920 ▶sub in_range {
Subroutine in_range: called CC=9 SCAR=22.0
Pod documented
104 119920 my( $self, $version, $range ) = @_;
105 119920 my( @original ) = ($version, $range);
106 119920 ▶ return unless defined $version && defined $range;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $version and defined $range 28 119892 119920
Condition100%3/3
defined $version and defined $range
!ll&&!rl&&r
271119892
MC/DC100%2/2
defined $version and defined $range
defined $version defined $range
Truth table100%3/3
A and B
A= defined $versionB= defined $range
ABresult
0X0
100
111
107 119892 ▶ return unless defined( $version = eval { version->parse($version) } );
Branch: 1 branch
BranchTrueFalseTotal
unless defined($version = eval { do { "version"->parse($version) } }) 186 119706 119892
108  
109 119706 my @ands = split /\s*,\s*/, $range;
110 119706 my $result = 1;
111  
112 119706 foreach my $and (@ands) {
113 124125 my( $op, $range_version ) = $and =~ m/^(<=|<|>=|>|==|!=)?\s*([^\s]+)$/;
114  
115 return
116 124125 ▶ unless defined( $range_version = eval { version->parse($range_version) } );
Branch: 1 branch
BranchTrueFalseTotal
unless defined($range_version = eval { do { "version"->parse($range_version) } }) 2 124123 124125
117  
118 124123 ▶ $op = '>=' unless defined $op;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $op 8544 115579 124123
119 0 ▶ unless( exists $ops->{$op} ) { $result = 0; last; }
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $ops->{$op}) 0 124123 124123
120  
121 15 ▶ no warnings qw(numeric);
Subroutine BEGIN: called CC=1 SCAR=0.0
122 124123 $result = $ops->{$op}->( version::vcmp($version, $range_version) );
123 124123 ▶ last if $result == 0;
Branch: 1 branch
BranchTrueFalseTotal
if $result == 0 70165 53958 124123
124 }
125  
126 119704 return $result;
127 }
128 }
129  
130 =back
131  
132 =head1 LICENSE
133  
134 Copyright (C) Viacheslav Tykhanovskyi.
135  
136 This library is free software; you can redistribute it and/or modify
137 it under the same terms as Perl itself.
138  
139 =head1 AUTHOR
140  
141 Viacheslav Tykhanovskyi E<lt>viacheslav.t@gmail.comE<gt>
142  
143 =cut
144  
145  
146 15 1;
« lib/CPAN/Audit/Query.pm ↑ Summary lib/CPAN/Audit/Discover/Cpanfile.pm »