blib/lib/App/Project/Doctor/Fixer.pm

filter: Statementstmt 100% 76 / 76 Branchbran 100% 18 / 18 Conditioncond 90.9% 10 / 11 MC/DCmcdc 83.3% 5 / 6 Subroutinesub 100% 15 / 15 Podpod 40.0% 2 / 5 totaltotal 96.1% 126 / 131 CC 16 SCAR 13.2
CC
16
Cov
99%
_interactive_loop
8.0
new
4.0
run
3.0
CRAP 3.7
« blib/lib/App/Project/Doctor/Check/Dependencies.pm ↑ Summary lib/App/Project/Doctor/Check/GitHubActions.pm »
1 package App::Project::Doctor::Fixer;
2  
3 # The Fixer presents a numbered menu of auto-fixable findings, reads the
4 # user's answer from STDIN, and calls each selected fix coderef with the
5 # current Context. In non-interactive mode (--fix flag) it applies all
6 # fixes immediately without prompting.
7  
8 5 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 5 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 5 ▶use autodie qw(:all);
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 # croak dies at the caller's location; carp warns there.
13 5 ▶use Carp qw(croak carp);
Subroutine BEGIN: called CC=1 SCAR=0.0
14 # Params::Get normalises @_ into a hashref before validate_strict sees it.
15 5 ▶use Params::Get;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 # validate_strict enforces parameter schemas and throws immediately on failure.
17 5 ▶use Params::Validate::Strict qw(validate_strict);
Subroutine BEGIN: called CC=1 SCAR=0.0
18 # blessed() checks whether a reference is a blessed object.
19 5 ▶use Scalar::Util qw(blessed);
Subroutine BEGIN: called CC=1 SCAR=0.0
20  
21 5 our $VERSION = '0.02';
22  
23 # ---------------------------------------------------------------------------
24 # Constructor
25 # ---------------------------------------------------------------------------
26  
27 51 ▶sub new {
Subroutine new: called CC=4 SCAR=13.9
Pod documented
28 51 my $class = shift;
29  
30 # validate_strict with type => 'object' guarantees that report and context
31 # are blessed references before we reach the isa() checks below.
32 # The redundant blessed() call was removed; isa() alone is sufficient.
33 51 ▶ my $args = validate_strict(
Condition50%1/2
Params::Get::get_params(undef, \@_) || {}
l!l
510
MC/DC0%0/1
Params::Get::get_params(undef, \@_) || {}
Params::Get::get_params(undef, \@_)
Truth table50%1/2
A
A= Params::Get::get_params(undef, \@_)
Aresult
11
00
34 schema => {
35 report => { type => 'object' },
36 context => { type => 'object' },
37 non_interactive => { type => 'scalar', optional => 1, default => 0 },
38 },
39 args => Params::Get::get_params(undef, \@_) || {},
40 );
41  
42 # isa() confirms the exact class; validate_strict only checked 'blessed'.
43 croak 'report must be an App::Project::Doctor::Report'
44 44 ▶ unless $args->{report}->isa('App::Project::Doctor::Report');
Branch: 1 branch
BranchTrueFalseTotal
unless $args->{"report"}->isa("App::Project::Doctor::Report") 1 43 44
45 croak 'context must be an App::Project::Doctor::Context'
46 43 ▶ unless $args->{context}->isa('App::Project::Doctor::Context');
Branch: 1 branch
BranchTrueFalseTotal
unless $args->{"context"}->isa("App::Project::Doctor::Context") 1 42 43
47  
48 # Store the validated args and return the new Fixer object.
49 42 return bless $args, $class;
50 }
51  
52 # ---------------------------------------------------------------------------
53 # Accessors (read-only after construction)
54 # ---------------------------------------------------------------------------
55  
56 # The Report whose fixable findings will be presented to the user.
57 47 ▶sub report { $_[0]->{report} }
Subroutine report: called CC=1 SCAR=0.0
Pod undocumented
58 # The Context passed to each fix coderef so it can find files.
59 40 ▶sub context { $_[0]->{context} }
Subroutine context: called CC=1 SCAR=0.0
Pod undocumented
60 # When true, all fixes are applied immediately without user prompting.
61 44 ▶sub non_interactive { $_[0]->{non_interactive} }
Subroutine non_interactive: called CC=1 SCAR=0.0
Pod undocumented
62  
63 # ---------------------------------------------------------------------------
64 # Public interface
65 # ---------------------------------------------------------------------------
66  
67 =head2 run
68  
69 Presents fixable findings, prompts (or auto-applies in non-interactive mode),
70 and calls each selected C<fix> coderef. Returns the count of fixes applied.
71  
72 =cut
73  
74 46 ▶sub run {
Subroutine run: called CC=3 SCAR=11.0
Pod documented
75 46 my $self = shift;
76 # Collect only the findings that have an associated fix coderef.
77 46 my @fixable = $self->report->fixable;
78 # Nothing to do if no fixable findings were found.
79 46 ▶ return 0 unless @fixable;
Branch: 1 branch
BranchTrueFalseTotal
unless @fixable 4 42 46
80 # Choose the right mode: silent auto-apply vs. interactive prompt.
81 42 ▶ return $self->non_interactive
Branch: 1 branch
BranchTrueFalseTotal
$self->non_interactive ? : 7 35 42
82 ? $self->_apply_all(\@fixable)
83 : $self->_interactive_loop(\@fixable);
84 }
85  
86 # ---------------------------------------------------------------------------
87 # Private helpers
88 # ---------------------------------------------------------------------------
89  
90 # Purpose: Print a numbered list of fixes to STDOUT.
91 # Entry: $fixable is a non-empty arrayref of Finding objects.
92 # Exit: Returns nothing; side-effect is printed output only.
93 # Side effects: Writes to STDOUT.
94 35 ▶sub _print_fix_list {
Subroutine _print_fix_list: called CC=2 SCAR=6.9
95 35 my $fixable = shift;
96 35 print "\nSuggested fixes:\n";
97 # Number each finding starting at 1 so the user can reference them by number.
98 35 my $i = 0;
99 35 printf " [%d] %s\n", ++$i, $_->message for @{$fixable};
100 35 return;
101 }
102  
103 # Purpose: Read the user's choice from STDIN and apply the selected fixes.
104 # Entry: $fixable is a non-empty arrayref of Finding objects.
105 # Exit: Integer count of fixes successfully applied.
106 # Side effects: Reads STDIN, writes STDOUT, may modify the filesystem via fix coderefs.
107 35 ▶sub _interactive_loop {
Subroutine _interactive_loop: called CC=8 SCAR=20.8
108 35 my ($self, $fixable) = @_;
109  
110 # Show the numbered list so the user knows what choices are available.
111 35 _print_fix_list($fixable);
112 35 print "\nWould you like me to apply them? [Y/n/1,3] ";
113  
114 # Read one line from the user; return 0 cleanly if STDIN is closed (e.g. in a pipe).
115 35 my $answer = <STDIN>;
116 35 ▶ return 0 unless defined $answer;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $answer 3 32 35
117 32 chomp $answer; # Remove the trailing newline before comparing.
118  
119 # Empty input or "yes" means apply everything.
120 32 ▶ return $self->_apply_all($fixable)
Branch: 1 branch
BranchTrueFalseTotal
if $answer eq "" or $answer =~ /^y(?:es)?$/i 10 22 32
Condition100%3/3
$answer eq "" or $answer =~ /^y(?:es)?$/i
l!l&&r!l&&!r
3722
MC/DC100%2/2
$answer eq "" or $answer =~ /^y(?:es)?$/i
$answer eq "" $answer =~ /^y(?:es)?$/i
Truth table100%3/3
A or B
A= $answer eq ""B= $answer =~ /^y(?:es)?$/i
ABresult
1X1
011
000
121 if $answer eq '' || $answer =~ /^y(?:es)?$/i;
122  
123 # Explicit "no" -- tell the user we skipped and return.
124 22 ▶ if ($answer =~ /^n(?:o)?$/i) {
Branch: 1 branch
BranchTrueFalseTotal
if ($answer =~ /^n(?:o)?$/i) 10 12 22
125 10 print "No fixes applied.\n";
126 10 return 0;
127 }
128  
129 # A comma/space-separated list of numbers selects individual fixes.
130 12 ▶ if ($answer =~ /^[\d,\s]+$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($answer =~ /^[\d,\s]+$/) 8 4 12
131 8 my $max = scalar @{$fixable}; # The highest valid index.
132 8 my %seen;
133 # Parse the numbers, clamp to valid range, and deduplicate.
134 12 ▶ my @indices = grep { $_ >= 1 && $_ <= $max && !$seen{$_}++ }
Condition100%6/6
$_ >= 1 && $_ <= $max && !$seen{$_}++
!ll&&!rl&&r
228
$_ >= 1 and $_ <= $max
!ll&&!rl&&r
1110
MC/DC100%3/3
$_ >= 1 && $_ <= $max && !$seen{$_}++
$_ >= 1 $_ <= $max !$seen{$_}++
Truth table100%4/4
A and B && not(C)
A= $_ >= 1B= $_ <= $maxC= !$seen{$_}++
ABCresult
0XX0
10X0
1100
1111
135 8 map { int($_) }
136 split /[\s,]+/, $answer;
137 # Convert 1-based user indices to 0-based array indices.
138 8 my @selected = map { $fixable->[$_ - 1] } @indices;
139 8 return $self->_apply_all(\@selected);
140 }
141  
142 # Anything else is unrecognised; be explicit rather than guessing.
143 4 print "Unrecognised input -- no fixes applied.\n";
144 4 return 0;
145 }
146  
147 # Purpose: Call every fix coderef in the list and count the successes.
148 # Entry: $fixable is an arrayref of Finding objects (may be empty).
149 # Exit: Integer count of fixes that ran without throwing.
150 # Side effects: Calls fix coderefs (may create/modify files), writes to STDOUT on
151 # success, calls carp for each failing fix.
152 25 ▶sub _apply_all {
Subroutine _apply_all: called CC=3 SCAR=11.0
153 25 my ($self, $fixable) = @_;
154 25 my $count = 0;
155 25 for my $f (@{$fixable}) {
156 # Wrap the fix in eval so a single failure doesn't abort all remaining fixes.
157 39 my $ok = eval { $f->fix->($self->context); 1 };
158 39 ▶ if ($ok) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ok) { } 36 3 39
159 # Print confirmation so the user can see what changed.
160 36 printf " Applied: %s\n", $f->message;
161 36 $count++;
162 } else {
163 # Report the failure but continue with the next fix.
164 3 carp "Fix failed for '" . $f->message . "': $@";
165 }
166 }
167 # Summary line always prints, even when count is 0.
168 25 printf "\n%d fix(es) applied.\n", $count;
169 25 return $count;
170 }
171  
172 5 1;
173  
174 __END__
« blib/lib/App/Project/Doctor/Check/Dependencies.pm ↑ Summary lib/App/Project/Doctor/Check/GitHubActions.pm »