PackageDetails/Entries.pm

filter: Statementstmt 81.9% 127 / 155 Branchbran 68.7% 33 / 48 Conditioncond 66.6% 14 / 21 MC/DCmcdc 50.0% 7 / 14 Subroutinesub 75.8% 22 / 29 Podpod 100% 18 / 18 totaltotal 77.5% 221 / 285 CC 36 SCAR 18.0
CC
36
Cov
78%
as_unique_sorted_list
12.8
add_entry
11.4
_parse_version
7.2
CRAP 6.0
« PackageDetails/Redis.pm ↑ Summary PackageDetails/Header.pm »
1 15 ▶use 5.008;
Subroutine BEGIN: called CC=1 SCAR=0.0
2  
3 package CPAN::PackageDetails::Entries;
4 15 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 15 ▶use warnings::register;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 15 our $VERSION = '0.263';
8  
9 15 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 15 ▶use version;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 0 ▶sub DESTROY { }
Subroutine DESTROY: not called CC=1 SCAR=0.0
13  
14 =encoding utf8
15  
16 =head1 NAME
17  
18 CPAN::PackageDetails::Entries - Handle the collection of records of 02packages.details.txt.gz
19  
20 =head1 SYNOPSIS
21  
22 Used internally by CPAN::PackageDetails
23  
24 =head1 DESCRIPTION
25  
26 =head2 Methods
27  
28 =over 4
29  
30 =item new
31  
32 Creates a new Entries object. This doesn't do anything fancy. To add
33 to it, use C<add_entry>.
34  
35 entry_class => the class to use for each entry object
36 columns => the column names, in order that you want them in the output
37  
38 If you specify the C<allow_packages_only_once> option with a true value
39 and you try to add that package twice, the object will die. See C<add_entry>.
40  
41 =cut
42  
43 25 ▶sub new {
Subroutine new: called CC=1 SCAR=0.0
Pod documented
44 25 my( $class, %args ) = @_;
45  
46 25 my %hash = (
47 entry_class => 'CPAN::PackageDetails::Entry',
48 allow_packages_only_once => 1,
49 allow_suspicious_names => 0,
50 columns => [],
51 entries => {},
52 %args
53 );
54  
55 25 $hash{max_widths} = [ (0) x @{ $hash{columns} } ];
56  
57 25 bless \%hash, $_[0]
58 }
59  
60 =item entry_class
61  
62 Returns the class that Entries uses to make a new Entry object.
63  
64 =cut
65  
66 1477 ▶sub entry_class { $_[0]->{entry_class} }
Subroutine entry_class: called CC=1 SCAR=0.0
Pod documented
67  
68 =item columns
69  
70 Returns a list of the column names in the entry
71  
72 =cut
73  
74 13 ▶sub columns { @{ $_[0]->{columns} } };
Subroutine columns: called CC=1 SCAR=0.0
Pod documented
75  
76 =item column_index_for( COLUMN )
77  
78 Returns the list position of the named COLUMN.
79  
80 =cut
81  
82 0 ▶sub column_index_for {
Subroutine column_index_for: not called CC=2 SCAR=17.9
Pod documented
83 0 my( $self, $column ) = @_;
84  
85  
86 my $index = grep {
87 0 $self->{columns}[$_] eq $column
88 0 } 0 .. @{ $self->columns };
89  
90 0 ▶ return unless defined $index;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $index 0 0 0
91 0 return $index;
92 }
93  
94 =item count
95  
96 Returns the number of entries. This is not the same as the number of
97 lines that would show up in the F<02packages.details.txt> file since
98 this method counts duplicates as well.
99  
100 =cut
101  
102 10 ▶sub count {
Subroutine count: called CC=2 SCAR=6.9
Pod documented
103 10 my $self = shift;
104  
105 10 my $count = 0;
106 10 foreach my $package ( keys %{ $self->{entries} } ) {
107 2896 $count += keys %{ $self->{entries}{$package} };
108 }
109  
110 10 return $count;
111 }
112  
113 =item entries
114  
115 DEPRECATED: use C<get_hash>
116  
117 =item get_hash
118  
119 Returns the list of entries as an hash reference. The hash key is the
120 package name.
121  
122 =cut
123  
124 0 ▶sub entries {
Subroutine entries: not called CC=1 SCAR=6.9
Pod documented
125 0 carp "entries is deprecated. Use get_hash instead";
126 0 &get_hash;
127 }
128  
129 7 ▶sub get_hash { $_[0]->{entries} }
Subroutine get_hash: called CC=1 SCAR=0.0
Pod documented
130  
131 =item allow_packages_only_once( [ARG] )
132  
133 Set or retrieve the value of the allow_packages_only_once setting. It's
134 a boolean.
135  
136 =cut
137  
138 1481 ▶sub allow_packages_only_once {
Subroutine allow_packages_only_once: called CC=2 SCAR=7.2
Pod documented
139 1481 ▶ $_[0]->{allow_packages_only_once} = !! $_[1] if defined $_[1];
Branch: 1 branch
BranchTrueFalseTotal
if defined $_[1] 0 1481 1481
140  
141 1481 $_[0]->{allow_packages_only_once};
142 }
143  
144 =item allow_suspicious_names( [ARG] )
145  
146 Allow an entry to accept an illegal name. Normally you shouldn't use this,
147 but PAUSE has made bad files before.
148  
149 =cut
150  
151 1 ▶sub allow_suspicious_names {
Subroutine allow_suspicious_names: called CC=2 SCAR=7.2
Pod documented
152 1 ▶ $_[0]->{allow_suspicious_names} = !! $_[1] if defined $_[1];
Branch: 1 branch
BranchTrueFalseTotal
if defined $_[1] 0 1 1
153  
154 1 $_[0]->{allow_suspicious_names};
155 }
156  
157 =item disallow_alpha_versions( [ARG] )
158  
159 Set or retrieve the value of the disallow_alpha_versions settings. It's
160 a boolean.
161  
162 =cut
163  
164 1499 ▶sub disallow_alpha_versions {
Subroutine disallow_alpha_versions: called CC=2 SCAR=7.2
Pod documented
165 1499 ▶ $_[0]->{disallow_alpha_versions} = !! $_[1] if defined $_[1];
Branch: 1 branch
BranchTrueFalseTotal
if defined $_[1] 0 1499 1499
166  
167 1499 $_[0]->{disallow_alpha_versions};
168 }
169  
170 =item add_entry
171  
172 Add an entry to the collection. Call this on the C<CPAN::PackageDetails>
173 object and it will take care of finding the right handler.
174  
175 If you've set C<allow_packages_only_once> to a true value (which is the
176 default, too), C<add_entry> will die if you try to add another entry with
177 the same package name even if it has a different or greater version. You can
178 set this to a false value and add as many entries as you like then use
179 C<as_unqiue_sorted_list> to get just the entries with the highest
180 versions for each package.
181  
182 =cut
183  
184 1493 ▶sub _parse_version {
Subroutine _parse_version: called CC=7 SCAR=19.7
185 1493 my( $self, $version ) = @_;
186  
187 1493 my $warning;
188 0 ▶ local $SIG{__WARN__} = sub { $warning = join "\n", @_ };
Subroutine __ANON__: not called CC=1 SCAR=1.2
189  
190 1493 my( $parsed, $alpha ) = eval {
191 1493 ▶ die "Version string is undefined\n" unless defined $version;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $version 0 1493 1493
192 1493 ▶ die "Version string is empty\n" if '' eq $version;
Branch: 1 branch
BranchTrueFalseTotal
if "" eq $version 2 1491 1493
193 1491 my $v = version->parse($version);
194 1483 map { $v->$_() } qw( numify is_alpha );
195 };
196 1493 do {
197 15 ▶ no warnings 'uninitialized';
Subroutine BEGIN: called CC=1 SCAR=0.0
198 1493 my $at = $@;
199 1493 chomp, s/\s+at\s+.*// for ( $at, $warning );
200 1493 ▶ $warning = undef if $warning =~ m/numify\(\) is lossy/i;
Branch: 1 branch
BranchTrueFalseTotal
if $warning =~ /numify\(\) is lossy/i 0 1493 1493
201 1493 ▶ if( $at ) { ( 0, $alpha, $at ) }
Branch: 2 branches
BranchTrueFalseTotal
if ($at) { } 10 1483 1493
elsif (defined $warning) { } 0 1483 1483
202 0 elsif( defined $warning ) { ( $parsed, $alpha, $warning ) }
203 1483 else { ( $parsed, $alpha, undef ) }
204 };
205 }
206  
207 1482 ▶sub add_entry {
Subroutine add_entry: called CC=11 SCAR=24.4
Pod documented
208 1482 my( $self, %args ) = @_;
209  
210 1482 $self->_mark_as_dirty;
211  
212 # The column name has a space in it, but that looks weird in a
213 # hash constructor and I keep doing it wrong. If I type "package_name"
214 # I'll just make it work.
215 1482 ▶ if( exists $args{package_name} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $args{"package_name"}) 18 1464 1482
216 18 $args{'package name'} = $args{package_name};
217 18 delete $args{package_name};
218 }
219  
220 1482 my( $parsed, $alpha, $warning ) = $self->_parse_version( $args{'version'} );
221  
222 1482 ▶ if( defined $warning and warnings::enabled() ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $warning and warnings::enabled()) 5 1477 1482
Condition66%2/3
defined $warning and warnings::enabled()
!ll&&!rl&&r
147705
MC/DC50%1/2
defined $warning and warnings::enabled()
defined $warning warnings::enabled()
Truth table66%2/3
A and B
A= defined $warningB= warnings::enabled()
ABresult
0X0
100
111
223 5 $warning = "add_entry has a problem parsing [$args{'version'}] for package [$args{'package name'}]: [$warning] I'm using [$parsed] as the version for [$args{'package name'}].\n";
224 5 warnings::warn( $warning );
225 }
226  
227 1482 ▶ if( $self->disallow_alpha_versions && $alpha ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->disallow_alpha_versions and $alpha) 3 1479 1482
Condition100%3/3
$self->disallow_alpha_versions and $alpha
!ll&&!rl&&r
147633
MC/DC100%2/2
$self->disallow_alpha_versions and $alpha
$self->disallow_alpha_versions $alpha
Truth table100%3/3
A and B
A= $self->disallow_alpha_versionsB= $alpha
ABresult
0X0
100
111
228 3 croak "add_entry interprets [$parsed] as an alpha version, and disallow_alpha_versions is on";
229 }
230  
231 1479 ▶ unless( defined $args{'package name'} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $args{"package name"}) 0 1479 1479
232 0 croak "No 'package name' parameter!";
233 0 return;
234 }
235  
236 1479 ▶ unless( $args{'package name'} =~ m/
Branch: 1 branch
BranchTrueFalseTotal
unless ($args{"package name"} =~ / ^ [A-Za-z0-9_]+ (?: (?:\::|') [A-Za-z0-9_]+ )* \z /x or $self->allow_suspicious_names) 1 1478 1479
Condition66%2/3
$args{"package name"} =~ / ^ [A-Za-z0-9_]+ (?: (?:\::|') [A-Za-z0-9_]+ )* \z /x or $self->allow_suspicious_names
l!l&&r!l&&!r
147801
MC/DC50%1/2
$args{"package name"} =~ / ^ [A-Za-z0-9_]+ (?: (?:\::|') [A-Za-z0-9_]+ )* \z /x or $self->allow_suspicious_names
$args{"package name"} =~ / ^ [A-Za-z0-9_]+ (?: (?:\::|') [A-Za-z0-9_]+ )* \z /x $self->allow_suspicious_names
Truth table66%2/3
A or B
A= $args{"package name"} =~ / ^ [A-Za-z0-9_]+ (?: (?:\::|') [A-Za-z0-9_]+ )* \z /xB= $self->allow_suspicious_names
ABresult
1X1
011
000
237 ^
238 [A-Za-z0-9_]+
239 (?:
240 (?:\::|')
241 [A-Za-z0-9_]+
242 )*
243 \z
244 /x || $self->allow_suspicious_names ) {
245 1 croak "Package name [$args{'package name'}] looks suspicious. Not adding it!";
246 0 return;
247 }
248  
249 1478 ▶ if( $self->allow_packages_only_once and $self->already_added( $args{'package name'} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->allow_packages_only_once and $self->already_added($args{"package name"})) 1 1477 1478
Condition100%3/3
$self->allow_packages_only_once and $self->already_added($args{"package name"})
!ll&&!rl&&r
2414531
MC/DC100%2/2
$self->allow_packages_only_once and $self->already_added($args{"package name"})
$self->allow_packages_only_once $self->already_added($args{"package name"})
Truth table100%3/3
A and B
A= $self->allow_packages_only_onceB= $self->already_added($args{"package name"})
ABresult
0X0
100
111
250 1 croak "$args{'package name'} was already added to CPAN::PackageDetails!";
251 0 return;
252 }
253  
254 # should check for allowed columns here
255 # XXX: this part needs to change based on storage
256 $self->{entries}{
257 $args{'package name'}
258 1477 }{$args{'version'}
259 } = $self->entry_class->new( %args );
260  
261 1477 return 1;
262 }
263  
264 1482 ▶sub _mark_as_dirty {
Subroutine _mark_as_dirty: called CC=1 SCAR=0.0
265 1482 delete $_[0]->{sorted};
266 }
267  
268 =item already_added( PACKAGE )
269  
270 Returns true if there is already an entry for PACKAGE.
271  
272 =cut
273  
274 # XXX: this part needs to change based on storage
275 1460 ▶sub already_added { exists $_[0]->{entries}{$_[1]} }
Subroutine already_added: called CC=1 SCAR=0.0
Pod documented
276  
277 =item as_string
278  
279 Returns a text version of the Entries object. This calls C<as_string>
280 on each Entry object, and concatenates the results for all Entry objects.
281  
282 =cut
283  
284 3 ▶sub as_string {
Subroutine as_string: called CC=3 SCAR=11.0
Pod documented
285 3 my( $self ) = @_;
286  
287 3 my $string;
288  
289 3 my( $return ) = $self->as_unique_sorted_list;
290  
291 3 foreach my $entry ( @$return ) {
292 6 $string .= $entry->as_string( $self->columns );
293 }
294  
295 3 ▶ $string || '';
Branch: 1 branch
BranchTrueFalseTotal
$string or "" 2 1 3
296 }
297  
298 =item as_unique_sorted_list
299  
300 In list context, this returns a list of entries sorted by package name
301 and version. Each package exists exactly once in the list and with the
302 largest version number seen.
303  
304 In scalar context this returns the count of the number of unique entries.
305  
306 Once called, it caches its result until you add more entries.
307  
308 =cut
309  
310 sub VERSION_PM () { 9 }
311 16 ▶sub as_unique_sorted_list {
Subroutine as_unique_sorted_list: called CC=11 SCAR=25.5
Pod documented
312 16 my( $self ) = @_;
313  
314 16 ▶ unless( ref $self->{sorted} eq ref [] ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $self->{"sorted"} eq ref []) 7 9 16
315 7 $self->{sorted} = [];
316  
317 7 my %Seen;
318  
319 7 my( $k1, $k2 ) = ( $self->columns )[0,1];
320  
321 7 my $e = $self->get_hash;
322  
323 # We only want the latest versions of everything:
324 7 foreach my $package ( sort keys %$e ) {
325 13 my $entries = $e->{$package};
326 eval {
327 13 ▶ eval { require version } or die "Could not load version.pm!";
Branch: 1 branch
BranchTrueFalseTotal
eval { do { require version } } or die "Could not load version.pm!" 13 0 13
328 13 ▶ die "Your version of the version module doesn't handle the parse method!"
Branch: 1 branch
BranchTrueFalseTotal
unless "version"->can("parse") 0 13 13
329 unless version->can('parse');
330 } or croak( {
331 message => $@,
332 0 ▶ have_version => eval { version->VERSION },
Branch: 1 branch
BranchTrueFalseTotal
eval { do { eval { do { require version } } or die "Could not load version.pm!"; die "Your version of the version module doesn't handle the parse method!" unless "version"->can("parse") } } or croak({"message", $@, "have_version", eval { do { "version"->VERSION } }, "need_version", "0.74", "inc", [@INC], "error", 9}) 13 0 13
333 need_version => 0.74,
334 inc => [ @INC ],
335 error => VERSION_PM,
336 }
337 );
338  
339 my( $highest_version ) =
340 16 map { $_->[0] }
341 3 sort { $b->[1] <=> $a->[1] } # sort on version objects
342 map {
343 13 my $w;
344 0 ▶ local $SIG{__WARN__} = sub { $w = join "\n", @_ };
Subroutine __ANON__: not called CC=1 SCAR=1.2
345 16 my $v = eval { version->new( $_ ) };
346 16 ▶ $w = $w || $@;
Condition33%1/3
$w || $@
l!l&&r!l&&!r
0016
MC/DC0%0/2
$w || $@
$w $@
Truth table33%1/3
A || B
A= $wB= $@
ABresult
1X1
011
000
347 16 $w = s/\s+at\s+//;
348 16 ▶ carp "Version [$_] for package [$package] parses with a warning: [$w]. Using [$v] as the version."
Branch: 1 branch
BranchTrueFalseTotal
if $w 0 16 16
349 if $w;
350 16 ▶ if( $self->disallow_alpha_versions and $v->is_alpha ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->disallow_alpha_versions and $v->is_alpha) { } 0 16 16
Condition33%1/3
$self->disallow_alpha_versions and $v->is_alpha
!ll&&!rl&&r
1600
MC/DC0%0/2
$self->disallow_alpha_versions and $v->is_alpha
$self->disallow_alpha_versions $v->is_alpha
Truth table33%1/3
A and B
A= $self->disallow_alpha_versionsB= $v->is_alpha
ABresult
0X0
100
111
351 0 carp "Skipping alpha version [$v] for [$package] while sorting versions.";
352 ()
353 0 }
354 16 else { [ $_, $v ] }
355 }
356 keys %$entries;
357  
358 13 push @{ $self->{sorted} }, $entries->{$highest_version};
359 }
360 }
361  
362 my $return = wantarray ?
363 $self->{sorted}
364 :
365 16 ▶ scalar @{ $self->{sorted} };
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 12 4 16
366  
367 16 return $return;
368 }
369  
370 =item get_entries_by_distribution( DISTRIBUTION )
371  
372 Returns the entry objects for the named DISTRIBUTION.
373  
374 =cut
375  
376 4 ▶sub get_entries_by_distribution {
Subroutine get_entries_by_distribution: called CC=3 SCAR=11.1
Pod documented
377 4 require CPAN::DistnameInfo;
378 4 my( $self, $distribution ) = @_;
379 4 ▶ croak "You must specify a distribution!" unless defined $distribution;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $distribution 0 4 4
380  
381 my @entries =
382 grep { # $_ is the entry hash
383 28 my $info = CPAN::DistnameInfo->new( $_->{'path'} );
384 28 ▶ defined $info->dist && $info->dist eq $distribution;
Condition66%2/3
defined $info->dist && $info->dist eq $distribution
!ll&&!rl&&r
0217
MC/DC50%1/2
defined $info->dist && $info->dist eq $distribution
defined $info->dist $info->dist eq $distribution
Truth table66%2/3
A && B
A= defined $info->distB= $info->dist eq $distribution
ABresult
0X0
100
111
385 }
386 map { # $_ is the package name
387 16 values %{ $self->{entries}{$_} }
388 }
389 4 keys %{ $self->{entries} };
390 }
391  
392 =item get_entries_by_package( PACKAGE )
393  
394 Returns the entry objects for the named PACKAGE.
395  
396 =cut
397  
398 3 ▶sub get_entries_by_package {
Subroutine get_entries_by_package: called CC=1 SCAR=0.0
Pod documented
399 3 my( $self, $package ) = @_;
400  
401 my @entries =
402 3 map { values %{$self->{entries}{$package}} }
403 12 grep { $_ eq $package }
404 3 keys %{ $self->{entries} };
405 }
406  
407 =item get_entries_by_path( PATH )
408  
409 Returns the entry objects for any entries with PATH.
410  
411 =cut
412  
413 0 ▶sub get_entries_by_path {
Subroutine get_entries_by_path: not called CC=1 SCAR=6.9
Pod documented
414 0 my( $self, $path ) = @_;
415  
416 my @entries =
417 0 map { $self->{entries}{$_}{$path} }
418 0 grep { exists $self->{entries}{$_}{$path} }
419 0 keys %{ $self->{entries} };
420 }
421  
422 =item get_entries_by_version( VERSION )
423  
424 Returns the entry objects for any entries with VERSION.
425  
426 =cut
427  
428 0 ▶sub get_entries_by_version {
Subroutine get_entries_by_version: not called CC=1 SCAR=6.9
Pod documented
429 0 my( $self, $version ) = @_;
430  
431 my @entries =
432 0 map { $self->{entries}{$_}{$version} }
433 0 grep { exists $self->{entries}{$_}{$version} }
434 0 keys %{ $self->{entries} };
435 }
436  
437 =back
438  
439 =head1 TO DO
440  
441 =head1 SEE ALSO
442  
443  
444 =head1 SOURCE AVAILABILITY
445  
446 This source is in Github:
447  
448 https://github.com/briandfoy/cpan-packagedetails
449  
450 =head1 AUTHOR
451  
452 brian d foy, C<< <briandfoy@pobox.com> >>
453  
454 =head1 COPYRIGHT AND LICENSE
455  
456 Copyright © 2009-2025, brian d foy <briandfoy@pobox.com>. All rights reserved.
457  
458 You may redistribute this under the terms of the Artistic License 2.0.
459  
460 =cut
461  
462 15 1;
463  
« PackageDetails/Redis.pm ↑ Summary PackageDetails/Header.pm »