« blib/lib/Test/CPAN/Health/Check/MinPerl.pm
↑ Summary
blib/lib/Test/CPAN/Health/Check/Perlcritic.pm »
| 1 | package Test::CPAN::Health::Check::PODCoverage; | ||
| 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 File::Spec; | ||
| 9 | use Readonly; | ||
| 10 | use Params::Validate::Strict qw(validate_strict); | ||
| 11 | |||
| 12 | use parent 'Test::CPAN::Health::Check'; | ||
| 13 | |||
| 14 | our $VERSION = '0.1.0'; | ||
| 15 | |||
| 16 | Readonly::Scalar my $SCORE_PASS => 90; # >= this -> pass | ||
| 17 | Readonly::Scalar my $SCORE_WARN => 50; # >= this -> warn; below -> fail | ||
| 18 | |||
| 19 | # Subs that are never expected to have their own POD section. | ||
| 20 | Readonly::Hash my %EXEMPT_SUBS => map { $_ => 1 } qw( | ||
| 21 | DESTROY AUTOLOAD BEGIN END INIT CHECK UNITCHECK import unimport | ||
| 22 | ); | ||
| 23 | |||
| 24 | =head1 NAME | ||
| 25 | |||
| 26 | Test::CPAN::Health::Check::PODCoverage - Check that all public methods have POD | ||
| 27 | |||
| 28 | =head1 SYNOPSIS | ||
| 29 | |||
| 30 | use Test::CPAN::Health::Check::PODCoverage; | ||
| 31 | |||
| 32 | my $check = Test::CPAN::Health::Check::PODCoverage->new; | ||
| 33 | my $result = $check->run($dist); | ||
| 34 | |||
| 35 | printf "%s: %s\n", $result->status, $result->summary; | ||
| 36 | |||
| 37 | =head1 DESCRIPTION | ||
| 38 | |||
| 39 | Parses each C<.pm> file under C<lib/> to identify public subroutines (those | ||
| 40 | not prefixed with C<_> and not in the exempt set: DESTROY, AUTOLOAD, BEGIN, | ||
| 41 | END, etc.) and checks whether each one appears as a C<=head2>, C<=head3>, or | ||
| 42 | C<=head4> entry in the file's POD. | ||
| 43 | |||
| 44 | Score = (documented_subs / total_public_subs) * 100, averaged across all | ||
| 45 | files. Status thresholds: pass E<ge> 90, warn E<ge> 50, fail otherwise. | ||
| 46 | |||
| 47 | =head1 LIMITATIONS | ||
| 48 | |||
| 49 | =over 4 | ||
| 50 | |||
| 51 | =item * Documentation inherited from a parent class is not counted -- each | ||
| 52 | file is analysed in isolation. | ||
| 53 | |||
| 54 | =item * Only C<=head2>/C<=head3>/C<=head4> headings are matched against sub | ||
| 55 | names; C<=item> entries are not counted. | ||
| 56 | |||
| 57 | =item * Generated or XS subs not visible in the C<.pm> source are not counted. | ||
| 58 | |||
| 59 | =back | ||
| 60 | |||
| 61 | =cut | ||
| 62 | |||
| 63 | sub id { return 'pod_coverage' } | ||
| 64 | sub name { return 'POD Coverage' } | ||
| 65 | sub description { return 'Checks that all public methods are documented in POD' } | ||
| 66 | sub weight { return 5 } | ||
| 67 | sub category { return 'quality' } | ||
| 68 | |||
| 69 | =head2 run | ||
| 70 | |||
| 71 | =head3 PURPOSE | ||
| 72 | |||
| 73 | Compute the fraction of public subroutines that have a corresponding POD | ||
| 74 | heading across all C<.pm> files in the distribution. | ||
| 75 | |||
| 76 | =head3 API SPECIFICATION | ||
| 77 | |||
| 78 | =head4 INPUT | ||
| 79 | |||
| 80 | dist Test::CPAN::Health::Distribution required | ||
| 81 | context Hashref optional | ||
| 82 | |||
| 83 | =head4 OUTPUT | ||
| 84 | |||
| 85 | L<Test::CPAN::Health::Result> with check_id C<'pod_coverage'>. | ||
| 86 | |||
| 87 | =head3 MESSAGES | ||
| 88 | |||
| 89 | Code | Severity | Message | Resolution | ||
| 90 | ------+----------+--------------------------------------------+----------- | ||
| 91 | PC001 | SKIP | No .pm files found under lib/ | Add lib/ modules | ||
| 92 | PC002 | SKIP | No public subs found in any .pm file | Add public methods | ||
| 93 | PC003 | PASS | POD coverage N% | | ||
| 94 | PC004 | WARN | POD coverage N% -- some methods undocumented| Add POD | ||
| 95 | PC005 | FAIL | POD coverage N% -- most methods undocumented| Add POD | ||
| 96 | |||
| 97 | =head3 FORMAL SPECIFICATION | ||
| 98 | |||
| 99 | -- Z schema (placeholder) -- | ||
| 100 | PODCoverageOp | ||
| 101 | subs_total : N | ||
| 102 | subs_covered : N | ||
| 103 | score : 0..100 | ||
| 104 | ------------------------------------------------------- | ||
| 105 | subs_total = 0 => status = skip | ||
| 106 | score >= 90 => status = pass | ||
| 107 | score >= 50 => status = warn | ||
| 108 | score < 50 => status = fail | ||
| 109 | |||
| 110 | =head3 SIDE EFFECTS | ||
| 111 | |||
| 112 | Reads source files; no network or subprocess I/O. | ||
| 113 | |||
| 114 | =head3 USAGE EXAMPLE | ||
| 115 | |||
| 116 | my $result = Test::CPAN::Health::Check::PODCoverage->new->run($dist); | ||
| 117 | print $result->summary; | ||
| 118 | |||
| 119 | =cut | ||
| 120 | |||
| 121 | sub run { | ||
| 122 | my ($self, $dist, $context) = @_; | ||
| 123 | |||
| 124 | croak 'dist must be a Test::CPAN::Health::Distribution' | ||
| 125 | unless ref($dist) && $dist->isa('Test::CPAN::Health::Distribution'); | ||
| 126 | |||
| 127 | my @pm_files = @{ $dist->pm_files }; | ||
| 128 | |||
| 129 | unless (@pm_files) { | ||
| 130 | return $self->_skip('No .pm files found under lib/'); | ||
| 131 | } | ||
| 132 | |||
| 133 | my ($total_subs, $covered_subs) = (0, 0); | ||
| 134 | my @undocumented; # arrayrefs of [ $rel_path, $sub_name ] | ||
| 135 | |||
| 136 | my $dist_root = $dist->path; | ||
| 137 | |||
| 138 | my %parent_pod_cache; # class => hashref-of-pod-names | ||
| 139 | |||
| 140 | for my $file (@pm_files) { | ||
| 141 | my $rel = File::Spec->abs2rel($file, $dist_root); | ||
| 142 | my ($pub_subs_ref, $pod_names_ref, $parents_ref) = _parse_file($file); | ||
| 143 | |||
| 144 | # Build merged parent POD map (lazy, cached per class name). | ||
| 145 | my %inherited_pod; | ||
| 146 | for my $parent_class (@{$parents_ref}) { | ||
| 147 | unless (exists $parent_pod_cache{$parent_class}) { | ||
| 148 | $parent_pod_cache{$parent_class} = _pod_names_for_class($parent_class); | ||
| 149 | } | ||
| 150 | %inherited_pod = (%inherited_pod, %{ $parent_pod_cache{$parent_class} }); | ||
| 151 | } | ||
| 152 | |||
| 153 | for my $sub_name (@{$pub_subs_ref}) { | ||
| 154 | $total_subs++; | ||
| 155 | if ($pod_names_ref->{$sub_name} || $inherited_pod{$sub_name}) { | ||
| 156 | $covered_subs++; | ||
| 157 | } else { | ||
| 158 | push @undocumented, [ $rel, $sub_name ]; | ||
| 159 | } | ||
| 160 | } | ||
| 161 | } | ||
| 162 | |||
| 163 | unless ($total_subs) { | ||
| 164 | return $self->_skip('No public subroutines found in .pm files'); | ||
| 165 | } | ||
| 166 | |||
| 167 | my $score = int($covered_subs / $total_subs * 100); | ||
| 168 | my $pct = sprintf('%d%%', $score); | ||
| 169 | my $status = $score >= $SCORE_PASS ? 'pass' | ||
| 170 | : $score >= $SCORE_WARN ? 'warn' | ||
| 171 | : 'fail'; | ||
| 172 | |||
| 173 | my @details = map { "Undocumented: $_->[0]: $_->[1]" } | ||
| 174 | sort { $a->[0] cmp $b->[0] || $a->[1] cmp $b->[1] } | ||
| 175 | @undocumented; | ||
| 176 | |||
| 177 | return $self->_result( | ||
| 178 | status => $status, | ||
| 179 | score => $score, | ||
| 180 | summary => sprintf( | ||
| 181 | 'POD coverage %s (%d of %d public sub(s) documented)', | ||
| 182 | $pct, $covered_subs, $total_subs, | ||
| 183 | ), | ||
| 184 | details => \@details, | ||
| 185 | data => { | ||
| 186 | name => $self->name, | ||
| 187 | total => $total_subs, | ||
| 188 | covered => $covered_subs, | ||
| 189 | undocumented => [ map { "$_->[0]: $_->[1]" } @undocumented ], | ||
| 190 | }, | ||
| 191 | ); | ||
| 192 | } | ||
| 193 | |||
| 194 | # --------------------------------------------------------------------------- | ||
| 195 | # Private helpers | ||
| 196 | # --------------------------------------------------------------------------- | ||
| 197 | |||
| 198 | # Parse a .pm file to identify public subs and POD-documented names. | ||
| 199 | # Also detects 'use parent' so caller can resolve inherited documentation. | ||
| 200 | # Returns (arrayref-of-public-sub-names, hashref-of-pod-names, arrayref-of-parent-classes). | ||
| 201 | sub _parse_file { | ||
| 202 | my ($file) = @_; | ||
| 203 | |||
| 204 | open my $fh, '<', $file or return ([], {}, []); | ||
| 205 | my @lines = <$fh>; | ||
| 206 | close $fh; | ||
| 207 | |||
| 208 | my @public_subs; | ||
| 209 | my %pod_names; | ||
| 210 | my @parents; | ||
| 211 | my $in_pod = 0; | ||
| 212 | |||
| 213 | for my $line (@lines) { | ||
| 214 | chomp $line; | ||
| 215 | |||
| 216 | # Update POD state first, then still process the directive line itself. | ||
| 217 | if ($line =~ / ^ = (\w+) /x) { | ||
| 218 | $in_pod = ($1 ne 'cut'); | ||
| 219 | } | ||
| 220 | |||
| 221 | if ($in_pod && $line =~ / ^ =head [234] \s+ (\w+) /x) { | ||
| 222 | $pod_names{$1}++; | ||
| 223 | } elsif (!$in_pod) { | ||
| 224 | if ($line =~ / ^ sub \s+ ([a-zA-Z] \w*) \b /x | ||
| 225 | && !$EXEMPT_SUBS{$1} && $1 !~ / ^ _ /x) { | ||
| 226 | push @public_subs, $1; | ||
| 227 | } else { | ||
| 228 | push @parents, _extract_parents($line); | ||
| 229 | } | ||
| 230 | } | ||
| 231 | } | ||
| 232 | |||
| 233 | return (\@public_subs, \%pod_names, \@parents); | ||
| 234 | } | ||
| 235 | |||
| 236 | # Extract parent class names from a 'use parent ...' line. | ||
| 237 | sub _extract_parents { | ||
| 238 | my ($line) = @_; | ||
| 239 | return unless $line =~ / \b use \s+ parent \b (.*) /x; | ||
| 240 | my $rest = $1; | ||
| 241 | my @found; | ||
| 242 | push @found, ($rest =~ / ['"] ( [^'"]+ ) ['"] /gx); | ||
| 243 | push @found, ($rest =~ / qw [(\s]+ ( [^\s)'"]+ ) /gx); | ||
| 244 | return @found; | ||
| 245 | } | ||
| 246 | |||
| 247 | # Collect all POD-documented names from a class's .pm file by searching | ||
| 248 | # the @INC path. Returns a hashref (name => 1) or empty hashref on miss. | ||
| 249 | sub _pod_names_for_class { | ||
| 250 | my ($class) = @_; | ||
| 251 | |||
| 252 | (my $rel = "$class.pm") =~ s{ :: }{/}gx; | ||
| 253 | |||
| 254 | for my $dir (@INC) { | ||
| 255 | next unless -d $dir; | ||
| 256 | my $candidate = File::Spec->catfile($dir, $rel); | ||
| 257 | if (-f $candidate) { | ||
| 258 | my (undef, $names) = _parse_file($candidate); | ||
| 259 | return $names; | ||
| 260 | } | ||
| 261 | } | ||
| 262 | return {}; | ||
| 263 | } | ||
| 264 | |||
| 265 | =head1 AUTHOR | ||
| 266 | |||
| 267 | Nigel Horne, C<< <njh at nigelhorne.com> >> | ||
| 268 | |||
| 269 | =head1 LICENSE AND COPYRIGHT | ||
| 270 | |||
| 271 | Copyright (C) 2025-2026 Nigel Horne. | ||
| 272 | |||
| 273 | This program is free software; you can redistribute it and/or modify it | ||
| 274 | under the terms of the GNU General Public License as published by the Free | ||
| 275 | Software Foundation; either version 2 of the License, or (at your option) | ||
| 276 | any later version. | ||
| 277 | |||
| 278 | =cut | ||
| 279 | |||
| 280 | 1; | ||