blib/lib/PPI/Xref.pm

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