« blib/lib/Test/CPAN/Health/Check/Changelog.pm
↑ Summary
blib/lib/Test/CPAN/Health/Check/DeclaredDeps.pm »
| 1 | package Test::CPAN::Health::Check::Complexity; | ||
| 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 | # McCabe cyclomatic complexity above this value is considered "too complex". | ||
| 17 | # 20 is a widely-used industry threshold (SEI, McCabe's original guidance). | ||
| 18 | Readonly::Scalar my $COMPLEXITY_THRESHOLD => 20; | ||
| 19 | |||
| 20 | # Status thresholds: (1 - ratio_complex) * 100. | ||
| 21 | Readonly::Scalar my $SCORE_PASS => 90; | ||
| 22 | Readonly::Scalar my $SCORE_WARN => 50; | ||
| 23 | |||
| 24 | =head1 NAME | ||
| 25 | |||
| 26 | Test::CPAN::Health::Check::Complexity - Check cyclomatic complexity of subroutines | ||
| 27 | |||
| 28 | =head1 SYNOPSIS | ||
| 29 | |||
| 30 | use Test::CPAN::Health::Check::Complexity; | ||
| 31 | |||
| 32 | my $check = Test::CPAN::Health::Check::Complexity->new; | ||
| 33 | my $result = $check->run($dist); | ||
| 34 | |||
| 35 | printf "%s: %s\n", $result->status, $result->summary; | ||
| 36 | |||
| 37 | =head1 DESCRIPTION | ||
| 38 | |||
| 39 | Uses L<Perl::Metrics::Simple> to compute the McCabe cyclomatic complexity of | ||
| 40 | every subroutine in the distribution's C<.pm> files. Subroutines with a | ||
| 41 | complexity score above 20 are flagged as "too complex". | ||
| 42 | |||
| 43 | Score = round((1 - complex_subs / total_subs) * 100). Status thresholds: | ||
| 44 | pass E<ge> 90, warn E<ge> 50, fail otherwise. | ||
| 45 | |||
| 46 | If no subroutines are found (e.g. a purely declarative module) the check | ||
| 47 | passes at 100. | ||
| 48 | |||
| 49 | =head1 LIMITATIONS | ||
| 50 | |||
| 51 | =over 4 | ||
| 52 | |||
| 53 | =item * Only C<.pm> files are analysed; script files (C<.pl>, C<bin/>) are | ||
| 54 | excluded because their top-level code is not part of a named subroutine. | ||
| 55 | |||
| 56 | =item * Perl::Metrics::Simple uses PPI for parsing and may not handle all | ||
| 57 | Perl syntax correctly. | ||
| 58 | |||
| 59 | =back | ||
| 60 | |||
| 61 | =cut | ||
| 62 | |||
| 63 | sub id { return 'complexity' } | ||
| 64 | sub name { return 'Cyclomatic Complexity' } | ||
| 65 | sub description { return 'Checks cyclomatic complexity of subroutines against a threshold' } | ||
| 66 | sub weight { return 4 } | ||
| 67 | sub category { return 'quality' } | ||
| 68 | |||
| 69 | =head2 run | ||
| 70 | |||
| 71 | =head3 PURPOSE | ||
| 72 | |||
| 73 | Measure McCabe complexity of all named subroutines and score by the fraction | ||
| 74 | below the threshold. | ||
| 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<'complexity'>. | ||
| 86 | |||
| 87 | =head3 MESSAGES | ||
| 88 | |||
| 89 | Code | Severity | Message | Resolution | ||
| 90 | ------+----------+---------------------------------------------+----------- | ||
| 91 | CX001 | SKIP | No .pm files found | Add lib/ modules | ||
| 92 | CX002 | PASS | All N subroutines within complexity limit | | ||
| 93 | CX003 | WARN | N subroutine(s) exceed complexity limit | Refactor subs | ||
| 94 | CX004 | FAIL | Most subroutines exceed complexity limit | Refactor subs | ||
| 95 | |||
| 96 | =head3 FORMAL SPECIFICATION | ||
| 97 | |||
| 98 | -- Z schema (placeholder) -- | ||
| 99 | ComplexityOp | ||
| 100 | total_subs : N | ||
| 101 | complex_subs : N | ||
| 102 | score : 0..100 | ||
| 103 | ------------------------------------------------------- | ||
| 104 | total_subs = 0 => status = pass /\ score = 100 | ||
| 105 | score >= 90 => status = pass | ||
| 106 | score >= 50 => status = warn | ||
| 107 | score < 50 => status = fail | ||
| 108 | |||
| 109 | =head3 SIDE EFFECTS | ||
| 110 | |||
| 111 | Invokes Perl::Metrics::Simple which performs in-process PPI parsing. No | ||
| 112 | network or subprocess I/O. | ||
| 113 | |||
| 114 | =head3 USAGE EXAMPLE | ||
| 115 | |||
| 116 | my $result = Test::CPAN::Health::Check::Complexity->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 | require Perl::Metrics::Simple; | ||
| 134 | my $analyzer = Perl::Metrics::Simple->new; | ||
| 135 | |||
| 136 | my $analysis; | ||
| 137 | my $ok = eval { | ||
| 138 | local $SIG{__WARN__} = sub { }; # suppress PPI noise | ||
| 139 | $analysis = $analyzer->analyze_files(@pm_files); | ||
| 140 | 1; | ||
| 141 | }; | ||
| 142 | if (!$ok) { | ||
| 143 | return $self->_error("Perl::Metrics::Simple failed: $@"); | ||
| 144 | } | ||
| 145 | |||
| 146 | my @all_subs = @{ $analysis->subs }; | ||
| 147 | |||
| 148 | unless (@all_subs) { | ||
| 149 | return $self->_result( | ||
| 150 | status => 'pass', | ||
| 151 | score => 100, | ||
| 152 | summary => 'No named subroutines found -- nothing to measure', | ||
| 153 | data => { name => $self->name, total => 0, complex => 0 }, | ||
| 154 | ); | ||
| 155 | } | ||
| 156 | |||
| 157 | my @complex = grep { $_->{mccabe_complexity} > $COMPLEXITY_THRESHOLD } @all_subs; | ||
| 158 | |||
| 159 | my $score = int((1 - @complex / @all_subs) * 100); | ||
| 160 | my $status = @complex == 0 ? 'pass' | ||
| 161 | : $score >= $SCORE_WARN ? 'warn' | ||
| 162 | : 'fail'; | ||
| 163 | |||
| 164 | my @details = map { | ||
| 165 | sprintf( | ||
| 166 | '%s::%s -- complexity %d (threshold %d)', | ||
| 167 | File::Spec->abs2rel($_->{path}, $dist->path), | ||
| 168 | $_->{name}, | ||
| 169 | $_->{mccabe_complexity}, | ||
| 170 | $COMPLEXITY_THRESHOLD, | ||
| 171 | ) | ||
| 172 | } @complex; | ||
| 173 | |||
| 174 | return $self->_result( | ||
| 175 | status => $status, | ||
| 176 | score => $score, | ||
| 177 | summary => sprintf( | ||
| 178 | '%d of %d subroutine(s) exceed McCabe complexity threshold of %d', | ||
| 179 | scalar @complex, scalar @all_subs, $COMPLEXITY_THRESHOLD, | ||
| 180 | ), | ||
| 181 | details => \@details, | ||
| 182 | data => { | ||
| 183 | name => $self->name, | ||
| 184 | total => scalar @all_subs, | ||
| 185 | complex => scalar @complex, | ||
| 186 | threshold => $COMPLEXITY_THRESHOLD, | ||
| 187 | }, | ||
| 188 | ); | ||
| 189 | } | ||
| 190 | |||
| 191 | =head1 AUTHOR | ||
| 192 | |||
| 193 | Nigel Horne, C<< <njh at nigelhorne.com> >> | ||
| 194 | |||
| 195 | =head1 LICENSE AND COPYRIGHT | ||
| 196 | |||
| 197 | Copyright (C) 2025-2026 Nigel Horne. | ||
| 198 | |||
| 199 | This program is free software; you can redistribute it and/or modify it | ||
| 200 | under the terms of the GNU General Public License as published by the Free | ||
| 201 | Software Foundation; either version 2 of the License, or (at your option) | ||
| 202 | any later version. | ||
| 203 | |||
| 204 | =cut | ||
| 205 | |||
| 206 | 1; | ||