« blib/lib/Test/CPAN/Health/Check/Deprecations.pm
↑ Summary
blib/lib/Test/CPAN/Health/Check/DuplicateCode.pm »
| 1 | package Test::CPAN::Health::Check::DocQuality; | ||
| 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 | # Required =head1 sections for a well-documented module. | ||
| 17 | Readonly::Array my @REQUIRED_SECTIONS => qw(NAME SYNOPSIS DESCRIPTION AUTHOR); | ||
| 18 | |||
| 19 | # Regex matching a LICENSE / COPYRIGHT heading (various spellings). | ||
| 20 | Readonly::Scalar my $LICENSE_HEAD_RE => qr/ ^ =head1 \s+ (?:LICEN[CS]E|COPYRIGHT) /xi; | ||
| 21 | |||
| 22 | # Per-file score assignments. | ||
| 23 | Readonly::Scalar my $SCORE_FILE_FULL => 100; # no errors, all sections present | ||
| 24 | Readonly::Scalar my $SCORE_FILE_NOSEC => 70; # no errors, some sections missing | ||
| 25 | Readonly::Scalar my $SCORE_FILE_ERRORS => 30; # POD syntax errors present | ||
| 26 | Readonly::Scalar my $SCORE_FILE_NOPOD => 0; # no POD at all | ||
| 27 | |||
| 28 | # Overall status thresholds. | ||
| 29 | Readonly::Scalar my $SCORE_PASS => 90; | ||
| 30 | Readonly::Scalar my $SCORE_WARN => 50; | ||
| 31 | |||
| 32 | =head1 NAME | ||
| 33 | |||
| 34 | Test::CPAN::Health::Check::DocQuality - Check POD syntax and required section presence | ||
| 35 | |||
| 36 | =head1 SYNOPSIS | ||
| 37 | |||
| 38 | use Test::CPAN::Health::Check::DocQuality; | ||
| 39 | |||
| 40 | my $check = Test::CPAN::Health::Check::DocQuality->new; | ||
| 41 | my $result = $check->run($dist); | ||
| 42 | |||
| 43 | printf "%s: %s\n", $result->status, $result->summary; | ||
| 44 | |||
| 45 | =head1 DESCRIPTION | ||
| 46 | |||
| 47 | For each C<.pm> file under C<lib/>, this check: | ||
| 48 | |||
| 49 | =over 4 | ||
| 50 | |||
| 51 | =item 1. | ||
| 52 | |||
| 53 | Runs L<Pod::Checker> to detect POD syntax errors. | ||
| 54 | |||
| 55 | =item 2. | ||
| 56 | |||
| 57 | Scans for the presence of required C<=head1> sections: NAME, SYNOPSIS, | ||
| 58 | DESCRIPTION, AUTHOR, and a LICENSE/COPYRIGHT section (under any of the | ||
| 59 | common spellings). | ||
| 60 | |||
| 61 | =back | ||
| 62 | |||
| 63 | Per-file score: 100 (no errors, all sections), 70 (no errors, some sections | ||
| 64 | missing), 30 (POD errors present), 0 (no POD). The check score is the | ||
| 65 | integer average across all files. | ||
| 66 | |||
| 67 | =head1 LIMITATIONS | ||
| 68 | |||
| 69 | =over 4 | ||
| 70 | |||
| 71 | =item * Only C<=head1> headings are matched for required sections. | ||
| 72 | |||
| 73 | =item * Pod::Checker errors/warnings are counted but not included verbatim | ||
| 74 | in the result details to keep output concise. | ||
| 75 | |||
| 76 | =back | ||
| 77 | |||
| 78 | =cut | ||
| 79 | |||
| 80 | sub id { return 'doc_quality' } | ||
| 81 | sub name { return 'Documentation Quality' } | ||
| 82 | sub description { return 'Checks POD syntax and the presence of required sections' } | ||
| 83 | sub weight { return 4 } | ||
| 84 | sub category { return 'quality' } | ||
| 85 | |||
| 86 | =head2 run | ||
| 87 | |||
| 88 | =head3 PURPOSE | ||
| 89 | |||
| 90 | Aggregate Pod::Checker results and required-section presence across all C<.pm> | ||
| 91 | files and return a single scored Result. | ||
| 92 | |||
| 93 | =head3 API SPECIFICATION | ||
| 94 | |||
| 95 | =head4 INPUT | ||
| 96 | |||
| 97 | dist Test::CPAN::Health::Distribution required | ||
| 98 | context Hashref optional | ||
| 99 | |||
| 100 | =head4 OUTPUT | ||
| 101 | |||
| 102 | L<Test::CPAN::Health::Result> with check_id C<'doc_quality'>. | ||
| 103 | |||
| 104 | =head3 MESSAGES | ||
| 105 | |||
| 106 | Code | Severity | Message | Resolution | ||
| 107 | ------+----------+--------------------------------------------------+----------- | ||
| 108 | DQ001 | SKIP | No .pm files found | Add lib/ modules | ||
| 109 | DQ002 | PASS | All files have valid POD with required sections | | ||
| 110 | DQ003 | WARN | Some files have POD issues | Fix listed files | ||
| 111 | DQ004 | FAIL | Most files have POD issues | Fix listed files | ||
| 112 | |||
| 113 | =head3 FORMAL SPECIFICATION | ||
| 114 | |||
| 115 | -- Z schema (placeholder) -- | ||
| 116 | DocQualityOp | ||
| 117 | files : seq FileName | ||
| 118 | file_scores : seq (0..100) | ||
| 119 | avg_score : 0..100 | ||
| 120 | ------------------------------------------------------- | ||
| 121 | #files = 0 => status = skip | ||
| 122 | avg_score >= 90 => status = pass | ||
| 123 | avg_score >= 50 => status = warn | ||
| 124 | avg_score < 50 => status = fail | ||
| 125 | |||
| 126 | =head3 SIDE EFFECTS | ||
| 127 | |||
| 128 | Reads source files; invokes Pod::Checker which performs in-process parsing. | ||
| 129 | |||
| 130 | =head3 USAGE EXAMPLE | ||
| 131 | |||
| 132 | my $result = Test::CPAN::Health::Check::DocQuality->new->run($dist); | ||
| 133 | print $result->summary; | ||
| 134 | |||
| 135 | =cut | ||
| 136 | |||
| 137 | sub run { | ||
| 138 | my ($self, $dist, $context) = @_; | ||
| 139 | |||
| 140 | croak 'dist must be a Test::CPAN::Health::Distribution' | ||
| 141 | unless ref($dist) && $dist->isa('Test::CPAN::Health::Distribution'); | ||
| 142 | |||
| 143 | my @pm_files = @{ $dist->pm_files }; | ||
| 144 | |||
| 145 | unless (@pm_files) { | ||
| 146 | return $self->_skip('No .pm files found under lib/'); | ||
| 147 | } | ||
| 148 | |||
| 149 | require Pod::Checker; | ||
| 150 | |||
| 151 | my $total_score = 0; | ||
| 152 | my @issues; | ||
| 153 | |||
| 154 | for my $file (@pm_files) { | ||
| 155 | my $rel = File::Spec->abs2rel($file, $dist->path); | ||
| 156 | my ($file_score, $issue) = _score_file($file, $rel); | ||
| 157 | $total_score += $file_score; | ||
| 158 | push @issues, $issue if defined $issue; | ||
| 159 | } | ||
| 160 | |||
| 161 | my $score = int($total_score / scalar @pm_files); | ||
| 162 | my $status = $score >= $SCORE_PASS ? 'pass' | ||
| 163 | : $score >= $SCORE_WARN ? 'warn' | ||
| 164 | : 'fail'; | ||
| 165 | |||
| 166 | return $self->_result( | ||
| 167 | status => $status, | ||
| 168 | score => $score, | ||
| 169 | summary => sprintf( | ||
| 170 | 'Documentation quality score %d%% across %d file(s)%s', | ||
| 171 | $score, | ||
| 172 | scalar @pm_files, | ||
| 173 | @issues ? sprintf(' (%d issue(s) found)', scalar @issues) : '', | ||
| 174 | ), | ||
| 175 | details => \@issues, | ||
| 176 | data => { | ||
| 177 | name => $self->name, | ||
| 178 | files => scalar @pm_files, | ||
| 179 | score => $score, | ||
| 180 | }, | ||
| 181 | ); | ||
| 182 | } | ||
| 183 | |||
| 184 | # --------------------------------------------------------------------------- | ||
| 185 | # Private helpers | ||
| 186 | # --------------------------------------------------------------------------- | ||
| 187 | |||
| 188 | # Score a single file; returns (score, issue_string_or_undef). | ||
| 189 | sub _score_file { | ||
| 190 | my ($file, $rel) = @_; | ||
| 191 | |||
| 192 | my $errors = _count_pod_errors($file); | ||
| 193 | my %secs = _pod_sections($file); | ||
| 194 | my $has_pod = %secs ? 1 : 0; | ||
| 195 | |||
| 196 | if (!$has_pod && !$errors) { | ||
| 197 | return ($SCORE_FILE_NOPOD, "$rel: no POD found"); | ||
| 198 | } | ||
| 199 | if ($errors) { | ||
| 200 | return ($SCORE_FILE_ERRORS, sprintf('%s: %d POD error(s)', $rel, $errors)); | ||
| 201 | } | ||
| 202 | |||
| 203 | my @missing = grep { !$secs{$_} } @REQUIRED_SECTIONS; | ||
| 204 | push @missing, 'LICENSE' unless $secs{LICENSE} || $secs{LICENCE} || $secs{COPYRIGHT}; | ||
| 205 | |||
| 206 | if (@missing) { | ||
| 207 | return ($SCORE_FILE_NOSEC, sprintf('%s: missing sections: %s', $rel, join(', ', @missing))); | ||
| 208 | } | ||
| 209 | return ($SCORE_FILE_FULL, undef); | ||
| 210 | } | ||
| 211 | |||
| 212 | # Run Pod::Checker on $file; return count of errors (warnings ignored). | ||
| 213 | sub _count_pod_errors { | ||
| 214 | my ($file) = @_; | ||
| 215 | |||
| 216 | my $checker = Pod::Checker->new; | ||
| 217 | my $dev_null = File::Spec->devnull; | ||
| 218 | |||
| 219 | open my $sink, '>', $dev_null | ||
| 220 | or return 0; # can't open devnull; skip error counting | ||
| 221 | $checker->parse_from_file($file, $sink); | ||
| 222 | close $sink; | ||
| 223 | |||
| 224 | my $n = $checker->num_errors // 0; | ||
| 225 | return $n < 0 ? 0 : $n; # Pod::Checker returns -1 when there is no POD | ||
| 226 | } | ||
| 227 | |||
| 228 | # Return a hash of =head1 section names found in $file. | ||
| 229 | sub _pod_sections { | ||
| 230 | my ($file) = @_; | ||
| 231 | |||
| 232 | open my $fh, '<', $file or return (); | ||
| 233 | my @lines = <$fh>; | ||
| 234 | close $fh; | ||
| 235 | |||
| 236 | my %sections; | ||
| 237 | my $in_pod = 0; | ||
| 238 | |||
| 239 | for my $line (@lines) { | ||
| 240 | chomp $line; | ||
| 241 | if ($line =~ / ^ = (\w+) /x) { | ||
| 242 | $in_pod = ($1 ne 'cut'); | ||
| 243 | } | ||
| 244 | if ($in_pod && $line =~ / ^ =head1 \s+ (.+) /x) { | ||
| 245 | my $title = $1; | ||
| 246 | $title =~ s/ \s+ $ //x; | ||
| 247 | $sections{$title}++; | ||
| 248 | $sections{LICENSE}++ if $title =~ / LICEN [CS] E /xi; | ||
| 249 | $sections{LICENCE}++ if $title =~ / LICEN [CS] E /xi; | ||
| 250 | $sections{COPYRIGHT}++ if $title =~ / COPYRIGHT /xi; | ||
| 251 | } | ||
| 252 | } | ||
| 253 | |||
| 254 | return %sections; | ||
| 255 | } | ||
| 256 | |||
| 257 | =head1 AUTHOR | ||
| 258 | |||
| 259 | Nigel Horne, C<< <njh at nigelhorne.com> >> | ||
| 260 | |||
| 261 | =head1 LICENSE AND COPYRIGHT | ||
| 262 | |||
| 263 | Copyright (C) 2025-2026 Nigel Horne. | ||
| 264 | |||
| 265 | This program is free software; you can redistribute it and/or modify it | ||
| 266 | under the terms of the GNU General Public License as published by the Free | ||
| 267 | Software Foundation; either version 2 of the License, or (at your option) | ||
| 268 | any later version. | ||
| 269 | |||
| 270 | =cut | ||
| 271 | |||
| 272 | 1; | ||