blib/lib/PPI/Xref.pm

filter: Statementstmt 85.6% 791 / 924 Branchbran 69.7% 279 / 400 Conditioncond 59.7% 95 / 159 MC/DCmcdc 28.4% 25 / 88 Subroutinesub 84.7% 111 / 131 Podpod 70.5% 24 / 34 totaltotal 76.3% 1325 / 1736 CC 291 SCAR 25.0
CC
291
Cov
79%
__process_id
51.4
__incs_chains_recurse
30.0
__process_file
21.6
CRAP 12.2
« blib/lib/PPI/Xref/Help.pm ↑ Summary util/ppixref »
1 package PPI::Xref;
2  
3 33 require v5.14; # 5.10: defined-or; 5.14: package Sub { ... }
4  
5 33 our $VERSION = '0.009';
6  
7 33 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 33 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 33 ▶use PPI;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 # We will use file ids (integers) instead of filenames, mainly for
13 # space savings, but also speed, and only convert back to filenames
14 # on leaving the API boundary.
15 33 my $FILE_ID = 0;
16 33 my %FILE_BY_ID;
17  
18 my %CTOR_OPTS =
19 33 map { $_ => 1} qw/process_verbose cache_verbose recurse_verbose
20 recurse INC
21 cache_directory
22 cache_read_only
23 __allow_relative/;
24  
25 33 my $HASHALGO = 'sha1';
26  
27 33 package Sub { # For getting the current sub name.
28 33 ▶ sub TIESCALAR { bless \$_[1], $_[0] }
Subroutine TIESCALAR: called CC=1 SCAR=0.0
29 119 ▶ sub FETCH { (caller(1))[3] }
Subroutine FETCH: called CC=1 SCAR=0.0
30 }
31 33 tie my $Sub, 'Sub';
32  
33 sub __is_readwrite_directory {
34 58 ▶ my ($self, $dir) = @_;
Subroutine __is_readwrite_directory: called CC=3 SCAR=14.2
35 58 ▶ return -d $dir && -r $dir && -w $dir;
Condition33%2/6
-d $dir && -r $dir && -w $dir
!ll&&!rl&&r
0058
-d $dir and -r $dir
!ll&&!rl&&r
0058
MC/DC0%0/3
-d $dir && -r $dir && -w $dir
-d $dir -r $dir -w $dir
Truth table25%1/4
A and B && C
A= -d $dirB= -r $dirC= -w $dir
ABCresult
0XX0
10X0
1100
1111
36 }
37  
38 # Our constructor.
39 sub new {
40 38 ▶ my ($class, $opt) = @_;
Subroutine new: called CC=6 SCAR=18.0
Pod undocumented
41 38 ▶ $opt //= {};
Condition100%2/2
$opt //= {}
l!l
362
MC/DC100%1/1
$opt //= {}
$opt
Truth table100%2/2
A
A= $opt
Aresult
11
00
42 # In the opt you can specify:
43 # - process_verbose: for process progress
44 # - cache_verbose: for cache activity
45 # - recurse_verbose: for recurse, show revisits
46 # - INC: an aref for a custom @INC
47 # - recurse: or not (default: yes)
48 # - cache_directory: directory where the cache is
49 # - cache_read_only: only read the cache
50  
51 38 ▶ $opt->{recurse} //= 1;
Condition100%2/2
$opt->{"recurse"} //= 1
l!l
335
MC/DC100%1/1
$opt->{"recurse"} //= 1
$opt->{"recurse"}
Truth table100%2/2
A
A= $opt->{"recurse"}
Aresult
11
00
52  
53 38 my %unexpopt = %$opt;
54 38 delete @unexpopt{keys %CTOR_OPTS};
55  
56 38 for my $k (sort keys %unexpopt) {
57 0 warn "$Sub: unexpected option: $k\n";
58 }
59  
60 38 my $self = { opt => $opt };
61  
62 38 my $cache_directory = $opt->{cache_directory};
63 38 ▶ if (defined $cache_directory) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $cache_directory) 8 30 38
64 8 ▶ unless (PPI::Xref->__is_readwrite_directory($cache_directory)) {
Branch: 1 branch
BranchTrueFalseTotal
unless ("PPI::Xref"->__is_readwrite_directory($cache_directory)) 0 8 8
65 0 warn "$Sub: cache_directory '$cache_directory': not a read-write directory\n";
66 }
67  
68 8 $self->{__cache_prefix_length} = length($cache_directory) + 1;
69  
70 33 ▶ use Sereal::Encoder;
Subroutine BEGIN: called CC=1 SCAR=0.0
71 33 ▶ use Sereal::Decoder;
Subroutine BEGIN: called CC=1 SCAR=0.0
72 8 $self->{encoder} = Sereal::Encoder->new;
73 8 $self->{decoder} = Sereal::Decoder->new;
74 }
75  
76 38 bless $self, $class;
77 }
78  
79 # Unless $self->{inc_dirs} is set, set it from either opt INC, if set,
80 # or from the system @INC.
81 sub __inc_dirs {
82 167 ▶ my $self = shift;
Subroutine __inc_dirs: called CC=7 SCAR=20.1
83 167 ▶ unless (defined $self->{inc_dirs}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self->{"inc_dirs"}) 37 130 167
84 37 ▶ for my $d ($self->{opt}{INC} ? @{ $self->{opt}{INC}} : @INC) {
Branch: 1 branch
BranchTrueFalseTotal
$self->{'opt'}{'INC'} ? : 35 2 37
85 73 ▶ next if ref $d;
Branch: 1 branch
BranchTrueFalseTotal
if ref $d 0 73 73
86 73 ▶ next unless -d $d && -x $d;
Branch: 1 branch
BranchTrueFalseTotal
unless -d $d and -x $d 0 73 73
Condition33%1/3
-d $d and -x $d
!ll&&!rl&&r
0073
MC/DC0%0/2
-d $d and -x $d
-d $d -x $d
Truth table33%1/3
A and B
A= -d $dB= -x $d
ABresult
0X0
100
111
87 73 push @{ $self->{inc_dirs} }, $d;
88 }
89 }
90 }
91  
92 sub PPI::Xref::INC {
93 36 ▶ my $self = shift;
Subroutine INC: called CC=1 SCAR=0.0
Pod documented
94 36 $self->__inc_dirs;
95 36 return $self->{inc_dirs};
96 }
97  
98 # Given a file, look for it in @INC.
99 sub __find_file {
100 131 ▶ my ($self, $file) = @_;
Subroutine __find_file: called CC=7 SCAR=19.5
101 131 $self->__inc_dirs;
102 131 ▶ unless (exists $self->{inc_file}{$file}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $self->{"inc_file"}{$file}) 131 0 131
103 131 for my $d (@{ $self->{inc_dirs}}) {
104 156 ▶ unless ($self->{opt}{__allow_relative}) { # For testing.
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"opt"}{"__allow_relative"}) 8 148 156
105 33 ▶ use File::Spec;
Subroutine BEGIN: called CC=1 SCAR=0.0
106 8 ▶ $d = File::Spec->rel2abs($d) unless
Branch: 1 branch
BranchTrueFalseTotal
unless "File::Spec"->file_name_is_absolute($d) 1 7 8
107 File::Spec->file_name_is_absolute($d);
108 }
109 156 my $f = "$d/$file";
110 156 ▶ if (-f $f) {
Branch: 1 branch
BranchTrueFalseTotal
if (-f $f) 127 29 156
111 127 $self->{inc_file}{$file} = $f;
112 127 last;
113 }
114 }
115 131 ▶ unless (exists $self->{inc_file}{$file}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $self->{"inc_file"}{$file}) 4 127 131
116 4 $self->{inc_file}{$file} = undef;
117 }
118 }
119 131 return $self->{inc_file}{$file};
120 }
121  
122 # Given a module name, look for its file in @INC.
123 sub __find_module {
124 136 ▶ my ($self, $module_name) = @_;
Subroutine __find_module: called CC=2 SCAR=6.9
125 136 ▶ unless (exists $self->{module_file}{$module_name}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $self->{"module_file"}{$module_name}) 83 53 136
126 83 my $m = $module_name;
127 83 $m =~ s{::}{/}g;
128 83 $m .= '.pm';
129 83 $self->{module_file}{$module_name} = $self->__find_file($m);
130 }
131 136 return $self->{module_file}{$module_name};
132 }
133  
134 # Remove comments and tokens, and squeeze
135 # multiple whitespaces into one.
136 sub __normalize_whitespace {
137 1316 ▶ my @n;
Subroutine __normalize_whitespace: called CC=6 SCAR=17.9
138 my $prev_ws;
139 1316 my $curr_ws;
140 1316 for my $n (@_) {
141 4247 ▶ next if $n->isa('PPI::Token::Comment');
Branch: 1 branch
BranchTrueFalseTotal
if $n->isa("PPI::Token::Comment") 4 4243 4247
142 4243 ▶ next if $n->isa('PPI::Token::Pod');
Branch: 1 branch
BranchTrueFalseTotal
if $n->isa("PPI::Token::Pod") 4 4239 4243
143 4239 $curr_ws = $n->isa('PPI::Token::Whitespace');
144 4239 ▶ next if $prev_ws && $curr_ws;
Branch: 1 branch
BranchTrueFalseTotal
if $prev_ws and $curr_ws 38 4201 4239
Condition100%3/3
$prev_ws and $curr_ws
!ll&&!rl&&r
2849135238
MC/DC100%2/2
$prev_ws and $curr_ws
$prev_ws $curr_ws
Truth table100%3/3
A and B
A= $prev_wsB= $curr_ws
ABresult
0X0
100
111
145 4201 push @n, $n;
146 4201 $prev_ws = $curr_ws;
147 }
148 1316 return @n;
149 }
150  
151 # For a filename, assign it a file id (an integer) if it does not
152 # have one, and in any case return its file id.
153 sub __assign_file_id {
154 181 ▶ my ($self, $filename) = @_;
Subroutine __assign_file_id: called CC=2 SCAR=6.9
155 181 my $file_id = $self->{file_id}{$filename};
156 181 ▶ unless (defined $file_id) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $file_id) 163 18 181
157 163 $file_id = $self->{file_id}{$filename} = $FILE_ID++;
158 163 $FILE_BY_ID{$file_id} = $filename;
159 }
160 181 return $file_id;
161 }
162  
163 # Close the current package, if any.
164 sub __close_package {
165 318 ▶ my ($self, $file_id, $package, $elem) = @_;
Subroutine __close_package: called CC=4 SCAR=13.9
166 318 ▶ if (exists $self->{file_packages} &&
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->{"file_packages"} and ref $self->{"file_packages"}{$file_id} and @{$self->{"file_packages"}{$file_id};}) 260 58 318
Condition83%5/6
exists $self->{"file_packages"} and ref $self->{"file_packages"}{$file_id} and @{$self->{"file_packages"}{$file_id};}
!ll&&!rl&&r
580260
exists $self->{"file_packages"} and ref $self->{"file_packages"}{$file_id}
!ll&&!rl&&r
949260
MC/DC66%2/3
exists $self->{"file_packages"} and ref $self->{"file_packages"}{$file_id} and @{$self->{"file_packages"}{$file_id};}
exists $self->{"file_packages"} ref $self->{"file_packages"}{$file_id} @{$self->{"file_packages"}{$file_id};}
Truth table75%3/4
A and B and C
A= exists $self->{"file_packages"}B= ref $self->{"file_packages"}{$file_id}C= @{$self->{"file_packages"}{$file_id};}
ABCresult
0XX0
10X0
1100
1111
167 ref $self->{file_packages}{$file_id} &&
168 260 @{ $self->{file_packages}{$file_id} }) {
169 260 push @{ $self->{file_packages}{$file_id}[-1] },
170 $elem->line_number,
171 $elem->column_number;
172 }
173 }
174  
175 # Open a new package.
176 sub __open_package {
177 260 ▶ my ($self, $file_id, $package, $elem) = @_;
Subroutine __open_package: called CC=1 SCAR=0.0
178 260 push @{ $self->{file_packages}{$file_id} },
179 [
180 $package, # 0
181 $elem->line_number, # 1
182 $elem->column_number, # 2
183 ];
184 }
185  
186 # Close the current package, if any, and open a new one.
187 sub __close_open_package {
188 154 ▶ my ($self, $file_id, $old_package, $old_elem,
Subroutine __close_open_package: called CC=3 SCAR=12.0
189 $new_package, $new_elem, $fileloc) = @_;
190 154 ▶ if (defined $old_package && $old_package ne 'main') {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $old_package and $old_package ne "main") 154 0 154
Condition33%1/3
defined $old_package and $old_package ne "main"
!ll&&!rl&&r
00154
MC/DC0%0/2
defined $old_package and $old_package ne "main"
defined $old_package $old_package ne "main"
Truth table33%1/3
A and B
A= defined $old_packageB= $old_package ne "main"
ABresult
0X0
100
111
191 154 $self->__close_package($file_id, $old_package, $old_elem, $fileloc);
192 }
193 154 $self->__open_package($file_id, $new_package, $new_elem);
194 }
195  
196 # Function for portably turning the directory portion of a pathname
197 # into a directory name. If the $flatten_volume is true, loses
198 # information in platforms that have a volume name in pathnames, but
199 # the main idea is to safely split the argument into a new directory
200 # name (possibly modified by prepending the volume name as a
201 # directory), and the filename. E.g. '/a/b/c' -> ('/a/b', 'c')
202 # 'c:/d/e' -> ('/c/d', 'e')
203 sub __safe_vol_dir_file {
204 79 ▶ my ($self, $path, $flatten_volume) = @_;
Subroutine __safe_vol_dir_file: called CC=3 SCAR=11.8
205 79 my ($vol, $dirs, $file) = File::Spec->splitpath($path);
206 79 ▶ if ($flatten_volume && $^O eq 'MSWin32') {
Branch: 1 branch
BranchTrueFalseTotal
if ($flatten_volume and $^O eq "MSWin32") 0 79 79
Condition66%2/3
$flatten_volume and $^O eq "MSWin32"
!ll&&!rl&&r
29500
MC/DC0%0/2
$flatten_volume and $^O eq "MSWin32"
$flatten_volume $^O eq "MSWin32"
Truth table66%2/3
A and B
A= $flatten_volumeB= $^O eq "MSWin32"
ABresult
0X0
100
111
207 0 $vol =~ s/:$//; # splitpath() leaves the $vol as e.g. "c:"
208 }
209 79 return (File::Spec->catpath($vol, $dirs), $file);
210 }
211  
212 # Returns the directory part and the file part. Note that this will
213 # convert the volume name (if any) in the $path into a directory name,
214 # e.g. 'c:/d/e' -> ('/c/d', 'e'). This is useful for re-rooting
215 # a pathname under a new directory.
216 sub __safe_dir_and_file_flatten_volume {
217 50 ▶ my ($self, $path) = @_;
Subroutine __safe_dir_and_file_flatten_volume: called CC=1 SCAR=0.0
218 50 return $self->__safe_vol_dir_file($path, 1);
219 }
220  
221 # Returns the directory part (with the possible volume part prepended)
222 # and the file part. Kind of like a safe dirname().
223 sub __safe_dir_and_file_same_volume {
224 29 ▶ my ($self, $path) = @_;
Subroutine __safe_dir_and_file_same_volume: called CC=1 SCAR=0.0
225 29 return $self->__safe_vol_dir_file($path, 0);
226 }
227  
228 33 my $CACHE_EXT = '.cache';
229  
230 # "shadow file" is a filename rooted into a new, "shadow", directory.
231 sub __shadow_cache_filename {
232 50 ▶ my ($self, $shadowdir, $filename) = @_;
Subroutine __shadow_cache_filename: called CC=3 SCAR=11.5
233  
234 # Paranoia check. (Either absolute or relative is fine, though.)
235 50 ▶ if ($filename =~ m{\.\.}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($filename =~ /\.\./) 0 50 50
236 0 warn "$Sub: Skipping unexpected file: '$filename'\n";
237 0 return;
238 }
239  
240 33 ▶ use File::Spec;
Subroutine BEGIN: called CC=1 SCAR=0.0
241 50 ▶ my $absfile =
Branch: 1 branch
BranchTrueFalseTotal
'File::Spec'->file_name_is_absolute($filename) ? : 50 0 50
242 File::Spec->file_name_is_absolute($filename) ?
243 $filename :
244 File::Spec->rel2abs($filename);
245 50 my ($redir, $file) = $self->__safe_dir_and_file_flatten_volume($absfile);
246  
247 # For portable filenames, we cannot just keep on
248 # appending filename extensions with dots, and we
249 # are going to append the cache filename extension.
250 # So we mangle the .pm or .pl as _pm and _pl.
251 50 $file =~ s{\.(p[ml])$}{_$1};
252  
253 50 return File::Spec->catfile($shadowdir, $redir, $file . $CACHE_EXT);
254 }
255  
256 # The hash checksum for the file, and the mtime timestamp.
257 sub __current_filehash_and_mtime {
258 168 ▶ my ($self, $origfilename) = @_;
Subroutine __current_filehash_and_mtime: called CC=3 SCAR=11.2
259 168 ▶ return unless -f $origfilename;
Branch: 1 branch
BranchTrueFalseTotal
unless -f $origfilename 1 167 168
260 167 my $origfilefh;
261 167 ▶ unless (open($origfilefh, $origfilename)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (open $origfilefh, $origfilename) 0 167 167
262 0 warn qq[$Sub: Failed to open "$origfilename": $!\n];
263 0 return;
264 }
265 33 ▶ use Digest::SHA;
Subroutine BEGIN: called CC=1 SCAR=0.0
266 167 my $sha = Digest::SHA->new($HASHALGO);
267 167 $sha->addfile($origfilefh);
268 return (
269 167 "$HASHALGO:". $sha->hexdigest,
270 (stat($origfilefh))[9], # mtime
271 );
272 }
273  
274 # Create the directory of the filename.
275 sub __make_path_file {
276 29 ▶ my ($self, $base) = @_;
Subroutine __make_path_file: called CC=2 SCAR=6.9
277 33 ▶ use File::Path qw[make_path];
Subroutine BEGIN: called CC=1 SCAR=0.0
278 29 my ($dir, $file) = $self->__safe_dir_and_file_same_volume($base);
279 29 ▶ return eval { make_path($dir) unless -d $dir; 1; };
Branch: 1 branch
BranchTrueFalseTotal
unless -d $dir 6 23 29
280 }
281  
282 # The attributes that are written to and read from cache.
283 33 my @CACHE_FIELDS =
284 qw[
285 file_incs
286 file_lines
287 file_modules
288 file_packages
289 file_subs
290 file_missing_modules
291 file_parse_errors
292 ];
293  
294 # Error fields are cache fields but they should not be cleared
295 # since they accumulate and are hrefs as opposed to arefs.
296 my %CACHE_FIELDS_KEEP =
297 33 map { $_ => 1 }
298 qw[
299 file_missing_modules
300 file_parse_errors
301 ];
302  
303 my @CACHE_FIELDS_CLEAR =
304 33 grep { ! exists $CACHE_FIELDS_KEEP{$_} } @CACHE_FIELDS;
305  
306 # Given the href, serialize it to the file.
307 sub __encode_to_file {
308 29 ▶ my ($self, $file, $cached) = @_;
Subroutine __encode_to_file: called CC=9 SCAR=27.4
309  
310 29 my $success = 0;
311  
312 29 my $temp = "$file.$$"; # For atomic renaming.
313  
314 # If anything goes wrong, abort the commit.
315 COMMIT: {
316 29 my $blob = $self->{encoder}->encode($cached);
317 29 ▶ unless (defined $blob) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $blob) 0 29 29
318 0 warn "$Sub: Failed to encode into '$temp'\n";
319 0 last COMMIT;
320 }
321  
322 29 ▶ unless ($self->__make_path_file($temp)) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->__make_path_file($temp)) 0 29 29
323 0 warn "$Sub: Failed to create path for '$temp'\n";
324 0 last COMMIT;
325 }
326  
327 29 my $fh;
328 33 ▶ use Fcntl qw[O_CREAT O_WRONLY];
Subroutine BEGIN: called CC=1 SCAR=0.0
329 29 ▶ unless (sysopen($fh, $temp, O_CREAT|O_WRONLY, 0644)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (sysopen $fh, $temp, 65, 420) 0 29 29
330 0 warn "$Sub: Failed to open '$temp' for writing: $!\n";
331 0 last COMMIT;
332 }
333  
334 29 my $size = length($blob);
335 29 my $wrote = syswrite($fh, $blob);
336  
337 29 ▶ unless (defined $wrote && $wrote == $size) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $wrote and $wrote == $size) 0 29 29
Condition33%1/3
defined $wrote and $wrote == $size
!ll&&!rl&&r
0029
MC/DC0%0/2
defined $wrote and $wrote == $size
defined $wrote $wrote == $size
Truth table33%1/3
A and B
A= defined $wroteB= $wrote == $size
ABresult
0X0
100
111
338 0 warn "$Sub: Failed to write $size bytes to '$temp': $!\n";
339 0 last COMMIT;
340 }
341  
342 29 ▶ unless (close($fh)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (close $fh) 0 29 29
343 0 warn "$Sub: Failed to close '$temp': $!\n";
344 0 last COMMIT;
345 }
346  
347 29 ▶ unless (rename($temp, $file)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (rename $temp, $file) 0 29 29
348 0 warn "$Sub: Failed to rename '$temp' as '$file': !$\n";
349 0 last COMMIT;
350 }
351  
352 # Finally we are happy.
353 29 $success = 1;
354  
355 } # COMMIT
356  
357 29 ▶ if (-f $temp) {
Branch: 1 branch
BranchTrueFalseTotal
if (-f $temp) 0 29 29
358 0 warn "$Sub: Cleaning temporary file '$temp'\n";
359 }
360 29 unlink $temp; # In any case.
361  
362 29 return $success;
363 }
364  
365 # Write the results to the file.
366 sub __write_cachefile {
367 29 ▶ my ($self, $cache_filename, $hash_current, $file_id, $file_mtime) = @_;
Subroutine __write_cachefile: called CC=5 SCAR=16.1
368  
369 29 ▶ return if $self->{opt}{cache_read_only};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"opt"}{"cache_read_only"} 0 29 29
370  
371 29 ▶ if ($self->{opt}{cache_verbose}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"opt"}{"cache_verbose"}) 20 9 29
372 20 print "$Sub: writing $cache_filename\n";
373 }
374  
375 29 my $cached; # Re-root the data we care about.
376 29 for my $k (@CACHE_FIELDS) {
377 203 ▶ if (defined $self->{$k}{$file_id}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{$k}{$file_id}) 117 86 203
378 117 $cached->{$k} = $self->{$k}{$file_id};
379 }
380 }
381 29 $cached->{file_hash} = $hash_current;
382  
383 # The mtime is in UTC, and should only be used for
384 # maintenance / statistics. In other words, it should
385 # NOT be used for uptodateness.
386 29 $cached->{file_mtime} = $file_mtime;
387  
388 # Mark also in the object that we have processed this one.
389 29 $self->{file_hash}{$file_id} = $hash_current;
390  
391 29 return $self->__encode_to_file($cache_filename, $cached);
392 }
393  
394 # Compose a cache filename, given an original filename.
395 # The filenames are re-rooted in the cache_directory.
396 sub __cache_filename {
397 50 ▶ my ($self, $path) = @_;
Subroutine __cache_filename: called CC=5 SCAR=19.3
398 50 ▶ return if $path eq '-';
Branch: 1 branch
BranchTrueFalseTotal
if $path eq "-" 0 50 50
399  
400 50 my $cache_directory = $self->{opt}{cache_directory};
401 50 ▶ return unless defined $cache_directory;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $cache_directory 0 50 50
402  
403 50 ▶ unless ($self->__is_readwrite_directory($cache_directory)) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->__is_readwrite_directory($cache_directory)) 0 50 50
404 0 warn "$Sub: Not a read-write directory '$cache_directory'\n";
405 0 return;
406 }
407  
408 50 ▶ if ($path !~ /\.p[ml]$/) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($path =~ /\.p[ml]$/) 0 50 50
409 0 warn "$Sub: Unexpected filename: '$path'\n";
410 0 return;
411 }
412  
413 50 return $self->__shadow_cache_filename($cache_directory, $path);
414 }
415  
416 # Deserialize from the file.
417 sub __decode_from_file {
418 43 ▶ my ($self, $file) = @_;
Subroutine __decode_from_file: called CC=3 SCAR=11.1
419  
420 43 my $fh;
421 33 ▶ use Fcntl qw[O_RDONLY];
Subroutine BEGIN: called CC=1 SCAR=0.0
422 43 ▶ unless (sysopen($fh, $file, O_RDONLY)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (sysopen $fh, $file, 0) 28 15 43
423 # warn "$Sub: Failed to open '$file' for reading: $!\n";
424 28 return;
425 }
426  
427 15 my $size = -s $fh;
428 15 my $read = sysread($fh, my $blob, $size);
429 15 ▶ unless ($read == $size) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($read == $size) 0 15 15
430 0 warn "$Sub: Failed to read $size bytes from '$file': $!\n";
431 0 return;
432 }
433  
434 15 return $self->{decoder}->decode($blob);
435 }
436  
437 # Check if we have the results for this file cached.
438 sub __check_cached {
439 167 ▶ my ($self, $origfile) = @_;
Subroutine __check_cached: called CC=10 SCAR=23.2
440 167 ▶ return if $origfile eq '-';
Branch: 1 branch
BranchTrueFalseTotal
if $origfile eq "-" 0 167 167
441  
442 167 my ($hash_current, $file_mtime) =
443 $self->__current_filehash_and_mtime($origfile);
444 167 my $cache_directory = $self->{opt}{cache_directory};
445 167 my $cache_filename;
446 my $cached;
447 167 my $hash_previous;
448 167 my $hash_match;
449  
450 167 ▶ if (defined $cache_directory) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $cache_directory) 42 125 167
451 42 $cache_filename = $self->__cache_filename($origfile);
452 42 ▶ if (defined $cache_filename) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $cache_filename) 42 0 42
453 42 ▶ if ($self->{opt}{cache_verbose}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"opt"}{"cache_verbose"}) 25 17 42
454 25 print "$Sub: reading $cache_filename\n";
455 }
456 42 $cached = $self->__decode_from_file($cache_filename);
457 42 ▶ if (defined $cached) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $cached) { } 14 28 42
458 14 ▶ if ($self->{opt}{cache_verbose}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"opt"}{"cache_verbose"}) 5 9 14
459 5 print "$Sub: reading $cache_filename SUCCESS\n";
460 }
461 14 $hash_previous = $cached->{file_hash};
462 14 ▶ $hash_match =
Condition50%3/6
defined $hash_previous && defined $hash_current && $hash_previous eq $hash_current
!ll&&!rl&&r
0113
defined $hash_previous and defined $hash_current
!ll&&!rl&&r
0014
MC/DC33%1/3
defined $hash_previous && defined $hash_current && $hash_previous eq $hash_current
defined $hash_previous defined $hash_current $hash_previous eq $hash_current
Truth table50%2/4
A and B && C
A= defined $hash_previousB= defined $hash_currentC= $hash_previous eq $hash_current
ABCresult
0XX0
10X0
1100
1111
463 defined $hash_previous &&
464 defined $hash_current &&
465 $hash_previous eq $hash_current;
466 } else {
467 28 ▶ if ($self->{opt}{cache_verbose}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"opt"}{"cache_verbose"}) 20 8 28
468 20 print "$Sub: reading $cache_filename FAILURE\n";
469 }
470 }
471 }
472 }
473  
474 167 return ($cache_filename,
475 $cached,
476 $hash_current,
477 $hash_match,
478 $file_mtime);
479 }
480  
481 # Write to the cache and tick various counters.
482 sub __to_cache {
483 29 ▶ my ($self, $cache_filename, $hash_current, $file_id, $file_mtime) = @_;
Subroutine __to_cache: called CC=6 SCAR=19.2
484  
485 29 ▶ return if $self->{opt}{cache_read_only};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"opt"}{"cache_read_only"} 0 29 29
486  
487 29 my $had_cache = -f $cache_filename;
488 29 ▶ if ($self->__write_cachefile($cache_filename, $hash_current,
Branch: 1 branch
BranchTrueFalseTotal
if ($self->__write_cachefile($cache_filename, $hash_current, $file_id, $file_mtime)) { } 29 0 29
489 $file_id, $file_mtime)) {
490 29 ▶ if ($self->{opt}{cache_verbose}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"opt"}{"cache_verbose"}) 20 9 29
491 20 print "$Sub: writing $cache_filename SUCCESS\n";
492 }
493 29 $self->{__cachewrites}++;
494 29 ▶ unless ($had_cache) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($had_cache) 28 1 29
495 28 $self->{__cachecreates}++;
496 }
497 } else {
498 0 ▶ if ($self->{opt}{cache_verbose}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"opt"}{"cache_verbose"}) 0 0 0
499 0 print "$Sub: writing $cache_filename FAILURE\n";
500 }
501 }
502 }
503  
504 # Import the fields we care about from the cached data.
505 sub __import_cached {
506 13 ▶ my ($self, $file_id, $cached) = @_;
Subroutine __import_cached: called CC=2 SCAR=6.9
507  
508 13 for my $k (@CACHE_FIELDS) {
509 91 $self->{$k}{$file_id} = $cached->{$k};
510 }
511  
512 13 return 1;
513 }
514  
515 # Clear the cached fields. Used especially in preparation of import.
516 sub __clear_cached {
517 166 ▶ my ($self, $file_id) = @_;
Subroutine __clear_cached: called CC=2 SCAR=6.9
518  
519 166 for my $k (@CACHE_FIELDS_CLEAR) {
520 830 delete $self->{$k}{$file_id};
521 }
522  
523 166 return 1;
524 }
525  
526 sub __parse_error {
527 5 ▶ my ($self, $file_id, $file, $fileloc, $error) = @_;
Subroutine __parse_error: called CC=3 SCAR=12.7
528 5 ▶ if (defined $fileloc) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $fileloc) { } 5 0 5
529 5 warn qq[$Sub: $error in $fileloc\n];
530 } else {
531 0 warn qq[$Sub: $error\n];
532 }
533 5 ▶ $self->{file_parse_errors}{$file_id}{$fileloc // $file} = $error;
Condition33%1/3
$fileloc // $file
l!l&&r!l&&!r
500
MC/DC0%0/2
$fileloc // $file
$fileloc $file
Truth table33%1/3
A // B
A= $filelocB= $file
ABresult
1X1
011
000
534 }
535  
536 sub __doc_create {
537 165 ▶ my ($self, $arg, $file, $file_id) = @_;
Subroutine __doc_create: called CC=7 SCAR=20.7
538 165 my $doc;
539 165 eval { $doc = PPI::Document->new($arg) };
540 165 ▶ unless (defined $doc) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $doc) { } 164 1 165
541 1 $self->__parse_error($file_id, $file, $file,
542 "PPI::Document creation failed");
543 } else {
544 164 my $complete;
545 164 eval { $complete = $doc->complete };
546 164 ▶ unless ($complete) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($complete) 4 160 164
547 4 my $pseudo = $file eq '-';
548 4 ▶ if (!$pseudo && ! -f $file) {
Branch: 2 branches
BranchTrueFalseTotal
if (not $pseudo || -f $file) { } 0 4 4
elsif (not $pseudo || -s $file) { } 0 4 4
Condition33%1/3
$pseudo || -f $file
l!l&&r!l&&!r
400
MC/DC0%0/2
$pseudo || -f $file
$pseudo -f $file
Truth table33%1/3
A || B
A= $pseudoB= -f $file
ABresult
1X1
011
000
549 0 $self->__parse_error($file_id, $file, undef,
550 "Missing file");
551 ▶ } elsif (!$pseudo && ! -s $file) {
Condition33%1/3
$pseudo || -s $file
l!l&&r!l&&!r
400
MC/DC0%0/2
$pseudo || -s $file
$pseudo -s $file
Truth table33%1/3
A || B
A= $pseudoB= -s $file
ABresult
1X1
011
000
552 0 $self->__parse_error($file_id, $file, undef,
553 "Empty file");
554 } else {
555 4 $self->__parse_error($file_id, $file, $file,
556 "PPI::Document incomplete");
557 }
558 }
559 }
560 165 return $doc;
561 }
562  
563 # Process a given filename.
564 sub __process_file {
565 230 ▶ my ($self, $arg, $file, $process_depth) = @_;
Subroutine __process_file: called CC=20 SCAR=30.7
566 230 ▶ $file //= $arg;
Condition66%2/3
$file //= $arg
l!l&&r!l&&!r
431870
MC/DC0%0/2
$file //= $arg
$file $arg
Truth table66%2/3
A //= B
A= $fileB= $arg
ABresult
1X1
011
000
567 230 $self->{file_counts}{$file}++;
568 230 ▶ if ($file eq '-') { # Pseudofile.
Branch: 3 branches
BranchTrueFalseTotal
if ($file eq '-') { } 11 219 230
elsif ($self->{'seen_file'}{$file}) { } 52 167 219
elsif (not $self->{'seen_file'}{$file}++) { } 167 0 167
569 11 ▶ if ($self->{opt}{process_verbose}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"opt"}{"process_verbose"}) 0 11 11
570 0 printf "$Sub: %*s%s\n", $process_depth + 1, ' ', $file;
571 }
572 11 my $file_id = $self->__assign_file_id($file);
573 11 my $doc = $self->__doc_create($arg, $file, $file_id);
574 11 ▶ return unless defined $doc;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $doc 0 11 11
575 11 $self->{__docscreated}++;
576 11 $self->__process_id($doc, $file_id, $process_depth);
577 } elsif ($self->{seen_file}{$file}) {
578 52 ▶ if ($self->{opt}{process_verbose} && $self->{opt}{recurse_verbose}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"opt"}{"process_verbose"} and $self->{"opt"}{"recurse_verbose"}) 0 52 52
Condition66%2/3
$self->{"opt"}{"process_verbose"} and $self->{"opt"}{"recurse_verbose"}
!ll&&!rl&&r
4660
MC/DC0%0/2
$self->{"opt"}{"process_verbose"} and $self->{"opt"}{"recurse_verbose"}
$self->{"opt"}{"process_verbose"} $self->{"opt"}{"recurse_verbose"}
Truth table66%2/3
A and B
A= $self->{"opt"}{"process_verbose"}B= $self->{"opt"}{"recurse_verbose"}
ABresult
0X0
100
111
579 0 printf "$Sub: %*s%s [seen]\n", $process_depth + 1, ' ', $file;
580 }
581 } elsif (! $self->{seen_file}{$file}++) {
582 167 ▶ if ($self->{opt}{process_verbose}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"opt"}{"process_verbose"}) 18 149 167
583 18 printf "$Sub: %*s%s\n", $process_depth + 1, ' ', $file;
584 }
585 167 my $file_id = $self->__assign_file_id($file);
586 167 my ($cache_filename, $cached, $hash_current,
587 $hash_match, $file_mtime) =
588 $self->__check_cached($file);
589 167 ▶ if ($hash_match) {
Branch: 2 branches
BranchTrueFalseTotal
if ($hash_match) { } 13 154 167
elsif (not $self->{'opt'}{'cache_read_only'}) { } 154 0 154
590 13 $self->__clear_cached($file_id);
591 13 $self->__import_cached($file_id, $cached);
592 13 $self->__process_cached_incs($file_id, $process_depth);
593 13 $self->{__cachereads}++;
594 } elsif (!$self->{opt}{cache_read_only}) {
595 154 my $doc = $self->__doc_create($arg, $file, $file_id);
596 154 ▶ return unless defined $doc;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $doc 1 153 154
597 153 $self->__clear_cached($file_id);
598 153 $self->__process_id($doc, $file_id, $process_depth);
599 153 $self->{__docscreated}++;
600 }
601 166 ▶ if (defined $cache_filename &&
Branch: 1 branch
BranchTrueFalseTotal
if (defined $cache_filename and defined $hash_current and not $hash_match and not $self->{"opt"}{"cache_read_only"}) 29 137 166
Condition77%7/9
defined $cache_filename and defined $hash_current and not $hash_match and not $self->{"opt"}{"cache_read_only"}
!ll&&!rl&&r
137029
defined $cache_filename and defined $hash_current and not $hash_match
!ll&&!rl&&r
1241329
defined $cache_filename and defined $hash_current
!ll&&!rl&&r
124042
MC/DC50%2/4
defined $cache_filename and defined $hash_current and not $hash_match and not $self->{"opt"}{"cache_read_only"}
defined $cache_filename defined $hash_current not $hash_match not $self->{"opt"}{"cache_read_only"}
Truth table60%3/5
A and B and not(C) and not(D)
A= defined $cache_filenameB= defined $hash_currentC= not $hash_matchD= not $self->{"opt"}{"cache_read_only"}
ABCDresult
0XXX0
10XX0
110X0
11100
11111
602 defined $hash_current &&
603 !$hash_match &&
604 !$self->{opt}{cache_read_only}) {
605 29 ▶ if ($self->__to_cache($cache_filename, $hash_current,
Branch: 1 branch
BranchTrueFalseTotal
if ($self->__to_cache($cache_filename, $hash_current, $file_id, $file_mtime)) 23 6 29
606 $file_id, $file_mtime)) {
607 23 ▶ if (!$hash_match && defined $cached) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $hash_match and defined $cached) 1 22 23
Condition66%2/3
not $hash_match and defined $cached
!ll&&!rl&&r
0221
MC/DC50%1/2
not $hash_match and defined $cached
not $hash_match defined $cached
Truth table66%2/3
not(A) and B
A= not $hash_matchB= defined $cached
ABresult
0X0
100
111
608 1 $self->{__cacheupdates}++;
609 }
610 }
611 }
612 }
613 229 return $self->{file_id}{$file};
614 }
615  
616 # Counter getters.
617  
618 sub docs_created {
619 10 ▶ my ($self) = @_;
Subroutine docs_created: called CC=2 SCAR=6.9
Pod documented
620 10 ▶ return $self->{__docscreated} // 0;
Condition100%2/2
$self->{"__docscreated"} // 0
l!l
91
MC/DC100%1/1
$self->{"__docscreated"} // 0
$self->{"__docscreated"}
Truth table100%2/2
A
A= $self->{"__docscreated"}
Aresult
11
00
621 }
622  
623 sub cache_reads {
624 9 ▶ my ($self) = @_;
Subroutine cache_reads: called CC=2 SCAR=6.9
Pod documented
625 9 ▶ return $self->{__cachereads} // 0;
Condition100%2/2
$self->{"__cachereads"} // 0
l!l
36
MC/DC100%1/1
$self->{"__cachereads"} // 0
$self->{"__cachereads"}
Truth table100%2/2
A
A= $self->{"__cachereads"}
Aresult
11
00
626 }
627  
628 sub cache_writes {
629 12 ▶ my ($self) = @_;
Subroutine cache_writes: called CC=2 SCAR=6.9
Pod documented
630 12 ▶ return $self->{__cachewrites} // 0;
Condition100%2/2
$self->{"__cachewrites"} // 0
l!l
75
MC/DC100%1/1
$self->{"__cachewrites"} // 0
$self->{"__cachewrites"}
Truth table100%2/2
A
A= $self->{"__cachewrites"}
Aresult
11
00
631 }
632  
633 sub cache_creates {
634 6 ▶ my ($self) = @_;
Subroutine cache_creates: called CC=2 SCAR=6.9
Pod undocumented
635 6 ▶ return $self->{__cachecreates} // 0;
Condition100%2/2
$self->{"__cachecreates"} // 0
l!l
42
MC/DC100%1/1
$self->{"__cachecreates"} // 0
$self->{"__cachecreates"}
Truth table100%2/2
A
A= $self->{"__cachecreates"}
Aresult
11
00
636 }
637  
638 sub cache_updates {
639 6 ▶ my ($self) = @_;
Subroutine cache_updates: called CC=2 SCAR=6.9
Pod undocumented
640 6 ▶ return $self->{__cacheupdates} // 0;
Condition100%2/2
$self->{"__cacheupdates"} // 0
l!l
24
MC/DC100%1/1
$self->{"__cacheupdates"} // 0
$self->{"__cacheupdates"}
Truth table100%2/2
A
A= $self->{"__cacheupdates"}
Aresult
11
00
641 }
642  
643 sub cache_deletes {
644 0 ▶ my ($self) = @_;
Subroutine cache_deletes: not called CC=2 SCAR=17.9
Pod undocumented
645 0 ▶ return $self->{__cachedeletes} // 0;
Condition0%0/2
$self->{"__cachedeletes"} // 0
l!l
00
MC/DC0%0/1
$self->{"__cachedeletes"} // 0
$self->{"__cachedeletes"}
Truth table0%0/2
A
A= $self->{"__cachedeletes"}
Aresult
11
00
646 }
647  
648 # For results imported from cache, process any cached inclusions.
649 # The [6] is the include file, the [7] will become its (new) file id.
650 sub __process_cached_incs {
651 13 ▶ my ($self, $file_id, $process_depth) = @_;
Subroutine __process_cached_incs: called CC=2 SCAR=6.9
652 13 for my $inc (@{ $self->{file_incs}{$file_id} }) {
653 11 my $include_file = $inc->[6];
654 11 $self->__process_file($include_file, undef,
655 $process_depth + 1);
656 11 $inc->[7] = $self->{file_id}{$include_file};
657 }
658 }
659  
660 # For freshly computed results, process any cached inclusions.
661 # The [6] is the include file, the [7] will become its (new) file id.
662 sub __process_pending_incs {
663 164 ▶ my ($self, $file_id, $process_depth) = @_;
Subroutine __process_pending_incs: called CC=3 SCAR=11.0
664 164 ▶ if ($self->{__incs_flush}{$file_id}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"__incs_flush"}{$file_id}) 164 0 164
665 $self->{file_incs}{$file_id} =
666 164 delete $self->{__incs_pending}{$file_id};
667 164 for my $inc (@{ $self->{file_incs}{$file_id} }) {
668 176 my $include_file = $inc->[6];
669 176 $self->__process_file($include_file, undef,
670 $process_depth + 1);
671 176 $inc->[7] = $self->{file_id}{$include_file};
672 }
673 164 delete $self->{__incs_flush}{$file_id}; # Defuse the trigger.
674 }
675 }
676  
677 # Process a given PPI document, that has a given file id.
678 sub __process_id {
679 164 ▶ my ($self, $doc, $file_id, $process_depth) = @_;
Subroutine __process_id: called CC=43 SCAR=39.4
680 164 my @queue = @{$doc->{children}};
681 164 my $scope_depth = 0;
682 164 my %package = ( 0 => 'main' );
683 164 my $package = $package{$scope_depth};
684 164 my $prev_package;
685 my $elem;
686 164 my $prev_elem;
687 164 my $filename = $FILE_BY_ID{$file_id};
688 164 my $fileloc;
689 164 ▶ while (@queue) {
Branch: 1 branch
BranchTrueFalseTotal
if (@queue) 6301 164 6465
690 6301 $elem = shift @queue;
691 6301 my $linenumber = $elem->line_number;
692 6301 $fileloc = "$filename:$linenumber";
693 6301 if (0) {
694 printf("$fileloc elem = %s[%s]\n",
695 $filename, ref $elem, $elem->content);
696 }
697 my @children = exists $elem->{children} ?
698 6301 ▶ __normalize_whitespace(@{$elem->{children}}) : ();
Branch: 1 branch
BranchTrueFalseTotal
exists $elem->{'children'} ? : 1316 4985 6301
699 6301 ▶ if ($elem->isa('PPI::Token::Structure')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($elem->isa("PPI::Token::Structure")) 1192 5109 6301
700 # { ... }
701 1192 ▶ if ($elem->content eq '{') {
Branch: 2 branches
BranchTrueFalseTotal
if ($elem->content eq '{') { } 414 778 1192
elsif ($elem->content eq '}') { } 414 364 778
702 414 $scope_depth++;
703 } elsif ($elem->content eq '}') {
704 414 ▶ if ($scope_depth <= 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($scope_depth <= 0) { } 0 414 414
705 0 $self->__parse_error($file_id, $filename, $fileloc,
706 "scope pop underflow");
707 } else {
708 414 $scope_depth--;
709 414 delete @package{ grep { $_ > $scope_depth } keys %package };
710 33 ▶ use List::Util qw[first];
Subroutine BEGIN: called CC=1 SCAR=0.0
711 $package =
712 $package{$scope_depth} //
713 414 ▶ first { defined } @package{reverse 0..$scope_depth};
Subroutine __ANON__: called CC=1 SCAR=0.1
Condition66%2/3
$package{$scope_depth} // &first(sub { defined $_; } , @package{reverse 0 .. $scope_depth})
l!l&&r!l&&!r
363510
MC/DC0%0/2
$package{$scope_depth} // &first(sub { defined $_; } , @package{reverse 0 .. $scope_depth})
$package{$scope_depth} &first(sub { defined $_; } , @package{reverse 0 .. $scope_depth})
Truth table66%2/3
A // B
A= $package{$scope_depth}B= &first(sub { defined $_; } , @package{reverse 0 .. $scope_depth})
ABresult
1X1
011
000
714 414 ▶ if (defined $prev_package && $package ne $prev_package) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $prev_package and $package ne $prev_package) 26 388 414
Condition66%2/3
defined $prev_package and $package ne $prev_package
!ll&&!rl&&r
038826
MC/DC50%1/2
defined $prev_package and $package ne $prev_package
defined $prev_package $package ne $prev_package
Truth table66%2/3
A and B
A= defined $prev_packageB= $package ne $prev_package
ABresult
0X0
100
111
715 26 if (0) {
716 print "$fileloc: package change: $prev_package -> $package\n";
717 }
718 $self->__close_open_package(
719 26 $file_id, $prev_package, $prev_elem,
720 $package, $elem);
721 }
722 }
723 }
724 }
725 6301 ▶ if (@children) {
Branch: 1 branch
BranchTrueFalseTotal
if (@children) 1010 5291 6301
726 1010 ▶ if ($elem->isa('PPI::Statement::Package') && @children >= 2) {
Branch: 3 branches
BranchTrueFalseTotal
if ($elem->isa('PPI::Statement::Package') and @children >= 2) { } 234 776 1010
elsif ($elem->isa('PPI::Statement::Sub') and defined $elem->name and not $elem->forward) { } 363 413 776
elsif ($elem->isa('PPI::Statement')) { } 305 108 413
Condition66%2/3
$elem->isa("PPI::Statement::Package") and @children >= 2
!ll&&!rl&&r
7760234
MC/DC50%1/2
$elem->isa("PPI::Statement::Package") and @children >= 2
$elem->isa("PPI::Statement::Package") @children >= 2
Truth table66%2/3
A and B
A= $elem->isa("PPI::Statement::Package")B= @children >= 2
ABresult
0X0
100
111
727 # package ...
728 #
729 # Remember to test 'use mro' and look for next::can().
730 234 $package = $children[2]->content;
731 234 $package{$scope_depth} = $package;
732 234 ▶ if (defined $package && length $package) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $package and length $package) { } 234 0 234
Condition33%1/3
defined $package and length $package
!ll&&!rl&&r
00234
MC/DC0%0/2
defined $package and length $package
defined $package length $package
Truth table33%1/3
A and B
A= defined $packageB= length $package
ABresult
0X0
100
111
733 # Okay, keep going.
734 } else {
735 0 $self->__parse_error($file_id,
736 $filename,
737 $fileloc, "missing package");
738 }
739 234 ▶ if (defined $prev_package) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $prev_package) { } 128 106 234
740 128 ▶ if ($package ne $prev_package) {
Branch: 1 branch
BranchTrueFalseTotal
if ($package ne $prev_package) 128 0 128
741 128 if (0) {
742 print "$fileloc: package change: $prev_package -> $package\n";
743 }
744 $self->__close_open_package(
745 128 $file_id, $prev_package, $prev_elem,
746 $package, $elem);
747 }
748 } else {
749 106 if (0) {
750 print "$fileloc: package first: $package\n";
751 }
752 106 $self->__open_package($file_id, $package, $elem);
753 }
754 ▶ } elsif ($elem->isa('PPI::Statement::Sub') &&
Condition66%4/6
$elem->isa("PPI::Statement::Sub") and defined $elem->name and not $elem->forward
!ll&&!rl&&r
4130363
$elem->isa("PPI::Statement::Sub") and defined $elem->name
!ll&&!rl&&r
4130363
MC/DC33%1/3
$elem->isa("PPI::Statement::Sub") and defined $elem->name and not $elem->forward
$elem->isa("PPI::Statement::Sub") defined $elem->name not $elem->forward
Truth table50%2/4
A and B and not(C)
A= $elem->isa("PPI::Statement::Sub")B= defined $elem->nameC= not $elem->forward
ABCresult
0XX0
10X0
1100
1111
755 defined $elem->name &&
756 !$elem->forward # Not a forward declaration.
757 ) {
758 # sub ...
759 363 my $sub = $elem->name;
760 363 ▶ unless ($sub =~ /::/) { # sub x::y::z { ... }
Branch: 1 branch
BranchTrueFalseTotal
unless ($sub =~ /::/) 338 25 363
761 338 ▶ $package //= 'main';
Condition50%1/2
$package //= "main"
l!l
3380
MC/DC0%0/1
$package //= "main"
$package
Truth table50%1/2
A
A= $package
Aresult
11
00
762 338 $sub = $package . '::' . $sub;
763 }
764 363 my $finish = $elem->block->finish;
765 363 ▶ unless (defined $finish) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $finish) 0 363 363
766 # E.g. Devel::Peek:debug_flags() fails to have a finish.
767 0 $finish = $elem; # Fake it.
768 0 $self->__parse_error($file_id,
769 $filename,
770 $fileloc, "missing finish");
771 }
772 363 push @{ $self->{file_subs}{$file_id} },
773 [
774 $sub, # 0
775 $elem->line_number, # 1
776 $elem->column_number, # 2
777 $finish->line_number, # 3
778 $finish->column_number, # 4
779 ];
780 } elsif ($elem->isa('PPI::Statement')) {
781 # use, no, require
782 305 my $stmt_content = $children[0]->content;
783 305 my $include = $children[2];
784 305 ▶ next unless defined $include;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $include 100 205 305
785 205 my $include_content = $include->content;
786 205 my $including_module;
787 my $including_file;
788 205 ▶ if ($elem->isa('PPI::Statement::Include') &&
Branch: 2 branches
BranchTrueFalseTotal
if ($elem->isa('PPI::Statement::Include') and $stmt_content =~ /^(?:use|no|require)$/ and $include->isa('PPI::Token::Word') and not $include_content =~ /^v?5/) { } 136 69 205
elsif ($stmt_content =~ /^(?:require|do)$/ and $include->isa('PPI::Token::Quote')) { } 48 21 69
Condition77%7/9
$elem->isa("PPI::Statement::Include") and $stmt_content =~ /^(?:use|no|require)$/ and $include->isa("PPI::Token::Word") and not $include_content =~ /^v?5/
!ll&&!rl&&r
690136
$elem->isa("PPI::Statement::Include") and $stmt_content =~ /^(?:use|no|require)$/ and $include->isa("PPI::Token::Word")
!ll&&!rl&&r
4227136
$elem->isa("PPI::Statement::Include") and $stmt_content =~ /^(?:use|no|require)$/
!ll&&!rl&&r
420163
MC/DC50%2/4
$elem->isa("PPI::Statement::Include") and $stmt_content =~ /^(?:use|no|require)$/ and $include->isa("PPI::Token::Word") and not $include_content =~ /^v?5/
$elem->isa("PPI::Statement::Include") $stmt_content =~ /^(?:use|no|require)$/ $include->isa("PPI::Token::Word") not $include_content =~ /^v?5/
Truth table60%3/5
A and B and C and not(D)
A= $elem->isa("PPI::Statement::Include")B= $stmt_content =~ /^(?:use|no|require)$/C= $include->isa("PPI::Token::Word")D= not $include_content =~ /^v?5/
ABCDresult
0XXX0
10XX0
110X0
11100
11111
789 # use/no/require Module
790 $stmt_content =~ /^(?:use|no|require)$/ &&
791 $include->isa('PPI::Token::Word') &&
792 $include_content !~ /^v?5/) {
793 136 $including_module = 1;
794 ▶ } elsif ($stmt_content =~ /^(?:require|do)$/ &&
Condition66%2/3
$stmt_content =~ /^(?:require|do)$/ and $include->isa("PPI::Token::Quote")
!ll&&!rl&&r
21048
MC/DC50%1/2
$stmt_content =~ /^(?:require|do)$/ and $include->isa("PPI::Token::Quote")
$stmt_content =~ /^(?:require|do)$/ $include->isa("PPI::Token::Quote")
Truth table66%2/3
A and B
A= $stmt_content =~ /^(?:require|do)$/B= $include->isa("PPI::Token::Quote")
ABresult
0X0
100
111
795 $include->isa('PPI::Token::Quote')) {
796 # require/do "file"
797 48 $including_file = 1;
798 } else {
799 # Not a use/no/require/do, quietly exit stage left.
800 21 next;
801 }
802 184 ▶ unless (defined $include) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $include) 0 184 184
803 0 $self->__parse_error($file_id, $fileloc, "missing include");
804 0 next;
805 }
806 184 my $last = $children[-1];
807 184 my $include_file;
808 my $include_string;
809 184 ▶ if ($including_module) {
Branch: 2 branches
BranchTrueFalseTotal
if ($including_module) { } 136 48 184
elsif ($including_file) { } 48 0 48
810 136 $include_string = $include_content;
811 136 $include_file = $self->__find_module($include_content);
812 136 $self->{file_modules}{$file_id}{$include_content}++;
813 136 ▶ unless (defined $include_file) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $include_file) 5 131 136
814 5 $self->{file_missing_modules}{$file_id}{$include_content}{$fileloc}++;
815 5 warn "$Sub: warning: Failed to find module '$include_string' in $fileloc\n";
816 }
817 } elsif ($including_file) {
818 48 $include_string = $include->string;
819 48 $include_file = $self->__find_file($include_string);
820 48 ▶ unless (defined $include_file) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $include_file) 0 48 48
821 0 warn "$Sub: warning: Failed to find file '$include_string' in $fileloc\n";
822 }
823 }
824 184 ▶ if (defined $include_file) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $include_file) 179 5 184
825 179 ▶ if ($self->{opt}{recurse}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'opt'}{'recurse'}) { } 176 3 179
826 176 push @{ $self->{__incs_pending}{$file_id} },
827 [
828 $stmt_content, # 0
829 $elem->line_number, # 1
830 $elem->column_number, # 2
831 $last->line_number, # 3
832 $last->column_number, # 4
833 $include_string, # 5
834 $include_file, # 6
835 # 7 will be the file_id of include_file
836 ];
837 } else {
838 3 $self->__assign_file_id($include_file);
839 }
840 }
841 }
842 }
843 6180 ▶ if ($elem->isa('PPI::Structure')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($elem->isa('PPI::Structure')) { } 414 5766 6180
844 # { ... }
845 414 ▶ unshift @queue, $elem->finish if $elem->finish;
Branch: 1 branch
BranchTrueFalseTotal
if $elem->finish 414 0 414
846 414 unshift @queue, @children;
847 414 ▶ unshift @queue, $elem->start if $elem->start;
Branch: 1 branch
BranchTrueFalseTotal
if $elem->start 414 0 414
848 } else {
849 5766 unshift @queue, @children;
850 }
851 6180 $prev_elem = $elem;
852 6180 $prev_package = $package;
853 }
854 164 ▶ if (defined $elem) {
Branch: 2 branches
BranchTrueFalseTotal
if (defined $elem) { } 164 0 164
elsif (@{$doc->{'children'};}) { } 0 0 0
855 164 $self->{file_lines}{$file_id} = $elem->line_number;
856 164 $self->__close_package($file_id, $package, $elem);
857 0 } elsif (@{ $doc->{children} }) {
858 0 $self->__parse_error($file_id, $filename,
859 "Undefined token when leaving");
860 }
861  
862 # Mark the __incs_pending as ready to be recursed into.
863 164 $self->{__incs_flush}{$file_id}++;
864  
865 164 $self->__process_pending_incs($file_id, $process_depth);
866 }
867  
868 sub __trash_cache {
869 44 ▶ my $self = shift;
Subroutine __trash_cache: called CC=1 SCAR=0.0
870 44 delete $self->{result_cache};
871 44 delete $self->{seen_file};
872 }
873  
874 # Parse the given filenames (or if a scalar ref, a string of code,
875 # in which case filename is assumed to be '-').
876 sub process {
877 44 ▶ my $self = shift;
Subroutine process: called CC=5 SCAR=16.1
Pod undocumented
878 44 $self->__trash_cache;
879 44 my $success = 1;
880 44 for my $arg (@_) {
881 44 my $file;
882 44 my $ref = ref $arg;
883 44 ▶ if ($ref eq '') {
Branch: 2 branches
BranchTrueFalseTotal
if ($ref eq '') { } 32 12 44
elsif ($ref eq 'SCALAR') { } 11 1 12
884 32 $file = $arg;
885 } elsif ($ref eq 'SCALAR') {
886 11 $file = '-';
887 } else {
888 1 warn "$Sub: Unexpected argument '$arg' (ref: $ref)\n";
889 1 $success = 0;
890 1 next;
891 }
892 43 my $file_id = $self->__process_file($arg, $file, 0);
893 43 ▶ unless (defined $file_id) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $file_id) 1 42 43
894 1 $success = 0;
895 1 next;
896 }
897 42 $self->{__process}{ $file_id }++;
898 }
899 44 return $success;
900 }
901  
902 sub process_files_from_cache {
903 0 ▶ my $self = shift;
Subroutine process_files_from_cache: not called CC=3 SCAR=24.8
Pod undocumented
904 0 my %files;
905 0 $self->find_cache_files(\%files);
906 0 ▶ if ($self->{opt}{process_verbose} || $self->{opt}{cache_verbose}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"opt"}{"process_verbose"} or $self->{"opt"}{"cache_verbose"}) 0 0 0
Condition0%0/3
$self->{"opt"}{"process_verbose"} or $self->{"opt"}{"cache_verbose"}
l!l&&r!l&&!r
000
MC/DC0%0/2
$self->{"opt"}{"process_verbose"} or $self->{"opt"}{"cache_verbose"}
$self->{"opt"}{"process_verbose"} $self->{"opt"}{"cache_verbose"}
Truth table0%0/3
A or B
A= $self->{"opt"}{"process_verbose"}B= $self->{"opt"}{"cache_verbose"}
ABresult
1X1
011
000
907 0 my $cache_directory = $self->{opt}{cache_directory};
908 0 printf("$Sub: found %d cache files from %s\n",
909 scalar keys %files, $cache_directory);
910 }
911 0 $self->process(sort keys %files);
912 }
913  
914 sub process_files_from_system {
915 0 ▶ my $self = shift;
Subroutine process_files_from_system: not called CC=2 SCAR=17.9
Pod undocumented
916 0 my %files;
917 0 $self->find_system_files(\%files);
918 0 ▶ if ($self->{opt}{process_verbose}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"opt"}{"process_verbose"}) 0 0 0
919 0 my $cache_directory = $self->{opt}{cache_directory};
920 0 printf("$Sub: found %d system files from @INC\n",
921 scalar keys %files);
922 }
923 0 $self->process(sort keys %files);
924 }
925  
926 # Returns the seen filenames.
927 sub files {
928 9 ▶ my $self = shift;
Subroutine files: called CC=3 SCAR=11.2
Pod documented
929 9 ▶ unless (defined $self->{result_cache}{files}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self->{"result_cache"}{"files"}) 9 0 9
930 9 ▶ return unless $self->{file_id};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"file_id"} 0 9 9
931 9 $self->{result_cache}{files} = [ sort keys %{ $self->{file_id} } ];
932 }
933 9 return @{ $self->{result_cache}{files} };
934 }
935  
936 # Computes the total number of lines.
937 sub total_lines {
938 1 ▶ my $self = shift;
Subroutine total_lines: called CC=3 SCAR=11.1
Pod documented
939 1 ▶ unless (defined $self->{result_cache}{total_lines}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self->{"result_cache"}{"total_lines"}) 1 0 1
940 1 ▶ return unless $self->{file_lines};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"file_lines"} 0 1 1
941 33 ▶ use List::Util qw[sum];
Subroutine BEGIN: called CC=1 SCAR=0.0
942 1 $self->{result_cache}{total_lines} = sum grep { defined } values %{ $self->{file_lines} };
943 }
944 1 return $self->{result_cache}{total_lines};
945 }
946  
947 # Lines in a file.
948 sub file_lines {
949 6 ▶ my ($self, $file) = @_;
Subroutine file_lines: called CC=1 SCAR=0.0
Pod documented
950 6 return $self->{file_lines}{$self->{file_id}{$file}};
951 }
952  
953 # Returns the known file ids.
954 sub __file_ids {
955 49 ▶ my $self = shift;
Subroutine __file_ids: called CC=3 SCAR=11.0
956 49 ▶ unless (defined $self->{result_cache}{__file_ids}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self->{"result_cache"}{"__file_ids"}) 31 18 49
957 31 ▶ return unless $FILE_ID;
Branch: 1 branch
BranchTrueFalseTotal
unless $FILE_ID 0 31 31
958 31 $self->{result_cache}{__file_ids} = [ 0..$FILE_ID-1 ];
959 }
960 49 return @{ $self->{result_cache}{__file_ids} };
961 }
962  
963 # Returns the reference count of a filename.
964 sub file_count {
965 6 ▶ my ($self, $file) = @_;
Subroutine file_count: called CC=2 SCAR=7.1
Pod documented
966 6 ▶ return unless $self->{file_counts};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"file_counts"} 0 6 6
967 6 return $self->{file_counts}->{$file};
968 }
969  
970 # Computes the seen modules.
971 sub __modules {
972 12 ▶ my $self = shift;
Subroutine __modules: called CC=5 SCAR=16.1
973 12 ▶ unless (defined $self->{result_cache}{modules}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self->{"result_cache"}{"modules"}) 8 4 12
974 8 ▶ return unless $self->{file_modules};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"file_modules"} 0 8 8
975 8 delete $self->{modules};
976 8 for my $f ($self->__file_ids) {
977 51 for my $m (keys %{ $self->{file_modules}{$f} }) {
978 21 $self->{modules}{$m} += $self->{file_modules}{$f}{$m};
979 }
980 }
981 8 $self->{result_cache}{modules} = [ sort keys %{ $self->{modules} } ];
982 }
983 }
984  
985 # Returns the seen modules.
986 sub modules {
987 9 ▶ my $self = shift;
Subroutine modules: called CC=1 SCAR=0.0
Pod documented
988 9 $self->__modules;
989 9 return @{ $self->{result_cache}{modules} };
990 }
991  
992 # Computes the missing modules.
993 sub __missing_modules {
994 11 ▶ my $self = shift;
Subroutine __missing_modules: called CC=9 SCAR=22.0
995 11 ▶ unless (defined $self->{result_cache}{missing_modules}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self->{"result_cache"}{"missing_modules"}) 5 6 11
996 5 ▶ $self->{result_cache}{missing_modules} //= [];
Condition50%1/2
$self->{"result_cache"}{"missing_modules"} //= []
l!l
05
MC/DC0%0/1
$self->{"result_cache"}{"missing_modules"} //= []
$self->{"result_cache"}{"missing_modules"}
Truth table50%1/2
A
A= $self->{"result_cache"}{"missing_modules"}
Aresult
11
00
997 5 ▶ $self->{result_cache}{missing_modules_files} //= {};
Condition50%1/2
$self->{"result_cache"}{"missing_modules_files"} //= {}
l!l
05
MC/DC0%0/1
$self->{"result_cache"}{"missing_modules_files"} //= {}
$self->{"result_cache"}{"missing_modules_files"}
Truth table50%1/2
A
A= $self->{"result_cache"}{"missing_modules_files"}
Aresult
11
00
998 5 ▶ $self->{result_cache}{missing_modules_count} //= {};
Condition50%1/2
$self->{"result_cache"}{"missing_modules_count"} //= {}
l!l
05
MC/DC0%0/1
$self->{"result_cache"}{"missing_modules_count"} //= {}
$self->{"result_cache"}{"missing_modules_count"}
Truth table50%1/2
A
A= $self->{"result_cache"}{"missing_modules_count"}
Aresult
11
00
999 5 ▶ return unless $self->{file_missing_modules};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"file_missing_modules"} 2 3 5
1000 3 delete $self->{missing_modules};
1001 3 delete $self->{missing_modules_files};
1002 3 delete $self->{missing_modules_lines};
1003 3 delete $self->{missing_modules_count};
1004 3 for my $f ($self->__file_ids) {
1005 3 my $file = $FILE_BY_ID{$f};
1006 3 for my $m (keys %{ $self->{file_missing_modules}{$f} }) {
1007 5 for my $l (keys %{ $self->{file_missing_modules}{$f}{$m} }) {
1008 5 my $c = $self->{file_missing_modules}{$f}{$m}{$l};
1009 5 $self->{missing_modules_files}{$m}{$file} += $c;
1010 5 $self->{missing_modules_lines}{$m}{$l} += $c;
1011 5 $self->{missing_modules_count}{$m} += $c;
1012 }
1013 }
1014 }
1015 $self->{result_cache}{missing_modules} =
1016 3 [ sort keys %{ $self->{missing_modules_files} } ];
1017 }
1018 }
1019  
1020 # Returns the missing modules.
1021 sub missing_modules {
1022 5 ▶ my $self = shift;
Subroutine missing_modules: called CC=1 SCAR=0.0
Pod documented
1023 5 $self->__missing_modules;
1024 5 return @{ $self->{result_cache}{missing_modules} };
1025 }
1026  
1027 # Returns the total reference count of a module name.
1028 sub module_count {
1029 3 ▶ my ($self, $module) = @_;
Subroutine module_count: called CC=3 SCAR=11.4
Pod documented
1030 3 $self->__modules;
1031 3 ▶ return 0 unless $self->{modules};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"modules"} 0 3 3
1032 3 ▶ return $self->{modules}->{$module} || 0;
Condition50%1/2
$self->{'modules'}{$module} || 0
l!l
30
MC/DC0%0/1
$self->{'modules'}{$module} || 0
$self->{'modules'}{$module}
Truth table50%1/2
A
A= $self->{'modules'}{$module}
Aresult
11
00
1033 }
1034  
1035 # Returns the files referring a missing module.
1036 sub missing_module_files {
1037 1 ▶ my ($self, $module) = @_;
Subroutine missing_module_files: called CC=2 SCAR=7.0
Pod documented
1038 1 $self->__missing_modules;
1039 1 ▶ return unless $self->{missing_modules_files}{$module};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"missing_modules_files"}{$module} 0 1 1
1040 1 return sort keys %{ $self->{missing_modules_files}{$module} };
1041 }
1042  
1043 # Returns the lines referring a missing module.
1044 sub missing_module_lines {
1045 1 ▶ my ($self, $module) = @_;
Subroutine missing_module_lines: called CC=4 SCAR=15.5
Pod documented
1046 1 $self->__missing_modules;
1047 1 ▶ return unless $self->{missing_modules_lines}{$module};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"missing_modules_lines"}{$module} 0 1 1
1048 1 return map { "$_->[0]:$_->[1]" }
1049 0 ▶ sort { $a->[0] cmp $b->[0] || $a->[1] <=> $b->[1] }
Branch: 1 branch
BranchTrueFalseTotal
$PPI::Xref::a->[0] cmp $PPI::Xref::b->[0] or $PPI::Xref::a->[1] <=> $PPI::Xref::b->[1] 0 0 0
1050 1 ▶ map { /^(.+):(\d+)$/ ? [ $1, $2 ] : [ $_, 0 ] }
Branch: 1 branch
BranchTrueFalseTotal
/^(.+):(\d+)$/ ? : 1 0 1
1051 1 keys %{ $self->{missing_modules_lines}{$module} };
1052 }
1053  
1054 # Returns the times a missing module was referred.
1055 sub missing_module_count {
1056 4 ▶ my ($self, $module) = @_;
Subroutine missing_module_count: called CC=3 SCAR=11.0
Pod documented
1057 4 $self->__missing_modules;
1058 4 ▶ return 0 unless $self->{missing_modules_count}{$module};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"missing_modules_count"}{$module} 2 2 4
1059 2 ▶ return $self->{missing_modules_count}{$module} || 0;
Condition50%1/2
$self->{'missing_modules_count'}{$module} || 0
l!l
20
MC/DC0%0/1
$self->{'missing_modules_count'}{$module} || 0
$self->{'missing_modules_count'}{$module}
Truth table50%1/2
A
A= $self->{'missing_modules_count'}{$module}
Aresult
11
00
1060 }
1061  
1062 # Computes the parse errors.
1063 sub __parse_errors {
1064 5 ▶ my $self = shift;
Subroutine __parse_errors: called CC=6 SCAR=17.9
1065 5 ▶ unless (defined $self->{result_cache}{parse_errors_files}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self->{"result_cache"}{"parse_errors_files"}) 3 2 5
1066 3 ▶ $self->{result_cache}{parse_errors_files} //= [];
Condition50%1/2
$self->{"result_cache"}{"parse_errors_files"} //= []
l!l
03
MC/DC0%0/1
$self->{"result_cache"}{"parse_errors_files"} //= []
$self->{"result_cache"}{"parse_errors_files"}
Truth table50%1/2
A
A= $self->{"result_cache"}{"parse_errors_files"}
Aresult
11
00
1067 3 ▶ return unless exists $self->{file_parse_errors};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $self->{"file_parse_errors"} 2 1 3
1068 1 delete $self->{parse_errors_files};
1069 1 for my $f ($self->__file_ids) {
1070 1 for my $l (keys %{ $self->{file_parse_errors}{$f} }) {
1071 1 $self->{parse_errors_files}{$FILE_BY_ID{$f}}++;
1072 }
1073 }
1074 $self->{result_cache}{parse_errors_files} =
1075 1 [ sort keys %{ $self->{parse_errors_files} } ];
1076 }
1077 }
1078  
1079 # Return the files with parse errors.
1080 sub parse_errors_files {
1081 4 ▶ my $self = shift;
Subroutine parse_errors_files: called CC=1 SCAR=0.0
Pod documented
1082 4 $self->__parse_errors;
1083 4 return @{ $self->{result_cache}{parse_errors_files} };
1084 }
1085  
1086 # Return the parse errors in a file, as a hash of filelocation -> error.
1087 sub file_parse_errors {
1088 1 ▶ my ($self, $file) = @_;
Subroutine file_parse_errors: called CC=5 SCAR=16.7
Pod documented
1089 1 $self->__parse_errors;
1090 1 ▶ return unless exists $self->{file_parse_errors};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $self->{"file_parse_errors"} 0 1 1
1091 1 ▶ return unless defined $file;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $file 0 1 1
1092 1 my $file_id = $self->{file_id}{$file};
1093 1 ▶ return unless defined $file_id;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $file_id 0 1 1
1094 1 ▶ return unless exists $self->{file_parse_errors}{$file_id};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $self->{"file_parse_errors"}{$file_id} 0 1 1
1095 1 return %{ $self->{file_parse_errors}{$file_id} };
1096 }
1097  
1098 # Generates the subs or packages.
1099 sub __subs_or_packages {
1100 22 ▶ my ($self, $key, $cache) = @_;
Subroutine __subs_or_packages: called CC=4 SCAR=13.9
1101 22 ▶ unless (defined $self->{result_cache}{$cache}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self->{"result_cache"}{$cache}) 20 2 22
1102 20 ▶ return unless $self->{$key};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{$key} 0 20 20
1103 20 my %uniq;
1104 20 for my $f ($self->__file_ids) {
1105 106 @uniq{ map { $_->[0] } @{ $self->{$key}{$f} } } = ();
1106 }
1107 20 $self->{result_cache}{$cache} = [ sort keys %uniq ];
1108 }
1109 22 return @{ $self->{result_cache}{$cache} };
1110 }
1111  
1112 # Returns the subs.
1113 sub subs {
1114 13 ▶ my $self = shift;
Subroutine subs: called CC=1 SCAR=0.0
Pod documented
1115 13 return $self->__subs_or_packages(file_subs => 'subs');
1116 }
1117  
1118 # Returns the packages.
1119 sub packages {
1120 9 ▶ my $self = shift;
Subroutine packages: called CC=1 SCAR=0.0
Pod documented
1121 9 return $self->__subs_or_packages(file_packages => 'packages');
1122 }
1123  
1124 # Generates the subs' or packages' files.
1125 sub __subs_or_packages_and_files {
1126 7 ▶ my ($self, $key, $cache) = @_;
Subroutine __subs_or_packages_and_files: called CC=8 SCAR=21.5
1127 7 ▶ unless (defined $self->{result_cache}{$cache}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self->{"result_cache"}{$cache}) 7 0 7
1128 7 ▶ return [] unless $self->{$key};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{$key} 0 7 7
1129 7 my @cache;
1130 7 for my $f ($self->__file_ids) {
1131 38 for my $s (@{ $self->{$key}{$f} }) {
1132 push @cache,
1133 82 [ $s->[0], $f, @{$s}[1..$#$s] ];
1134 }
1135 }
1136 $self->{result_cache}{$cache} = [
1137 7 sort { $a->[0] cmp $b->[0] ||
1138 203 ▶ $FILE_BY_ID{$a->[1]} cmp $FILE_BY_ID{$b->[1]} ||
Branch: 1 branch
BranchTrueFalseTotal
$PPI::Xref::a->[0] cmp $PPI::Xref::b->[0] or $FILE_BY_ID{$PPI::Xref::a->[1]} cmp $FILE_BY_ID{$PPI::Xref::b->[1]} or $PPI::Xref::a->[2] <=> $PPI::Xref::b->[2] or $PPI::Xref::a->[3] <=> $PPI::Xref::b->[3] 0 203 203
Condition66%4/6
$PPI::Xref::a->[0] cmp $PPI::Xref::b->[0] or $FILE_BY_ID{$PPI::Xref::a->[1]} cmp $FILE_BY_ID{$PPI::Xref::b->[1]} or $PPI::Xref::a->[2] <=> $PPI::Xref::b->[2]
l!l&&r!l&&!r
20120
$PPI::Xref::a->[0] cmp $PPI::Xref::b->[0] or $FILE_BY_ID{$PPI::Xref::a->[1]} cmp $FILE_BY_ID{$PPI::Xref::b->[1]}
l!l&&r!l&&!r
20102
MC/DC0%0/3
$PPI::Xref::a->[0] cmp $PPI::Xref::b->[0] or $FILE_BY_ID{$PPI::Xref::a->[1]} cmp $FILE_BY_ID{$PPI::Xref::b->[1]} or $PPI::Xref::a->[2] <=> $PPI::Xref::b->[2]
$PPI::Xref::a->[0] cmp $PPI::Xref::b->[0] $FILE_BY_ID{$PPI::Xref::a->[1]} cmp $FILE_BY_ID{$PPI::Xref::b->[1]} $PPI::Xref::a->[2] <=> $PPI::Xref::b->[2]
Truth table0%0/4
A or B or C
A= $PPI::Xref::a->[0] cmp $PPI::Xref::b->[0]B= $FILE_BY_ID{$PPI::Xref::a->[1]} cmp $FILE_BY_ID{$PPI::Xref::b->[1]}C= $PPI::Xref::a->[2] <=> $PPI::Xref::b->[2]
ABCresult
1XX1
01X1
0011
0000
1139 $a->[2] <=> $b->[2] ||
1140 $a->[3] <=> $b->[3] }
1141 @cache ];
1142 }
1143 7 return $self->{result_cache}{$cache};
1144 }
1145  
1146 # Returns the subs' files (an aref to iterate through).
1147 sub __subs_files_full {
1148 6 ▶ my $self = shift;
Subroutine __subs_files_full: called CC=1 SCAR=0.0
1149 6 $self->__subs_or_packages_and_files(file_subs => '__subs_files');
1150 }
1151  
1152 # Returns the packages' files (an aref to iterate through).
1153 sub __packages_files_full {
1154 1 ▶ my $self = shift;
Subroutine __packages_files_full: called CC=1 SCAR=0.0
1155 1 $self->__subs_or_packages_and_files(file_packages => '__packages_files');
1156 }
1157  
1158 # Base class for returning iterator results.
1159 33 package PPI::Xref::IterResultBase {
1160 # Result as a string.
1161 # The desired fields are selected, and concatenated.
1162 sub string {
1163 98 ▶ my $self = shift;
Subroutine string: called CC=1 SCAR=0.0
1164 98 return $self->{cb}->($self->{it});
1165 }
1166 # Result as an array.
1167 sub array {
1168 0 ▶ my $self = shift;
Subroutine array: not called CC=1 SCAR=6.9
1169 0 return @{ $self->{it} };
1170 }
1171 }
1172  
1173 33 package PPI::Xref::FileIterResult {
1174 33 ▶ use parent -norequire, 'PPI::Xref::IterResultBase';
Subroutine BEGIN: called CC=1 SCAR=0.0
1175 }
1176  
1177 33 package PPI::Xref::FileIterBase {
1178 # Base class method for stepping through the subs' and packages' files.
1179 # Converts the file id into filename, and returns iterator result.
1180 sub next {
1181 # NOTE: while this is an iterator in the sense of returning the next
1182 # result, this does not compute the next result because all the results
1183 # have already been computed when the iterator was constructed.
1184 89 ▶ my $self = shift;
Subroutine next: called CC=2 SCAR=6.9
1185 89 ▶ if ($self->{ix} < @{$self->{it}}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"ix"} < @{$self->{"it"};}) 82 7 89
1186 82 my @it = @{ $self->{it}->[$self->{ix}++] };
1187 82 $it[1] = $FILE_BY_ID{$it[1]};
1188 return bless { it => \@it, cb => $self->{cb} },
1189 82 'PPI::Xref::FileIterResult'
1190 }
1191 7 return;
1192 }
1193 }
1194  
1195 33 package PPI::Xref::SubsFilesIter {
1196 33 ▶ use parent -norequire, 'PPI::Xref::FileIterBase';
Subroutine BEGIN: called CC=1 SCAR=0.0
1197 }
1198  
1199 33 package PPI::Xref::PackagesFilesIter {
1200 33 ▶ use parent -norequire, 'PPI::Xref::FileIterBase';
Subroutine BEGIN: called CC=1 SCAR=0.0
1201 }
1202  
1203 # Callback generator for subs' and packages' files.
1204 # Selects the desired fields, and concatenates the fields.
1205 sub __file_iter_callback {
1206 7 ▶ my $opt = shift;
Subroutine __file_iter_callback: called CC=1 SCAR=0.0
1207 sub {
1208 82 ▶ my $self = shift;
Subroutine __ANON__: called CC=5 SCAR=16.1
1209 join($opt->{separator} // "\t",
1210 82 ▶ @{$self}[0, 1, 2],
Branch: 3 branches
BranchTrueFalseTotal
$opt->{'column'} ? : 28 54 82
$opt->{'finish'} ? : 14 14 28
$opt->{'finish'} ? : 14 40 54
1211 82 @{$self}[$opt->{column} ?
1212 ($opt->{finish} ? (3, 4, 5) : (3)) :
1213 82 ▶ ($opt->{finish} ? (4) : ())]
Condition100%2/2
$opt->{"separator"} // "\t"
l!l
1666
MC/DC100%1/1
$opt->{"separator"} // "\t"
$opt->{"separator"}
Truth table100%2/2
A
A= $opt->{"separator"}
Aresult
11
00
1214 );
1215 }
1216 7 }
1217  
1218 # Constructor for iterating through the subs' files.
1219 sub subs_files_iter {
1220 6 ▶ my ($self, $opt) = @_;
Subroutine subs_files_iter: called CC=1 SCAR=0.0
Pod documented
1221 6 bless {
1222 it => $self->__subs_files_full,
1223 ix => 0,
1224 cb => __file_iter_callback($opt),
1225 }, 'PPI::Xref::SubsFilesIter';
1226 }
1227  
1228 # Constructor for iterating through the packages' files.
1229 sub packages_files_iter {
1230 1 ▶ my ($self, $opt) = @_;
Subroutine packages_files_iter: called CC=1 SCAR=0.0
Pod documented
1231 1 bless {
1232 it => $self->__packages_files_full,
1233 ix => 0,
1234 cb => __file_iter_callback($opt),
1235 }, 'PPI::Xref::PackagesFilesIter';
1236 }
1237  
1238 # Generates all the inclusion files and caches them.
1239 # The inclusion files are the file id, followed all its inclusions.
1240 # Sorting is by filename, line, column, and include_string (e.g. Data::Dumper).
1241 sub __incs_files_full {
1242 2 ▶ my ($self) = @_;
Subroutine __incs_files_full: called CC=7 SCAR=20.4
1243 2 ▶ unless (defined $self->{result_cache}{__incs_files}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self->{"result_cache"}{"__incs_files"}) 2 0 2
1244 2 my @cache;
1245 2 for my $f ($self->__file_ids) {
1246 8 for my $i (@{ $self->{file_incs}{$f} }) {
1247 push @cache, [ $f, # 0: fileid1
1248 8 @{$i}[1, # 1: line
1249 7, # 2: fileid2
1250 0, # 3: stmt
1251 5, # 4: include_string
1252 2, # 5: col
1253 3, # 6: line
1254 4, # 7: col
1255 ] ],
1256 }
1257 }
1258 $self->{result_cache}{__incs_files} = [
1259 2 ▶ sort { $FILE_BY_ID{$a->[0]} cmp $FILE_BY_ID{$b->[0]} ||
Branch: 1 branch
BranchTrueFalseTotal
$FILE_BY_ID{$PPI::Xref::a->[0]} cmp $FILE_BY_ID{$PPI::Xref::b->[0]} or $PPI::Xref::a->[1] <=> $PPI::Xref::b->[1] or $PPI::Xref::a->[2] <=> $PPI::Xref::b->[2] or $PPI::Xref::a->[5] cmp $PPI::Xref::b->[5] 0 11 11
Condition50%3/6
$FILE_BY_ID{$PPI::Xref::a->[0]} cmp $FILE_BY_ID{$PPI::Xref::b->[0]} or $PPI::Xref::a->[1] <=> $PPI::Xref::b->[1] or $PPI::Xref::a->[2] <=> $PPI::Xref::b->[2]
l!l&&r!l&&!r
1100
$FILE_BY_ID{$PPI::Xref::a->[0]} cmp $FILE_BY_ID{$PPI::Xref::b->[0]} or $PPI::Xref::a->[1] <=> $PPI::Xref::b->[1]
l!l&&r!l&&!r
650
MC/DC0%0/3
$FILE_BY_ID{$PPI::Xref::a->[0]} cmp $FILE_BY_ID{$PPI::Xref::b->[0]} or $PPI::Xref::a->[1] <=> $PPI::Xref::b->[1] or $PPI::Xref::a->[2] <=> $PPI::Xref::b->[2]
$FILE_BY_ID{$PPI::Xref::a->[0]} cmp $FILE_BY_ID{$PPI::Xref::b->[0]} $PPI::Xref::a->[1] <=> $PPI::Xref::b->[1] $PPI::Xref::a->[2] <=> $PPI::Xref::b->[2]
Truth table0%0/4
A or B or C
A= $FILE_BY_ID{$PPI::Xref::a->[0]} cmp $FILE_BY_ID{$PPI::Xref::b->[0]}B= $PPI::Xref::a->[1] <=> $PPI::Xref::b->[1]C= $PPI::Xref::a->[2] <=> $PPI::Xref::b->[2]
ABCresult
1XX1
01X1
0011
0000
1260 $a->[1] <=> $b->[1] || # line
1261 $a->[2] <=> $b->[2] || # column
1262 $a->[5] cmp $b->[5] } # include_string
1263 @cache ];
1264 }
1265 2 return $self->{result_cache}{__incs_files};
1266 }
1267  
1268 # Callback generator for inclusion files.
1269 # Selects the desired fields, and concatenates the fields with the separator.
1270 sub __incs_files_iter_callback {
1271 2 ▶ my $opt = shift;
Subroutine __incs_files_iter_callback: called CC=1 SCAR=0.2
1272 sub {
1273 8 ▶ my $self = shift;
Subroutine __ANON__: called CC=5 SCAR=17.8
1274 join($opt->{separator} // "\t",
1275 8 ▶ @{$self}[0..4],
Branch: 3 branches
BranchTrueFalseTotal
$opt->{'column'} ? : 0 8 8
$opt->{'finish'} ? : 0 0 0
$opt->{'finish'} ? : 0 8 8
1276 8 @{$self}[$opt->{column} ?
1277 ($opt->{finish} ? (5, 6, 7) : (5)) :
1278 8 ▶ ($opt->{finish} ? (6) : ())],
Condition100%2/2
$opt->{"separator"} // "\t"
l!l
17
MC/DC100%1/1
$opt->{"separator"} // "\t"
$opt->{"separator"}
Truth table100%2/2
A
A= $opt->{"separator"}
Aresult
11
00
1279 );
1280 }
1281 2 }
1282  
1283 33 package PPI::Xref::IncsFilesIterResult {
1284 33 ▶ use parent -norequire, 'PPI::Xref::IterResultBase';
Subroutine BEGIN: called CC=1 SCAR=0.0
1285 }
1286  
1287 33 package PPI::Xref::IncsFilesIter {
1288 # Iterator stepper for iterating through the inclusion files.
1289 # Converts the file ids to filenames, and returns iterator result.
1290 sub next {
1291 # NOTE: while this is an iterator in the sense of returning the next
1292 # result, this does not compute the next result because all the results
1293 # have already been computed when the iterator was constructed.
1294 10 ▶ my $self = shift;
Subroutine next: called CC=2 SCAR=6.9
1295 10 ▶ if ($self->{ix} < @{$self->{it}}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"ix"} < @{$self->{"it"};}) 8 2 10
1296 8 my @it = @{ $self->{it}->[$self->{ix}++] };
1297 8 $it[0] = $FILE_BY_ID{$it[0]};
1298 8 $it[2] = $FILE_BY_ID{$it[2]};
1299 return bless { it => \@it, cb => $self->{cb} },
1300 8 'PPI::Xref::IncsFilesIterResult'
1301 }
1302 2 return;
1303 }
1304 }
1305  
1306 # Constructor for iterating through the inclusion files.
1307 sub incs_files_iter {
1308 2 ▶ my ($self, $opt) = @_;
Subroutine incs_files_iter: called CC=1 SCAR=0.0
Pod documented
1309 2 bless {
1310 it => $self->__incs_files_full,
1311 ix => 0,
1312 cb => __incs_files_iter_callback($opt),
1313 }, 'PPI::Xref::IncsFilesIter';
1314 }
1315  
1316 # Recursive generator for inclusion chains. If there are inclusions
1317 # from this file, recurse for them; if not, aggregate into the result.
1318 sub __incs_chains_recurse {
1319 0 ▶ my ($self, $file_id, $path, $seen, $result) = @_;
Subroutine __incs_chains_recurse: not called CC=5 SCAR=34.0
1320 my @s =
1321 exists $self->{file_incs}{$file_id} ?
1322 0 ▶ @{ $self->{file_incs}{$file_id} } : ();
Branch: 1 branch
BranchTrueFalseTotal
exists $self->{'file_incs'}{$file_id} ? : 0 0 0
1323 0 $seen->{$file_id}++;
1324 # print "recurse: $FILE_BY_ID{$file_id} path: [@{[map { $FILE_BY_ID{$_} // $_ } @$path]}] \n";
1325 0 my $s = 0;
1326 0 for my $i (@s) {
1327 0 my ($line, $next_file_id) = ($i->[1], $i->[-1]);
1328 # print "recurse: $FILE_BY_ID{$file_id}:$line -> $FILE_BY_ID{$next_file_id} path: [@{[map { $FILE_BY_ID{$_} // $_ } @$path]}] seen: [@{[sort map { $FILE_BY_ID{$_} } keys %$seen]}]\n";
1329 # E.g. Carp uses strict, strict requires Carp.
1330 0 ▶ unless ($seen->{$next_file_id}++) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($seen->{$next_file_id}++) 0 0 0
1331 0 $self->__incs_chains_recurse($next_file_id, [ @$path, $line, $next_file_id ], $seen, $result);
1332 0 $s++;
1333 }
1334 }
1335 0 ▶ if ($s == 0) { # If this was a leaf (no paths leading out), aggregrate result.
Branch: 1 branch
BranchTrueFalseTotal
if ($s == 0) 0 0 0
1336 0 push @{$result}, [ @$path ];
1337 }
1338 0 delete $seen->{$file_id};
1339 }
1340  
1341 sub __incs_deps {
1342 4 ▶ my ($self) = @_;
Subroutine __incs_deps: called CC=11 SCAR=24.0
1343 4 ▶ unless (defined $self->{result_cache}{__incs_deps}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self->{"result_cache"}{"__incs_deps"}) 4 0 4
1344 4 my %pred;
1345 my %succ;
1346 4 my %line;
1347 4 for my $fi ($self->__file_ids) {
1348 25 ▶ if (exists $self->{file_incs}{$fi}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->{"file_incs"}{$fi}) 19 6 25
1349 19 for my $g (@{ $self->{file_incs}{$fi} }) {
1350 21 my ($gl, $gi) = @{ $g }[ 1, 7 ];
1351 21 $succ{$fi}{$gi}{$gl}++;
1352 21 $pred{$gi}{$fi}{$gl}++;
1353 21 $line{$fi}{$gl}{$gi}++;
1354 }
1355 }
1356 }
1357 4 my %singleton;
1358 my %leaf;
1359 4 my %root;
1360 4 my %branch;
1361 4 for my $s ($self->__file_ids) {
1362 25 ▶ my @s = exists $succ{$s} ? keys %{$succ{$s}} : ();
Branch: 1 branch
BranchTrueFalseTotal
exists $succ{$s} ? : 9 16 25
1363 25 ▶ my @p = exists $pred{$s} ? keys %{$pred{$s}} : ();
Branch: 1 branch
BranchTrueFalseTotal
exists $pred{$s} ? : 15 10 25
1364 25 ▶ if (@s == 0) {
Branch: 2 branches
BranchTrueFalseTotal
if (@s == 0) { } 16 9 25
elsif (@p == 0) { } 3 6 9
1365 16 ▶ if (@p == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (@p == 0) { } 7 9 16
1366 7 $singleton{$s}++;
1367 } else {
1368 9 $leaf{$s}++;
1369 }
1370 } elsif (@p == 0) {
1371 3 $root{$s}++;
1372 } else {
1373 6 $branch{$s}++;
1374 }
1375 }
1376 $self->{result_cache}{__incs_deps} = {
1377 4 pred => \%pred,
1378 succ => \%succ,
1379 line => \%line,
1380 singleton => \%singleton,
1381 leaf => \%leaf,
1382 root => \%root,
1383 branch => \%branch,
1384 parent => $self,
1385 };
1386 }
1387 4 return $self->{result_cache}{__incs_deps};
1388 }
1389  
1390 33 package PPI::Xref::IncsDeps {
1391 sub files {
1392 1 ▶ my ($self) = @_;
Subroutine files: called CC=1 SCAR=0.0
1393 1 return $self->{parent}->files;
1394 }
1395 sub __file_id {
1396 8 ▶ my ($self, $file) = @_;
Subroutine __file_id: called CC=1 SCAR=0.0
1397 8 return $self->{parent}{file_id}{$file};
1398 }
1399 sub __by_file {
1400 0 ▶ my ($self, $key, $file) = @_;
Subroutine __by_file: not called CC=3 SCAR=24.8
1401 0 my $file_id = $self->__file_id($file);
1402 0 ▶ return unless defined $file_id && exists $self->{$key}{$file_id};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $file_id and exists $self->{$key}{$file_id} 0 0 0
Condition0%0/3
defined $file_id and exists $self->{$key}{$file_id}
!ll&&!rl&&r
000
MC/DC0%0/2
defined $file_id and exists $self->{$key}{$file_id}
defined $file_id exists $self->{$key}{$file_id}
Truth table0%0/3
A and B
A= defined $file_idB= exists $self->{$key}{$file_id}
ABresult
0X0
100
111
1403 0 return keys %{ $self->{$key}{$file_id} };
1404 }
1405 sub __filenames {
1406 0 ▶ my $self = shift;
Subroutine __filenames: not called CC=1 SCAR=6.9
1407 0 return map { $FILE_BY_ID{$_} } @_;
1408 }
1409 sub __predecessors {
1410 0 ▶ my ($self, $file) = @_;
Subroutine __predecessors: not called CC=1 SCAR=6.9
1411 0 return _$self->__by_file(pred => $file);
1412 }
1413 sub __successors {
1414 0 ▶ my ($self, $file) = @_;
Subroutine __successors: not called CC=1 SCAR=6.9
1415 0 return $self->__by_file(succ => $file);
1416 }
1417 sub predecessors {
1418 0 ▶ my ($self, $file) = @_;
Subroutine predecessors: not called CC=1 SCAR=6.9
1419 0 return $self->__filenames(_$self->__predecessors($file));
1420 }
1421 sub successors {
1422 0 ▶ my ($self, $file) = @_;
Subroutine successors: not called CC=1 SCAR=6.9
1423 0 return $self->__filenames($self->__successors($file));
1424 }
1425 sub __files {
1426 0 ▶ my ($self, $key) = @_;
Subroutine __files: not called CC=2 SCAR=17.9
1427 return exists $self->{$key} ?
1428 0 ▶ map { $FILE_BY_ID{$_} } keys %{ $self->{$key} } : ();
Branch: 1 branch
BranchTrueFalseTotal
exists $self->{$key} ? : 0 0 0
1429 }
1430 sub __roots {
1431 2 ▶ my ($self) = @_;
Subroutine __roots: called CC=2 SCAR=7.1
1432 2 ▶ return exists $self->{root} ? keys %{ $self->{root} } : ();
Branch: 1 branch
BranchTrueFalseTotal
exists $self->{'root'} ? : 2 0 2
1433 }
1434 sub __singletons {
1435 2 ▶ my ($self) = @_;
Subroutine __singletons: called CC=2 SCAR=7.1
1436 2 ▶ return exists $self->{singleton} ? keys %{ $self->{singleton} } : ();
Branch: 1 branch
BranchTrueFalseTotal
exists $self->{'singleton'} ? : 2 0 2
1437 }
1438 sub roots {
1439 0 ▶ my ($self) = @_;
Subroutine roots: not called CC=1 SCAR=6.9
1440 0 return $self->__files('root');
1441 }
1442 sub leaves {
1443 0 ▶ my ($self) = @_;
Subroutine leaves: not called CC=1 SCAR=6.9
1444 0 return $self->__files('leaf');
1445 }
1446 sub singletons {
1447 0 ▶ my ($self) = @_;
Subroutine singletons: not called CC=1 SCAR=6.9
1448 0 return $self->__files('singleton');
1449 }
1450 sub branches {
1451 0 ▶ my ($self) = @_;
Subroutine branches: not called CC=1 SCAR=6.9
1452 0 return $self->__files('branch');
1453 }
1454 sub __file_kind {
1455 10 ▶ my ($self, $file_id) = @_;
Subroutine __file_kind: called CC=6 SCAR=18.0
1456 10 ▶ return unless defined $file_id;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $file_id 1 9 10
1457 9 ▶ return 'branch' if exists $self->{branch} {$file_id};
Branch: 1 branch
BranchTrueFalseTotal
if exists $self->{"branch"}{$file_id} 2 7 9
1458 7 ▶ return 'leaf' if exists $self->{leaf} {$file_id};
Branch: 1 branch
BranchTrueFalseTotal
if exists $self->{"leaf"}{$file_id} 3 4 7
1459 4 ▶ return 'root' if exists $self->{root} {$file_id};
Branch: 1 branch
BranchTrueFalseTotal
if exists $self->{"root"}{$file_id} 3 1 4
1460 1 ▶ return 'singleton' if exists $self->{singleton}{$file_id};
Branch: 1 branch
BranchTrueFalseTotal
if exists $self->{"singleton"}{$file_id} 1 0 1
1461 0 return;
1462 }
1463 sub file_kind {
1464 8 ▶ my ($self, $file) = @_;
Subroutine file_kind: called CC=1 SCAR=0.0
1465 8 return $self->__file_kind($self->__file_id($file));
1466 }
1467 }
1468  
1469 sub incs_deps {
1470 4 ▶ my ($self) = @_;
Subroutine incs_deps: called CC=1 SCAR=0.0
Pod documented
1471 4 bless $self->__incs_deps, 'PPI::Xref::IncsDeps';
1472 }
1473  
1474 sub __incs_chains_iter {
1475 2 ▶ my ($self, $opt) = @_;
Subroutine __incs_chains_iter: called CC=2 SCAR=7.0
1476 2 my %iter;
1477 2 my $deps = $self->incs_deps;
1478 2 ▶ if (defined $deps) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $deps) 2 0 2
1479 $iter{next} = sub {
1480 10 ▶ my ($iterself) = @_;
Subroutine __ANON__: called CC=21 SCAR=30.7
1481 10 ▶ until ($iterself->{done}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($iterself->{"done"}) 12 0 12
1482 12 ▶ unless (defined $iterself->{path} && @{ $iterself->{path} }) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $iterself->{"path"} and @{$iterself->{"path"};}) 4 8 12
Condition100%3/3
defined $iterself->{"path"} and @{$iterself->{"path"};}
!ll&&!rl&&r
228
MC/DC100%2/2
defined $iterself->{"path"} and @{$iterself->{"path"};}
defined $iterself->{"path"} @{$iterself->{"path"};}
Truth table100%3/3
A and B
A= defined $iterself->{"path"}B= @{$iterself->{"path"};}
ABresult
0X0
100
111
1483 4 ▶ unless (defined $iterself->{roots}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $iterself->{"roots"}) 2 2 4
1484 2 my @roots = (
1485 $deps->__roots,
1486 $deps->__singletons,
1487 );
1488 2 my %roots;
1489 2 @roots{@roots} = ();
1490 2 ▶ if (exists $self->{__process}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->{"__process"}) 2 0 2
1491 2 for my $id (keys %{ $self->{__process} }) {
1492 2 ▶ push @roots, $id unless exists $roots{$id};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $roots{$id} 0 2 2
1493 }
1494 }
1495 2 $iterself->{roots} = \@roots;
1496 }
1497 4 my $root = shift @{ $iterself->{roots} };
1498 4 ▶ unless (defined $root) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $root) 2 2 4
1499 2 $iterself->{done}++;
1500 2 return;
1501 }
1502 2 $iterself->{path} = [ $root ];
1503 # E.g. Carp uses strict, strict requires Carp, and also
1504 # the dependency trees are very probably not clean DAGs.
1505 2 $iterself->{seen} = { $root => { 0 => 1 } };
1506 };
1507 10 ▶ while (@{ $iterself->{path} }) {
Branch: 1 branch
BranchTrueFalseTotal
if (@{$iterself->{"path"};}) 30 2 32
1508 30 my $curr = $iterself->{path}[-1];
1509 30 my $pushed = 0;
1510 SUCC: {
1511 30 ▶ if (exists $deps->{line}{$curr}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $deps->{"line"}{$curr}) 22 8 30
1512 22 for my $line (sort { $a <=> $b }
1513 22 keys %{ $deps->{line}{$curr} }) {
1514 42 for my $succ (sort { $a cmp $b }
1515 42 keys %{ $deps->{line}{$curr}{$line} }) {
1516 42 ▶ unless ($iterself->{seen}{$succ}{$line}++) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($iterself->{"seen"}{$succ}{$line}++) 14 28 42
1517 14 push @{ $iterself->{path} }, $line, $succ;
1518 14 $pushed++;
1519 14 last SUCC;
1520 }
1521 }
1522 }
1523 }
1524 }
1525 30 ▶ unless ($pushed) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($pushed) 16 14 30
1526 16 ▶ if (my @path = @{ $iterself->{path} }) {
Branch: 1 branch
BranchTrueFalseTotal
if (my(@path) = @{$iterself->{"path"};}) 16 0 16
1527 16 ▶ @path = reverse @path if $opt->{reverse_chains};
Branch: 1 branch
BranchTrueFalseTotal
if $opt->{"reverse_chains"} 8 8 16
1528 16 ▶ if (@path > 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (@path > 1) { } 14 2 16
1529 14 splice @{ $iterself->{path} }, -2; # Double-pop.
1530 14 ▶ if ($self->{lastpush}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"lastpush"}) 8 6 14
1531 8 $self->{lastpush} = $pushed;
1532 8 return @path;
1533 }
1534 } else {
1535 2 $iterself->{path} = [];
1536 2 my $kind = $deps->__file_kind($curr);
1537 2 ▶ if (defined $kind && $kind eq 'singleton') {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $kind and $kind eq "singleton") 0 2 2
Condition33%1/3
defined $kind and $kind eq "singleton"
!ll&&!rl&&r
020
MC/DC0%0/2
defined $kind and $kind eq "singleton"
defined $kind $kind eq "singleton"
Truth table33%1/3
A and B
A= defined $kindB= $kind eq "singleton"
ABresult
0X0
100
111
1538 0 return @path;
1539 }
1540 }
1541 }
1542 }
1543 22 $self->{lastpush} = $pushed;
1544 } # while
1545 }
1546 2 };
1547 }
1548 2 return \%iter;
1549 }
1550  
1551 # Callback generator for inclusion chains.
1552 # Simply concatenates the fields with the separator.
1553 sub __incs_chains_iter_callback {
1554 2 ▶ my $opt = shift;
Subroutine __incs_chains_iter_callback: called CC=1 SCAR=0.0
1555 sub {
1556 8 ▶ my $self = shift;
Subroutine __ANON__: called CC=2 SCAR=7.1
1557 8 ▶ join($opt->{separator} // "\t", @{$self} );
Condition50%1/2
$opt->{"separator"} // "\t"
l!l
08
MC/DC0%0/1
$opt->{"separator"} // "\t"
$opt->{"separator"}
Truth table50%1/2
A
A= $opt->{"separator"}
Aresult
11
00
1558 }
1559 2 }
1560  
1561 33 package PPI::Xref::IncsChainsIterResult {
1562 33 ▶ use parent -norequire, 'PPI::Xref::IterResultBase';
Subroutine BEGIN: called CC=1 SCAR=0.0
1563 }
1564  
1565 33 package PPI::Xref::IncsChainsIter {
1566 # Iterator stepper for iterating through the inclusion chains.
1567 # Converts the file ids to filenames, and returns iterator result.
1568 sub next {
1569 10 ▶ my $self = shift;
Subroutine next: called CC=3 SCAR=11.0
1570 10 ▶ if (my @it = $self->{it}{next}->($self)) {
Branch: 1 branch
BranchTrueFalseTotal
if (my(@it) = $self->{"it"}{"next"}->($self)) 8 2 10
1571 8 ▶ for (my $i = 0; $i < @it; $i += 2) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @it) 26 8 34
1572 26 $it[$i] = $FILE_BY_ID{$it[$i]};
1573 }
1574 return bless { it => \@it, cb => $self->{cb} },
1575 8 'PPI::Xref::IncsChainsIterResult';
1576 }
1577 2 return;
1578 }
1579 }
1580  
1581 # Constructor for iterating through the inclusion chains.
1582 sub incs_chains_iter {
1583 2 ▶ my ($self, $opt) = @_;
Subroutine incs_chains_iter: called CC=1 SCAR=0.0
Pod documented
1584 2 bless {
1585 it => $self->__incs_chains_iter($opt),
1586 ix => 0,
1587 cb => __incs_chains_iter_callback($opt),
1588 }, 'PPI::Xref::IncsChainsIter';
1589 }
1590  
1591 sub looks_like_cache_file {
1592 9 ▶ my ($self, $file) = @_;
Subroutine looks_like_cache_file: called CC=3 SCAR=11.3
Pod undocumented
1593  
1594 9 my $cache_directory = $self->{opt}{cache_directory};
1595 9 ▶ return unless defined $cache_directory;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $cache_directory 0 9 9
1596  
1597 9 ▶ return 0 if $file =~ m{\.\.};
Branch: 1 branch
BranchTrueFalseTotal
if $file =~ /\.\./ 0 9 9
1598  
1599 9 return $file =~ m{^\Q$cache_directory\E[/\\].+\Q$CACHE_EXT\E$};
1600 }
1601  
1602 sub cache_delete {
1603 3 ▶ my $self = shift;
Subroutine cache_delete: called CC=11 SCAR=28.3
Pod documented
1604 3 my $cache_directory = $self->{opt}{cache_directory};
1605 3 ▶ unless (defined $cache_directory) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $cache_directory) 0 3 3
1606 0 warn "$Sub: cache_directory undefined\n";
1607 0 return;
1608 }
1609 3 my $delete_count = 0;
1610 3 for my $file (@_) {
1611 3 ▶ if (!File::Spec->file_name_is_absolute($file) ||
Branch: 1 branch
BranchTrueFalseTotal
if (not "File::Spec"->file_name_is_absolute($file) or $file =~ /\.\./ or not $file =~ /_p[ml](?:\Q$CACHE_EXT\E)?$/ || $file =~ /.p[ml]$/) 0 3 3
Condition44%4/9
not "File::Spec"->file_name_is_absolute($file) or $file =~ /\.\./ or not $file =~ /_p[ml](?:\Q$CACHE_EXT\E)?$/ || $file =~ /.p[ml]$/
l!l&&r!l&&!r
003
not "File::Spec"->file_name_is_absolute($file) or $file =~ /\.\./
l!l&&r!l&&!r
003
$file =~ /_p[ml](?:\Q$CACHE_EXT\E)?$/ || $file =~ /.p[ml]$/
l!l&&r!l&&!r
120
MC/DC0%0/4
not "File::Spec"->file_name_is_absolute($file) or $file =~ /\.\./ or not $file =~ /_p[ml](?:\Q$CACHE_EXT\E)?$/ || $file =~ /.p[ml]$/
not "File::Spec"->file_name_is_absolute($file) $file =~ /\.\./ $file =~ /_p[ml](?:\Q$CACHE_EXT\E)?$/ $file =~ /.p[ml]$/
Truth table40%2/5
not(A) or B or not(C || D)
A= not "File::Spec"->file_name_is_absolute($file)B= $file =~ /\.\./C= $file =~ /_p[ml](?:\Q$CACHE_EXT\E)?$/D= $file =~ /.p[ml]$/
ABCDresult
1XXX1
01XX1
00001
001X0
00010
1612 $file =~ m{\.\.} ||
1613 ($file !~ m{_p[ml](?:\Q$CACHE_EXT\E)?$} &&
1614 $file !~ m{.p[ml]$})) {
1615 # Paranoia check one.
1616 0 warn "$Sub: Skipping unexpected file: '$file'\n";
1617 0 next;
1618 }
1619 3 ▶ my $cache_file =
Branch: 1 branch
BranchTrueFalseTotal
$file =~ /\Q$CACHE_EXT\E$/ ? : 1 2 3
1620 $file =~ /\Q$CACHE_EXT\E$/ ?
1621 $file : $self->__cache_filename($file);
1622 # Paranoia check two. Both paranoia checks are needed.
1623 3 ▶ unless ($self->looks_like_cache_file($cache_file)) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->looks_like_cache_file($cache_file)) 0 3 3
1624 0 warn "$Sub: Skipping unexpected cache file: '$cache_file'\n";
1625 0 next;
1626 }
1627 3 ▶ if ($self->{opt}{cache_verbose}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"opt"}{"cache_verbose"}) 3 0 3
1628 3 print "cache_delete: deleting $cache_file\n";
1629 }
1630 3 ▶ if (unlink $cache_file) {
Branch: 1 branch
BranchTrueFalseTotal
if (unlink $cache_file) 2 1 3
1631 2 $delete_count++;
1632 2 $self->{__cachedeletes}++;
1633 }
1634 }
1635 3 return $delete_count;
1636 }
1637  
1638 sub __unparse_cache_filename {
1639 1 ▶ my ($self, $cache_filename) = @_;
Subroutine __unparse_cache_filename: called CC=8 SCAR=26.3
1640  
1641 1 my $cache_directory = $self->{opt}{cache_directory};
1642 1 ▶ return unless defined $cache_directory;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $cache_directory 0 1 1
1643  
1644 1 ▶ return unless $cache_filename =~ s{\Q$CACHE_EXT\E$}{};
Branch: 1 branch
BranchTrueFalseTotal
unless $cache_filename =~ s/\Q$CACHE_EXT\E$// 0 1 1
1645  
1646 1 my $cache_prefix_length = $self->{__cache_prefix_length};
1647 1 ▶ return unless length($cache_filename) > $cache_prefix_length;
Branch: 1 branch
BranchTrueFalseTotal
unless length $cache_filename > $cache_prefix_length 0 1 1
1648  
1649 1 my $prefix = substr($cache_filename, 0, $cache_prefix_length);
1650 1 ▶ return unless $prefix =~ m{^\Q$cache_directory\E(?:/|\\)$};
Branch: 1 branch
BranchTrueFalseTotal
unless $prefix =~ m[^\Q$cache_directory\E(?:/|\\)$] 0 1 1
1651  
1652 1 my $path = substr($cache_filename, $cache_prefix_length - 1);
1653  
1654 1 $path =~ s{_(p[ml])$}{\.$1}; # _pm -> .pm, _pl -> .pl
1655  
1656 1 ▶ if ($^O eq 'MSWin32') {
Branch: 1 branch
BranchTrueFalseTotal
if ($^O eq "MSWin32") 0 1 1
1657 # \c\a\b -> c:/a/b
1658 0 $path =~ s{\\}{/}g;
1659 0 ▶ if ($path =~ m{^/([A-Z])(/.+)}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($path =~ m[^/([A-Z])(/.+)]) 0 0 0
1660 0 my $volpath = "$1:$2";
1661 0 ▶ if (-f $volpath) {
Branch: 1 branch
BranchTrueFalseTotal
if (-f $volpath) 0 0 0
1662 0 $path = $volpath;
1663 }
1664 }
1665 }
1666  
1667 1 return $path;
1668 }
1669  
1670 # Given an xref, find all the cache files under its cache directory,
1671 # and add their filenames to href.
1672 sub find_cache_files {
1673 0 ▶ my ($self, $files) = @_;
Subroutine find_cache_files: not called CC=2 SCAR=14.2
Pod undocumented
1674  
1675 0 my $cache_directory = $self->{opt}{cache_directory};
1676 0 ▶ unless (defined $cache_directory) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $cache_directory) 0 0 0
1677 0 warn "$Sub: cache_directory undefined\n";
1678 0 return;
1679 }
1680  
1681 33 ▶ use File::Find qw[find];
Subroutine BEGIN: called CC=1 SCAR=0.0
1682  
1683 find(
1684 sub {
1685 0 ▶ if (/\.p[ml]\Q$CACHE_EXT\E$/) {
Subroutine __ANON__: not called CC=2 SCAR=17.9
Branch: 1 branch
BranchTrueFalseTotal
if (/\.p[ml]\Q$CACHE_EXT\E$/) 0 0 0
1686 0 my $name = $self->__unparse_cache_filename($File::Find::name);
1687 0 $files->{$name} = $File::Find::name;
1688 }
1689 },
1690 0 $cache_directory);
1691 }
1692  
1693 # Given an xref, find all the pm files under its INC,
1694 # and add their filenames to href.
1695 sub find_system_files {
1696 0 ▶ my ($self, $files) = @_;
Subroutine find_system_files: not called CC=2 SCAR=12.6
Pod undocumented
1697  
1698 33 ▶ use File::Find qw[find];
Subroutine BEGIN: called CC=1 SCAR=0.0
1699  
1700 0 for my $d (@{ $self->INC }) {
1701 find(
1702 sub {
1703 0 ▶ if (/\.p[ml]$/) {
Subroutine __ANON__: not called CC=2 SCAR=17.9
Branch: 1 branch
BranchTrueFalseTotal
if (/\.p[ml]$/) 0 0 0
1704 0 $files->{$File::Find::name} = $File::Find::name;
1705 }
1706 },
1707 0 $d);
1708 }
1709 }
1710  
1711 33 1;
1712 __DATA__
« blib/lib/PPI/Xref/Help.pm ↑ Summary util/ppixref »