App/lcpan/Cmd/deps_by_dependent_count.pm

filter: Statementstmt 26.0% 19 / 73 Branchbran 0.0% 0 / 18 Conditioncond 0.0% 0 / 19 MC/DCmcdc 0.0% 0 / 11 Subroutinesub 80.0% 4 / 5 Podpod 100% 1 / 1 totaltotal 18.9% 24 / 127 CC 19 SCAR 57.5
CC
19
Cov
17%
handle_cmd
380.0
BEGIN
1.0
BEGIN
1.0
CRAP 314.1
« App/lcpan/Cmd/scripts.pm ↑ Summary LWP/UserAgent/Plugin/FilterLcpan.pm »
1 package App::lcpan::Cmd::deps_by_dependent_count;
2  
3 2 ▶use 5.010;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 2 ▶use Log::ger;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 2 require App::lcpan;
9  
10 2 our $AUTHORITY = 'cpan:PERLANCAR'; # AUTHORITY
11 2 our $DATE = '2023-09-26'; # DATE
12 2 our $DIST = 'App-lcpan'; # DIST
13 2 our $VERSION = '1.074'; # VERSION
14  
15 2 our %SPEC;
16  
17 2 $SPEC{'handle_cmd'} = {
18 v => 1.1,
19 summary => 'List dependencies, sorted by number of dependents',
20 description => <<'_',
21  
22 This subcommand is like `deps`, except that this subcommand does not support
23 recursing and it sorts the result by number of dependent dists. For example,
24 Suppose that dist `Foo` depends on `Mod1` and `Mod2`, `Bar` depends on `Mod2`
25 and `Mod3`, and `Baz` depends on `Mod2` and `Mod3`, then `lcpan
26 deps-by-dependent-count Foo Bar Baz` will return `Mod2` (3 dependents), `Mod3`
27 (2 dependents), `Mod1` (1 dependent).
28  
29 _
30 args => {
31 %App::lcpan::common_args,
32 %App::lcpan::mods_args,
33  
34 %App::lcpan::deps_phase_args,
35 %App::lcpan::deps_rel_args,
36 %App::lcpan::finclude_core_args,
37 %App::lcpan::finclude_noncore_args,
38 %App::lcpan::perl_version_args,
39 # XXX with_xs_or_pp
40 authors => {
41 'x.name.is_plural' => 1,
42 'x.name.singular' => 'author',
43 schema => ['array*', of=>'str*', min_len=>1],
44 tags => ['category:filtering'],
45 element_completion => \&App::lcpan::_complete_cpanid,
46 },
47 authors_arent => {
48 'x.name.is_plural' => 1,
49 'x.name.singular' => 'author_isnt',
50 schema => ['array*', of=>'str*', min_len=>1],
51 tags => ['category:filtering'],
52 element_completion => \&App::lcpan::_complete_cpanid,
53 },
54 },
55 };
56 0 ▶sub handle_cmd {
Subroutine handle_cmd: not called CC=19 SCAR=59.4
Pod documented
57 0 require Module::CoreList::More;
58  
59 0 my %args = @_;
60  
61 0 my $state = App::lcpan::_init(\%args, 'ro');
62 0 my $dbh = $state->{dbh};
63  
64 0 ▶ my $phase = $args{phase} // 'runtime';
Condition0%0/2
$args{"phase"} // "runtime"
l!l
00
MC/DC0%0/1
$args{"phase"} // "runtime"
$args{"phase"}
Truth table0%0/2
A
A= $args{"phase"}
Aresult
11
00
65 0 ▶ my $rel = $args{rel} // 'requires';
Condition0%0/2
$args{"rel"} // "requires"
l!l
00
MC/DC0%0/1
$args{"rel"} // "requires"
$args{"rel"}
Truth table0%0/2
A
A= $args{"rel"}
Aresult
11
00
66 0 ▶ my $include_core = $args{include_core} // 1;
Condition0%0/2
$args{"include_core"} // 1
l!l
00
MC/DC0%0/1
$args{"include_core"} // 1
$args{"include_core"}
Truth table0%0/2
A
A= $args{"include_core"}
Aresult
11
00
67 0 ▶ my $include_noncore = $args{include_noncore} // 1;
Condition0%0/2
$args{"include_noncore"} // 1
l!l
00
MC/DC0%0/1
$args{"include_noncore"} // 1
$args{"include_noncore"}
Truth table0%0/2
A
A= $args{"include_noncore"}
Aresult
11
00
68 0 ▶ my $plver = $args{perl_version} // "$^V";
Condition0%0/2
$args{"perl_version"} // "$^V"
l!l
00
MC/DC0%0/1
$args{"perl_version"} // "$^V"
$args{"perl_version"}
Truth table0%0/2
A
A= $args{"perl_version"}
Aresult
11
00
69  
70 # first, get the file ID's of the requested modules
71 0 my @file_ids;
72 {
73 0 my $mods_s = join(", ", map {$dbh->quote($_)} @{$args{modules}});
74 0 my $sth = $dbh->prepare("SELECT id FROM file WHERE is_latest_dist AND id IN (SELECT file_id FROM module WHERE name IN ($mods_s))");
75 0 $sth->execute;
76 0 ▶ while (my ($id) = $sth->fetchrow_array) { push @file_ids, $id }
Branch: 1 branch
BranchTrueFalseTotal
if (my($id) = $sth->fetchrow_array) 0 0 0
77 0 ▶ return [404, "No such module(s)"] unless @file_ids;
Branch: 1 branch
BranchTrueFalseTotal
unless @file_ids 0 0 0
78 }
79  
80 0 my @cols = (
81 # name, dbcolname/expr
82 ["module", "m.name"],
83 ["author", "m.cpanid"],
84 ["version", "m.version"],
85 ["is_core", undef],
86 ["dependent_count", "COUNT(*)"],
87 );
88  
89 0 my @wheres = (
90 "m.id IS NOT NULL",
91 "dep.file_id IN (".join(",", @file_ids).")",
92 );
93 0 my @binds;
94  
95 0 ▶ if ($phase ne 'ALL') {
Branch: 1 branch
BranchTrueFalseTotal
if ($phase ne "ALL") 0 0 0
96 0 push @wheres, "dep.phase=?";
97 0 push @binds, $phase;
98 }
99 0 ▶ if ($rel ne 'ALL') {
Branch: 1 branch
BranchTrueFalseTotal
if ($rel ne "ALL") 0 0 0
100 0 push @wheres, "dep.rel=?";
101 0 push @binds, $rel;
102 }
103 0 ▶ if ($args{authors}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($args{"authors"}) 0 0 0
104 0 push @wheres, "(".join(" OR ", map {"author=?"} @{$args{authors}}).")";
105 0 push @binds, map {uc $_} @{ $args{authors} };
106 }
107 0 ▶ if ($args{authors_arent}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($args{"authors_arent"}) 0 0 0
108 0 for (@{ $args{authors_arent} }) {
109 0 push @wheres, "author<>?";
110 0 push @binds, uc $_;
111 }
112 }
113  
114 my $sth = $dbh->prepare("SELECT
115 0 ".join(",\n", map {" $_->[1] AS $_->[0]"} grep {defined $_->[1]} @cols)."
116 FROM dep
117 LEFT JOIN module m ON dep.module_id=m.id
118 WHERE ".join(" AND ", @wheres)."
119 GROUP BY dep.module_id
120 ORDER BY COUNT(*) DESC, m.name
121 ");
122 0 $sth->execute(@binds);
123  
124 0 my @rows;
125 0 ▶ while (my $row = $sth->fetchrow_hashref) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $row = $sth->fetchrow_hashref) 0 0 0
126 $row->{is_core} = $row->{module} eq 'perl' ||
127 Module::CoreList::More->is_still_core(
128 $row->{module}, undef,
129 0 ▶ version->parse($plver)->numify);
Condition0%0/3
$row->{'module'} eq 'perl' || 'Module::CoreList::More'->is_still_core($row->{'module'}, undef, 'version'->parse($plver)->numify)
l!l&&r!l&&!r
000
MC/DC0%0/2
$row->{'module'} eq 'perl' || 'Module::CoreList::More'->is_still_core($row->{'module'}, undef, 'version'->parse($plver)->numify)
$row->{'module'} eq 'perl' 'Module::CoreList::More'->is_still_core($row->{'module'}, undef, 'version'->parse($plver)->numify)
Truth table0%0/3
A || B
A= $row->{'module'} eq 'perl'B= 'Module::CoreList::More'->is_still_core($row->{'module'}, undef, 'version'->parse($plver)->numify)
ABresult
1X1
011
000
130 0 ▶ next if !$include_core && $row->{is_core};
Branch: 1 branch
BranchTrueFalseTotal
if not $include_core and $row->{"is_core"} 0 0 0
Condition0%0/3
not $include_core and $row->{"is_core"}
!ll&&!rl&&r
000
MC/DC0%0/2
not $include_core and $row->{"is_core"}
not $include_core $row->{"is_core"}
Truth table0%0/3
not(A) and B
A= not $include_coreB= $row->{"is_core"}
ABresult
0X0
100
111
131 0 ▶ next if !$include_noncore && !$row->{is_core};
Branch: 1 branch
BranchTrueFalseTotal
unless $include_noncore or $row->{"is_core"} 0 0 0
Condition0%0/3
$include_noncore or $row->{"is_core"}
l!l&&r!l&&!r
000
MC/DC0%0/2
$include_noncore or $row->{"is_core"}
$include_noncore $row->{"is_core"}
Truth table0%0/3
A or B
A= $include_noncoreB= $row->{"is_core"}
ABresult
1X1
011
000
132 0 push @rows, $row;
133 }
134  
135 0 require Data::TableData::Rank;
136 0 Data::TableData::Rank::add_rank_column_to_table(table => \@rows, data_columns => ['dependent_count']);
137  
138 0 [200, "OK", \@rows, {'table.fields'=>['rank', map {$_->[0]} @cols]}];
139 }
140  
141 2 1;
142 # ABSTRACT: List dependencies, sorted by number of dependents
143  
144 __END__
« App/lcpan/Cmd/scripts.pm ↑ Summary LWP/UserAgent/Plugin/FilterLcpan.pm »