blib/lib/Perl/Critic/Distribution.pm

filter: Statementstmt 100% 228 / 228 Branchbran 87.2% 89 / 102 Conditioncond 80.0% 76 / 95 MC/DCmcdc 53.0% 26 / 49 Subroutinesub 100% 42 / 42 Podpod 100% 14 / 14 totaltotal 89.6% 475 / 530 CC 94 SCAR 24.8
CC
94
Cov
92%
__ANON__
29.4
root_of
18.0
__ANON__
10.6
CRAP 11.9
↑ Summary
1 package Perl::Critic::Distribution 0.003;
2  
3 # ABSTRACT: Parse a distribution once, for every policy that needs all of it.
4  
5 2 ▶use 5.014;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 2 ▶use warnings FATAL => 'all';
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 2 ▶no autovivification;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 2 ▶use re '/aa';
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 2 ▶use Readonly;
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 2 ▶use Cwd ();
Subroutine BEGIN: called CC=1 SCAR=0.0
17 2 ▶use Cpanel::JSON::XS ();
Subroutine BEGIN: called CC=1 SCAR=0.0
18 2 ▶use File::Basename ();
Subroutine BEGIN: called CC=1 SCAR=0.0
19 2 ▶use File::Find ();
Subroutine BEGIN: called CC=1 SCAR=0.0
20 2 ▶use File::Path ();
Subroutine BEGIN: called CC=1 SCAR=0.0
21 2 ▶use File::Slurper ();
Subroutine BEGIN: called CC=1 SCAR=0.0
22 2 ▶use File::Slurper::Temp ();
Subroutine BEGIN: called CC=1 SCAR=0.0
23 2 ▶use File::Spec ();
Subroutine BEGIN: called CC=1 SCAR=0.0
24 2 ▶use Digest::SHA ();
Subroutine BEGIN: called CC=1 SCAR=0.0
25 2 ▶use List::Util ();
Subroutine BEGIN: called CC=1 SCAR=0.0
26 2 ▶use IO::Compress::Gzip ();
Subroutine BEGIN: called CC=1 SCAR=0.0
27 2 ▶use IO::Uncompress::Gunzip ();
Subroutine BEGIN: called CC=1 SCAR=0.0
28 2 ▶use PPI ();
Subroutine BEGIN: called CC=1 SCAR=0.0
29 2 ▶use Time::HiRes ();
Subroutine BEGIN: called CC=1 SCAR=0.0
30  
31 2 ▶use Perl::Critic::Utils qw{ all_perl_files };
Subroutine BEGIN: called CC=1 SCAR=0.0
32  
33  
34 # One object per distribution root and cache directory, for the life of the
35 # process. our, so that a test can empty it and read the cache on disk as a
36 # new process does.
37 2 our %FOR;
38  
39 # The collectors registered in this process, by name. our for the same
40 # reason.
41 2 our %COLLECTORS;
42  
43 2 Readonly::Array my @AREAS => qw{ bin lib t xt };
44  
45 2 Readonly::Array my @DIST_MARKERS => qw{ dist.ini Makefile.PL Build.PL META.json META.yml cpanfile .git };
46  
47 # Change it when what the cache holds changes shape.
48 2 Readonly::Scalar my $CACHE_FORMAT => 1;
49  
50 2 Readonly::Scalar my $CACHE_NAME => 'perl-critic-distribution';
51  
52 2 Readonly::Array my @CALLED_BY_PERL => qw{
53 BEGIN END INIT CHECK UNITCHECK AUTOLOAD DESTROY import unimport CLONE CLONE_SKIP
54 BUILD BUILDARGS DEMOLISH FOREIGNBUILDARGS
55 TIESCALAR TIEARRAY TIEHASH TIEHANDLE FETCH STORE FETCHSIZE STORESIZE EXTEND
56 EXISTS DELETE CLEAR PUSH POP SHIFT UNSHIFT SPLICE FIRSTKEY NEXTKEY SCALAR UNTIE
57 PRINT PRINTF WRITE READ READLINE GETC CLOSE OPEN BINMODE EOF FILENO SEEK TELL
58 };
59  
60 # What the files in the cache directory are named.
61 2 Readonly::Scalar my $CACHE_FILE_RX => qr/\A [[:xdigit:]]{40} [.]json[.]gz \z/xs;
62  
63 38 ▶my $cache_path = sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
64 38 my ($self) = @_;
65  
66 38 ▶ return if !defined $self->{cache_dir};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $$self{"cache_dir"} 5 33 38
67 33 return File::Spec->catfile( $self->{cache_dir}, Digest::SHA::sha1_hex( $self->{root} ) . '.json.gz' );
68 2 };
69  
70 # The format of the cache and the stamp of this file. A cache made by another
71 # version of this module, or by this one before an edit, has a different key.
72 25 ▶my $cache_key = sub {
Subroutine __ANON__: called CC=2 SCAR=7.6
73 25 ▶ return join q{/}, $CACHE_FORMAT, __PACKAGE__->stamp(__FILE__) // q{};
Condition50%1/2
__PACKAGE__->stamp(__FILE__) // ""
l!l
250
MC/DC0%0/1
__PACKAGE__->stamp(__FILE__) // ""
__PACKAGE__->stamp(__FILE__)
Truth table50%1/2
A
A= __PACKAGE__->stamp(__FILE__)
Aresult
11
00
74 2 };
75  
76 # Replaces the file whole, so a reader never sees half of it. A cache that
77 # cannot be written costs the next run a parse, and nothing else.
78 18 ▶my $write_cache = sub {
Subroutine __ANON__: called CC=5 SCAR=16.6
79 18 my ($self) = @_;
80  
81 18 ▶ my $path = $self->$cache_path() or return;
Branch: 1 branch
BranchTrueFalseTotal
my $path = $self->$cache_path or return 15 3 18
82 15 my $json = Cpanel::JSON::XS->new->canonical->encode( { key => $cache_key->(), root => $self->{root}, files => $self->{files}, stash => $self->{stash} } );
83  
84 # The root goes in the gzip header too, so that the pruning below can read
85 # it without decompressing the file.
86 15 ▶ IO::Compress::Gzip::gzip( \$json => \my $gz, Comment => $self->{root} ) or return;
Branch: 1 branch
BranchTrueFalseTotal
IO::Compress::Gzip::gzip(\$json, \my $gz, "Comment", $$self{"root"}) or return 15 0 15
87  
88 15 File::Path::make_path( $self->{cache_dir}, { error => \my $errors } );
89 15 ▶ return if @$errors;
Branch: 1 branch
BranchTrueFalseTotal
if @$errors 0 15 15
90  
91 15 ▶ eval { File::Slurper::Temp::write_binary( $path, $gz ); 1 } or return;
Branch: 1 branch
BranchTrueFalseTotal
eval { do { File::Slurper::Temp::write_binary($path, $gz); 1 } } or return 15 0 15
92  
93 # Removes the cache of each root that is gone, such as a deleted checkout.
94 # A file whose header cannot be read is removed too, since nothing can read
95 # it either.
96 15 my $dir = $self->{cache_dir};
97 File::Find::find(
98 {
99 no_chdir => 1,
100 32 ▶ wanted => sub {
Subroutine __ANON__: called CC=9 SCAR=23.6
101 32 ▶ $File::Find::prune = 1 if -d $File::Find::name && $File::Find::name ne $dir;
Branch: 1 branch
BranchTrueFalseTotal
if -d $File::Find::name and $File::Find::name ne $dir 0 32 32
Condition66%2/3
-d $File::Find::name and $File::Find::name ne $dir
!ll&&!rl&&r
17150
MC/DC0%0/2
-d $File::Find::name and $File::Find::name ne $dir
-d $File::Find::name $File::Find::name ne $dir
Truth table66%2/3
A and B
A= -d $File::Find::nameB= $File::Find::name ne $dir
ABresult
0X0
100
111
102 32 ▶ return if File::Basename::basename($File::Find::name) !~ m/$CACHE_FILE_RX/xs;
Branch: 1 branch
BranchTrueFalseTotal
unless File::Basename::basename($File::Find::name) =~ /$CACHE_FILE_RX/aasx 16 16 32
103  
104 16 my $z = IO::Uncompress::Gunzip->new($File::Find::name);
105 16 ▶ my $header = $z && $z->getHeaderInfo();
Condition33%1/3
$z && $z->getHeaderInfo
!ll&&!rl&&r
0016
MC/DC0%0/2
$z && $z->getHeaderInfo
$z $z->getHeaderInfo
Truth table33%1/3
A && B
A= $zB= $z->getHeaderInfo
ABresult
0X0
100
111
106 16 ▶ $z->close() if $z;
Branch: 1 branch
BranchTrueFalseTotal
if $z 16 0 16
107 16 ▶ my $root = ref $header eq 'HASH' ? $header->{Comment} : undef;
Branch: 1 branch
BranchTrueFalseTotal
ref $header eq 'HASH' ? : 16 0 16
108 16 ▶ unlink $File::Find::name if !defined $root || !-d $root;
Branch: 1 branch
BranchTrueFalseTotal
if not defined $root && -d $root 1 15 16
Condition66%2/3
defined $root && -d $root
!ll&&!rl&&r
0115
MC/DC50%1/2
defined $root && -d $root
defined $root -d $root
Truth table66%2/3
A && B
A= defined $rootB= -d $root
ABresult
0X0
100
111
109 },
110 },
111 15 $dir
112 );
113 15 return 1;
114 2 };
115  
116 # Brings what each registered collector returned up to date, for each file of
117 # the distribution: from this object, then from the cache on disk, and by
118 # parsing what neither has. Each file is parsed at most once, whatever number
119 # of collectors need it.
120 28 ▶my $refresh = sub {
Subroutine __ANON__: called CC=29 SCAR=33.8
121 28 my ($self) = @_;
122  
123 # Every registered collector, at its version, already has data for every
124 # file, in this process.
125 28 ▶ return if $self->{files} && !List::Util::any { ( $self->{current}{$_} // q{} ) ne $COLLECTORS{$_}{version} } keys %COLLECTORS;
Subroutine __ANON__: called CC=2 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
if $$self{"files"} and not &List::Util::any(sub { ($$self{"current"}{$_} // "") ne $COLLECTORS{$_}{"version"}; } , keys %Perl::Critic::Distribution::COLLECTORS) 7 21 28
Condition100%5/5
$$self{"files"} and not &List::Util::any(sub { ($$self{"current"}{$_} // "") ne $COLLECTORS{$_}{"version"}; } , keys %Perl::Critic::Distribution::COLLECTORS)
!ll&&!rl&&r
2017
$$self{"current"}{$_} // ""
l!l
121
MC/DC100%2/2
$$self{"files"} and not &List::Util::any(sub { ($$self{"current"}{$_} // "") ne $COLLECTORS{$_}{"version"}; } , keys %Perl::Critic::Distribution::COLLECTORS)
$$self{"files"} not &List::Util::any(sub { ($$self{"current"}{$_} // "") ne $COLLECTORS{$_}{"version"}; } , keys %Perl::Critic::Distribution::COLLECTORS)
MC/DC100%1/1
$$self{"current"}{$_} // ""
$$self{"current"}{$_}
Truth table100%3/3
A and not(B)
A= $$self{"files"}B= not &List::Util::any(sub { ($$self{"current"}{$_} // "") ne $COLLECTORS{$_}{"version"}; } , keys %Perl::Critic::Distribution::COLLECTORS)
ABresult
0X0
100
111
Truth table100%2/2
A
A= $$self{"current"}{$_}
Aresult
11
00
126  
127 21 ▶ my $cache = $self->{files} ? { files => $self->{files}, stash => $self->{stash} } : {};
Branch: 1 branch
BranchTrueFalseTotal
$$self{'files'} ? : 1 20 21
128 21 ▶ if ( !$self->{files} && ( my $path = $self->$cache_path() ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $$self{"files"} and my $path = $self->$cache_path) 18 3 21
Condition100%3/3
not $$self{"files"} and my $path = $self->$cache_path
!ll&&!rl&&r
1218
MC/DC100%2/2
not $$self{"files"} and my $path = $self->$cache_path
not $$self{"files"} my $path = $self->$cache_path
Truth table100%3/3
not(A) and B
A= not $$self{"files"}B= my $path = $self->$cache_path
ABresult
0X0
100
111
129  
130 # A cache that is not there fails to read, like one that is unreadable.
131 18 my $read = eval {
132 18 my $gz = File::Slurper::read_binary($path);
133 12 ▶ IO::Uncompress::Gunzip::gunzip( \$gz => \my $json ) or return;
Branch: 1 branch
BranchTrueFalseTotal
IO::Uncompress::Gunzip::gunzip(\$gz, \my $json) or return 11 1 12
134 11 Cpanel::JSON::XS->new->decode($json);
135 };
136 18 ▶ $cache = $read if ref $read eq 'HASH' && ( $read->{key} // q{} ) eq $cache_key->() && ref $read->{files} eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
if ref $read eq "HASH" and ($$read{"key"} // "") eq &$cache_key() and ref $$read{"files"} eq "HASH" 9 9 18
Condition75%6/8
ref $read eq "HASH" and ($$read{"key"} // "") eq &$cache_key() and ref $$read{"files"} eq "HASH"
!ll&&!rl&&r
909
ref $read eq "HASH" and ($$read{"key"} // "") eq &$cache_key()
!ll&&!rl&&r
819
$$read{"key"} // ""
l!l
100
MC/DC33%1/3
ref $read eq "HASH" and ($$read{"key"} // "") eq &$cache_key() and ref $$read{"files"} eq "HASH"
ref $read eq "HASH" $$read{"key"} ref $$read{"files"} eq "HASH"
Truth table50%2/4
A and B and C
A= ref $read eq "HASH"B= $$read{"key"}C= ref $$read{"files"} eq "HASH"
ABCresult
0XX0
10X0
1100
1111
137 }
138  
139 21 ▶ my $cached = $cache->{files} // {};
Condition100%2/2
$$cache{"files"} // {}
l!l
1011
MC/DC100%1/1
$$cache{"files"} // {}
$$cache{"files"}
Truth table100%2/2
A
A= $$cache{"files"}
Aresult
11
00
140 21 my %now;
141 21 my $changed = 0;
142  
143 21 foreach my $area (@AREAS) {
144 84 my $path = File::Spec->catdir( $self->{root}, $area );
145 84 ▶ next if !-d $path;
Branch: 1 branch
BranchTrueFalseTotal
unless -d $path 0 84 84
146  
147 84 foreach my $file ( sort( all_perl_files($path) ) ) {
148 103 ▶ my $stamp = $self->stamp($file) // next;
Condition50%1/2
$self->stamp($file) // (next)
l!l
1030
MC/DC0%0/1
$self->stamp($file) // (next)
$self->stamp($file)
Truth table50%1/2
A
A= $self->stamp($file)
Aresult
11
00
149  
150 103 my $entry = $cached->{$file};
151 103 ▶ if ( ref $entry ne 'HASH' || ( $entry->{stamp} // q{} ) ne $stamp || ref $entry->{collected} ne 'HASH' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $entry ne "HASH" or ($$entry{"stamp"} // "") ne $stamp or ref $$entry{"collected"} ne "HASH") 60 43 103
Condition87%7/8
ref $entry ne "HASH" or ($$entry{"stamp"} // "") ne $stamp or ref $$entry{"collected"} ne "HASH"
l!l&&r!l&&!r
59143
ref $entry ne "HASH" or ($$entry{"stamp"} // "") ne $stamp
l!l&&r!l&&!r
58144
$$entry{"stamp"} // ""
l!l
450
MC/DC0%0/3
ref $entry ne "HASH" or ($$entry{"stamp"} // "") ne $stamp or ref $$entry{"collected"} ne "HASH"
ref $entry ne "HASH" $$entry{"stamp"} ref $$entry{"collected"} ne "HASH"
Truth table50%2/4
A or B or C
A= ref $entry ne "HASH"B= $$entry{"stamp"}C= ref $$entry{"collected"} ne "HASH"
ABCresult
1XX1
01X1
0011
0000
152 60 $entry = { stamp => $stamp, area => $area, collected => {} };
153 60 $changed = 1;
154 }
155  
156 # The collectors that have nothing for this file, at their version.
157 my @needed = grep {
158 103 my $held = $entry->{collected}{$_};
159 !( ref $held eq 'HASH' && exists $held->{data} && ( $held->{version} // q{} ) eq $COLLECTORS{$_}{version} )
160 156 ▶ } sort keys %COLLECTORS;
Condition75%6/8
ref $held eq 'HASH' && exists $$held{'data'} && ($$held{'version'} // '') eq $Perl::Critic::Distribution::COLLECTORS{$_}{'version'}
!ll&&!rl&&r
851061
ref $held eq "HASH" and exists $$held{"data"}
!ll&&!rl&&r
85071
$$held{"version"} // ""
l!l
710
MC/DC33%1/3
ref $held eq 'HASH' && exists $$held{'data'} && ($$held{'version'} // '') eq $Perl::Critic::Distribution::COLLECTORS{$_}{'version'}
ref $held eq "HASH" exists $$held{"data"} $$held{"version"}
Truth table50%2/4
A and B && C
A= ref $held eq "HASH"B= exists $$held{"data"}C= $$held{"version"}
ABCresult
0XX0
10X0
1100
1111
161  
162 103 ▶ if (@needed) {
Branch: 1 branch
BranchTrueFalseTotal
if (@needed) 75 28 103
163  
164 # Not kept, so a file that PPI cannot read is tried again by
165 # the next process, as it would be with no cache.
166 75 ▶ my $ppi = PPI::Document->new($file) or next;
Branch: 1 branch
BranchTrueFalseTotal
my $ppi = "PPI::Document"->new($file) or next 75 0 75
167 75 foreach my $name (@needed) {
168 95 my $collector = $COLLECTORS{$name};
169 95 $entry->{collected}{$name} = { version => $collector->{version}, data => $collector->{collect}->( $ppi, $file, $area ) };
170 }
171 75 $changed = 1;
172 }
173 103 $now{$file} = $entry;
174 }
175 }
176  
177 # A file in the cache that is gone is a change too.
178 21 ▶ $changed ||= List::Util::any { !$now{$_} } keys %$cached;
Subroutine __ANON__: called CC=1 SCAR=0.0
Condition100%3/3
$changed ||= &List::Util::any(sub { not $now{$_}; } , keys %$cached)
l!l&&r!l&&!r
1614
MC/DC100%2/2
$changed ||= &List::Util::any(sub { not $now{$_}; } , keys %$cached)
$changed &List::Util::any(sub { not $now{$_}; } , keys %$cached)
Truth table100%3/3
A ||= B
A= $changedB= &List::Util::any(sub { not $now{$_}; } , keys %$cached)
ABresult
1X1
011
000
179  
180 21 $self->{files} = \%now;
181 21 ▶ $self->{stash} = ref $cache->{stash} eq 'HASH' ? $cache->{stash} : {};
Branch: 1 branch
BranchTrueFalseTotal
ref $$cache{'stash'} eq 'HASH' ? : 9 12 21
182 21 $self->{current} = { map { $_ => $COLLECTORS{$_}{version} } keys %COLLECTORS };
183  
184 21 ▶ $self->$write_cache() if $changed;
Branch: 1 branch
BranchTrueFalseTotal
if $changed 17 4 21
185 21 return;
186 2 };
187  
188  
189 16 ▶sub register {
Subroutine register: called CC=5 SCAR=16.1
Pod documented
190 16 my ( $class, %opts ) = @_;
191  
192 16 my $name = $opts{name};
193 16 ▶ die "A collector needs a name\n" if ( $name // q{} ) eq q{};
Branch: 1 branch
BranchTrueFalseTotal
if ($name // "") eq "" 1 15 16
Condition100%2/2
$name // ""
l!l
151
MC/DC100%1/1
$name // ""
$name
Truth table100%2/2
A
A= $name
Aresult
11
00
194 15 ▶ die "The collector $name needs collect, a code reference\n" if ref $opts{collect} ne 'CODE';
Branch: 1 branch
BranchTrueFalseTotal
if ref $opts{"collect"} ne "CODE" 1 14 15
195  
196 14 ▶ $COLLECTORS{$name} = { version => $opts{version} // q{}, collect => $opts{collect} };
Condition100%2/2
$opts{"version"} // ""
l!l
131
MC/DC100%1/1
$opts{"version"} // ""
$opts{"version"}
Truth table100%2/2
A
A= $opts{"version"}
Aresult
11
00
197 14 return $name;
198 }
199  
200  
201 129 ▶sub stamp {
Subroutine stamp: called CC=2 SCAR=7.0
Pod documented
202 129 my ( $class, $file ) = @_;
203  
204 129 ▶ my @st = Time::HiRes::stat($file) or return;
Branch: 1 branch
BranchTrueFalseTotal
my(@st) = &Time::HiRes::stat($file) or return 129 0 129
205 129 return join q{:}, @st[ 0, 1, 7 ], map { sprintf '%.9f', $_ } @st[ 9, 10 ];
206 }
207  
208  
209 41 ▶sub root_of {
Subroutine root_of: called CC=18 SCAR=28.9
Pod documented
210 41 my ( $class, $file ) = @_;
211  
212 41 ▶ return if !defined $file;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $file 1 40 41
213 40 ▶ my $path = Cwd::abs_path($file) // return;
Condition50%1/2
Cwd::abs_path($file) // (return)
l!l
400
MC/DC0%0/1
Cwd::abs_path($file) // (return)
Cwd::abs_path($file)
Truth table50%1/2
A
A= Cwd::abs_path($file)
Aresult
11
00
214  
215 40 my ( $volume, $directories ) = File::Spec->splitpath($path);
216 40 my @dirs = File::Spec->splitdir($directories);
217 40 ▶ pop @dirs while @dirs && $dirs[-1] eq q{};
Branch: 1 branch
BranchTrueFalseTotal
@dirs and $dirs[-1] eq "" and do { pop @dirs } 40 40 80
Condition66%2/3
@dirs and $dirs[-1] eq ""
!ll&&!rl&&r
04040
MC/DC50%1/2
@dirs and $dirs[-1] eq ""
@dirs $dirs[-1] eq ""
Truth table66%2/3
A and B
A= @dirsB= $dirs[-1] eq ""
ABresult
0X0
100
111
218  
219 40 my %area = map { $_ => 1 } @AREAS;
220 40 my @fallback;
221 40 foreach my $depth ( reverse 0 .. $#dirs ) {
222 87 my $ancestor = File::Spec->catpath( $volume, File::Spec->catdir( @dirs[ 0 .. $depth ] ), q{} );
223 87 ▶ my $area = $depth < $#dirs && $area{ $dirs[ $depth + 1 ] } ? $dirs[ $depth + 1 ] : undef;
Branch: 1 branch
BranchTrueFalseTotal
$depth < $#dirs && $area{$dirs[$depth + 1]} ? : 38 49 87
Condition100%3/3
$depth < $#dirs and $area{$dirs[$depth + 1]}
!ll&&!rl&&r
40938
MC/DC100%2/2
$depth < $#dirs and $area{$dirs[$depth + 1]}
$depth < $#dirs $area{$dirs[$depth + 1]}
Truth table100%3/3
A and B
A= $depth < $#dirsB= $area{$dirs[$depth + 1]}
ABresult
0X0
100
111
224 87 ▶ $area //= 'lib' if $depth + 1 < $#dirs && $dirs[ $depth + 1 ] eq 'blib' && $dirs[ $depth + 2 ] eq 'lib';
Branch: 1 branch
BranchTrueFalseTotal
if $depth + 1 < $#dirs and $dirs[$depth + 1] eq "blib" and $dirs[$depth + 2] eq "lib" 1 86 87
Condition87%7/8
$depth + 1 < $#dirs and $dirs[$depth + 1] eq "blib" and $dirs[$depth + 2] eq "lib"
!ll&&!rl&&r
8511
$depth + 1 < $#dirs and $dirs[$depth + 1] eq "blib"
!ll&&!rl&&r
8052
$area //= "lib"
l!l
01
MC/DC100%3/3
$depth + 1 < $#dirs and $dirs[$depth + 1] eq "blib" and $dirs[$depth + 2] eq "lib"
$depth + 1 < $#dirs $dirs[$depth + 1] eq "blib" $dirs[$depth + 2] eq "lib"
MC/DC0%0/1
$area //= "lib"
$area
Truth table100%4/4
A and B and C
A= $depth + 1 < $#dirsB= $dirs[$depth + 1] eq "blib"C= $dirs[$depth + 2] eq "lib"
ABCresult
0XX0
10X0
1100
1111
Truth table50%1/2
A
A= $area
Aresult
11
00
225  
226 87 my $marked = 0;
227 87 foreach my $marker (@DIST_MARKERS) {
228 375 ▶ $marked = -e File::Spec->catfile( $ancestor, $marker ) and last;
Branch: 1 branch
BranchTrueFalseTotal
$marked = -e "File::Spec"->catfile($ancestor, $marker) and last 39 336 375
229 }
230  
231 87 ▶ if ($marked) {
Branch: 1 branch
BranchTrueFalseTotal
if ($marked) 39 48 87
232 39 ▶ return if !defined $area;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $area 3 36 39
233 36 return ( $ancestor, $area );
234 }
235 48 ▶ @fallback = ( $ancestor, $area ) if !@fallback && defined $area;
Branch: 1 branch
BranchTrueFalseTotal
if not @fallback and defined $area 3 45 48
Condition100%3/3
not @fallback and defined $area
!ll&&!rl&&r
2433
MC/DC100%2/2
not @fallback and defined $area
not @fallback defined $area
Truth table100%3/3
not(A) and B
A= not @fallbackB= defined $area
ABresult
0X0
100
111
236 }
237 1 return @fallback;
238 }
239  
240  
241 31 ▶sub default_cache_dir {
Subroutine default_cache_dir: called CC=4 SCAR=16.1
Pod documented
242 31 ▶ my $base = $ENV{XDG_CACHE_HOME} || ( $ENV{HOME} && File::Spec->catdir( $ENV{HOME}, '.cache' ) ) or return;
Branch: 1 branch
BranchTrueFalseTotal
my $base = $ENV{"XDG_CACHE_HOME"} || $ENV{"HOME"} && "File::Spec"->catdir($ENV{"HOME"}, ".cache") or return 31 0 31
Condition50%3/6
$ENV{'XDG_CACHE_HOME'} || $ENV{'HOME'} && 'File::Spec'->catdir($ENV{'HOME'}, '.cache')
l!l&&r!l&&!r
8230
$ENV{"HOME"} and "File::Spec"->catdir($ENV{"HOME"}, ".cache")
!ll&&!rl&&r
0023
MC/DC0%0/3
$ENV{'XDG_CACHE_HOME'} || $ENV{'HOME'} && 'File::Spec'->catdir($ENV{'HOME'}, '.cache')
$ENV{'XDG_CACHE_HOME'} $ENV{"HOME"} "File::Spec"->catdir($ENV{"HOME"}, ".cache")
Truth table50%2/4
A || B and C
A= $ENV{'XDG_CACHE_HOME'}B= $ENV{"HOME"}C= "File::Spec"->catdir($ENV{"HOME"}, ".cache")
ABCresult
1XX1
0111
00X0
0100
243 31 return File::Spec->catdir( $base, $CACHE_NAME );
244 }
245  
246  
247 32 ▶sub for_file {
Subroutine for_file: called CC=9 SCAR=22.0
Pod documented
248 32 my ( $class, $file, %opts ) = @_;
249  
250 32 ▶ my ($root) = $class->root_of($file) or return;
Branch: 1 branch
BranchTrueFalseTotal
my($root) = $class->root_of($file) or return 31 1 32
251 31 my $cache_dir = $class->default_cache_dir();
252 31 ▶ if ( exists $opts{cache} ) {
Branch: 2 branches
BranchTrueFalseTotal
if (exists $opts{'cache'}) { } 3 28 31
elsif (exists $opts{'cache_dir'}) { } 26 2 28
253 3 ▶ $cache_dir = !$opts{cache} ? undef : ( $opts{cache_dir} // q{} ) eq q{} ? $cache_dir : $opts{cache_dir};
Branch: 2 branches
BranchTrueFalseTotal
!$opts{'cache'} ? : 1 2 3
($opts{'cache_dir'} // '') eq '' ? : 1 1 2
Condition50%1/2
$opts{"cache_dir"} // ""
l!l
20
MC/DC0%0/1
$opts{"cache_dir"} // ""
$opts{"cache_dir"}
Truth table50%1/2
A
A= $opts{"cache_dir"}
Aresult
11
00
254 }
255 elsif ( exists $opts{cache_dir} ) {
256 26 $cache_dir = $opts{cache_dir};
257 }
258  
259 31 ▶ return $FOR{ join "\0", $root, $cache_dir // q{} } //= bless { root => $root, cache_dir => $cache_dir }, $class;
Condition100%4/4
$Perl::Critic::Distribution::FOR{join "\0", $root, $cache_dir // ""} //= bless({"root", $root, "cache_dir", $cache_dir}, $class)
l!l
625
$cache_dir // ""
l!l
256
MC/DC100%1/1
$Perl::Critic::Distribution::FOR{join "\0", $root, $cache_dir // ""} //= bless({"root", $root, "cache_dir", $cache_dir}, $class)
$Perl::Critic::Distribution::FOR{join "\0", $root, $cache_dir // ""}
MC/DC100%1/1
$cache_dir // ""
$cache_dir
Truth table100%2/2
A
A= $Perl::Critic::Distribution::FOR{join "\0", $root, $cache_dir // ""}
Aresult
11
00
Truth table100%2/2
A
A= $cache_dir
Aresult
11
00
260 }
261  
262  
263 1 ▶sub root {
Subroutine root: called CC=1 SCAR=0.0
Pod documented
264 1 my ($self) = @_;
265 1 return $self->{root};
266 }
267  
268  
269 21 ▶sub collected {
Subroutine collected: called CC=2 SCAR=6.9
Pod documented
270 21 my ( $self, $name ) = @_;
271  
272 21 ▶ die "No collector is registered as $name\n" if !$COLLECTORS{$name};
Branch: 1 branch
BranchTrueFalseTotal
unless $Perl::Critic::Distribution::COLLECTORS{$name} 1 20 21
273 20 $self->$refresh();
274  
275 20 my $files = $self->{files};
276 20 return { map { $_ => $files->{$_}{collected}{$name}{data} } keys %$files };
277 }
278  
279  
280 2 ▶sub area_of {
Subroutine area_of: called CC=2 SCAR=6.9
Pod documented
281 2 my ( $self, $file ) = @_;
282  
283 2 $self->$refresh();
284 2 ▶ my $entry = $self->{files}{$file} or return;
Branch: 1 branch
BranchTrueFalseTotal
my $entry = $$self{"files"}{$file} or return 1 1 2
285 1 return $entry->{area};
286 }
287  
288  
289 1 ▶sub stamp_of {
Subroutine stamp_of: called CC=2 SCAR=7.0
Pod documented
290 1 my ( $self, $file ) = @_;
291  
292 1 $self->$refresh();
293 1 ▶ my $entry = $self->{files}{$file} or return;
Branch: 1 branch
BranchTrueFalseTotal
my $entry = $$self{"files"}{$file} or return 1 0 1
294 1 return $entry->{stamp};
295 }
296  
297  
298 4 ▶sub stash {
Subroutine stash: called CC=5 SCAR=16.2
Pod documented
299 4 my ( $self, $name ) = @_;
300  
301 4 ▶ die "No collector is registered as $name\n" if !$COLLECTORS{$name};
Branch: 1 branch
BranchTrueFalseTotal
unless $Perl::Critic::Distribution::COLLECTORS{$name} 0 4 4
302 4 $self->$refresh();
303  
304 4 my $kept = $self->{stash}{$name};
305 4 ▶ return if ref $kept ne 'HASH' || ( $kept->{version} // q{} ) ne $COLLECTORS{$name}{version};
Branch: 1 branch
BranchTrueFalseTotal
if ref $kept ne "HASH" or ($$kept{"version"} // "") ne $Perl::Critic::Distribution::COLLECTORS{$name}{"version"} 2 2 4
Condition80%4/5
ref $kept ne "HASH" or ($$kept{"version"} // "") ne $Perl::Critic::Distribution::COLLECTORS{$name}{"version"}
l!l&&r!l&&!r
112
$$kept{"version"} // ""
l!l
30
MC/DC0%0/2
ref $kept ne "HASH" or ($$kept{"version"} // "") ne $Perl::Critic::Distribution::COLLECTORS{$name}{"version"}
ref $kept ne "HASH" $$kept{"version"}
Truth table66%2/3
A or B
A= ref $kept ne "HASH"B= $$kept{"version"}
ABresult
1X1
011
000
306 2 return $kept->{data};
307 }
308  
309  
310 1 ▶sub keep {
Subroutine keep: called CC=2 SCAR=7.0
Pod documented
311 1 my ( $self, $name, $data ) = @_;
312  
313 1 ▶ die "No collector is registered as $name\n" if !$COLLECTORS{$name};
Branch: 1 branch
BranchTrueFalseTotal
unless $Perl::Critic::Distribution::COLLECTORS{$name} 0 1 1
314 1 $self->$refresh();
315  
316 1 $self->{stash}{$name} = { version => $COLLECTORS{$name}{version}, data => $data };
317 1 return $self->$write_cache();
318 }
319  
320  
321 1 ▶sub policy_parameters {
Subroutine policy_parameters: called CC=1 SCAR=0.0
Pod documented
322 return (
323 {
324 1 name => 'cache',
325 description => 'Keep what was read of each distribution on disk between runs.',
326 default_string => '1',
327 behavior => 'boolean',
328 },
329 {
330 name => 'cache_dir',
331 description => 'Where it is kept. Empty means the default of Perl::Critic::Distribution.',
332 default_string => q{},
333 behavior => 'string',
334 },
335 );
336 }
337  
338  
339 1 ▶sub called_by_perl {
Subroutine called_by_perl: called CC=1 SCAR=0.0
Pod documented
340 1 return @CALLED_BY_PERL;
341 }
342  
343  
344 3 ▶sub walk {
Subroutine walk: called CC=10 SCAR=23.0
Pod documented
345 3 my ( $class, $node, $visit, %context ) = @_;
346  
347 # Each frame is the children of a node, how far through them the walk is,
348 # and the package and the sub there. A loop and not recursion, because a deep
349 # document would otherwise warn of deep recursion, which is fatal here.
350 3 ▶ my @frames = ( [ [ $node->children() ], 0, $context{package} // 'main', $context{sub} ] );
Condition100%2/2
$context{"package"} // "main"
l!l
12
MC/DC100%1/1
$context{"package"} // "main"
$context{"package"}
Truth table100%2/2
A
A= $context{"package"}
Aresult
11
00
351 3 ▶ while (@frames) {
Branch: 1 branch
BranchTrueFalseTotal
if (@frames) 190 3 193
352 190 my $frame = $frames[-1];
353 190 my ( $children, $at, $package, $sub ) = @$frame;
354 190 my $child = $children->[$at];
355 190 ▶ if ( !defined $child ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $child) 49 141 190
356 49 pop @frames;
357 49 next;
358 }
359 141 $frame->[1]++;
360  
361 141 $visit->( $child, $package, $sub );
362  
363 141 ▶ if ( $child->isa('PPI::Statement::Package') ) {
Branch: 3 branches
BranchTrueFalseTotal
if ($child->isa('PPI::Statement::Package')) { } 4 137 141
elsif ($child->isa('PPI::Statement::Sub') and not $child->forward) { } 8 129 137
elsif ($child->isa('PPI::Node')) { } 36 93 129
364 4 my ($block) = grep { $_->isa('PPI::Structure::Block') } $child->schildren();
365 4 ▶ if ($block) {
Branch: 1 branch
BranchTrueFalseTotal
if ($block) { } 2 2 4
366 2 push @frames, [ [ $block->children() ], 0, $child->namespace(), $sub ];
367 }
368 else {
369 2 $frame->[2] = $child->namespace();
370 }
371 }
372 ▶ elsif ( $child->isa('PPI::Statement::Sub') && !$child->forward() ) {
Condition100%3/3
$child->isa("PPI::Statement::Sub") and not $child->forward
!ll&&!rl&&r
12728
MC/DC100%2/2
$child->isa("PPI::Statement::Sub") and not $child->forward
$child->isa("PPI::Statement::Sub") not $child->forward
Truth table100%3/3
A and not(B)
A= $child->isa("PPI::Statement::Sub")B= not $child->forward
ABresult
0X0
100
111
373 8 ( my $name = $child->name() ) =~ s/\A::/main::/xs;
374 8 ▶ push @frames, [ [ $child->children() ], 0, $package, index( $name, '::' ) >= 0 ? $name : "${package}::$name" ];
Branch: 1 branch
BranchTrueFalseTotal
index($name, '::') != -1 ? : 4 4 8
375 }
376 elsif ( $child->isa('PPI::Node') ) {
377 36 push @frames, [ [ $child->children() ], 0, $package, $sub ];
378 }
379 }
380 3 return;
381 }
382  
383 2 1;
384  
385 __END__
↑ Summary