blib/lib/PPI/Xref.pm

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