« blib/lib/Test/CPAN/Health/Check/DeclaredDeps.pm
↑ Summary
blib/lib/Test/CPAN/Health/Check/DocQuality.pm »
| 1 | package Test::CPAN::Health::Check::Deprecations; | ||
| 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 | # Each entry is a pair: [ qr/PATTERN/, 'Human-readable label' ]. | ||
| 17 | # Patterns use /m so ^ and $ match per-line within slurped file content. | ||
| 18 | Readonly::Array my @DEPRECATED_PATTERNS => ( | ||
| 19 | [ qr/ \b given \s* \( /mx, 'given/when (experimental; removed in Perl 5.36 feature bundle)' ], | ||
| 20 | [ qr/ \b when \s* \( /mx, 'when (experimental; removed in Perl 5.36 feature bundle)' ], | ||
| 21 | [ qr/ \$ \[ /mx, '$[ array-base variable (deprecated since Perl 5.12)' ], | ||
| 22 | [ qr/ \b UNIVERSAL::isa \s* \( /mx, 'UNIVERSAL::isa() as function (deprecated since Perl 5.26)' ], | ||
| 23 | [ qr/ \b UNIVERSAL::can \s* \( /mx, 'UNIVERSAL::can() as function (deprecated since Perl 5.26)' ], | ||
| 24 | [ qr/ ^ \s* use \s+ UNIVERSAL \b /mx, 'use UNIVERSAL (deprecated since Perl 5.22)' ], | ||
| 25 | [ qr/ ^ \s* use \s+ Switch \b /mx, 'use Switch (removed from core since Perl 5.10)' ], | ||
| 26 | ); | ||
| 27 | |||
| 28 | # Score thresholds (files with at least one hit / total files). | ||
| 29 | Readonly::Scalar my $SCORE_PASS => 90; | ||
| 30 | Readonly::Scalar my $SCORE_WARN => 50; | ||
| 31 | |||
| 32 | =head1 NAME | ||
| 33 | |||
| 34 | Test::CPAN::Health::Check::Deprecations - Detect use of deprecated Perl features | ||
| 35 | |||
| 36 | =head1 SYNOPSIS | ||
| 37 | |||
| 38 | use Test::CPAN::Health::Check::Deprecations; | ||
| 39 | |||
| 40 | my $check = Test::CPAN::Health::Check::Deprecations->new; | ||
| 41 | my $result = $check->run($dist); | ||
| 42 | |||
| 43 | printf "%s: %s\n", $result->status, $result->summary; | ||
| 44 | |||
| 45 | =head1 DESCRIPTION | ||
| 46 | |||
| 47 | Scans all source files for a curated set of deprecated or removed Perl | ||
| 48 | features and modules, using regular-expression matching (no external CPAN | ||
| 49 | module required): | ||
| 50 | |||
| 51 | =over 4 | ||
| 52 | |||
| 53 | =item * C<given>/C<when> blocks (removed from default feature set in Perl 5.36). | ||
| 54 | |||
| 55 | =item * C<$[> array-base variable (deprecated since Perl 5.12). | ||
| 56 | |||
| 57 | =item * C<UNIVERSAL::isa()> and C<UNIVERSAL::can()> called as functions | ||
| 58 | (deprecated since Perl 5.26). | ||
| 59 | |||
| 60 | =item * C<use UNIVERSAL> (deprecated since Perl 5.22). | ||
| 61 | |||
| 62 | =item * C<use Switch> (removed from core since Perl 5.10). | ||
| 63 | |||
| 64 | =back | ||
| 65 | |||
| 66 | Score = round((clean_files / total_files) * 100). Files with any hit count | ||
| 67 | as "affected". Status thresholds: pass E<ge> 90 %, warn E<ge> 50 %, fail | ||
| 68 | below 50 %. | ||
| 69 | |||
| 70 | =head1 LIMITATIONS | ||
| 71 | |||
| 72 | =over 4 | ||
| 73 | |||
| 74 | =item * Pattern matching does not parse Perl; occurrences inside strings, | ||
| 75 | heredocs, or comments will produce false positives. The check deliberately | ||
| 76 | errs on the side of over-reporting to prompt manual review. | ||
| 77 | |||
| 78 | =item * The list of deprecated features is curated manually and may be | ||
| 79 | incomplete. | ||
| 80 | |||
| 81 | =back | ||
| 82 | |||
| 83 | =cut | ||
| 84 | |||
| 85 | sub id { return 'deprecations' } | ||
| 86 | sub name { return 'Deprecations' } | ||
| 87 | sub description { return 'Detects use of deprecated Perl features or removed modules' } | ||
| 88 | sub weight { return 4 } | ||
| 89 | sub category { return 'quality' } | ||
| 90 | |||
| 91 | =head2 run | ||
| 92 | |||
| 93 | =head3 PURPOSE | ||
| 94 | |||
| 95 | Search every source file for known-deprecated constructs and return a | ||
| 96 | proportional score. | ||
| 97 | |||
| 98 | =head3 API SPECIFICATION | ||
| 99 | |||
| 100 | =head4 INPUT | ||
| 101 | |||
| 102 | dist Test::CPAN::Health::Distribution required | ||
| 103 | context Hashref optional | ||
| 104 | |||
| 105 | =head4 OUTPUT | ||
| 106 | |||
| 107 | L<Test::CPAN::Health::Result> with check_id C<'deprecations'>. | ||
| 108 | |||
| 109 | =head3 MESSAGES | ||
| 110 | |||
| 111 | Code | Severity | Message | Resolution | ||
| 112 | ------+----------+-----------------------------------------------+----------- | ||
| 113 | DP001 | SKIP | No source files found | Add source files | ||
| 114 | DP002 | PASS | No deprecated constructs found | | ||
| 115 | DP003 | WARN | Deprecated constructs found in N file(s) | Remove usage | ||
| 116 | DP004 | FAIL | Most files use deprecated constructs | Remove usage | ||
| 117 | |||
| 118 | =head3 FORMAL SPECIFICATION | ||
| 119 | |||
| 120 | -- Z schema (placeholder) -- | ||
| 121 | DeprecationsOp | ||
| 122 | clean_files : N | ||
| 123 | total_files : N | ||
| 124 | score : 0..100 | ||
| 125 | ------------------------------------------------------- | ||
| 126 | total_files = 0 => status = skip | ||
| 127 | score >= 90 => status = pass | ||
| 128 | score >= 50 => status = warn | ||
| 129 | score < 50 => status = fail | ||
| 130 | |||
| 131 | =head3 SIDE EFFECTS | ||
| 132 | |||
| 133 | Reads source files only; no network or subprocess I/O. | ||
| 134 | |||
| 135 | =head3 USAGE EXAMPLE | ||
| 136 | |||
| 137 | my $result = Test::CPAN::Health::Check::Deprecations->new->run($dist); | ||
| 138 | print $result->summary; | ||
| 139 | |||
| 140 | =cut | ||
| 141 | |||
| 142 | sub run { | ||
| 143 | my ($self, $dist, $context) = @_; | ||
| 144 | |||
| 145 | croak 'dist must be a Test::CPAN::Health::Distribution' | ||
| 146 | unless ref($dist) && $dist->isa('Test::CPAN::Health::Distribution'); | ||
| 147 | |||
| 148 | my @files = @{ $dist->all_source_files }; | ||
| 149 | |||
| 150 | unless (@files) { | ||
| 151 | return $self->_skip('No source files found'); | ||
| 152 | } | ||
| 153 | |||
| 154 | my ($clean, $total) = (0, 0); | ||
| 155 | my @details; | ||
| 156 | |||
| 157 | for my $file (@files) { | ||
| 158 | my $content; | ||
| 159 | my $ok = eval { | ||
| 160 | open my $fh, '<', $file; | ||
| 161 | local $/ = undef; | ||
| 162 | $content = <$fh>; | ||
| 163 | close $fh; | ||
| 164 | 1; | ||
| 165 | }; | ||
| 166 | if (!$ok || !defined $content) { | ||
| 167 | carp "Could not read $file: $@"; | ||
| 168 | next; | ||
| 169 | } | ||
| 170 | |||
| 171 | $total++; | ||
| 172 | my @hits; | ||
| 173 | for my $entry (@DEPRECATED_PATTERNS) { | ||
| 174 | my ($pattern, $label) = @{$entry}; | ||
| 175 | push @hits, $label if $content =~ $pattern; | ||
| 176 | } | ||
| 177 | |||
| 178 | if (@hits) { | ||
| 179 | my $rel = File::Spec->abs2rel($file, $dist->path); | ||
| 180 | push @details, "$rel: " . join('; ', @hits); | ||
| 181 | } else { | ||
| 182 | $clean++; | ||
| 183 | } | ||
| 184 | } | ||
| 185 | |||
| 186 | unless ($total) { | ||
| 187 | return $self->_skip('No files could be read'); | ||
| 188 | } | ||
| 189 | |||
| 190 | my $score = int($clean / $total * 100); | ||
| 191 | my $status = $score >= $SCORE_PASS ? 'pass' | ||
| 192 | : $score >= $SCORE_WARN ? 'warn' | ||
| 193 | : 'fail'; | ||
| 194 | |||
| 195 | return $self->_result( | ||
| 196 | status => $status, | ||
| 197 | score => $score, | ||
| 198 | summary => $score == 100 | ||
| 199 | ? 'No deprecated constructs found in any source file' | ||
| 200 | : sprintf( | ||
| 201 | 'Deprecated constructs found in %d of %d file(s)', | ||
| 202 | $total - $clean, $total, | ||
| 203 | ), | ||
| 204 | details => \@details, | ||
| 205 | data => { | ||
| 206 | name => $self->name, | ||
| 207 | total => $total, | ||
| 208 | clean => $clean, | ||
| 209 | affected => $total - $clean, | ||
| 210 | }, | ||
| 211 | ); | ||
| 212 | } | ||
| 213 | |||
| 214 | =head1 AUTHOR | ||
| 215 | |||
| 216 | Nigel Horne, C<< <njh at nigelhorne.com> >> | ||
| 217 | |||
| 218 | =head1 LICENSE AND COPYRIGHT | ||
| 219 | |||
| 220 | Copyright (C) 2025-2026 Nigel Horne. | ||
| 221 | |||
| 222 | This program is free software; you can redistribute it and/or modify it | ||
| 223 | under the terms of the GNU General Public License as published by the Free | ||
| 224 | Software Foundation; either version 2 of the License, or (at your option) | ||
| 225 | any later version. | ||
| 226 | |||
| 227 | =cut | ||
| 228 | |||
| 229 | 1; | ||