blib/lib/Test/CPAN/Health/Check/ReverseDeps.pm

filter: Statementstmt 100% 9 / 9 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 3 / 3 Podpod n/a totaltotal 100% 12 / 12 CC 1 SCAR 0.0
CC
1
Cov
100%
BEGIN
1.0
BEGIN
1.0
BEGIN
1.0
CRAP 1.0
« blib/lib/Test/CPAN/Health/Check/ReadmeSync.pm ↑ Summary blib/lib/Test/CPAN/Health/Check/SecurityAdvisories.pm »
1 package Test::CPAN::Health::Check::ReverseDeps;
2  
3 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 2 ▶use autodie qw(:all);
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 use Carp qw(croak carp);
8 use HTTP::Tiny (); # load at compile time so mock_scoped works in tests
9 use JSON::MaybeXS (); # same reason -- lazy require inside _http_get is overwritten
10 use Readonly;
11 use Params::Validate::Strict qw(validate_strict);
12  
13 use parent 'Test::CPAN::Health::Check';
14  
15 our $VERSION = '0.1.0';
16  
17 Readonly::Scalar my $METACPAN_API => 'https://fastapi.metacpan.org/v1';
18 Readonly::Scalar my $HTTP_TIMEOUT => 30;
19  
20 # Score brackets based on reverse-dependency count.
21 # Having zero revdeps is unusual but not a defect; score is warn, not fail.
22 Readonly::Scalar my $SCORE_NONE => 50; # 0 reverse deps
23 Readonly::Scalar my $SCORE_FEW => 75; # 1-9
24 Readonly::Scalar my $SCORE_SOME => 90; # 10-99
25 Readonly::Scalar my $SCORE_MANY => 100; # 100+
26  
27 Readonly::Scalar my $THRESH_FEW => 1;
28 Readonly::Scalar my $THRESH_SOME => 10;
29 Readonly::Scalar my $THRESH_MANY => 100;
30  
31 =head1 NAME
32  
33 Test::CPAN::Health::Check::ReverseDeps - Count how many CPAN distributions depend on this one
34  
35 =head1 SYNOPSIS
36  
37 use Test::CPAN::Health::Check::ReverseDeps;
38  
39 my $check = Test::CPAN::Health::Check::ReverseDeps->new;
40 my $result = $check->run($dist);
41  
42 =head1 DESCRIPTION
43  
44 Queries the MetaCPAN API to count how many other CPAN distributions declare a
45 dependency on this distribution. A higher reverse-dependency count is a
46 positive quality signal: widely-depended-upon code tends to be well-maintained.
47  
48 The count is stored in C<data-E<gt>{count}> and is read from the runner context
49 by the C<SecurityAdvisories> check to scale the urgency of its messages.
50  
51 Score brackets: 0 E<rarr> 50 (warn), 1-9 E<rarr> 75 (pass), 10-99 E<rarr> 90
52 (pass), 100+ E<rarr> 100 (pass).
53  
54 =head1 LIMITATIONS
55  
56 =over 4
57  
58 =item * Results are accurate only for distributions that have been indexed on
59 MetaCPAN. A purely local path will usually still match by dist name from the
60 META file.
61  
62 =item * The count reflects CPAN metadata at query time and is cached for 24 h
63 by the Runner's C<Test::CPAN::Health::Cache>.
64  
65 =back
66  
67 =cut
68  
69 sub id { return 'reverse_deps' }
70 sub name { return 'Reverse Dependencies' }
71 sub description { return 'Reports how many CPAN distributions depend on this one' }
72 sub weight { return 2 }
73 sub category { return 'quality' }
74  
75 =head2 run
76  
77 =head3 PURPOSE
78  
79 Query MetaCPAN for the distribution's reverse-dependency count and return a
80 scored result. The count is stored in C<data-E<gt>{count}>.
81  
82 =head3 API SPECIFICATION
83  
84 =head4 INPUT
85  
86 dist Test::CPAN::Health::Distribution required
87 context Hashref optional
88  
89 =head4 OUTPUT
90  
91 L<Test::CPAN::Health::Result> with check_id C<'reverse_deps'>.
92  
93 =head3 MESSAGES
94  
95 Code | Severity | Message | Resolution
96 ------+----------+---------------------------------------+-----------
97 RD001 | SKIP | Network checks disabled | Remove --no-network
98 RD002 | SKIP | Distribution name not available | Add META.yml with name field
99 RD003 | ERROR | MetaCPAN API error: ... | Transient; retry
100 RD004 | WARN | 0 reverse dependencies found | Publish / promote the dist
101 RD005 | PASS | N reverse dependencies found |
102  
103 =head3 FORMAL SPECIFICATION
104  
105 -- Z schema (placeholder) --
106 ReverseDepsOp
107 count : N
108 score : {50, 75, 90, 100}
109 -------------------------------------------------------
110 count = 0 => score = 50 /\ status = warn
111 1 <= count < 10 => score = 75 /\ status = pass
112 10 <= count < 100 => score = 90 /\ status = pass
113 count >= 100 => score = 100 /\ status = pass
114  
115 =head3 SIDE EFFECTS
116  
117 Makes one HTTPS GET request to C<fastapi.metacpan.org>.
118  
119 =head3 USAGE EXAMPLE
120  
121 my $result = Test::CPAN::Health::Check::ReverseDeps->new->run($dist);
122 printf "Reverse deps: %d\n", $result->data->{count};
123  
124 =cut
125  
126 sub run {
127 my ($self, $dist, $context) = @_;
128  
129 croak 'dist must be a Test::CPAN::Health::Distribution'
130 unless ref($dist) && $dist->isa('Test::CPAN::Health::Distribution');
131  
132 return $self->_skip('Network checks disabled (--no-network)')
133 if $self->no_network;
134  
135 my $dist_name = $dist->name;
136 return $self->_skip('Distribution name not available')
137 unless defined $dist_name && length $dist_name;
138  
139 # MetaCPAN uses dash-separated dist names, not double-colon
140 (my $dist_slug = $dist_name) =~ s/ :: /-/gx;
141  
142 my ($data, $err) = _http_get(
143 "$METACPAN_API/reverse_dependencies/dist/$dist_slug?size=0",
144 );
145 return $self->_error("MetaCPAN API error: $err") if $err;
146  
147 # MetaCPAN may return:
148 # {total: N} -- legacy top-level form
149 # {hits: {total: {value: N}}} -- Elasticsearch 7+ wrapped form
150 # {hits: {total: N}} -- Elasticsearch 6 plain integer form
151 # Guard the {value} deref so a plain integer does not crash under strict.
152 my $hits_total = $data->{hits}{total};
153 my $count = $data->{total}
154 // ( ref($hits_total) eq 'HASH' ? $hits_total->{value} : $hits_total )
155 // 0;
156  
157 my ($score, $status);
158 if ($count >= $THRESH_MANY) {
159 ($score, $status) = ($SCORE_MANY, 'pass');
160 } elsif ($count >= $THRESH_SOME) {
161 ($score, $status) = ($SCORE_SOME, 'pass');
162 } elsif ($count >= $THRESH_FEW) {
163 ($score, $status) = ($SCORE_FEW, 'pass');
164 } else {
165 ($score, $status) = ($SCORE_NONE, 'warn');
166 }
167  
168 my $noun = $count == 1 ? 'reverse dependency' : 'reverse dependencies';
169  
170 return $self->_result(
171 status => $status,
172 score => $score,
173 summary => "$count $noun found on CPAN",
174 data => {
175 name => $self->name,
176 count => $count,
177 },
178 );
179 }
180  
181 # ---------------------------------------------------------------------------
182 # Private helpers
183 # ---------------------------------------------------------------------------
184  
185 sub _http_get {
186 my ($url) = @_;
187  
188 # HTTP::Tiny and JSON::MaybeXS are loaded at compile time (use at top of
189 # file) so that test mocks installed via mock_scoped are not overwritten
190 # when a lazy require fires inside this helper on first invocation.
191 my $ua = HTTP::Tiny->new(timeout => $HTTP_TIMEOUT);
192 my $res = $ua->get($url, { headers => { 'Accept' => 'application/json' } });
193  
194 return (undef, "HTTP $res->{status} $res->{reason}") unless $res->{success};
195  
196 my $data = eval { JSON::MaybeXS::decode_json($res->{content}) };
197 return (undef, "JSON parse error: $@") if $@;
198  
199 return ($data, undef);
200 }
201  
202 =head1 AUTHOR
203  
204 Nigel Horne, C<< <njh at nigelhorne.com> >>
205  
206 =head1 LICENSE AND COPYRIGHT
207  
208 Copyright (C) 2025-2026 Nigel Horne.
209  
210 This program is free software; you can redistribute it and/or modify it
211 under the terms of the GNU General Public License as published by the Free
212 Software Foundation; either version 2 of the License, or (at your option)
213 any later version.
214  
215 =cut
216  
217 1;
« blib/lib/Test/CPAN/Health/Check/ReadmeSync.pm ↑ Summary blib/lib/Test/CPAN/Health/Check/SecurityAdvisories.pm »