Critique/Command/collect.pm

filter: Statementstmt 15.0% 21 / 140 Branchbran 0.0% 0 / 56 Conditioncond 0.0% 0 / 60 MC/DCmcdc 0.0% 0 / 31 Subroutinesub 33.3% 6 / 18 Podpod 40.0% 2 / 5 totaltotal 9.3% 29 / 310 CC 52 SCAR 53.6
CC
52
Cov
8%
generate_file_predicate
506.0
is_perl_file
182.0
traverse_filesystem
90.0
CRAP 212.3
« Critique/Command/remove.pm ↑ Summary Critique/Command/process.pm »
1 package App::Critique::Command::collect;
2  
3 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 1 our $VERSION = '0.03';
7 1 our $AUTHORITY = 'cpan:STEVAN';
8  
9 1 ▶use Path::Tiny ();
Subroutine BEGIN: called CC=1 SCAR=0.0
10 1 ▶use Term::ANSIColor ':constants';
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 1 ▶use App::Critique::Session;
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 1 ▶use App::Critique -command;
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 sub opt_spec {
17 0 ▶ my ($class) = @_;
Subroutine opt_spec: not called CC=1 SCAR=6.9
Pod documented
18 return (
19 0 [ 'root=s', 'directory to start traversal from (default is root of git work tree)' ],
20 [],
21 [ 'no-violation', 'filter files that contain no Perl::Critic violations ' ],
22 [],
23 [ 'filter|f=s', 'filter files to remove with this regular expression' ],
24 [ 'match|m=s', 'match files to keep with this regular expression' ],
25 [],
26 [ 'dry-run', 'display list of files, but do not store them' ],
27 [],
28 $class->SUPER::opt_spec,
29 );
30 }
31  
32 sub execute {
33 0 ▶ my ($self, $opt, $args) = @_;
Subroutine execute: not called CC=6 SCAR=37.4
Pod documented
34  
35 0 local $Term::ANSIColor::AUTORESET = 1;
36  
37 0 my $session = $self->cautiously_load_session( $opt, $args );
38  
39 0 info('Session file loaded.');
40  
41 0 ▶ my $root = $opt->root
Branch: 1 branch
BranchTrueFalseTotal
$opt->root ? : 0 0 0
42 ? Path::Tiny::path( $opt->root )
43 : $session->git_work_tree;
44  
45 0 my @all;
46 0 traverse_filesystem(
47 root => $session->git_work_tree,
48 path => $root,
49 predicate => generate_file_predicate(
50 $session => (
51 filter => $opt->filter,
52 match => $opt->match,
53 no_violation => $opt->no_violation
54 )
55 ),
56 accumulator => \@all,
57 verbose => $opt->verbose,
58 );
59  
60 0 my $num_files = scalar @all;
61 0 info('Collected %d perl file(s) for critique.', $num_files);
62  
63 0 foreach my $file ( @all ) {
64 0 info(
65 ITALIC('Including %s'),
66 Path::Tiny::path( $file )->relative( $root )
67 );
68 }
69  
70 0 ▶ if ( $opt->verbose && $opt->no_violation ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opt->verbose and $opt->no_violation) 0 0 0
Condition0%0/3
$opt->verbose and $opt->no_violation
!ll&&!rl&&r
000
MC/DC0%0/2
$opt->verbose and $opt->no_violation
$opt->verbose $opt->no_violation
Truth table0%0/3
A and B
A= $opt->verboseB= $opt->no_violation
ABresult
0X0
100
111
71 0 my $stats = $session->perl_critic->statistics;
72 0 info(HR_DARK);
73 0 info('STATISTICS(Perl::Critic)');
74 0 info(HR_LIGHT);
75 0 info(BOLD(' VIOLATIONS : %s'), format_number($stats->total_violations));
76 0 info('== PERL '.('=' x (TERM_WIDTH() - 8)));
77 0 info(' modules : %s', format_number($stats->modules));
78 0 info(' subs : %s', format_number($stats->subs));
79 0 info(' statements : %s', format_number($stats->statements));
80 0 info('== LINES '.('=' x (TERM_WIDTH() - 9)));
81 0 info(BOLD('TOTAL : %s'), format_number($stats->lines));
82 0 info(' perl : %s', format_number($stats->lines_of_perl));
83 0 info(' pod : %s', format_number($stats->lines_of_pod));
84 0 info(' comments : %s', format_number($stats->lines_of_comment));
85 0 info(' data : %s', format_number($stats->lines_of_data));
86 0 info(' blank : %s', format_number($stats->lines_of_blank));
87 0 info(HR_DARK);
88 }
89  
90 0 ▶ if ( $opt->dry_run ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opt->dry_run) { } 0 0 0
91 0 info('[dry run] %s file(s) found, 0 files added.', format_number($num_files));
92 }
93 else {
94 0 $session->set_tracked_files( @all );
95 0 $session->reset_file_idx;
96 0 info('Sucessfully added %s file(s).', format_number($num_files));
97  
98 0 $self->cautiously_store_session( $session, $opt, $args );
99 0 info('Session file stored successfully (%s).', $session->session_file_path);
100 }
101 }
102  
103 sub traverse_filesystem {
104 0 ▶ my %args = @_;
Subroutine traverse_filesystem: not called CC=9 SCAR=45.0
Pod undocumented
105 0 my $root = $args{root};
106 0 my $path = $args{path};
107 0 my $predicate = $args{predicate};
108 0 my $acc = $args{accumulator};
109 0 my $verbose = $args{verbose};
110  
111 0 ▶ if ( $path->is_file ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($path->is_file) { } 0 0 0
elsif (-l $path) { } 0 0 0
112  
113 #warn "GOT A FILE: $path";
114  
115 # ignore anything but perl files ...
116 0 ▶ return unless is_perl_file( $path->stringify );
Branch: 1 branch
BranchTrueFalseTotal
unless is_perl_file($path->stringify) 0 0 0
117  
118 #warn "NOT PERL FILE: $path";
119  
120 # only accept things that match the path
121 0 ▶ if ( $predicate->( $root, $path ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (&$predicate($root, $path)) { } 0 0 0
122 0 ▶ info(BOLD('Matched: keeping file (%s)'), $path->relative( $root )) if $verbose;
Branch: 1 branch
BranchTrueFalseTotal
if $verbose 0 0 0
123 0 push @$acc => $path;
124 }
125 else {
126 0 ▶ info('Not Matched: skipping file (%s)', $path->relative( $root )) if $verbose;
Branch: 1 branch
BranchTrueFalseTotal
if $verbose 0 0 0
127 }
128 }
129 elsif ( -l $path ) { # Path::Tiny does not have a test for symlinks
130 ;
131 }
132 else {
133  
134 #warn "GOT A DIR: $path";
135  
136 0 my @children = $path->children( qr/^[^.]/ );
137  
138 # prune the directories we really don't care about
139 0 ▶ if ( my $ignore = $App::Critique::CONFIG{'IGNORE'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $ignore = $App::Critique::CONFIG{"IGNORE"}) 0 0 0
140 0 @children = grep !$ignore->{ $_->basename }, @children;
141 }
142  
143 # recurse ...
144 0 foreach my $child ( @children ) {
145 0 traverse_filesystem(
146 root => $root,
147 path => $child,
148 predicate => $predicate,
149 accumulator => $acc,
150 verbose => $verbose,
151 );
152 }
153 }
154  
155 0 return;
156 }
157  
158 sub generate_file_predicate {
159 0 ▶ my ($session, %args) = @_;
Subroutine generate_file_predicate: not called CC=22 SCAR=62.3
Pod undocumented
160  
161 0 my $filter = $args{filter};
162 0 my $match = $args{match};
163 0 my $no_violation = $args{no_violation};
164  
165 0 my $predicate;
166  
167 # ------------------------------#
168 # filter | match | no-violation #
169 # ------------------------------#
170 # 0 | 0 | 0 # collect
171 # ------------------------------#
172 # 1 | 0 | 0 # collect with filter
173 # 1 | 1 | 0 # collect with filter and match
174 # 1 | 1 | 1 # collect with filter match and no violations
175 # 1 | 0 | 1 # collect with filter and no violations
176 # ------------------------------#
177 # 0 | 1 | 0 # collect with match
178 # 0 | 1 | 1 # collect with match and no violations
179 # ------------------------------#
180 # 0 | 0 | 1 # collect with no violations
181 # ------------------------------#
182  
183 # filter only
184 0 ▶ if ( $filter && not($match) && not($no_violation) ) {
Branch: 7 branches
BranchTrueFalseTotal
if ($filter and not $match and not $no_violation) { } 0 0 0
elsif ($filter and $match and not $no_violation) { } 0 0 0
elsif ($filter and $match and $no_violation) { } 0 0 0
elsif ($filter and not $match and $no_violation) { } 0 0 0
elsif (not $filter and $match and not $no_violation) { } 0 0 0
elsif (not $filter and $match and $no_violation) { } 0 0 0
elsif (not $filter || $match and $no_violation) { } 0 0 0
Condition0%0/6
$filter and not $match and not $no_violation
!ll&&!rl&&r
000
$filter and not $match
!ll&&!rl&&r
000
MC/DC0%0/3
$filter and not $match and not $no_violation
$filter not $match not $no_violation
Truth table0%0/4
A and not(B) and not(C)
A= $filterB= not $matchC= not $no_violation
ABCresult
0XX0
10X0
1100
1111
185 $predicate = sub {
186 0 ▶ my $root = $_[0];
Subroutine __ANON__: not called CC=1 SCAR=6.9
187 0 my $path = $_[1];
188 0 my $rel = $path->relative( $root );
189 0 return $rel !~ /$filter/;
190 0 };
191 }
192 # filter and match
193 ▶ elsif ( $filter && $match && not($no_violation) ) {
Condition0%0/6
$filter and $match and not $no_violation
!ll&&!rl&&r
000
$filter and $match
!ll&&!rl&&r
000
MC/DC0%0/3
$filter and $match and not $no_violation
$filter $match not $no_violation
Truth table0%0/4
A and B and not(C)
A= $filterB= $matchC= not $no_violation
ABCresult
0XX0
10X0
1100
1111
194 $predicate = sub {
195 0 ▶ my $root = $_[0];
Subroutine __ANON__: not called CC=2 SCAR=17.9
196 0 my $path = $_[1];
197 0 my $rel = $path->relative( $root );
198 0 ▶ return $rel =~ /$match/
Condition0%0/3
$rel =~ /$match/ && !($rel =~ /$filter/)
!ll&&!rl&&r
000
MC/DC0%0/2
$rel =~ /$match/ && !($rel =~ /$filter/)
$rel =~ /$match/ !($rel =~ /$filter/)
Truth table0%0/3
A && not(B)
A= $rel =~ /$match/B= !($rel =~ /$filter/)
ABresult
0X0
100
111
199 && $rel !~ /$filter/;
200 0 };
201 }
202 # filter and match and check violations
203 ▶ elsif ( $filter && $match && $no_violation ) {
Condition0%0/6
$filter and $match and $no_violation
!ll&&!rl&&r
000
$filter and $match
!ll&&!rl&&r
000
MC/DC0%0/3
$filter and $match and $no_violation
$filter $match $no_violation
Truth table0%0/4
A and B and C
A= $filterB= $matchC= $no_violation
ABCresult
0XX0
10X0
1100
1111
204 0 my $c = $session->perl_critic;
205 $predicate = sub {
206 0 ▶ my $root = $_[0];
Subroutine __ANON__: not called CC=3 SCAR=24.8
207 0 my $path = $_[1];
208 0 my $rel = $path->relative( $root );
209 0 ▶ return $rel =~ /$match/
Condition0%0/5
$rel =~ /$match/ && !($rel =~ /$filter/) && scalar $c->critique($path->stringify)
!ll
00
$rel =~ /$match/ and not $rel =~ /$filter/
!ll&&!rl&&r
000
MC/DC0%0/2
$rel =~ /$match/ && !($rel =~ /$filter/) && scalar $c->critique($path->stringify)
$rel =~ /$match/ not $rel =~ /$filter/
Truth table0%0/3
A and not(B)
A= $rel =~ /$match/B= not $rel =~ /$filter/
ABresult
0X0
100
111
210 && $rel !~ /$filter/
211 && scalar $c->critique( $path->stringify );
212 0 };
213 }
214 # filter and check violations
215 ▶ elsif ( $filter && not($match) && $no_violation ) {
Condition0%0/6
$filter and not $match and $no_violation
!ll&&!rl&&r
000
$filter and not $match
!ll&&!rl&&r
000
MC/DC0%0/3
$filter and not $match and $no_violation
$filter not $match $no_violation
Truth table0%0/4
A and not(B) and C
A= $filterB= not $matchC= $no_violation
ABCresult
0XX0
10X0
1100
1111
216 0 my $c = $session->perl_critic;
217 $predicate = sub {
218 0 ▶ my $root = $_[0];
Subroutine __ANON__: not called CC=2 SCAR=17.9
219 0 my $path = $_[1];
220 0 my $rel = $path->relative( $root );
221 0 ▶ return $rel !~ /$filter/
Condition0%0/2
!($rel =~ /$filter/) && scalar $c->critique($path->stringify)
!ll
00
MC/DC0%0/1
!($rel =~ /$filter/) && scalar $c->critique($path->stringify)
!($rel =~ /$filter/)
Truth table0%0/2
not(A)
A= !($rel =~ /$filter/)
Aresult
00
11
222 && scalar $c->critique( $path->stringify );
223 0 };
224 }
225 # match only
226 ▶ elsif ( not($filter) && $match && not($no_violation) ) {
Condition0%0/6
not $filter and $match and not $no_violation
!ll&&!rl&&r
000
not $filter and $match
!ll&&!rl&&r
000
MC/DC0%0/3
not $filter and $match and not $no_violation
not $filter $match not $no_violation
Truth table0%0/4
not(A) and B and not(C)
A= not $filterB= $matchC= not $no_violation
ABCresult
0XX0
10X0
1100
1111
227 $predicate = sub {
228 0 ▶ my $root = $_[0];
Subroutine __ANON__: not called CC=1 SCAR=6.9
229 0 my $path = $_[1];
230 0 my $rel = $path->relative( $root );
231 0 return $rel =~ /$match/;
232 0 };
233 }
234 # match and check violations
235 ▶ elsif ( not($filter) && $match && $no_violation ) {
Condition0%0/6
not $filter and $match and $no_violation
!ll&&!rl&&r
000
not $filter and $match
!ll&&!rl&&r
000
MC/DC0%0/3
not $filter and $match and $no_violation
not $filter $match $no_violation
Truth table0%0/4
not(A) and B and C
A= not $filterB= $matchC= $no_violation
ABCresult
0XX0
10X0
1100
1111
236 0 my $c = $session->perl_critic;
237 $predicate = sub {
238 0 ▶ my $root = $_[0];
Subroutine __ANON__: not called CC=2 SCAR=17.9
239 0 my $path = $_[1];
240 0 my $rel = $path->relative( $root );
241 0 ▶ return $rel =~ /$match/
Condition0%0/2
$rel =~ /$match/ && scalar $c->critique($path->stringify)
!ll
00
MC/DC0%0/1
$rel =~ /$match/ && scalar $c->critique($path->stringify)
$rel =~ /$match/
Truth table0%0/2
A
A= $rel =~ /$match/
Aresult
00
11
242 && scalar $c->critique( $path->stringify );
243 0 };
244 }
245 # check violations only
246 ▶ elsif ( not($filter) && not($match) && $no_violation ) {
Condition0%0/6
not $filter || $match and $no_violation
!ll&&!rl&&r
000
$filter || $match
l!l&&r!l&&!r
000
MC/DC0%0/3
not $filter || $match and $no_violation
$filter $match $no_violation
Truth table0%0/4
not(A || B) and C
A= $filterB= $matchC= $no_violation
ABCresult
1XX0
01X0
0000
0011
247 0 my $c = $session->perl_critic;
248 $predicate = sub {
249 0 ▶ my $path = $_[1];
Subroutine __ANON__: not called CC=1 SCAR=6.9
250 0 return scalar $c->critique( $path->stringify );
251 0 };
252 }
253 # none of the above
254 else {
255 0 $predicate = sub () { 1 };
256 }
257  
258 0 $session->set_file_criteria({
259 filter => $filter,
260 match => $match,
261 no_violation => $no_violation
262 });
263  
264 0 return $predicate;
265 }
266  
267 # NOTE:
268 # This was mostly taken from the guts of
269 # Perl::Critic::Util::{_is_perl,_is_backup}
270 # - SL
271 sub is_perl_file {
272 0 ▶ my ($file) = @_;
Subroutine is_perl_file: not called CC=13 SCAR=52.0
Pod undocumented
273  
274 # skip all the backups
275 0 ▶ return 0 if $file =~ m{ [.] swp \z}xms;
Branch: 1 branch
BranchTrueFalseTotal
if $file =~ / [.] swp \z/msx 0 0 0
276 0 ▶ return 0 if $file =~ m{ [.] bak \z}xms;
Branch: 1 branch
BranchTrueFalseTotal
if $file =~ / [.] bak \z/msx 0 0 0
277 0 ▶ return 0 if $file =~ m{ ~ \z}xms;
Branch: 1 branch
BranchTrueFalseTotal
if $file =~ / ~ \z/msx 0 0 0
278 0 ▶ return 0 if $file =~ m{ \A [#] .+ [#] \z}xms;
Branch: 1 branch
BranchTrueFalseTotal
if $file =~ / \A [#] .+ [#] \z/msx 0 0 0
279  
280 # but grab the perl files
281 0 ▶ return 1 if $file =~ m{ [.] PL \z}xms;
Branch: 1 branch
BranchTrueFalseTotal
if $file =~ / [.] PL \z/msx 0 0 0
282 0 ▶ return 1 if $file =~ m{ [.] p[lm] \z}xms;
Branch: 1 branch
BranchTrueFalseTotal
if $file =~ / [.] p[lm] \z/msx 0 0 0
283 0 ▶ return 1 if $file =~ m{ [.] t \z}xms;
Branch: 1 branch
BranchTrueFalseTotal
if $file =~ / [.] t \z/msx 0 0 0
284 0 ▶ return 1 if $file =~ m{ [.] psgi \z}xms;
Branch: 1 branch
BranchTrueFalseTotal
if $file =~ / [.] psgi \z/msx 0 0 0
285 0 ▶ return 1 if $file =~ m{ [.] cgi \z}xms;
Branch: 1 branch
BranchTrueFalseTotal
if $file =~ / [.] cgi \z/msx 0 0 0
286  
287 # if we have to, check for shebang
288 0 my $first;
289 {
290 0 ▶ open my $fh, '<', $file or return 0;
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, "<", $file or return 0 0 0 0
291 0 $first = <$fh>;
292 0 close $fh;
293 }
294  
295 0 ▶ return 1 if defined $first && ( $first =~ m{ \A [#]!.*perl }xms );
Branch: 1 branch
BranchTrueFalseTotal
if defined $first and $first =~ / \A [#]!.*perl /msx 0 0 0
Condition0%0/3
defined $first and $first =~ / \A [#]!.*perl /msx
!ll&&!rl&&r
000
MC/DC0%0/2
defined $first and $first =~ / \A [#]!.*perl /msx
defined $first $first =~ / \A [#]!.*perl /msx
Truth table0%0/3
A and B
A= defined $firstB= $first =~ / \A [#]!.*perl /msx
ABresult
0X0
100
111
296 0 return 0;
297 }
298  
299 1 1;
300  
301 =pod
302  
303 =head1 NAME
304  
305 App::Critique::Command::collect - Collect set of files for current critique session
306  
307 =head1 VERSION
308  
309 version 0.03
310  
311 =head1 DESCRIPTION
312  
313 This command will traverse the critque directory and gather all available Perl
314 files for critiquing. It will then store this list inside the correct critique
315 session file for processing during the critique session.
316  
317 It should be noted that this is a destructive command, meaning that if you have
318 begun critiquing your files and you re-run this command it will overwrite that
319 list and you will loose any tracking information you currently have.
320  
321 =head1 AUTHOR
322  
323 Stevan Little <stevan@cpan.org>
324  
325 =head1 COPYRIGHT AND LICENSE
326  
327 This software is copyright (c) 2016 by Stevan Little.
328  
329 This is free software; you can redistribute it and/or modify it under
330 the same terms as the Perl 5 programming language system itself.
331  
332 =cut
333  
334 __END__
335  
336 # ABSTRACT: Collect set of files for current critique session
337  
« Critique/Command/remove.pm ↑ Summary Critique/Command/process.pm »