blib/lib/Code/TidyAll.pm

filter: Statementstmt 93.6% 395 / 422 Branchbran 69.2% 108 / 156 Conditioncond 67.3% 33 / 49 MC/DCmcdc 50.0% 15 / 30 Subroutinesub 98.6% 75 / 76 Podpod 58.3% 7 / 12 totaltotal 84.9% 633 / 745 CC 107 SCAR 21.1
CC
107
Cov
85%
_recurse_dump
40.3
process_source
19.2
process_file
14.9
CRAP 8.2
« blib/lib/Code/TidyAll/Git/Prereceive.pm ↑ Summary blib/lib/Code/TidyAll/Plugin/PodTidy.pm »
1 package Code::TidyAll;
2  
3 35 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 35 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 35 ▶use Code::TidyAll::Cache;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 35 ▶use Code::TidyAll::CacheModel;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 35 ▶use Code::TidyAll::Config::INI::Reader;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 35 ▶use Code::TidyAll::Plugin;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 35 ▶use Code::TidyAll::Result;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 35 ▶use Code::TidyAll::Zglob qw(zglob);
Subroutine BEGIN: called CC=1 SCAR=0.0
12 35 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 35 ▶use Date::Format;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 35 ▶use Digest::SHA qw(sha1_hex);
Subroutine BEGIN: called CC=1 SCAR=0.0
15 35 ▶use File::Find qw(find);
Subroutine BEGIN: called CC=1 SCAR=0.0
16 35 ▶use File::pushd qw( pushd );
Subroutine BEGIN: called CC=1 SCAR=0.0
17 35 ▶use List::SomeUtils qw(uniq);
Subroutine BEGIN: called CC=1 SCAR=0.0
18 35 ▶use Module::Runtime qw( use_module );
Subroutine BEGIN: called CC=1 SCAR=0.0
19 35 ▶use Path::Tiny qw(path);
Subroutine BEGIN: called CC=1 SCAR=0.0
20 35 ▶use Scalar::Util qw(blessed);
Subroutine BEGIN: called CC=1 SCAR=0.0
21 35 ▶use Specio 0.40;
Subroutine BEGIN: called CC=1 SCAR=0.0
22 35 ▶use Specio::Declare;
Subroutine BEGIN: called CC=1 SCAR=0.0
23 35 ▶use Specio::Library::Builtins;
Subroutine BEGIN: called CC=1 SCAR=0.0
24 35 ▶use Specio::Library::Numeric;
Subroutine BEGIN: called CC=1 SCAR=0.0
25 35 ▶use Specio::Library::Path::Tiny 0.04;
Subroutine BEGIN: called CC=1 SCAR=0.0
26 35 ▶use Specio::Library::String;
Subroutine BEGIN: called CC=1 SCAR=0.0
27 35 ▶use Time::Duration::Parse qw(parse_duration);
Subroutine BEGIN: called CC=1 SCAR=0.0
28 35 ▶use Try::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
29  
30 35 ▶use Moo 2.000000;
Subroutine BEGIN: called CC=1 SCAR=0.0
31  
32 35 our $VERSION = '0.83';
33  
34 8 ▶sub default_conf_names { ( 'tidyall.ini', '.tidyallrc' ) }
Subroutine default_conf_names: called CC=1 SCAR=0.0
Pod undocumented
35  
36 # External
37 35 has backup_ttl => (
38 is => 'ro',
39 isa => t('NonEmptyStr'),
40 default => '1 hour',
41 );
42  
43 35 has cache => (
44 is => 'lazy',
45 isa => object_can_type( methods => [qw( get set )] ),
46 );
47  
48 35 has cache_model_class => (
49 is => 'ro',
50 isa => t('ClassName'),
51 default => 'Code::TidyAll::CacheModel',
52 );
53  
54 35 has check_only => (
55 is => 'ro',
56 isa => t('Bool'),
57 );
58  
59 35 has data_dir => (
60 is => 'lazy',
61 isa => t('Path'),
62 coerce => t('Path')->coercion_sub,
63 );
64  
65 35 has iterations => (
66 is => 'ro',
67 isa => t('PositiveInt'),
68 default => 1,
69 );
70  
71 35 has jobs => (
72 is => 'ro',
73 isa => t('Int'),
74 default => 1,
75 );
76  
77 35 has list_only => (
78 is => 'ro',
79 isa => t('Bool'),
80 );
81  
82 35 has mode => (
83 is => 'ro',
84 isa => t('NonEmptyStr'),
85 default => 'cli',
86 );
87  
88 35 has msg_outputter => (
89 is => 'ro',
90 isa => t('CodeRef'),
91 builder => '_build_msg_outputter',
92 );
93  
94 35 has no_backups => (
95 is => 'ro',
96 isa => t('Bool'),
97 );
98  
99 35 has no_cache => (
100 is => 'ro',
101 isa => t('Bool'),
102 );
103  
104 35 has output_suffix => (
105 is => 'ro',
106 isa => t('Str'),
107 default => q{},
108 );
109  
110 35 has plugins => (
111 is => 'ro',
112 isa => t('HashRef'),
113 required => 1,
114 );
115  
116 has selected_plugins => (
117 is => 'ro',
118 isa => t( 'ArrayRef', of => t('NonEmptyStr') ),
119 lazy => 1,
120 78 ▶ default => sub { [] },
Subroutine __ANON__: called CC=1 SCAR=0.0
121 35 );
122  
123 35 has quiet => (
124 is => 'ro',
125 isa => t('Bool'),
126 );
127 35 has recursive => (
128 is => 'ro',
129 isa => t('Bool'),
130 );
131  
132 35 has refresh_cache => (
133 is => 'ro',
134 isa => t('Bool'),
135 );
136  
137 35 has root_dir => (
138 is => 'ro',
139 isa => t('RealDir'),
140 coerce => t('RealDir')->coercion_sub,
141 required => 1
142 );
143  
144 35 has verbose => (
145 is => 'ro',
146 isa => t('Bool'),
147 default => 0,
148 );
149  
150 has inc => (
151 is => 'ro',
152 isa => t( 'ArrayRef', of => t('NonEmptyStr') ),
153 81 ▶ default => sub { [] },
Subroutine __ANON__: called CC=1 SCAR=0.0
154 35 );
155  
156 35 has _backup_dir => (
157 is => 'ro',
158 isa => t('Path'),
159 init_arg => undef,
160 lazy => 1,
161 builder => '_build_backup_dir',
162 );
163  
164 35 has _backup_ttl_secs => (
165 is => 'ro',
166 isa => t('Int'),
167 init_arg => undef,
168 lazy => 1,
169 builder => '_build_backup_ttl_secs',
170 );
171  
172 35 has _base_sig => (
173 is => 'ro',
174 isa => t('NonEmptyStr'),
175 init_arg => undef,
176 lazy => 1,
177 builder => '_build_base_sig',
178 );
179  
180 35 has _plugin_objects => (
181 is => 'ro',
182 isa => t( 'ArrayRef', of => object_isa_type('Code::TidyAll::Plugin') ),
183 init_arg => undef,
184 lazy => 1,
185 builder => '_build_plugin_objects',
186 );
187  
188 35 has _plugins_to_run => (
189 is => 'ro',
190 isa => t( 'HashRef', of => t('HashRef') ),
191 init_arg => undef,
192 lazy => 1,
193 builder => '_build_plugins_to_run',
194 );
195  
196 has _plugins_for_path => (
197 is => 'ro',
198 isa => t( 'HashRef', of => t('HashRef') ),
199 init_arg => undef,
200 lazy => 1,
201 75 ▶ default => sub { {} },
Subroutine __ANON__: called CC=1 SCAR=0.0
202 35 );
203  
204 35 with qw( Code::TidyAll::Role::HasIgnore Code::TidyAll::Role::Tempdir );
205  
206 sub _build_backup_dir {
207 71 ▶ my $self = shift;
Subroutine _build_backup_dir: called CC=1 SCAR=0.0
208 71 return $self->data_dir->child('backups');
209 }
210  
211 sub _build_backup_ttl_secs {
212 71 ▶ my $self = shift;
Subroutine _build_backup_ttl_secs: called CC=1 SCAR=0.0
213 71 return parse_duration( $self->backup_ttl );
214 }
215  
216 sub _build_base_sig {
217 45 ▶ my $self = shift;
Subroutine _build_base_sig: called CC=2 SCAR=7.0
218 45 my $active_plugins = join( q{|}, map { $_->name } @{ $self->_plugin_objects } );
219 45 ▶ return $self->_sig( [ $Code::TidyAll::VERSION || 0, $active_plugins ] );
Condition50%1/2
$Code::TidyAll::VERSION || 0
l!l
450
MC/DC0%0/1
$Code::TidyAll::VERSION || 0
$Code::TidyAll::VERSION
Truth table50%1/2
A
A= $Code::TidyAll::VERSION
Aresult
11
00
220 }
221  
222 sub _sig {
223 45 ▶ my ( $self, $data ) = @_;
Subroutine _sig: called CC=1 SCAR=0.0
224 45 return sha1_hex( join( ',', @$data ) );
225 }
226  
227 sub _build_cache {
228 69 ▶ my $self = shift;
Subroutine _build_cache: called CC=1 SCAR=0.0
229 69 return Code::TidyAll::Cache->new( cache_dir => $self->data_dir->child('cache') );
230 }
231  
232 sub _build_data_dir {
233 79 ▶ my $self = shift;
Subroutine _build_data_dir: called CC=1 SCAR=0.0
234 79 return $self->root_dir->child('/.tidyall.d');
235 }
236  
237 sub _build_plugins_to_run {
238 80 ▶ my $self = shift;
Subroutine _build_plugins_to_run: called CC=4 SCAR=13.9
239  
240 80 my $all_plugins = $self->plugins;
241 80 my %selected = map { $_ => 1 } @{ $self->selected_plugins };
242 80 my %plugins;
243  
244 80 ▶ if (%selected) {
Branch: 2 branches
BranchTrueFalseTotal
if (%selected) { } 2 78 80
elsif (my $mode = $self->mode) { } 78 0 78
245 2 my @unknown = sort grep { !$all_plugins->{$_} } keys %selected;
246 2 ▶ die "Asked for unknown plugins: [@unknown]" if @unknown;
Branch: 1 branch
BranchTrueFalseTotal
if @unknown 1 1 2
247 1 %plugins = map { $_ => $all_plugins->{$_} } keys %selected;
248 }
249 elsif ( my $mode = $self->mode ) {
250 99 %plugins = map { $_ => $all_plugins->{$_} }
251 100 grep { $self->_plugin_conf_matches_mode( $all_plugins->{$_}, $mode ) }
252 78 keys %{$all_plugins};
253 }
254  
255 79 return \%plugins;
256 }
257  
258 sub _plugin_conf_matches_mode {
259 100 ▶ my ( $self, $conf, $mode ) = @_;
Subroutine _plugin_conf_matches_mode: called CC=5 SCAR=17.2
260  
261 100 ▶ if ( my $only_modes = $conf->{only_modes} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $only_modes = $conf->{"only_modes"}) 2 98 100
262 2 ▶ return 0 if ( q{ } . $only_modes . q{ } ) !~ / $mode /;
Branch: 1 branch
BranchTrueFalseTotal
unless (' ' . $only_modes . ' ') =~ / $mode / 1 1 2
263 }
264 99 ▶ if ( my $except_modes = $conf->{except_modes} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $except_modes = $conf->{"except_modes"}) 0 99 99
265 0 ▶ return 0 if ( q{ } . $except_modes . q{ } ) =~ / $mode /;
Branch: 1 branch
BranchTrueFalseTotal
if (' ' . $except_modes . ' ') =~ / $mode / 0 0 0
266 }
267 99 return 1;
268 }
269  
270 sub _build_plugin_objects {
271 80 ▶ my $self = shift;
Subroutine _build_plugin_objects: called CC=2 SCAR=6.9
272  
273 # Sort tidiers by weight (by default validators have a weight of 60 and non-
274 # validators a weight of 50 meaning non-validators normally go first), then
275 # alphabetical
276 # TODO: These should probably sort in a consistent way independent of locale
277 return [
278 34 ▶ sort { ( $a->weight <=> $b->weight ) || ( $a->name cmp $b->name ) }
Branch: 1 branch
BranchTrueFalseTotal
$a->weight <=> $b->weight or $a->name cmp $b->name 21 13 34
279 100 map { $self->_load_plugin( $_, $self->_plugins_to_run->{$_} ) }
280 80 keys %{ $self->_plugins_to_run }
281 ];
282 }
283  
284 sub _load_plugin {
285 100 ▶ my ( $self, $plugin_name, $plugin_conf ) = @_;
Subroutine _load_plugin: called CC=2 SCAR=6.9
286  
287 # Extract first name in case there is a description
288 #
289 100 my ($plugin_fname) = ( $plugin_name =~ /^(\S+)/ );
290  
291 100 ▶ my $plugin_class = (
Branch: 1 branch
BranchTrueFalseTotal
$plugin_fname =~ /^\+/ ? : 76 24 100
292 $plugin_fname =~ /^\+/
293 ? substr( $plugin_fname, 1 )
294 : "Code::TidyAll::Plugin::$plugin_fname"
295 );
296 try {
297 100 ▶ use_module($plugin_class) || die 'not found';
Subroutine __ANON__: called CC=2 SCAR=7.6
Branch: 1 branch
BranchTrueFalseTotal
&use_module($plugin_class) or die "not found" 0 99 99
298 }
299 catch {
300 1 ▶ die qq{could not load plugin class '$plugin_class': $_};
Subroutine __ANON__: called CC=1 SCAR=0.0
301 100 };
302  
303 99 return $plugin_class->new(
304 name => $plugin_name,
305 tidyall => $self,
306 %$plugin_conf
307 );
308 }
309  
310 sub BUILD {
311 84 ▶ my ( $self, $params ) = @_;
Subroutine BUILD: called CC=4 SCAR=13.9
Pod undocumented
312  
313 # Strict constructor
314 #
315 84 ▶ if ( my @bad_params = grep { !$self->can($_) } keys(%$params) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my(@bad_params) = grep({not $self->can($_);} keys %$params)) 1 83 84
316 die sprintf(
317 'unknown constructor param%s %s for %s',
318 @bad_params > 1 ? 's' : q{},
319 1 ▶ join( ', ', sort map {qq['$_']} @bad_params ),
Branch: 1 branch
BranchTrueFalseTotal
@bad_params > 1 ? : 1 0 1
320 ref($self)
321 );
322 }
323  
324 83 ▶ unless ( $self->no_backups ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->no_backups) 71 12 83
325 71 $self->_backup_dir->mkpath( { mode => 0775 } );
326 71 $self->_purge_backups_periodically();
327 }
328  
329 83 @INC = ( @{ $self->inc }, @INC );
330 }
331  
332 sub _purge_backups_periodically {
333 71 ▶ my ($self) = @_;
Subroutine _purge_backups_periodically: called CC=3 SCAR=11.0
334 71 my $cache = $self->cache;
335 71 ▶ my $last_purge_backups = $cache->get('last_purge_backups') || 0;
Condition100%2/2
$cache->get('last_purge_backups') || 0
l!l
2348
MC/DC100%1/1
$cache->get('last_purge_backups') || 0
$cache->get('last_purge_backups')
Truth table100%2/2
A
A= $cache->get('last_purge_backups')
Aresult
11
00
336 71 ▶ if ( time > $last_purge_backups + $self->_backup_ttl_secs ) {
Branch: 1 branch
BranchTrueFalseTotal
if (time > $last_purge_backups + $self->_backup_ttl_secs) 48 23 71
337 48 $self->_purge_backups();
338 48 $cache->set( 'last_purge_backups', time() );
339 }
340 }
341  
342 sub _purge_backups {
343 48 ▶ my ($self) = @_;
Subroutine _purge_backups: called CC=2 SCAR=7.8
344 48 ▶ $self->msg('purging old backups') if $self->verbose;
Branch: 1 branch
BranchTrueFalseTotal
if $self->verbose 6 42 48
345 find(
346 {
347 follow => 0,
348 wanted => sub {
349 48 ▶ unlink $_ if -f && /\.bak$/ && time > ( stat($_) )[9] + $self->_backup_ttl_secs;
Subroutine __ANON__: called CC=4 SCAR=19.1
Branch: 1 branch
BranchTrueFalseTotal
if -f $_ and /\.bak$/ and time > (stat $_)[9] + $self->_backup_ttl_secs 0 48 48
Condition33%2/6
-f $_ and /\.bak$/ and time > (stat $_)[9] + $self->_backup_ttl_secs
!ll&&!rl&&r
4800
-f $_ and /\.bak$/
!ll&&!rl&&r
4800
MC/DC0%0/3
-f $_ and /\.bak$/ and time > (stat $_)[9] + $self->_backup_ttl_secs
-f $_ /\.bak$/ time > (stat $_)[9] + $self->_backup_ttl_secs
Truth table25%1/4
A and B and C
A= -f $_B= /\.bak$/C= time > (stat $_)[9] + $self->_backup_ttl_secs
ABCresult
0XX0
10X0
1100
1111
350 },
351 48 no_chdir => 1
352 },
353 $self->_backup_dir,
354 );
355 }
356  
357 sub new_from_conf_file {
358 9 ▶ my ( $class, $conf_file, %params ) = @_;
Subroutine new_from_conf_file: called CC=8 SCAR=24.6
Pod documented
359  
360 9 $conf_file = path($conf_file);
361  
362 9 ▶ die qq{no such file '$conf_file'} unless $conf_file->is_file;
Branch: 1 branch
BranchTrueFalseTotal
unless $conf_file->is_file 0 9 9
363 9 my $conf_params = $class->_read_conf_file($conf_file);
364 9 ▶ my $main_params = delete( $conf_params->{'_'} ) || {};
Condition100%2/2
delete $conf_params->{'_'} || {}
l!l
81
MC/DC100%1/1
delete $conf_params->{'_'} || {}
delete $conf_params->{'_'}
Truth table100%2/2
A
A= delete $conf_params->{'_'}
Aresult
11
00
365  
366 %params = (
367 plugins => $conf_params,
368 root_dir => path($conf_file)->realpath->parent,
369 9 %{$main_params},
370 %params
371 );
372  
373 # Initialize with alternate class if given
374 #
375 9 ▶ if ( my $tidyall_class = delete( $params{tidyall_class} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $tidyall_class = delete $params{"tidyall_class"}) 0 9 9
376 0 ▶ local @INC = ( @{ $conf_params->{inc} }, @INC ) if $conf_params->{inc};
Branch: 1 branch
BranchTrueFalseTotal
if $conf_params->{"inc"} 0 0 0
377 0 ▶ use_module($tidyall_class) or die qq{cannot load '$tidyall_class'};
Branch: 1 branch
BranchTrueFalseTotal
&use_module($tidyall_class) or die "cannot load '${tidyall_class}'" 0 0 0
378 0 $class = $tidyall_class;
379 }
380  
381 9 ▶ if ( $params{verbose} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($params{"verbose"}) 2 7 9
382 2 ▶ my $msg_outputter = $params{msg_outputter} || $class->_build_msg_outputter();
Condition33%1/3
$params{'msg_outputter'} || $class->_build_msg_outputter
l!l&&r!l&&!r
020
MC/DC0%0/2
$params{'msg_outputter'} || $class->_build_msg_outputter
$params{'msg_outputter'} $class->_build_msg_outputter
Truth table33%1/3
A || B
A= $params{'msg_outputter'}B= $class->_build_msg_outputter
ABresult
1X1
011
000
383 2 $msg_outputter->(
384 'constructing %s with these params: %s', $class,
385 _dump_params( \%params )
386 );
387 }
388  
389 9 return $class->new(%params);
390 }
391  
392 sub _read_conf_file {
393 9 ▶ my ( $class, $conf_file ) = @_;
Subroutine _read_conf_file: called CC=2 SCAR=7.0
394 9 my $conf_string = $conf_file->slurp_utf8;
395 9 my $root_dir = $conf_file->parent;
396 9 $conf_string =~ s/\$ROOT/$root_dir/g;
397 9 my $conf_hash = Code::TidyAll::Config::INI::Reader->read_string($conf_string);
398 9 ▶ die qq{'$conf_file' did not evaluate to a hash}
Branch: 1 branch
BranchTrueFalseTotal
unless ref $conf_hash eq "HASH" 0 9 9
399 unless ( ref($conf_hash) eq 'HASH' );
400 9 return $conf_hash;
401 }
402  
403 sub _dump_params {
404 2 ▶ my $p = shift;
Subroutine _dump_params: called CC=1 SCAR=0.0
405  
406 2 return Data::Dumper->new( [ _recurse_dump($p) ] )->Indent(0)->Sortkeys(1)->Quotekeys(0)
407 ->Terse(1)->Dump;
408 }
409  
410 # This is all a ridiculous workaround around the fact that there is no good
411 # way to tell Data::Dumper how to serialize a Path::Tiny object.
412 sub _recurse_dump {
413 2 ▶ my ($p) = @_;
Subroutine _recurse_dump: called CC=12 SCAR=37.0
414  
415 2 ▶ return $p unless ref $p;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $p 0 2 2
416  
417 2 ▶ if ( ref $p eq 'HASH' ) {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $p eq 'HASH') { } 2 0 2
elsif (ref $p eq 'ARRAY') { } 0 0 0
418 2 my %dump;
419 2 for my $k ( keys %{$p} ) {
420 10 my $v = $p->{$k};
421 10 ▶ if ( blessed $v ) {
Branch: 2 branches
BranchTrueFalseTotal
if (builtin::blessed($v)) { } 2 8 10
elsif (ref $v =~ /^(?:HASH|ARRAY)$/) { } 0 8 8
422 2 ▶ if ( $v->isa('Path::Tiny') ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($v->isa('Path::Tiny')) { } 2 0 2
423 2 $dump{$k} = $v . q{};
424 }
425 else {
426 0 die 'Cannot dump ' . ref($v) . ' object';
427 }
428 }
429 elsif ( ref $v =~ /^(?:HASH|ARRAY)$/ ) {
430 0 $dump{$k} = _recurse_dump($v);
431 }
432 else {
433 8 $dump{$k} = $v;
434 }
435 }
436 2 return \%dump;
437 }
438 elsif ( ref $p eq 'ARRAY' ) {
439 0 my @dump;
440 0 for my $v ( @{$p} ) {
441 0 ▶ if ( blessed $v ) {
Branch: 2 branches
BranchTrueFalseTotal
if (builtin::blessed($v)) { } 0 0 0
elsif (ref $v =~ /^(?:HASH|ARRAY)$/) { } 0 0 0
442 0 ▶ if ( $v->isa('Path::Tiny') ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($v->isa('Path::Tiny')) { } 0 0 0
443 0 push @dump, $v . q{};
444 }
445 else {
446 0 die 'Cannot dump ' . ref($v) . ' object';
447 }
448 }
449 elsif ( ref $v =~ /^(?:HASH|ARRAY)$/ ) {
450 0 push @dump, _recurse_dump($v);
451 }
452 else {
453 0 push @dump, $v;
454 }
455 }
456 0 return \@dump;
457 }
458  
459 0 die "_recurse_dump was called with a value that was not a scalar, hashref, or an arrayref: $p";
460 }
461  
462 sub process_all {
463 17 ▶ my $self = shift;
Subroutine process_all: called CC=1 SCAR=0.0
Pod undocumented
464  
465 17 return $self->process_paths( $self->find_matched_files );
466 }
467  
468 sub process_paths {
469 98 ▶ my ( $self, @paths ) = @_;
Subroutine process_paths: called CC=4 SCAR=13.9
Pod documented
470  
471 @paths = map {
472 119 ▶ try { $_->realpath }
Subroutine __ANON__: called CC=1 SCAR=0.0
473 119 ▶ || $_->absolute
Branch: 1 branch
BranchTrueFalseTotal
&try(sub { $_->realpath; } ) or $_->absolute 1 118 119
474 98 } map { path($_) } @paths;
475  
476 98 my $dir = pushd( $self->root_dir );
477 98 ▶ if ( $self->jobs > 1 && @paths > 1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->jobs > 1 and @paths > 1) { } 5 93 98
Condition66%2/3
$self->jobs > 1 and @paths > 1
!ll&&!rl&&r
9305
MC/DC50%1/2
$self->jobs > 1 and @paths > 1
$self->jobs > 1 @paths > 1
Truth table66%2/3
A and B
A= $self->jobs > 1B= @paths > 1
ABresult
0X0
100
111
478 5 return $self->_process_parallel(@paths);
479 }
480 else {
481 93 return map { $self->process_path($_) } @paths;
482 }
483 }
484  
485 sub _process_parallel {
486 5 ▶ my ( $self, @paths ) = @_;
Subroutine _process_parallel: called CC=4 SCAR=14.0
487  
488 5 ▶ unless ( eval { require Parallel::ForkManager; 1; } ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (eval { do { require Parallel::ForkManager; 1 } }) 0 5 5
489 0 die 'Running Code::TidyAll with multiple jobs requires Parallel::ForkManager';
490 }
491  
492 5 my @results;
493 my %path_to_pid;
494  
495 5 my $pm = Parallel::ForkManager->new( $self->jobs );
496 5 $pm->set_waitpid_blocking_sleep(0.01);
497 $pm->run_on_finish(
498 sub {
499 5 ▶ my ( $pid, $code, $result ) = @_[ 0, 1, 5 ];
Subroutine __ANON__: called CC=2 SCAR=7.6
500  
501 5 ▶ if ($code) {
Branch: 1 branch
BranchTrueFalseTotal
if ($code) { } 0 5 5
502 0 warn "Error running tidyall on $path_to_pid{$pid}. Got exit status of $code.";
503 }
504 else {
505 5 push @results, $result;
506 }
507 }
508 5 );
509  
510 5 for my $path (@paths) {
511 14 ▶ if ( my $pid = $pm->start ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $pid = $pm->start) 10 4 14
512 10 $path_to_pid{$path} = $pid;
513 10 next;
514 }
515  
516 4 $pm->finish( 0, $self->process_path($path) );
517 }
518  
519 1 $pm->wait_all_children;
520  
521 1 return @results;
522 }
523  
524 sub process_path {
525 103 ▶ my ( $self, $path ) = @_;
Subroutine process_path: called CC=4 SCAR=13.9
Pod undocumented
526  
527 103 ▶ if ( $path->is_dir ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($path->is_dir) { } 2 101 103
elsif ($path->is_file) { } 100 1 101
528 2 ▶ if ( $self->recursive ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->recursive) { } 1 1 2
529 1 return $self->process_paths( $path->children );
530 }
531 else {
532 1 return ( $self->_error_result( "$path: is a directory (try -r/--recursive)", $path ) );
533 }
534 }
535 elsif ( $path->is_file ) {
536 100 return ( $self->process_file($path) );
537 }
538 else {
539 1 return ( $self->_error_result( "$path: not a file or directory", $path ) );
540 }
541 }
542  
543 sub process_file {
544 102 ▶ my ( $self, $full_path ) = @_;
Subroutine process_file: called CC=11 SCAR=27.0
Pod documented
545  
546 102 $full_path = path($full_path);
547 102 ▶ die "$full_path is not a file" unless $full_path->is_file;
Branch: 1 branch
BranchTrueFalseTotal
unless $full_path->is_file 0 102 102
548  
549 102 my $path = $self->_small_path($full_path);
550  
551 101 ▶ if ( $self->list_only ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->list_only) 0 101 101
552 0 ▶ if ( my @plugins = $self->plugins_for_path($path) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my(@plugins) = $self->plugins_for_path($path)) 0 0 0
553 0 $self->msg( '%s (%s)', $path, join( ', ', map { $_->name } @plugins ) );
554 }
555 0 return Code::TidyAll::Result->new( path => $path, state => 'checked' );
556 }
557  
558 101 my $cache_model = $self->_cache_model_for( $path, $full_path );
559 101 ▶ if ( $self->refresh_cache ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($self->refresh_cache) { } 0 101 101
elsif ($cache_model->is_cached) { } 8 93 101
560 0 $cache_model->remove;
561 }
562 elsif ( $cache_model->is_cached ) {
563 8 ▶ $self->msg( '[cached] %s', $path ) if $self->verbose;
Branch: 1 branch
BranchTrueFalseTotal
if $self->verbose 0 8 8
564 8 return Code::TidyAll::Result->new( path => $path, state => 'cached' );
565 }
566  
567 93 ▶ my $contents = $cache_model->file_contents || $full_path->slurp_raw;
Condition33%1/3
$cache_model->file_contents || $full_path->slurp_raw
l!l&&r!l&&!r
9300
MC/DC0%0/2
$cache_model->file_contents || $full_path->slurp_raw
$cache_model->file_contents $full_path->slurp_raw
Truth table33%1/3
A || B
A= $cache_model->file_contentsB= $full_path->slurp_raw
ABresult
1X1
011
000
568 93 my $result = $self->process_source( $contents, $path );
569  
570 93 ▶ if ( $result->state eq 'tidied' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($result->state eq "tidied") 58 35 93
571  
572 # backup original contents
573 58 $self->_backup_file( $path, $contents );
574  
575 # write new contents out to disk
576 58 $contents = $result->new_contents;
577  
578 # We don't use ->spew because that creates a new file and renames it,
579 # losing the existing mode setting in the process.
580 58 path( $full_path . $self->output_suffix )->append_raw( { truncate => 1 }, $contents );
581  
582 # change the in memory contents of the cache (but don't update yet)
583 58 ▶ $cache_model->file_contents($contents) unless $self->output_suffix;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->output_suffix 58 0 58
584 }
585  
586 93 ▶ $cache_model->update if $result->ok;
Branch: 1 branch
BranchTrueFalseTotal
if $result->ok 83 10 93
587 93 return $result;
588 }
589  
590 sub _small_path {
591 106 ▶ my ( $self, $path ) = @_;
Subroutine _small_path: called CC=2 SCAR=6.9
592 106 ▶ die sprintf( q{'%s' is not underneath root dir '%s'!}, $path, $self->root_dir )
Branch: 1 branch
BranchTrueFalseTotal
unless index($path, $self->root_dir) == 0 1 105 106
593 unless index( $path, $self->root_dir ) == 0;
594 105 return path( substr( $path . q{}, length( $self->root_dir ) + 1 ) );
595 }
596  
597 sub plugins_for_path {
598 119 ▶ my ( $self, $path ) = @_;
Subroutine plugins_for_path: called CC=2 SCAR=6.9
Pod documented
599  
600 $self->_plugins_for_path->{$path}
601 119 ▶ ||= [ grep { $_->matches_path($path) } @{ $self->_plugin_objects } ];
Condition100%2/2
$self->_plugins_for_path->{$path} ||= [grep({$_->matches_path($path);} @{$self->_plugin_objects;})]
l!l
6158
MC/DC100%1/1
$self->_plugins_for_path->{$path} ||= [grep({$_->matches_path($path);} @{$self->_plugin_objects;})]
$self->_plugins_for_path->{$path}
Truth table100%2/2
A
A= $self->_plugins_for_path->{$path}
Aresult
11
00
602 119 return @{ $self->_plugins_for_path->{$path} };
603 }
604  
605 sub _cache_model_for {
606 101 ▶ my ( $self, $path, $full_path ) = @_;
Subroutine _cache_model_for: called CC=2 SCAR=6.9
607 101 ▶ return $self->cache_model_class->new(
Branch: 1 branch
BranchTrueFalseTotal
$self->no_cache ? : 32 69 101
608 path => $path,
609 full_path => $full_path,
610 ( $self->no_cache ? () : ( cache_engine => $self->cache ) ),
611 base_sig => $self->_base_sig,
612 );
613 }
614  
615 sub _backup_file {
616 58 ▶ my ( $self, $path, $contents ) = @_;
Subroutine _backup_file: called CC=2 SCAR=6.9
617 58 ▶ unless ( $self->no_backups ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->no_backups) 42 16 58
618 42 my $backup_file = $self->_backup_dir->child( $self->_backup_filename($path) );
619 42 $backup_file->parent->mkpath( { mode => 0775 } );
620 42 $backup_file->spew_raw($contents);
621 }
622 }
623  
624 sub _backup_filename {
625 42 ▶ my ( $self, $path ) = @_;
Subroutine _backup_filename: called CC=1 SCAR=0.0
626  
627 42 return join( q{}, $path, '-', time2str( '%Y%m%d-%H%M%S', time ), '.bak' );
628 }
629  
630 sub process_source {
631 118 ▶ my ( $self, $contents, $path ) = @_;
Subroutine process_source: called CC=19 SCAR=29.5
Pod documented
632  
633 118 $path = path($path);
634  
635 118 ▶ die 'contents and path required' unless defined($contents) && defined($path);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $contents and defined $path 0 118 118
Condition33%1/3
defined $contents and defined $path
!ll&&!rl&&r
00118
MC/DC0%0/2
defined $contents and defined $path
defined $contents defined $path
Truth table33%1/3
A and B
A= defined $contentsB= defined $path
ABresult
0X0
100
111
636 118 my @plugins = $self->plugins_for_path($path);
637  
638 118 ▶ if ( !@plugins ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@plugins) 1 117 118
639 1 ▶ $self->msg(
Branch: 2 branches
BranchTrueFalseTotal
if $self->verbose 0 1 1
$self->mode ? : 0 0 0
640 '[no plugins apply%s] %s',
641 $self->mode ? q{ for mode '} . $self->mode . q{'} : q{}, $path
642 ) if $self->verbose;
643 1 return Code::TidyAll::Result->new( path => $path, state => 'no_match' );
644 }
645  
646 117 ▶ if ( $self->verbose ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->verbose) 6 111 117
647 6 my @names = join ', ', map { $_->name } @plugins;
648 6 $self->msg("[applying the following plugins: @names]");
649 }
650  
651 117 my $new_contents = my $orig_contents = $contents;
652 117 my $plugin;
653 my $error;
654 117 my @diffs;
655 try {
656 117 ▶ foreach my $method (qw(preprocess_source process_source_or_file postprocess_source)) {
Subroutine __ANON__: called CC=4 SCAR=13.9
657 336 foreach $plugin (@plugins) {
658 398 my $diff;
659 398 ( $new_contents, $diff )
660 = $plugin->$method( $new_contents, $path, $self->check_only );
661 383 ▶ if ($diff) {
Branch: 1 branch
BranchTrueFalseTotal
if ($diff) 2 381 383
662 2 push @diffs, [ $plugin->name, $diff ];
663 }
664 }
665 }
666 }
667 catch {
668 15 ▶ chomp;
Subroutine __ANON__: called CC=2 SCAR=7.1
669 15 $error = $_;
670 15 ▶ $error = sprintf( q{*** '%s': %s}, $plugin->name, $_ ) if $plugin;
Branch: 1 branch
BranchTrueFalseTotal
if $plugin 0 15 15
671 117 };
672  
673 117 ▶ my $was_tidied = !$error && ( $new_contents ne $orig_contents );
Condition100%3/3
!$error && $new_contents ne $orig_contents
!ll&&!rl&&r
153171
MC/DC100%2/2
!$error && $new_contents ne $orig_contents
!$error $new_contents ne $orig_contents
Truth table100%3/3
not(A) && B
A= !$errorB= $new_contents ne $orig_contents
ABresult
0X0
100
111
674 117 ▶ if ( $was_tidied && $self->check_only ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($was_tidied and $self->check_only) 5 112 117
Condition100%3/3
$was_tidied and $self->check_only
!ll&&!rl&&r
46665
MC/DC100%2/2
$was_tidied and $self->check_only
$was_tidied $self->check_only
Truth table100%3/3
A and B
A= $was_tidiedB= $self->check_only
ABresult
0X0
100
111
675 5 $error = '*** needs tidying';
676  
677 # Github annotations parsable output to highlight code in pull requests
678 5 ▶ if ( $ENV{GITHUB_ACTIONS} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ENV{"GITHUB_ACTIONS"}) 1 4 5
679 1 $error .= "\n::error file=${path}::File ${path} needs tidying";
680 }
681  
682 5 foreach my $diff (@diffs) {
683 2 $error .= "\n\n";
684 2 $error .= "$diff->[0] made the following change:\n$diff->[1]";
685 }
686 5 ▶ $error .= "\n\n" if @diffs;
Branch: 1 branch
BranchTrueFalseTotal
if @diffs 2 3 5
687 5 undef $was_tidied;
688 }
689  
690 117 ▶ if ( !$self->quiet || $error ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $self->quiet or $error) 99 18 117
Condition100%3/3
not $self->quiet or $error
l!l&&r!l&&!r
881118
MC/DC100%2/2
not $self->quiet or $error
not $self->quiet $error
Truth table100%3/3
not(A) or B
A= not $self->quietB= $error
ABresult
1X1
011
000
691 99 ▶ my $status = $was_tidied ? '[tidied] ' : '[checked] ';
Branch: 1 branch
BranchTrueFalseTotal
$was_tidied ? : 55 44 99
692 my $plugin_names
693 99 ▶ = $self->verbose ? sprintf( ' (%s)', join( ', ', map { $_->name } @plugins ) ) : q{};
Branch: 1 branch
BranchTrueFalseTotal
$self->verbose ? : 6 93 99
694 99 $self->msg( '%s%s%s', $status, $path, $plugin_names );
695 }
696  
697 117 ▶ if ($error) {
Branch: 2 branches
BranchTrueFalseTotal
if ($error) { } 20 97 117
elsif ($was_tidied) { } 66 31 97
698 20 return $self->_error_result( $error, $path, $orig_contents, $new_contents );
699 }
700 elsif ($was_tidied) {
701 66 return Code::TidyAll::Result->new(
702 path => $path,
703 state => 'tidied',
704 orig_contents => $orig_contents,
705 new_contents => $new_contents
706 );
707 }
708 else {
709 31 return Code::TidyAll::Result->new( path => $path, state => 'checked' );
710 }
711 }
712  
713 sub _error_result {
714 22 ▶ my ( $self, $msg, $path, $orig_contents, $new_contents ) = @_;
Subroutine _error_result: called CC=5 SCAR=16.3
715 22 $self->msg( '%s', $msg );
716 22 ▶ return Code::TidyAll::Result->new(
Branch: 2 branches
BranchTrueFalseTotal
defined $orig_contents && length $orig_contents ? : 20 2 22
defined $new_contents && length $new_contents ? : 20 2 22
Condition66%4/6
defined $orig_contents and length $orig_contents
!ll&&!rl&&r
2020
defined $new_contents and length $new_contents
!ll&&!rl&&r
2020
MC/DC50%1/2
defined $orig_contents and length $orig_contents
defined $orig_contents length $orig_contents
MC/DC50%1/2
defined $new_contents and length $new_contents
defined $new_contents length $new_contents
Truth table66%2/3
A and B
A= defined $orig_contentsB= length $orig_contents
ABresult
0X0
100
111
Truth table66%2/3
A and B
A= defined $new_contentsB= length $new_contents
ABresult
0X0
100
111
717 path => $path,
718 state => 'error',
719 error => $msg,
720 (
721 ( defined $orig_contents && length $orig_contents )
722 ? ( orig_contents => $orig_contents )
723 : ()
724 ),
725 (
726 ( defined $new_contents && length $new_contents )
727 ? ( new_contents => $new_contents )
728 : ()
729 ),
730 );
731 }
732  
733 sub find_conf_file {
734 6 ▶ my ( $class, $conf_names, $start_dir ) = @_;
Subroutine find_conf_file: called CC=4 SCAR=16.1
Pod documented
735  
736 6 $start_dir = path($start_dir);
737 6 my $path1 = $start_dir->absolute;
738 6 my $path2 = $start_dir->realpath;
739 6 ▶ my $conf_file = $class->_find_conf_file_upward( $conf_names, $path1 )
Condition33%1/3
$class->_find_conf_file_upward($conf_names, $path1) || $class->_find_conf_file_upward($conf_names, $path2)
l!l&&r!l&&!r
600
MC/DC0%0/2
$class->_find_conf_file_upward($conf_names, $path1) || $class->_find_conf_file_upward($conf_names, $path2)
$class->_find_conf_file_upward($conf_names, $path1) $class->_find_conf_file_upward($conf_names, $path2)
Truth table33%1/3
A || B
A= $class->_find_conf_file_upward($conf_names, $path1)B= $class->_find_conf_file_upward($conf_names, $path2)
ABresult
1X1
011
000
740 || $class->_find_conf_file_upward( $conf_names, $path2 );
741 6 ▶ unless ( defined $conf_file ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $conf_file) 0 6 6
742 0 ▶ die sprintf(
Branch: 1 branch
BranchTrueFalseTotal
$path1 eq $path2 ? : 0 0 0
743 'could not find %s upwards from %s',
744 join( ' or ', @$conf_names ),
745 ( $path1 eq $path2 ) ? qq{'$path1'} : qq{'$path1' or '$path2'}
746 );
747 }
748 6 return $conf_file;
749 }
750  
751 sub _find_conf_file_upward {
752 6 ▶ my ( $class, $conf_names, $search_dir ) = @_;
Subroutine _find_conf_file_upward: called CC=5 SCAR=16.2
753  
754 6 my $cnt = 0;
755 6 while (1) {
756 8 foreach my $conf_name (@$conf_names) {
757 13 my $try_path = $search_dir->child($conf_name);
758 13 ▶ return $try_path if $try_path->is_file;
Branch: 1 branch
BranchTrueFalseTotal
if $try_path->is_file 6 7 13
759 }
760  
761 2 my $parent = $search_dir->parent;
762 2 ▶ last if $parent eq $search_dir;
Branch: 1 branch
BranchTrueFalseTotal
if $parent eq $search_dir 0 2 2
763 2 $search_dir = $parent;
764  
765 2 ▶ die 'inf loop!' if ++$cnt > 100;
Branch: 1 branch
BranchTrueFalseTotal
if ++$cnt > 100 0 2 2
766 }
767 }
768  
769 sub find_matched_files {
770 19 ▶ my ($self) = @_;
Subroutine find_matched_files: called CC=4 SCAR=13.9
Pod documented
771  
772 19 my $plugins_for_path = $self->_plugins_for_path;
773 19 my $root_length = length( $self->root_dir );
774  
775 19 my @all;
776 19 for my $plugin ( @{ $self->_plugin_objects } ) {
777 30 my @matched = $self->_matched_by_plugin($plugin);
778 30 push @all, @matched;
779  
780 # When we end up in process_source we'll need to know which plugins
781 # match a given file. This could be (re-)calculated When we call
782 # ->plugins_for_path($file) there but since we already know the path
783 # to plugin mapping, we might as well store it here.
784 30 for my $file (@matched) {
785 54 my $path = substr( $file, $root_length + 1 );
786 54 ▶ $plugins_for_path->{$path} ||= [];
Condition100%2/2
$plugins_for_path->{$path} ||= []
l!l
1341
MC/DC100%1/1
$plugins_for_path->{$path} ||= []
$plugins_for_path->{$path}
Truth table100%2/2
A
A= $plugins_for_path->{$path}
Aresult
11
00
787 54 push @{ $plugins_for_path->{$path} }, $plugin;
788 }
789 }
790  
791 19 return map { path($_) } uniq(@all);
792 }
793  
794 sub _matched_by_plugin {
795 30 ▶ my $self = shift;
Subroutine _matched_by_plugin: called CC=7 SCAR=19.5
796 30 my $plugin = shift;
797  
798 7 my %is_ignored = map { $_ => 1 }
799 30 ▶ $self->_zglob( [ @{ $self->ignores || [] }, @{ $plugin->ignores || [] } ] );
Branch: 2 branches
BranchTrueFalseTotal
$self->ignores or [] 0 30 30
$plugin->ignores or [] 0 30 30
800 my @matched
801 30 ▶ = grep { !$is_ignored{$_} } grep { -f && -s && !-l } $self->_zglob( $plugin->selects );
Branch: 1 branch
BranchTrueFalseTotal
-f $_ and -s $_ and not -l $_ 64 3 67
Condition100%3/3
-f $_ and -s $_
!ll&&!rl&&r
2164
MC/DC100%2/2
-f $_ and -s $_
-f $_ -s $_
Truth table100%3/3
A and B
A= -f $_B= -s $_
ABresult
0X0
100
111
802  
803 30 ▶ my $shebang = $plugin->shebang
Branch: 1 branch
BranchTrueFalseTotal
my $shebang = $plugin->shebang or return @matched 29 1 30
804 or return @matched;
805  
806 1 my $re = join '|', map {quotemeta} @{$shebang};
807 1 $re = qr/^#!.*\b(?:$re)\b/;
808 return grep {
809 1 my $fh;
810 6 ▶ open $fh, '<', $_ or die $!;
Branch: 1 branch
BranchTrueFalseTotal
open $fh, "<", $_ or die $! 0 6 6
811 6 scalar <$fh> =~ /$re/;
812 } @matched;
813 }
814  
815 sub _zglob {
816 60 ▶ my ( $self, $globs ) = @_;
Subroutine _zglob: called CC=2 SCAR=7.0
817  
818 60 local $Code::TidyAll::Zglob::NOCASE = 0;
819 60 my @files;
820 60 foreach my $glob (@$globs) {
821 try {
822 43 ▶ push @files, zglob( join( "/", $self->root_dir, $glob ) );
Subroutine __ANON__: called CC=1 SCAR=0.0
823 }
824 catch {
825 0 ▶ die qq{error parsing '$glob': $_};
Subroutine __ANON__: not called CC=1 SCAR=6.9
826 }
827 43 }
828 60 return uniq(@files);
829 }
830  
831 sub msg {
832 133 ▶ my ( $self, $format, @params ) = @_;
Subroutine msg: called CC=1 SCAR=0.0
Pod undocumented
833 133 $self->msg_outputter()->( $format, @params );
834 }
835  
836 sub _build_msg_outputter {
837 return sub {
838 135 ▶ my $format = shift;
Subroutine __ANON__: called CC=1 SCAR=0.0
839 135 printf "$format\n", @_;
840 86 ▶ };
Subroutine _build_msg_outputter: called CC=1 SCAR=0.0
841 }
842  
843 35 1;
844  
845 # ABSTRACT: Engine for tidyall, your all-in-one code tidier and validator
846  
847 __END__
848  
849 =pod
850  
851 =encoding UTF-8
852  
853 =head1 NAME
854  
855 Code::TidyAll - Engine for tidyall, your all-in-one code tidier and validator
856  
857 =head1 VERSION
858  
859 version 0.83
860  
861 =head1 SYNOPSIS
862  
863 use Code::TidyAll;
864  
865 my $ct = Code::TidyAll->new_from_conf_file(
866 '/path/to/conf/file',
867 ...
868 );
869  
870 # or
871  
872 my $ct = Code::TidyAll->new(
873 root_dir => '/path/to/root',
874 plugins => {
875 perltidy => {
876 select => 'lib/**/*.(pl|pm)',
877 argv => '-noll -it=2',
878 },
879 ...
880 }
881 );
882  
883 # then...
884  
885 $ct->process_paths($file1, $file2);
886  
887 =head1 DESCRIPTION
888  
889 This is the engine used by L<tidyall> - read that first to get an overview.
890  
891 You can call this API from your own program instead of executing C<tidyall>.
892  
893 =head1 METHODS
894  
895 This class offers the following methods:
896  
897 =head2 Code::TidyAll->new(%params)
898  
899 The regular constructor. Must pass at least I<plugins> and I<root_dir>.
900  
901 =head2 $tidyall->new_from_conf_file( $conf_file, %params )
902  
903 Takes a conf file path, followed optionally by a set of key/value parameters.
904 Reads parameters out of the conf file and combines them with the passed
905 parameters (the latter take precedence), and calls the regular constructor.
906  
907 If the conf file or params defines I<tidyall_class>, then that class is
908 constructed instead of C<Code::TidyAll>.
909  
910 =head3 Constructor parameters
911  
912 =over 4
913  
914 =item * plugins
915  
916 Specify a hash of plugins, each of which is itself a hash of options. This is
917 equivalent to what would be parsed out of the sections in the configuration
918 file.
919  
920 =item * selected_plugins
921  
922 An arrayref of plugins to be used. This overrides the C<mode> parameter.
923  
924 This is really only useful if you're getting configuration from a config file
925 and want to narrow the set of plugins to be run.
926  
927 Note that plugins will still only run on files which match their C<select> and
928 C<ignore> configuration.
929  
930 =item * cache_model_class
931  
932 The cache model class. Defaults to C<Code::TidyAll::CacheModel>
933  
934 =item * cache
935  
936 The cache instance (e.g. an instance of C<Code::TidyAll::Cache> or a C<CHI>
937 instance.) An instance of C<Code::TidyAll::Cache> is automatically instantiated
938 by default.
939  
940 =item * backup_ttl
941  
942 =item * check_only
943  
944 If this is true, then we simply check that files pass validation steps and that
945 tidying them does not change the file. Any changes from tidying are not
946 actually written back to the file.
947  
948 =item * no_cleanup
949  
950 A boolean indicating if we should skip cleaning temporary files or not.
951 Defaults to false.
952  
953 =item * inc
954  
955 An arrayref of directories to prepend to C<@INC>. This can be set via the
956 command-line as C<-I>, but you can also set it in a config file.
957  
958 This affects both loading and running plugins.
959  
960 =item * data_dir
961  
962 =item * iterations
963  
964 =item * mode
965  
966 =item * no_backups
967  
968 =item * no_cache
969  
970 =item * output_suffix
971  
972 =item * quiet
973  
974 =item * root_dir
975  
976 =item * ignore
977  
978 =item * verbose
979  
980 These options are the same as the equivalent C<tidyall> command-line options,
981 replacing dashes with underscore (e.g. the C<backup-ttl> option becomes
982 C<backup_ttl> here).
983  
984 =item * msg_outputter
985  
986 This is a subroutine reference that is called whenever a message needs to be
987 printed in some way. The sub receives a C<sprintf()> format string followed by
988 one or more parameters. The default sub used simply calls C<printf "$format\n",
989 @_> but L<Test::Code::TidyAll> overrides this to use the C<<
990 Test::Builder->diag >> method.
991  
992 =back
993  
994 =head2 $tidyall->process_paths( $path, ... )
995  
996 This method iterates through a list of paths, processing all the files it
997 finds. It will descend into subdirectories if C<recursive> flag is true.
998 Returns a list of L<Code::TidyAll::Result> objects, one for each file.
999  
1000 =head2 $tidyall->process_file( $file )
1001  
1002 Process the one I<file>, meaning:
1003  
1004 =over 4
1005  
1006 =item *
1007  
1008 Check the cache and return immediately if file has not changed.
1009  
1010 =item *
1011  
1012 Apply appropriate matching plugins.
1013  
1014 =item *
1015  
1016 Print success or failure result to STDOUT, depending on quiet/verbose settings.
1017  
1018 =item *
1019  
1020 Write to the cache if caching is enabled.
1021  
1022 =item *
1023  
1024 Return a L<Code::TidyAll::Result> object.
1025  
1026 =back
1027  
1028 =head2 $tidyall->process_source( $source, $path )
1029  
1030 Like C<process_file>, but process the I<source> string instead of a file, and
1031 does not read from or write to the cache. You must still pass the relative
1032 I<path> from the root as the second argument, so that we know which plugins to
1033 apply. Returns a L<Code::TidyAll::Result> object.
1034  
1035 =head2 $tidyall->plugins_for_path($path)
1036  
1037 Given a relative I<path> from the root, returns a list of
1038 L<Code::TidyAll::Plugin> objects that apply to it, or an empty list if no
1039 plugins apply.
1040  
1041 =head2 $tidyall->find_matched_files
1042  
1043 Returns a list of sorted files that match at least one plugin in configuration.
1044  
1045 =head2 Code::TidyAll->find_conf_file( $conf_names, $start_dir )
1046  
1047 Start in the I<start_dir> and work upwards, looking for a file matching one of
1048 the I<conf_names>. Returns the pathname if found or throw an error if not
1049 found.
1050  
1051 =head1 SUPPORT
1052  
1053 Bugs may be submitted at L<https://github.com/houseabsolute/perl-code-tidyall/issues>.
1054  
1055 =head1 SOURCE
1056  
1057 The source code repository for Code-TidyAll can be found at L<https://github.com/houseabsolute/perl-code-tidyall>.
1058  
1059 =head1 AUTHORS
1060  
1061 =over 4
1062  
1063 =item *
1064  
1065 Jonathan Swartz <swartz@pobox.com>
1066  
1067 =item *
1068  
1069 Dave Rolsky <autarch@urth.org>
1070  
1071 =back
1072  
1073 =head1 CONTRIBUTORS
1074  
1075 =for stopwords Adam Herzog Andreas Vögele Andy Jack Bernhard Schmalhofer Finn Smith George Hartzell Graham Knop Gregory Oschwald Joe Crotty Kenneth Ölwing Mark Fowler Grimes Martin Gruner Mohammad S Anwar Nick Tonkin Olaf Alders Paulo Custodio Pavel Dostál Pedro Melo Ricardo Signes Sergey Romanov Shlomi Fish timgimyee
1076  
1077 =over 4
1078  
1079 =item *
1080  
1081 Adam Herzog <adam@adamherzog.com>
1082  
1083 =item *
1084  
1085 Andreas Vögele <andreas@andreasvoegele.com>
1086  
1087 =item *
1088  
1089 Andy Jack <andyjack@cpan.org>
1090  
1091 =item *
1092  
1093 Bernhard Schmalhofer <Bernhard.Schmalhofer@gmx.de>
1094  
1095 =item *
1096  
1097 Finn Smith <finn@timeghost.net>
1098  
1099 =item *
1100  
1101 George Hartzell <georgewh@gene.com>
1102  
1103 =item *
1104  
1105 Graham Knop <haarg@haarg.org>
1106  
1107 =item *
1108  
1109 Gregory Oschwald <goschwald@maxmind.com>
1110  
1111 =item *
1112  
1113 Joe Crotty <joe.crotty@returnpath.net>
1114  
1115 =item *
1116  
1117 Kenneth Ölwing <kenneth.olwing@skatteverket.se>
1118  
1119 =item *
1120  
1121 Mark Fowler <mark@twoshortplanks.com>
1122  
1123 =item *
1124  
1125 Mark Grimes <mgrimes@cpan.org>
1126  
1127 =item *
1128  
1129 Martin Gruner <martin.gruner@otrs.com>
1130  
1131 =item *
1132  
1133 Mohammad S Anwar <mohammad.anwar@yahoo.com>
1134  
1135 =item *
1136  
1137 Nick Tonkin <ntonkin@bur-ntonkin-m1.corp.endurance.com>
1138  
1139 =item *
1140  
1141 Olaf Alders <olaf@wundersolutions.com>
1142  
1143 =item *
1144  
1145 Paulo Custodio <pauloscustodio@gmail.com>
1146  
1147 =item *
1148  
1149 Pavel Dostál <pdostal@suse.cz>
1150  
1151 =item *
1152  
1153 Pedro Melo <melo@simplicidade.org>
1154  
1155 =item *
1156  
1157 Ricardo Signes <rjbs@cpan.org>
1158  
1159 =item *
1160  
1161 Sergey Romanov <sromanov-dev@yandex.ru>
1162  
1163 =item *
1164  
1165 Shlomi Fish <shlomif@shlomifish.org>
1166  
1167 =item *
1168  
1169 timgimyee <tim.gim.yee@gmail.com>
1170  
1171 =back
1172  
1173 =head1 COPYRIGHT AND LICENSE
1174  
1175 This software is copyright (c) 2011 - 2022 by Jonathan Swartz.
1176  
1177 This is free software; you can redistribute it and/or modify it under
1178 the same terms as the Perl 5 programming language system itself.
1179  
1180 The full text of the license can be found in the
1181 F<LICENSE> file included with this distribution.
1182  
1183 =cut
« blib/lib/Code/TidyAll/Git/Prereceive.pm ↑ Summary blib/lib/Code/TidyAll/Plugin/PodTidy.pm »