blib/lib/Perl/Critic/Command.pm

filter: Statementstmt 33.6% 101 / 300 Branchbran 23.6% 27 / 114 Conditioncond 40.3% 25 / 62 MC/DCmcdc 35.1% 13 / 37 Subroutinesub 51.2% 20 / 39 Podpod 100% 1 / 1 totaltotal 33.8% 187 / 553 CC 82 SCAR 41.8
CC
82
Cov
32%
_render_report
240.0
_report_statistics
156.0
_critique
110.0
CRAP 65.3
« blib/lib/Perl/Critic/Policy/Variables/RequireNegativeIndices.pm ↑ Summary blib/lib/Perl/Critic/Policy/ErrorHandling/RequireCarping.pm »
1 package Perl::Critic::Command;
2  
3 3 ▶use 5.010001;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 3 ▶use English qw< -no_match_vars >;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 3 ▶use Readonly;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 3 ▶use Getopt::Long qw< GetOptions >;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 3 ▶use List::Util qw< first max >;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 3 ▶use Pod::Usage qw< pod2usage >;
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 3 ▶use Perl::Critic::Exception::Parse ();
Subroutine BEGIN: called CC=1 SCAR=0.0
15 3 use Perl::Critic::Utils qw<
16 :characters :severities policy_short_name
17 $DEFAULT_VERBOSITY $DEFAULT_VERBOSITY_WITH_FILE_NAME
18 3 ▶>;
Subroutine BEGIN: called CC=1 SCAR=0.0
19 3 ▶use Perl::Critic::Utils::Constants qw< $_MODULE_VERSION_TERM_ANSICOLOR >;
Subroutine BEGIN: called CC=1 SCAR=0.0
20 3 ▶use Perl::Critic::Violation qw<>;
Subroutine BEGIN: called CC=1 SCAR=0.0
21  
22 #-----------------------------------------------------------------------------
23  
24 3 our $VERSION = '1.150';
25  
26 #-----------------------------------------------------------------------------
27  
28 3 ▶use Exporter 'import';
Subroutine BEGIN: called CC=1 SCAR=0.0
29  
30 3 Readonly::Array our @EXPORT_OK => qw< run >;
31  
32 3 Readonly::Hash our %EXPORT_TAGS => (
33 all => [ @EXPORT_OK ],
34 );
35  
36 #-----------------------------------------------------------------------------
37  
38 3 Readonly::Scalar my $DEFAULT_VIOLATIONS_FOR_TOP => 20;
39  
40 3 Readonly::Scalar my $EXIT_SUCCESS => 0;
41 3 Readonly::Scalar my $EXIT_NO_FILES => 1;
42 3 Readonly::Scalar my $EXIT_HAD_VIOLATIONS => 2;
43 3 Readonly::Scalar my $EXIT_HAD_FILE_PROBLEMS => 3;
44  
45 #-----------------------------------------------------------------------------
46  
47 3 my @files;
48 my $critic;
49 3 my $output = \*STDOUT;
50  
51 #-----------------------------------------------------------------------------
52  
53 sub _out {
54 0 ▶ my @lines = @_;
Subroutine _out: not called CC=1 SCAR=6.9
55 0 return print {$output} @lines;
56 }
57  
58 #-----------------------------------------------------------------------------
59  
60 sub run {
61 0 ▶ my %options = _get_options();
Subroutine run: not called CC=4 SCAR=30.0
Pod documented
62 0 @files = _get_input(@ARGV);
63  
64 0 my ($violations, $had_error_in_file) = _critique(\%options, @files);
65  
66 0 ▶ return $EXIT_HAD_FILE_PROBLEMS if $had_error_in_file;
Branch: 1 branch
BranchTrueFalseTotal
if $had_error_in_file 0 0 0
67 0 ▶ return $EXIT_NO_FILES if not defined $violations;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $violations 0 0 0
68 0 ▶ return $EXIT_HAD_VIOLATIONS if $violations;
Branch: 1 branch
BranchTrueFalseTotal
if $violations 0 0 0
69  
70 0 return $EXIT_SUCCESS;
71 }
72  
73 #-----------------------------------------------------------------------------
74  
75 sub _get_options {
76  
77 52 ▶ my %opts = _parse_command_line();
Subroutine _get_options: called CC=7 SCAR=19.5
78 52 _dispatch_special_requests( %opts );
79 49 _validate_options( %opts );
80  
81 # Convert severity shortcut options. If multiple shortcuts
82 # are given, the lowest one wins. If an explicit --severity
83 # option has been given, then the shortcuts are ignored. The
84 # @SEVERITY_NAMES variable is exported by Perl::Critic::Utils.
85 44 ▶ $opts{-severity} ||= first { exists $opts{"-$_"} } @SEVERITY_NAMES;
Subroutine __ANON__: called CC=1 SCAR=0.0
Condition100%3/3
$opts{"-severity"} ||= &first(sub {
l!l&&r!l&&!r
9134
MC/DC100%2/2
$opts{"-severity"} ||= &first(sub { exists $opts{"-$_"}; } , @Perl::Critic::Command::SEVERITY_NAMES)
$opts{"-severity"} &first(sub { exists $opts{"-$_"}; } , @Perl::Critic::Command::SEVERITY_NAMES)
Truth table100%3/3
A ||= B
A= $opts{"-severity"}B= &first(sub { exists $opts{"-$_"}; } , @Perl::Critic::Command::SEVERITY_NAMES)
ABresult
1X1
011
000
86 44 ▶ $opts{-severity} ||= first { exists $opts{"-$_"} } ($SEVERITY_LOWEST .. $SEVERITY_HIGHEST);
Subroutine __ANON__: called CC=1 SCAR=0.0
Condition100%3/3
$opts{"-severity"} ||= &first(sub {
l!l&&r!l&&!r
10232
MC/DC100%2/2
$opts{"-severity"} ||= &first(sub { exists $opts{"-$_"}; } , $Perl::Critic::Command::SEVERITY_LOWEST .. $Perl::Critic::Command::SEVERITY_HIGHEST)
$opts{"-severity"} &first(sub { exists $opts{"-$_"}; } , $Perl::Critic::Command::SEVERITY_LOWEST .. $Perl::Critic::Command::SEVERITY_HIGHEST)
Truth table100%3/3
A ||= B
A= $opts{"-severity"}B= &first(sub { exists $opts{"-$_"}; } , $Perl::Critic::Command::SEVERITY_LOWEST .. $Perl::Critic::Command::SEVERITY_HIGHEST)
ABresult
1X1
011
000
87  
88  
89 # If --top is specified, default the severity level to 1, unless an
90 # explicit severity is defined. This provides us flexibility to
91 # report top-offenders across just some or all of the severity levels.
92 # We also default the --top count to twenty if none is given
93 44 ▶ if ( exists $opts{-top} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $opts{"-top"}) 5 39 44
94 5 ▶ $opts{-severity} ||= 1;
Condition100%2/2
$opts{"-severity"} ||= 1
l!l
32
MC/DC100%1/1
$opts{"-severity"} ||= 1
$opts{"-severity"}
Truth table100%2/2
A
A= $opts{"-severity"}
Aresult
11
00
95 5 ▶ $opts{-top} ||= $DEFAULT_VIOLATIONS_FOR_TOP;
Condition66%2/3
$opts{"-top"} ||= $DEFAULT_VIOLATIONS_FOR_TOP
l!l&&r!l&&!r
320
MC/DC0%0/2
$opts{"-top"} ||= $DEFAULT_VIOLATIONS_FOR_TOP
$opts{"-top"} $DEFAULT_VIOLATIONS_FOR_TOP
Truth table66%2/3
A ||= B
A= $opts{"-top"}B= $DEFAULT_VIOLATIONS_FOR_TOP
ABresult
1X1
011
000
96 }
97  
98 #Override profile, if --noprofile is specified
99 44 ▶ if ( exists $opts{-noprofile} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $opts{"-noprofile"}) 1 43 44
100 1 $opts{-profile} = $EMPTY;
101 }
102  
103 44 return %opts;
104 }
105  
106 #-----------------------------------------------------------------------------
107  
108 sub _parse_command_line {
109 52 ▶ my %opts;
Subroutine _parse_command_line: called CC=2 SCAR=7.0
110 52 my @opt_specs = _get_option_specification();
111 52 Getopt::Long::Configure('no_ignore_case');
112 52 ▶ GetOptions( \%opts, @opt_specs ) || pod2usage(); #Exits
Branch: 1 branch
BranchTrueFalseTotal
&GetOptions(\%opts, @opt_specs) or pod2usage() 0 52 52
113  
114 # I've adopted the convention of using key-value pairs for
115 # arguments to most functions. And to increase legibility,
116 # I have also adopted the familiar command-line practice
117 # of denoting argument names with a leading dash (-).
118 52 my %dashed_opts = map { ( "-$_" => $opts{$_} ) } keys %opts;
119 52 return %dashed_opts;
120 }
121  
122 #-----------------------------------------------------------------------------
123  
124 sub _dispatch_special_requests {
125 52 ▶ my (%opts) = @_;
Subroutine _dispatch_special_requests: called CC=10 SCAR=25.8
126 52 ▶ if ( $opts{-help} ) { pod2usage( -verbose => 0 ) } # Exits
Branch: 1 branch
BranchTrueFalseTotal
if ($opts{"-help"}) 1 51 52
127 51 ▶ if ( $opts{-options} ) { pod2usage( -verbose => 1 ) } # Exits
Branch: 1 branch
BranchTrueFalseTotal
if ($opts{"-options"}) 1 50 51
128 50 ▶ if ( $opts{-man} ) { pod2usage( -verbose => 2 ) } # Exits
Branch: 1 branch
BranchTrueFalseTotal
if ($opts{"-man"}) 1 49 50
129 49 ▶ if ( $opts{-version} ) { _display_version() } # Exits
Branch: 1 branch
BranchTrueFalseTotal
if ($opts{"-version"}) 0 49 49
130 49 ▶ if ( $opts{-list} ) { _render_all_policy_listing() } # Exits
Branch: 1 branch
BranchTrueFalseTotal
if ($opts{"-list"}) 0 49 49
131 49 ▶ if ( $opts{'-list-enabled'} ) { _render_policy_listing(%opts) } # Exits
Branch: 1 branch
BranchTrueFalseTotal
if ($opts{"-list-enabled"}) 0 49 49
132 49 ▶ if ( $opts{'-list-themes'} ) { _render_theme_listing() } # Exits
Branch: 1 branch
BranchTrueFalseTotal
if ($opts{"-list-themes"}) 0 49 49
133 49 ▶ if ( $opts{'-profile-proto'} ) { _render_profile_prototype() } # Exits
Branch: 1 branch
BranchTrueFalseTotal
if ($opts{"-profile-proto"}) 0 49 49
134 49 ▶ if ( $opts{-doc} ) { _render_policy_docs( %opts ) } # Exits
Branch: 1 branch
BranchTrueFalseTotal
if ($opts{"-doc"}) 0 49 49
135 49 return 1;
136 }
137  
138 #-----------------------------------------------------------------------------
139  
140 sub _validate_options {
141 49 ▶ my (%opts) = @_;
Subroutine _validate_options: called CC=11 SCAR=24.0
142 49 my $msg = $EMPTY;
143  
144  
145 49 ▶ if ( $opts{-noprofile} && $opts{-profile} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opts{"-noprofile"} and $opts{"-profile"}) 1 48 49
Condition100%3/3
$opts{"-noprofile"} and $opts{"-profile"}
!ll&&!rl&&r
4711
MC/DC100%2/2
$opts{"-noprofile"} and $opts{"-profile"}
$opts{"-noprofile"} $opts{"-profile"}
Truth table100%3/3
A and B
A= $opts{"-noprofile"}B= $opts{"-profile"}
ABresult
0X0
100
111
146 1 $msg .= qq{Warning: Cannot use -noprofile with -profile option.\n};
147 }
148  
149 49 ▶ if ( $opts{-verbose} && $opts{-verbose} !~ m{(?: \d+ | %[mfFlcCedrpPs] )}xms) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opts{"-verbose"} and not $opts{"-verbose"} =~ /(?: \d+ | %[mfFlcCedrpPs] )/msx) 1 48 49
Condition100%3/3
$opts{"-verbose"} and not $opts{"-verbose"} =~ /(?: \d+ | %[mfFlcCedrpPs] )/msx
!ll&&!rl&&r
4621
MC/DC100%2/2
$opts{"-verbose"} and not $opts{"-verbose"} =~ /(?: \d+ | %[mfFlcCedrpPs] )/msx
$opts{"-verbose"} not $opts{"-verbose"} =~ /(?: \d+ | %[mfFlcCedrpPs] )/msx
Truth table100%3/3
A and not(B)
A= $opts{"-verbose"}B= not $opts{"-verbose"} =~ /(?: \d+ | %[mfFlcCedrpPs] )/msx
ABresult
0X0
100
111
150 1 $msg .= qq<Warning: --verbose arg "$opts{-verbose}" looks odd. >;
151 1 $msg .= qq<Perhaps you meant to say "--verbose 3 $opts{-verbose}."\n>;
152 }
153  
154 49 ▶ if ( exists $opts{-top} && $opts{-top} < 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $opts{"-top"} and $opts{"-top"} < 0) 1 48 49
Condition100%3/3
exists $opts{"-top"} and $opts{"-top"} < 0
!ll&&!rl&&r
4351
MC/DC100%2/2
exists $opts{"-top"} and $opts{"-top"} < 0
exists $opts{"-top"} $opts{"-top"} < 0
Truth table100%3/3
A and B
A= exists $opts{"-top"}B= $opts{"-top"} < 0
ABresult
0X0
100
111
155 1 $msg .= qq<Warning: --top argument "$opts{-top}" is negative. >;
156 1 $msg .= qq<Perhaps you meant to say "$opts{-top} --top".\n>;
157 }
158  
159 49 ▶ if (
Branch: 1 branch
BranchTrueFalseTotal
if (exists $opts{"-severity"} and $opts{"-severity"} < $Perl::Critic::Command::SEVERITY_LOWEST || $opts{"-severity"} > $Perl::Critic::Command::SEVERITY_HIGHEST) 2 47 49
Condition100%6/6
exists $opts{"-severity"} and $opts{"-severity"} < $Perl::Critic::Command::SEVERITY_LOWEST || $opts{"-severity"} > $Perl::Critic::Command::SEVERITY_HIGHEST
!ll&&!rl&&r
3892
$opts{"-severity"} < $Perl::Critic::Command::SEVERITY_LOWEST or $opts{"-severity"} > $Perl::Critic::Command::SEVERITY_HIGHEST
l!l&&r!l&&!r
119
MC/DC66%2/3
exists $opts{"-severity"} and $opts{"-severity"} < $Perl::Critic::Command::SEVERITY_LOWEST || $opts{"-severity"} > $Perl::Critic::Command::SEVERITY_HIGHEST
exists $opts{"-severity"} $opts{"-severity"} < $Perl::Critic::Command::SEVERITY_LOWEST $opts{"-severity"} > $Perl::Critic::Command::SEVERITY_HIGHEST
Truth table75%3/4
A and B or C
A= exists $opts{"-severity"}B= $opts{"-severity"} < $Perl::Critic::Command::SEVERITY_LOWESTC= $opts{"-severity"} > $Perl::Critic::Command::SEVERITY_HIGHEST
ABCresult
0XX0
1000
11X1
1011
160 exists $opts{-severity}
161 && (
162 $opts{-severity} < $SEVERITY_LOWEST
163 || $opts{-severity} > $SEVERITY_HIGHEST
164 )
165 ) {
166 2 $msg .= qq<Warning: --severity arg "$opts{-severity}" out of range. >;
167 2 $msg .= qq<Severities range from "$SEVERITY_LOWEST" (lowest) to >;
168 2 $msg .= qq<"$SEVERITY_HIGHEST" (highest).\n>;
169 }
170  
171  
172 49 ▶ if ( $msg ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($msg) 5 44 49
173 5 pod2usage( -exitstatus => 1, -message => $msg, -verbose => 0); #Exits
174 }
175  
176  
177 44 return 1;
178 }
179  
180 #-----------------------------------------------------------------------------
181  
182 sub _get_input {
183  
184 0 ▶ my @args = @_;
Subroutine _get_input: not called CC=8 SCAR=42.8
185  
186 0 ▶ if ( !@args || (@args == 1 && $args[0] eq q{-}) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not @args or @args == 1 and $args[0] eq '-') { } 0 0 0
Condition0%0/6
not @args or @args == 1 and $args[0] eq "-"
l!l&&r!l&&!r
000
@args == 1 and $args[0] eq "-"
!ll&&!rl&&r
000
MC/DC0%0/3
not @args or @args == 1 and $args[0] eq "-"
not @args @args == 1 $args[0] eq "-"
Truth table0%0/4
not(A) or B and C
A= not @argsB= @args == 1C= $args[0] eq "-"
ABCresult
1XX1
0111
00X0
0100
187  
188 # Reading code from STDIN. All the code is slurped into
189 # a string. PPI will barf if the string is just whitespace.
190 0 my $code_string = do { local $RS = undef; <> };
191  
192 # Notice if STDIN was closed (pipe error, etc)
193 0 ▶ $code_string //= $EMPTY;
Condition0%0/3
$code_string //= $Perl::Critic::Command::EMPTY
l!l&&r!l&&!r
000
MC/DC0%0/2
$code_string //= $Perl::Critic::Command::EMPTY
$code_string $Perl::Critic::Command::EMPTY
Truth table0%0/3
A //= B
A= $code_stringB= $Perl::Critic::Command::EMPTY
ABresult
1X1
011
000
194  
195 0 ▶ $code_string =~ m{ \S+ }xms || die qq{Nothing to critique.\n};
Branch: 1 branch
BranchTrueFalseTotal
$code_string =~ / \S+ /msx or die "Nothing to critique.\n" 0 0 0
196 0 return \$code_string; #Convert to SCALAR ref for PPI
197 }
198 else {
199  
200 # Test to make sure all the specified files or directories
201 # actually exist. If any one of them is bogus, then die.
202 0 ▶ if ( my $nonexistent = first { ! -e } @args ) {
Subroutine __ANON__: not called CC=1 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
if (my $nonexistent = &first(sub { not -e $_; } , @args)) 0 0 0
203 0 my $msg = qq{No such file or directory: '$nonexistent'};
204 0 pod2usage( -exitstatus => 1, -message => $msg, -verbose => 0);
205 }
206  
207 # Reading code from files or dirs. If argument is a file,
208 # then we process it as-is (even though it may not actually
209 # be Perl code). If argument is a directory, recursively
210 # search the directory for files that look like Perl code.
211 0 ▶ return map { (-d) ? Perl::Critic::Utils::all_perl_files($_) : $_ } @args;
Branch: 1 branch
BranchTrueFalseTotal
-d $_ ? : 0 0 0
212 }
213 }
214  
215 #------------------------------------------------------------------------------
216  
217 sub _critique {
218  
219 0 ▶ my ( $opts_ref, @files_to_critique ) = @_;
Subroutine _critique: not called CC=10 SCAR=47.0
220 0 ▶ @files_to_critique || die "No perl files were found.\n";
Branch: 1 branch
BranchTrueFalseTotal
@files_to_critique or die "No perl files were found.\n" 0 0 0
221  
222 # Perl::Critic has lots of dependencies, so loading is delayed
223 # until it is really needed. This hack reduces startup time for
224 # doing other things like getting the version number or dumping
225 # the man page. Arguably, those things are pretty rare, but hey,
226 # why not save a few seconds if you can.
227  
228 0 require Perl::Critic;
229 0 $critic = Perl::Critic->new( %{$opts_ref} );
230 0 ▶ $critic->policies() || die "No policies selected.\n";
Branch: 1 branch
BranchTrueFalseTotal
$critic->policies or die "No policies selected.\n" 0 0 0
231  
232 0 _set_up_pager($critic->config()->pager());
233  
234 0 my $number_of_violations;
235 0 my $had_error_in_file = 0;
236  
237 0 for my $file (@files_to_critique) {
238  
239 eval {
240 0 my @violations = $critic->critique($file);
241 0 $number_of_violations += scalar @violations;
242  
243 0 ▶ if (not $opts_ref->{'-statistics-only'}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($opts_ref->{"-statistics-only"}) 0 0 0
244 0 _render_report( $file, $opts_ref, @violations );
245 }
246 0 1;
247 }
248 0 ▶ or do {
Branch: 1 branch
BranchTrueFalseTotal
eval { do { my(@violations) = $critic->critique($file); $number_of_violations += scalar @violations; unless ($opts_ref->{"-statistics-only"}) { _render_report($file, $opts_ref, @violations); } ; 1 } } or do { if ("Perl::Critic::Exception::Parse"->caught) { $had_error_in_file = 1; warn qq[Problem while critiquing "$file": $Perl::Critic::Command::EVAL_ERROR\n]; } elsif ($Perl::Critic::Command::EVAL_ERROR) { die qq[Fatal error while critiquing "$file": $Perl::Critic::Command::EVAL_ERROR\n]; } else { die qq[Fatal error while critiquing "$file". Unfortunately, ], "\$\@/\$EVAL_ERROR ", "is empty, so the reason can't be shown.\n"; } } 0 0 0
249 0 ▶ if ( Perl::Critic::Exception::Parse->caught() ) {
Branch: 2 branches
BranchTrueFalseTotal
if ('Perl::Critic::Exception::Parse'->caught) { } 0 0 0
elsif ($Perl::Critic::Command::EVAL_ERROR) { } 0 0 0
250 0 $had_error_in_file = 1;
251 0 warn qq<Problem while critiquing "$file": $EVAL_ERROR\n>;
252 }
253 elsif ($EVAL_ERROR) {
254 # P::C::Exception::Fatal includes the stack trace in its
255 # stringification.
256 0 die qq<Fatal error while critiquing "$file": $EVAL_ERROR\n>;
257 }
258 else {
259 0 die qq<Fatal error while critiquing "$file". Unfortunately, >,
260 q<$@/$EVAL_ERROR >, ## no critic (RequireInterpolationOfMetachars)
261 qq<is empty, so the reason can't be shown.\n>;
262 }
263 }
264 }
265  
266 0 ▶ if ( $opts_ref->{-statistics} or $opts_ref->{'-statistics-only'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opts_ref->{"-statistics"} or $opts_ref->{"-statistics-only"}) 0 0 0
Condition0%0/3
$opts_ref->{"-statistics"} or $opts_ref->{"-statistics-only"}
l!l&&r!l&&!r
000
MC/DC0%0/2
$opts_ref->{"-statistics"} or $opts_ref->{"-statistics-only"}
$opts_ref->{"-statistics"} $opts_ref->{"-statistics-only"}
Truth table0%0/3
A or B
A= $opts_ref->{"-statistics"}B= $opts_ref->{"-statistics-only"}
ABresult
1X1
011
000
267 0 my $stats = $critic->statistics();
268 0 _report_statistics( $opts_ref, $stats );
269 }
270  
271 0 return $number_of_violations, $had_error_in_file;
272 }
273  
274 #------------------------------------------------------------------------------
275  
276 sub _render_report {
277 0 ▶ my ( $file, $opts_ref, @violations ) = @_;
Subroutine _render_report: not called CC=15 SCAR=54.8
278  
279 # Only report the files, if asked.
280 0 my $number_of_violations = @violations;
281 0 ▶ if ( $opts_ref->{'-files-with-violations'} ||
Branch: 1 branch
BranchTrueFalseTotal
if ($opts_ref->{"-files-with-violations"} or $opts_ref->{"-files-without-violations"}) 0 0 0
Condition0%0/3
$opts_ref->{"-files-with-violations"} or $opts_ref->{"-files-without-violations"}
l!l&&r!l&&!r
000
MC/DC0%0/2
$opts_ref->{"-files-with-violations"} or $opts_ref->{"-files-without-violations"}
$opts_ref->{"-files-with-violations"} $opts_ref->{"-files-without-violations"}
Truth table0%0/3
A or B
A= $opts_ref->{"-files-with-violations"}B= $opts_ref->{"-files-without-violations"}
ABresult
1X1
011
000
282 $opts_ref->{'-files-without-violations'} ) {
283 not ref $file
284 0 ▶ and $opts_ref->{$number_of_violations ? '-files-with-violations' :
Branch: 2 branches
BranchTrueFalseTotal
not ref $file and $$opts_ref{$number_of_violations ? "-files-with-violations" : "-files-without-violations"} and _out("$file\n") 0 0 0
$number_of_violations ? : 0 0 0
Condition0%0/3
not ref $file and $$opts_ref{$number_of_violations ? "-files-with-violations" : "-files-without-violations"}
!ll&&!rl&&r
000
MC/DC0%0/2
not ref $file and $$opts_ref{$number_of_violations ? "-files-with-violations" : "-files-without-violations"}
not ref $file $$opts_ref{$number_of_violations ? "-files-with-violations" : "-files-without-violations"}
Truth table0%0/3
not(A) and B
A= not ref $fileB= $$opts_ref{$number_of_violations ? "-files-with-violations" : "-files-without-violations"}
ABresult
0X0
100
111
285 '-files-without-violations'}
286 and _out "$file\n";
287 0 return $number_of_violations;
288 }
289  
290 # Only report the number of violations, if asked.
291 0 ▶ if( $opts_ref->{-count} ){
Branch: 1 branch
BranchTrueFalseTotal
if ($opts_ref->{"-count"}) 0 0 0
292 0 ▶ ref $file || _out "$file: ";
Branch: 1 branch
BranchTrueFalseTotal
ref $file or _out("${file}: ") 0 0 0
293 0 _out "$number_of_violations\n";
294 0 return $number_of_violations;
295 }
296  
297 # Hail all-clear unless we should shut up.
298 0 ▶ if( !@violations && !$opts_ref->{-quiet} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@violations or $opts_ref->{"-quiet"}) 0 0 0
Condition0%0/3
@violations or $opts_ref->{"-quiet"}
l!l&&r!l&&!r
000
MC/DC0%0/2
@violations or $opts_ref->{"-quiet"}
@violations $opts_ref->{"-quiet"}
Truth table0%0/3
A or B
A= @violationsB= $opts_ref->{"-quiet"}
ABresult
1X1
011
000
299 0 ▶ ref $file || _out "$file ";
Branch: 1 branch
BranchTrueFalseTotal
ref $file or _out("$file ") 0 0 0
300 0 _out "source OK\n";
301 0 return 0;
302 }
303  
304 # Otherwise, format and print violations
305 0 my $verbosity = $critic->config->verbose();
306 # $verbosity can be numeric or string, so use "eq" for comparison;
307 0 ▶ $verbosity =
Branch: 1 branch
BranchTrueFalseTotal
$verbosity eq $Perl::Critic::Command::DEFAULT_VERBOSITY && @files > 1 ? : 0 0 0
Condition0%0/3
$verbosity eq $Perl::Critic::Command::DEFAULT_VERBOSITY and @files > 1
!ll&&!rl&&r
000
MC/DC0%0/2
$verbosity eq $Perl::Critic::Command::DEFAULT_VERBOSITY and @files > 1
$verbosity eq $Perl::Critic::Command::DEFAULT_VERBOSITY @files > 1
Truth table0%0/3
A and B
A= $verbosity eq $Perl::Critic::Command::DEFAULT_VERBOSITYB= @files > 1
ABresult
0X0
100
111
308 ($verbosity eq $DEFAULT_VERBOSITY && @files > 1)
309 ? $DEFAULT_VERBOSITY_WITH_FILE_NAME
310 : $verbosity;
311 0 my $fmt = Perl::Critic::Utils::verbosity_to_format( $verbosity );
312 0 ▶ if (not -f $file) { $fmt =~ s< \%[fF] ><STDIN>xms; } #HACK!
Branch: 1 branch
BranchTrueFalseTotal
unless (-f $file) 0 0 0
313 0 Perl::Critic::Violation::set_format( $fmt );
314  
315 0 my $color = $critic->config->color();
316 0 ▶ _out $color ? _colorize_by_severity(@violations) : @violations;
Branch: 1 branch
BranchTrueFalseTotal
$color ? : 0 0 0
317  
318 0 return $number_of_violations;
319 }
320  
321 #-----------------------------------------------------------------------------
322  
323 sub _set_up_pager {
324 0 ▶ my ($pager_command) = @_;
Subroutine _set_up_pager: not called CC=4 SCAR=30.0
325 0 ▶ return if not $pager_command;
Branch: 1 branch
BranchTrueFalseTotal
unless $pager_command 0 0 0
326 0 ▶ return if not _at_tty();
Branch: 1 branch
BranchTrueFalseTotal
unless _at_tty() 0 0 0
327  
328 0 ▶ open my $pager, q<|->, $pager_command ## no critic (InputOutput::RequireBriefOpen)
Branch: 1 branch
BranchTrueFalseTotal
open my $pager, "|-", $pager_command or die qq[Unable to pipe to pager "$pager_command": $Perl::Critic::Command::ERRNO\n] 0 0 0
329 or die qq<Unable to pipe to pager "$pager_command": $ERRNO\n>;
330  
331 0 $output = $pager;
332  
333 0 return;
334 }
335  
336 #-----------------------------------------------------------------------------
337  
338 sub _report_statistics {
339 0 ▶ my ($opts_ref, $statistics) = @_;
Subroutine _report_statistics: not called CC=12 SCAR=50.5
340  
341 0 ▶ if (
Branch: 1 branch
BranchTrueFalseTotal
if (not $opts_ref->{"-statistics-only"} and $statistics->total_violations || !$opts_ref->{"-quiet"} && $statistics->modules) 0 0 0
Condition0%0/9
not $opts_ref->{"-statistics-only"} and $statistics->total_violations || !$opts_ref->{"-quiet"} && $statistics->modules
!ll&&!rl&&r
000
$statistics->total_violations or not $opts_ref->{"-quiet"} and $statistics->modules
l!l&&r!l&&!r
000
not $opts_ref->{"-quiet"} and $statistics->modules
!ll&&!rl&&r
000
MC/DC0%0/4
not $opts_ref->{"-statistics-only"} and $statistics->total_violations || !$opts_ref->{"-quiet"} && $statistics->modules
not $opts_ref->{"-statistics-only"} $statistics->total_violations not $opts_ref->{"-quiet"} $statistics->modules
Truth table0%0/5
not(A) and B or not(C) and D
A= not $opts_ref->{"-statistics-only"}B= $statistics->total_violationsC= not $opts_ref->{"-quiet"}D= $statistics->modules
ABCDresult
0XXX0
100X0
10100
11XX1
10111
342 not $opts_ref->{'-statistics-only'}
343 and (
344 $statistics->total_violations()
345 or not $opts_ref->{-quiet} and $statistics->modules()
346 )
347 ) {
348 0 _out "\n"; # There's prior output that we want to separate from.
349 }
350  
351 0 my $files = _commaify($statistics->modules());
352 0 my $subroutines = _commaify($statistics->subs());
353 0 my $statements = _commaify($statistics->statements_other_than_subs());
354 0 my $lines = _commaify($statistics->lines());
355 0 my $width = max map { length } $files, $subroutines, $statements;
356  
357 0 _out sprintf "%*s %s.\n", $width, $files, 'files';
358 0 _out sprintf "%*s %s.\n", $width, $subroutines, 'subroutines/methods';
359 0 _out sprintf "%*s %s.\n", $width, $statements, 'statements';
360  
361 0 my $lines_of_blank = _commaify( $statistics->lines_of_blank() );
362 0 my $lines_of_comment = _commaify( $statistics->lines_of_comment() );
363 0 my $lines_of_data = _commaify( $statistics->lines_of_data() );
364 0 my $lines_of_perl = _commaify( $statistics->lines_of_perl() );
365 0 my $lines_of_pod = _commaify( $statistics->lines_of_pod() );
366  
367 $width =
368 0 max map { length }
369 $lines_of_blank, $lines_of_comment, $lines_of_data,
370 $lines_of_perl, $lines_of_pod;
371 0 _out sprintf "\n%s %s:\n", $lines, 'lines, consisting of';
372 0 _out sprintf " %*s %s.\n", $width, $lines_of_blank, 'blank lines';
373 0 _out sprintf " %*s %s.\n", $width, $lines_of_comment, 'comment lines';
374 0 _out sprintf " %*s %s.\n", $width, $lines_of_data, 'data lines';
375 0 _out sprintf " %*s %s.\n", $width, $lines_of_perl, 'lines of Perl code';
376 0 _out sprintf " %*s %s.\n", $width, $lines_of_pod, 'lines of POD';
377  
378 0 my $average_sub_mccabe = $statistics->average_sub_mccabe();
379 0 ▶ if (defined $average_sub_mccabe) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $average_sub_mccabe) 0 0 0
380 0 _out
381 sprintf
382 "\nAverage McCabe score of subroutines was %.2f.\n",
383 $average_sub_mccabe;
384 }
385  
386 0 _out "\n";
387  
388 0 _out _commaify($statistics->total_violations()), " violations.\n";
389  
390 0 my $violations_per_file = $statistics->violations_per_file();
391 0 ▶ if (defined $violations_per_file) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $violations_per_file) 0 0 0
392 0 _out
393 sprintf
394 "Violations per file was %.3f.\n",
395 $violations_per_file;
396 }
397 0 my $violations_per_statement = $statistics->violations_per_statement();
398 0 ▶ if (defined $violations_per_statement) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $violations_per_statement) 0 0 0
399 0 _out
400 sprintf
401 "Violations per statement was %.3f.\n",
402 $violations_per_statement;
403 }
404 0 my $violations_per_line = $statistics->violations_per_line_of_code();
405 0 ▶ if (defined $violations_per_line) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $violations_per_line) 0 0 0
406 0 _out
407 sprintf
408 "Violations per line of code was %.3f.\n",
409 $violations_per_line;
410 }
411  
412 0 ▶ if ( $statistics->total_violations() ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($statistics->total_violations) 0 0 0
413 0 _out "\n";
414  
415 0 my %severity_violations = %{ $statistics->violations_by_severity() };
416 0 my @severities = reverse sort keys %severity_violations;
417 $width =
418 max
419 0 map { length _commaify( $severity_violations{$_} ) }
420 @severities;
421 0 foreach my $severity (@severities) {
422 _out
423 sprintf
424 "%*s severity %d violations.\n",
425 $width,
426 0 _commaify( $severity_violations{$severity} ),
427 $severity;
428 }
429  
430 0 _out "\n";
431  
432 0 my %policy_violations = %{ $statistics->violations_by_policy() };
433 0 my @policies = sort keys %policy_violations;
434 $width =
435 max
436 0 map { length _commaify( $policy_violations{$_} ) }
437 @policies;
438 0 foreach my $policy (@policies) {
439 _out
440 sprintf
441 "%*s violations of %s.\n",
442 $width,
443 0 _commaify($policy_violations{$policy}),
444 policy_short_name($policy);
445 }
446 }
447  
448 0 return;
449 }
450  
451 #-----------------------------------------------------------------------------
452  
453 # Only works for integers.
454 sub _commaify {
455 0 ▶ my ( $number ) = @_;
Subroutine _commaify: not called CC=2 SCAR=17.9
456  
457 0 ▶ while ($number =~ s/ \A ( [-+]? \d+ ) ( \d{3} ) /$1,$2/xms) {
Branch: 1 branch
BranchTrueFalseTotal
if ($number =~ s/ \A ( [-+]? \d+ ) ( \d{3} ) /$1,$2/msx) 0 0 0
458 # nothing
459 }
460  
461 0 return $number;
462 }
463  
464 #-----------------------------------------------------------------------------
465  
466 sub _get_option_specification {
467  
468 52 ▶ return qw<
Subroutine _get_option_specification: called CC=1 SCAR=0.0
469 5 4 3 2 1
470 version
471 brutal
472 count|C
473 cruel
474 doc=s
475 exclude=s@
476 force!
477 gentle
478 harsh
479 help|?|H
480 include=s@
481 list
482 list-enabled
483 list-themes
484 man
485 color|colour!
486 noprofile
487 only!
488 options
489 pager=s
490 profile|p=s
491 profile-proto
492 quiet
493 severity=i
494 single-policy|s=s
495 stern
496 statistics!
497 statistics-only!
498 profile-strictness=s
499 theme=s
500 top:i
501 allow-unsafe
502 verbose=s
503 color-severity-highest|colour-severity-highest|color-severity-5|colour-severity-5=s
504 color-severity-high|colour-severity-high|color-severity-4|colour-severity-4=s
505 color-severity-medium|colour-severity-medium|color-severity-3|colour-severity-3=s
506 color-severity-low|colour-severity-low|color-severity-2|colour-severity-2=s
507 color-severity-lowest|colour-severity-lowest|color-severity-1|colour-severity-1=s
508 files-with-violations|l
509 files-without-violations|L
510 program-extensions=s@
511 >;
512 }
513  
514 #-----------------------------------------------------------------------------
515  
516 sub _colorize_by_severity {
517 0 ▶ my @violations = @_;
Subroutine _colorize_by_severity: not called CC=4 SCAR=30.0
518 0 ▶ return @violations if _this_is_windows() && !eval 'require Win32::Console::ANSI; 1';
Branch: 1 branch
BranchTrueFalseTotal
if _this_is_windows() and not eval "require Win32::Console::ANSI; 1" 0 0 0
Condition0%0/3
_this_is_windows() and not eval "require Win32::Console::ANSI; 1"
!ll&&!rl&&r
000
MC/DC0%0/2
_this_is_windows() and not eval "require Win32::Console::ANSI; 1"
_this_is_windows() not eval "require Win32::Console::ANSI; 1"
Truth table0%0/3
A and not(B)
A= _this_is_windows()B= not eval "require Win32::Console::ANSI; 1"
ABresult
0X0
100
111
519 0 ▶ return @violations if not eval {
Branch: 1 branch
BranchTrueFalseTotal
unless eval { do { require Term::ANSIColor; "Term::ANSIColor"->VERSION($Perl::Critic::Command::_MODULE_VERSION_TERM_ANSICOLOR); 1 } } 0 0 0
520 0 require Term::ANSIColor;
521 0 Term::ANSIColor->VERSION( $_MODULE_VERSION_TERM_ANSICOLOR );
522 0 1;
523 };
524  
525 0 my $config = $critic->config();
526 0 my %color_of = (
527 $SEVERITY_HIGHEST => $config->color_severity_highest(),
528 $SEVERITY_HIGH => $config->color_severity_high(),
529 $SEVERITY_MEDIUM => $config->color_severity_medium(),
530 $SEVERITY_LOW => $config->color_severity_low(),
531 $SEVERITY_LOWEST => $config->color_severity_lowest(),
532 );
533  
534 0 return map { _colorize( "$_", $color_of{$_->severity()} ) } @violations;
535  
536 }
537  
538 #-----------------------------------------------------------------------------
539  
540 sub _colorize {
541 0 ▶ my ($string, $color) = @_;
Subroutine _colorize: not called CC=4 SCAR=30.0
542 0 ▶ return $string if not defined $color;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $color 0 0 0
543 0 ▶ return $string if $color eq $EMPTY;
Branch: 1 branch
BranchTrueFalseTotal
if $color eq $Perl::Critic::Command::EMPTY 0 0 0
544 # $terminator is a purely cosmetic change to make the color end at the end
545 # of the line rather than right before the next line. It is here because
546 # if you use background colors, some console windows display a little
547 # fragment of colored background before the next uncolored (or
548 # differently-colored) line.
549 0 ▶ my $terminator = chomp $string ? "\n" : $EMPTY;
Branch: 1 branch
BranchTrueFalseTotal
chomp $string ? : 0 0 0
550 0 return Term::ANSIColor::colored( $string, $color ) . $terminator;
551 }
552  
553 #-----------------------------------------------------------------------------
554  
555 sub _this_is_windows {
556 0 ▶ return 1 if $OSNAME =~ m/MSWin32/xms;
Subroutine _this_is_windows: not called CC=2 SCAR=17.9
Branch: 1 branch
BranchTrueFalseTotal
if $Perl::Critic::Command::OSNAME =~ /MSWin32/msx 0 0 0
557 0 return 0;
558 }
559  
560 #-----------------------------------------------------------------------------
561  
562 sub _at_tty {
563 0 ▶ return -t STDOUT; ## no critic (ProhibitInteractiveTest);
Subroutine _at_tty: not called CC=1 SCAR=6.9
564 }
565  
566 #-----------------------------------------------------------------------------
567  
568 sub _render_all_policy_listing {
569 # Force P-C parameters, to catch all Policies on this site
570 0 ▶ my %pc_params = (-profile => $EMPTY, -severity => $SEVERITY_LOWEST);
Subroutine _render_all_policy_listing: not called CC=1 SCAR=6.9
571 0 return _render_policy_listing( %pc_params );
572 }
573  
574 #-----------------------------------------------------------------------------
575  
576 sub _render_policy_listing {
577 0 ▶ my %pc_params = @_;
Subroutine _render_policy_listing: not called CC=1 SCAR=6.9
578  
579 0 require Perl::Critic::PolicyListing;
580 0 require Perl::Critic;
581  
582 0 my @policies = Perl::Critic->new( %pc_params )->policies();
583 0 my $listing = Perl::Critic::PolicyListing->new( -policies => \@policies );
584 0 _out $listing;
585  
586 0 exit $EXIT_SUCCESS;
587 }
588  
589 #-----------------------------------------------------------------------------
590  
591 sub _render_theme_listing {
592  
593 0 ▶ require Perl::Critic::ThemeListing;
Subroutine _render_theme_listing: not called CC=1 SCAR=6.9
594 0 require Perl::Critic;
595  
596 0 my %pc_params = (-profile => $EMPTY, -severity => $SEVERITY_LOWEST);
597 0 my @policies = Perl::Critic->new( %pc_params )->policies();
598 0 my $listing = Perl::Critic::ThemeListing->new( -policies => \@policies );
599 0 _out $listing;
600  
601 0 exit $EXIT_SUCCESS;
602 }
603  
604 #-----------------------------------------------------------------------------
605  
606 sub _render_profile_prototype {
607  
608 0 ▶ require Perl::Critic::ProfilePrototype;
Subroutine _render_profile_prototype: not called CC=1 SCAR=6.9
609 0 require Perl::Critic;
610  
611 0 my %pc_params = (-profile => $EMPTY, -severity => $SEVERITY_LOWEST);
612 0 my @policies = Perl::Critic->new( %pc_params )->policies();
613 0 my $prototype = Perl::Critic::ProfilePrototype->new( -policies => \@policies );
614 0 _out $prototype;
615  
616 0 exit $EXIT_SUCCESS;
617 }
618  
619 #-----------------------------------------------------------------------------
620  
621 sub _render_policy_docs {
622  
623 0 ▶ my (%opts) = @_;
Subroutine _render_policy_docs: not called CC=1 SCAR=6.9
624 0 my $pattern = delete $opts{-doc};
625  
626 0 require Perl::Critic;
627 0 $critic = Perl::Critic->new(%opts);
628 0 _set_up_pager($critic->config()->pager());
629  
630 0 require Perl::Critic::PolicyFactory;
631 0 my @site_policies = Perl::Critic::PolicyFactory->site_policy_names();
632 0 my @matching_policies = grep { /$pattern/ixms } @site_policies;
633  
634 # "-T" means don't send to pager
635 0 my @perldoc_output = map {`perldoc -T $_`} @matching_policies; ## no critic (ProhibitBacktick)
636 0 _out @perldoc_output;
637  
638 0 exit $EXIT_SUCCESS;
639 }
640  
641 #-----------------------------------------------------------------------------
642  
643 sub _display_version {
644 0 ▶ _out "$VERSION\n";
Subroutine _display_version: not called CC=1 SCAR=6.9
645 0 exit $EXIT_SUCCESS;
646 }
647  
648 #-----------------------------------------------------------------------------
649 3 1;
650  
651 __END__
652  
653 #-----------------------------------------------------------------------------
654  
655 =pod
656  
657 =for stopwords
658 Twitter
659  
660 =head1 NAME
661  
662 Perl::Critic::Command - Guts of L<perlcritic|perlcritic>.
663  
664  
665 =head1 SYNOPSIS
666  
667 use Perl::Critic::Command qw< run >;
668  
669 local @ARGV = qw< --statistics-only lib bin >;
670 run();
671  
672  
673 =head1 DESCRIPTION
674  
675 This is the implementation of the L<perlcritic|perlcritic> command. You can use
676 this to run the command without going through a command interpreter.
677  
678  
679 =head1 INTERFACE SUPPORT
680  
681 This is considered to be a public class. However, its interface is
682 experimental, and will likely change.
683  
684  
685 =head1 IMPORTABLE SUBROUTINES
686  
687 =over
688  
689 =item C<run()>
690  
691 Does the equivalent of the L<perlcritic|perlcritic> command. Unfortunately, at
692 present, this doesn't take any parameters but uses C<@ARGV> to get its
693 input instead. Count on this changing; don't count on the current
694 interface.
695  
696  
697 =back
698  
699  
700 =head1 TO DO
701  
702 Make C<run()> take parameters. The equivalent of C<@ARGV> should be
703 passed as a reference.
704  
705 Turn this into an object.
706  
707  
708 =head1 AUTHOR
709  
710 Jeffrey Ryan Thalhammer <jeff@imaginative-software.com>
711  
712  
713 =head1 COPYRIGHT
714  
715 Copyright (c) 2005-2023 Imaginative Software Systems. All rights reserved.
716  
717 This program is free software; you can redistribute it and/or modify
718 it under the same terms as Perl itself. The full text of this license
719 can be found in the LICENSE file included with this module.
720  
721 =cut
722  
723 ##############################################################################
724 # Local Variables:
725 # mode: cperl
726 # cperl-indent-level: 4
727 # fill-column: 78
728 # indent-tabs-mode: nil
729 # c-indentation-style: bsd
730 # End:
731 # ex: set ts=8 sts=4 sw=4 tw=78 ft=perl expandtab shiftround :
« blib/lib/Perl/Critic/Policy/Variables/RequireNegativeIndices.pm ↑ Summary blib/lib/Perl/Critic/Policy/ErrorHandling/RequireCarping.pm »