blib/lib/App/CPAN/Dependents.pm

filter: Statementstmt 25.0% 18 / 72 Branchbran 0.0% 0 / 28 Conditioncond 0.0% 0 / 15 MC/DCmcdc 0.0% 0 / 8 Subroutinesub 50.0% 5 / 10 Podpod 100% 1 / 1 totaltotal 17.9% 24 / 134 CC 23 SCAR 39.7
CC
23
Cov
16%
_find_dependents
110.0
_module_dependents
56.0
find_all_dependents
30.0
CRAP 53.0
↑ Summary
1 package App::CPAN::Dependents;
2  
3 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 3 ▶use Carp 'croak';
Subroutine BEGIN: called CC=1 SCAR=0.0
6 3 ▶use Exporter 'import';
Subroutine BEGIN: called CC=1 SCAR=0.0
7 3 ▶use MetaCPAN::Client;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 3 our $VERSION = '1.002';
10  
11 3 our @EXPORT_OK = ('find_all_dependents');
12  
13 0 ▶sub find_all_dependents {
Subroutine find_all_dependents: not called CC=5 SCAR=34.0
Pod documented
14 0 my %options = @_;
15 0 my $mcpan = delete $options{mcpan};
16 0 ▶ unless (defined $mcpan) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $mcpan) 0 0 0
17 0 my $http = delete $options{http};
18 0 my %mcpan_params;
19 0 ▶ $mcpan_params{ua} = $http if defined $http;
Branch: 1 branch
BranchTrueFalseTotal
if defined $http 0 0 0
20 0 $mcpan = MetaCPAN::Client->new(%mcpan_params);
21 }
22 0 my $module = delete $options{module};
23 0 my $dist = delete $options{dist};
24 0 my %dependent_dists;
25 0 ▶ if (defined $dist) {
Branch: 2 branches
BranchTrueFalseTotal
if (defined $dist) { } 0 0 0
elsif (defined $module) { } 0 0 0
26 0 my $modules = _dist_modules($mcpan, $dist);
27 0 _find_dependents($mcpan, $modules, \%dependent_dists, \%options);
28 } elsif (defined $module) {
29 0 my $dist = _module_dist($mcpan, $module); # check if module is valid
30 0 _find_dependents($mcpan, [$module], \%dependent_dists, \%options);
31 } else {
32 0 croak 'No module or distribution defined';
33 }
34 0 return [sort keys %dependent_dists];
35 }
36  
37 0 ▶sub _find_dependents {
Subroutine _find_dependents: not called CC=10 SCAR=47.0
38 0 my ($mcpan, $modules, $dependent_dists, $options) = @_;
39 0 ▶ $dependent_dists //= {};
Condition0%0/2
$dependent_dists //= {}
l!l
00
MC/DC0%0/1
$dependent_dists //= {}
$dependent_dists
Truth table0%0/2
A
A= $dependent_dists
Aresult
11
00
40 0 ▶ $options //= {};
Condition0%0/2
$options //= {}
l!l
00
MC/DC0%0/1
$options //= {}
$options
Truth table0%0/2
A
A= $options
Aresult
11
00
41 0 my $dists = _module_dependents($mcpan, $modules, $options);
42 0 ▶ if ($options->{debug} and @$dists) {
Branch: 1 branch
BranchTrueFalseTotal
if ($options->{"debug"} and @$dists) 0 0 0
Condition0%0/3
$options->{"debug"} and @$dists
!ll&&!rl&&r
000
MC/DC0%0/2
$options->{"debug"} and @$dists
$options->{"debug"} @$dists
Truth table0%0/3
A and B
A= $options->{"debug"}B= @$dists
ABresult
0X0
100
111
43 0 my @names = map { $_->{name} } @$dists;
44 0 warn "Found dependent distributions: @names\n";
45 }
46 0 foreach my $dist (@$dists) {
47 0 my $name = $dist->{name};
48 0 ▶ next if exists $dependent_dists->{$name};
Branch: 1 branch
BranchTrueFalseTotal
if exists $dependent_dists->{$name} 0 0 0
49 0 $dependent_dists->{$name} = 1;
50 0 my $modules = $dist->{provides};
51 warn @$modules ? "Modules provided by $name: @$modules\n"
52 0 ▶ : "No modules provided by $name\n" if $options->{debug};
Branch: 2 branches
BranchTrueFalseTotal
if $options->{"debug"} 0 0 0
@$modules ? : 0 0 0
53 0 ▶ _find_dependents($mcpan, $modules, $dependent_dists, $options) if @$modules;
Branch: 1 branch
BranchTrueFalseTotal
if @$modules 0 0 0
54 }
55 0 return $dependent_dists;
56 }
57  
58 0 ▶sub _module_dependents {
Subroutine _module_dependents: not called CC=7 SCAR=40.3
59 0 my ($mcpan, $modules, $options) = @_;
60
61 0 my @relationships = ('requires');
62 0 ▶ push @relationships, 'recommends' if $options->{recommends};
Branch: 1 branch
BranchTrueFalseTotal
if $options->{"recommends"} 0 0 0
63 0 ▶ push @relationships, 'suggests' if $options->{suggests};
Branch: 1 branch
BranchTrueFalseTotal
if $options->{"suggests"} 0 0 0
64 0 my %dep_filters = (filter => [
65 { terms => { 'dependency.module' => $modules } },
66 { terms => { 'dependency.relationship' => \@relationships } },
67 ]);
68 $dep_filters{must_not} = { term => { 'dependency.phase' => 'develop' } }
69 0 ▶ unless $options->{develop};
Branch: 1 branch
BranchTrueFalseTotal
unless $options->{"develop"} 0 0 0
70
71 0 my %filter = (
72 bool => {filter => [
73 { term => { maturity => 'released' } },
74 { term => { status => 'latest' } },
75 { nested => {
76 path => 'dependency',
77 query => { bool => \%dep_filters },
78 } },
79 ]},
80 );
81
82 0 my $response = $mcpan->all('releases', {
83 fields => [ 'distribution', 'provides' ],
84 es_filter => \%filter,
85 });
86
87 0 my @results;
88 0 ▶ while (my $hit = $response->next) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $hit = $response->next) 0 0 0
89 0 my $name = $hit->distribution;
90 0 ▶ my $provides = $hit->provides // [];
Condition0%0/2
$hit->provides // []
l!l
00
MC/DC0%0/1
$hit->provides // []
$hit->provides
Truth table0%0/2
A
A= $hit->provides
Aresult
11
00
91 0 ▶ $provides = [$provides] unless ref $provides;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $provides 0 0 0
92 0 push @results, { name => $name, provides => $provides };
93 }
94 0 return \@results;
95 }
96  
97 0 ▶sub _dist_modules {
Subroutine _dist_modules: not called CC=3 SCAR=24.8
98 0 my ($mcpan, $dist) = @_;
99 0 ▶ my $response = $mcpan->release($dist) // return [];
Condition0%0/2
$mcpan->release($dist) // (return [])
l!l
00
MC/DC0%0/1
$mcpan->release($dist) // (return [])
$mcpan->release($dist)
Truth table0%0/2
A
A= $mcpan->release($dist)
Aresult
11
00
100 0 ▶ return $response->provides // [];
Condition0%0/2
$response->provides // []
l!l
00
MC/DC0%0/1
$response->provides // []
$response->provides
Truth table0%0/2
A
A= $response->provides
Aresult
11
00
101 }
102  
103 0 ▶sub _module_dist {
Subroutine _module_dist: not called CC=2 SCAR=17.9
104 0 my ($mcpan, $module) = @_;
105 0 ▶ my $response = $mcpan->module($module) // return undef;
Condition0%0/2
$mcpan->module($module) // (return undef)
l!l
00
MC/DC0%0/1
$mcpan->module($module) // (return undef)
$mcpan->module($module)
Truth table0%0/2
A
A= $mcpan->module($module)
Aresult
11
00
106 0 return $response->distribution;
107 }
108  
109 3 1;
110  
111 =head1 NAME
112  
113 App::CPAN::Dependents - Recursively find all reverse dependencies for a
114 distribution or module
115  
116 =head1 SYNOPSIS
117  
118 use App::CPAN::Dependents 'find_all_dependents';
119 my $dependents = find_all_dependents(module => 'JSON::Tiny'); # or dist => 'JSON-Tiny'
120 print "Distributions dependent on JSON::Tiny: @$dependents\n";
121
122 # From the commandline
123 $ cpan-dependents --with-recommends JSON::Tiny
124 $ cpan-dependents -c JSON-Tiny
125  
126 =head1 DESCRIPTION
127  
128 L<App::CPAN::Dependents> provides the function L</"find_all_dependents">
129 (exportable on demand) for the purpose of determining all distributions which
130 are dependent on a particular CPAN distribution or module.
131  
132 This module uses the MetaCPAN API, and must perform several requests
133 recursively, so it may take a long time (sometimes minutes) to complete. If the
134 function encounters HTTP errors (including when querying a nonexistent module
135 or distribution) or is unable to connect, it will die.
136  
137 This module will only find distributions that explicitly list prerequisites in
138 metadata; C<dynamic_config> will not be used. Also, it assumes the MetaCPAN API
139 will correctly extract the provided modules for distributions, so any unindexed
140 or unauthorized modules will be ignored.
141  
142 See L<cpan-dependents> for command-line usage.
143  
144 =head1 FUNCTIONS
145  
146 =head2 find_all_dependents
147  
148 my $dependents = find_all_dependents(module => 'JSON::Tiny', recommends => 1);
149  
150 Find all dependent distributions. Returns an array reference of distribution
151 names. The following parameters are accepted:
152  
153 =over
154  
155 =item module
156  
157 The module name to find dependents for. Mutually exclusive with C<dist>.
158  
159 =item dist
160  
161 The distribution to find dependents for. Mutually exclusive with C<module>.
162  
163 =item http
164  
165 Optional L<HTTP::Tiny> object to use for building the default
166 L<MetaCPAN::Client> object.
167  
168 =item mcpan
169  
170 Optional L<MetaCPAN::Client> object to use for querying MetaCPAN. If not
171 specified, a default L<MetaCPAN::Client> object will be created using
172 L</"http"> if specified.
173  
174 =item recommends
175  
176 Boolean value, if true then C<recommends> prerequisites will be considered in
177 the results. Defaults to false.
178  
179 =item suggests
180  
181 Boolean value, if true then C<suggests> prerequisites will be considered in the
182 results. Defaults to false.
183  
184 =item develop
185  
186 Boolean value, if true then C<develop> phase prerequisites will be considered
187 in the results. Defaults to false.
188  
189 =item debug
190  
191 Boolean value, if true then debugging information will be printed to STDERR as
192 it is retrieved.
193  
194 =back
195  
196 =head1 AUTHOR
197  
198 Dan Book, C<dbook@cpan.org>
199  
200 =head1 COPYRIGHT AND LICENSE
201  
202 Copyright 2015, Dan Book.
203  
204 This library is free software; you may redistribute it and/or modify it under
205 the terms of the Artistic License version 2.0.
206  
207 =head1 SEE ALSO
208  
209 L<cpan-dependents>, L<Test::DependentModules>, L<MetaCPAN::Client>,
210 L<CPAN::Meta::Spec>
↑ Summary