Code/CutNPaste.pm

filter: Statementstmt 79.5% 253 / 318 Branchbran 50.7% 66 / 130 Conditioncond 29.1% 7 / 24 MC/DCmcdc 0.0% 0 / 15 Subroutinesub 89.8% 44 / 49 Podpod 0.0% 0 / 3 totaltotal 68.6% 370 / 539 CC 89 SCAR 26.3
CC
89
Cov
69%
__ANON__
42.0
_search_for_dups
32.4
_get_text
28.7
CRAP 13.9
« B/CutNPaste.pm ↑ Summary Code/CutNPaste/Duplicate.pm »
1 package Code::CutNPaste;
2  
3 5 ▶use 5.006;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 5 ▶use autodie;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 5 ▶use Benchmark qw(timediff timestr);
Subroutine BEGIN: called CC=1 SCAR=0.0
7 5 ▶use Try::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 5 ▶use Capture::Tiny qw(capture);
Subroutine BEGIN: called CC=1 SCAR=0.0
9 5 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 5 ▶use File::Find::Rule;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 5 ▶use File::HomeDir;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 5 ▶use File::Slurp;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 5 ▶use File::Spec::Functions qw(catfile catdir);
Subroutine BEGIN: called CC=1 SCAR=0.0
14 5 ▶use Getopt::Long;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 5 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 5 ▶use Parallel::ForkManager;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 5 ▶use Term::ProgressBar;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 5 ▶use aliased 'Code::CutNPaste::Duplicate';
Subroutine BEGIN: called CC=1 SCAR=0.0
19 5 ▶use aliased 'Code::CutNPaste::Duplicate::Item';
Subroutine BEGIN: called CC=1 SCAR=0.0
20  
21 5 has 'renamed_vars' => ( is => 'ro' );
22 5 has 'renamed_subs' => ( is => 'ro' );
23 5 has 'verbose' => ( is => 'ro' );
24 4 ▶has 'window' => ( is => 'rwp', default => sub {5} );
Subroutine __ANON__: called CC=1 SCAR=0.0
25 4 ▶has 'jobs' => ( is => 'ro', default => sub {1} );
Subroutine __ANON__: called CC=1 SCAR=0.0
26 5 has 'show_warnings' => ( is => 'ro' );
27 5 has 'noutf8' => ( is => 'ro' );
28 has 'threshold' => (
29 is => 'rwp',
30 4 ▶ default => sub {.75},
Subroutine __ANON__: called CC=1 SCAR=0.0
31 4 ▶ isa => sub {
Subroutine __ANON__: called CC=3 SCAR=12.3
32 5 ▶ no warnings 'uninitialized';
Subroutine BEGIN: called CC=1 SCAR=0.0
33 4 my $threshold = 0 + shift;
34 4 ▶ if ( $threshold < 0 or $threshold > 1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($threshold < 0 or $threshold > 1) 0 4 4
Condition33%1/3
$threshold < 0 or $threshold > 1
l!l&&r!l&&!r
004
MC/DC0%0/2
$threshold < 0 or $threshold > 1
$threshold < 0 $threshold > 1
Truth table33%1/3
A or B
A= $threshold < 0B= $threshold > 1
ABresult
1X1
011
000
35 0 croak("threshold must be between 0 and 1, inclusive");
36 }
37 },
38 5 );
39 has 'dirs' => (
40 is => 'ro',
41 0 ▶ default => sub {'lib'},
Subroutine __ANON__: not called CC=1 SCAR=6.9
42 4 ▶ coerce => sub {
Subroutine __ANON__: called CC=2 SCAR=7.0
43 4 my $dirs = shift;
44 4 ▶ unless ( ref $dirs ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $dirs) 4 0 4
45 4 $dirs = [$dirs];
46 }
47 4 return $dirs;
48 },
49 4 ▶ isa => sub {
Subroutine __ANON__: called CC=3 SCAR=12.0
50 4 my $dirs = shift;
51 4 for my $dir (@$dirs) {
52 4 ▶ unless ( -d $dir ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (-d $dir) 0 4 4
53 0 croak("Cannot find directory '$dir'");
54 }
55 }
56 },
57 5 );
58  
59 has 'files' => (
60 is => 'ro',
61 4 ▶ default => sub { [] },
Subroutine __ANON__: called CC=1 SCAR=0.0
62 4 ▶ isa => sub {
Subroutine __ANON__: called CC=5 SCAR=25.9
63 4 my $files = shift;
64 4 ▶ unless ( 'ARRAY' eq ref $files ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ("ARRAY" eq ref $files) 0 4 4
65 0 croak("Argument to files must be an array reference of files");
66 }
67 4 for my $file (@$files) {
68 0 ▶ unless ( -f $file && -r _ ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (-f $file and -r _) 0 0 0
Condition0%0/3
-f $file and -r _
!ll&&!rl&&r
000
MC/DC0%0/2
-f $file and -r _
-f $file -r _
Truth table0%0/3
A and B
A= -f $fileB= -r _
ABresult
0X0
100
111
69 0 croak("File '$file' does not exist or cannot be read");
70 }
71 }
72 },
73 5 );
74  
75 has 'ignore' => (
76 is => 'ro',
77 0 ▶ coerce => sub {
Subroutine __ANON__: not called CC=6 SCAR=37.4
78 0 my $ignore = shift;
79 0 ▶ return unless defined $ignore;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $ignore 0 0 0
80 0 ▶ return $ignore if ref $ignore eq 'Regexp';
Branch: 1 branch
BranchTrueFalseTotal
if ref $ignore eq "Regexp" 0 0 0
81 0 ▶ if ( !ref $ignore ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $ignore) 0 0 0
82 0 $ignore = qr/$ignore/;
83 }
84 0 ▶ if ( 'ARRAY' eq ref $ignore ) {
Branch: 1 branch
BranchTrueFalseTotal
if ("ARRAY" eq ref $ignore) 0 0 0
85 0 ▶ return unless @$ignore;
Branch: 1 branch
BranchTrueFalseTotal
unless @$ignore 0 0 0
86 0 $ignore = join '|' => @$ignore;
87 0 $ignore = qr/$ignore/;
88 }
89 0 return $ignore;
90 },
91 0 ▶ isa => sub {
Subroutine __ANON__: not called CC=3 SCAR=24.8
92 0 ▶ return unless defined $_[0];
Branch: 1 branch
BranchTrueFalseTotal
unless defined $_[0] 0 0 0
93 0 ▶ croak("ignore must be a qr/regex/!")
Branch: 1 branch
BranchTrueFalseTotal
unless "Regexp" eq ref $_[0] 0 0 0
94 unless 'Regexp' eq ref $_[0];
95 },
96 5 );
97  
98 has 'cache_dir' => (
99 is => 'ro',
100 4 ▶ default => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
101 4 my $homedir = File::HomeDir->my_home;
102 4 return catdir( $homedir, '.cutnpaste' );
103 },
104 5 );
105  
106 has '_duplicates' => (
107 is => 'ro',
108 4 ▶ default => sub { [] },
Subroutine __ANON__: called CC=1 SCAR=0.0
109 5 );
110 5 has '_find_dups_called' => ( is => 'rw' );
111  
112 # XXX I don't expect this to be normal, but I have found this when I run this
113 # code against its own codebase due to "subroutine redefined" warnings
114 4 ▶has '_could_not_deparse' => ( is => 'ro', default => sub { {} } );
Subroutine __ANON__: called CC=1 SCAR=0.0
115  
116 =head1 NAME
117  
118 Code::CutNPaste - Find Duplicate Perl Code
119  
120 =head1 VERSION
121  
122 Version 0.31
123  
124 =cut
125  
126 5 our $VERSION = '0.31';
127  
128 =head1 SYNOPSIS
129  
130 use Code::CutNPaste;
131  
132 my $cutnpaste = Code::CutNPaste->new(
133 dirs => [ 'lib', 'path/to/other/lib' ],
134 renamed_vars => 1,
135 renamed_subs => 1,
136 );
137 my $duplicates = $cutnpaste->duplicates;
138  
139 foreach my $duplicate (@$duplicates) {
140 my ( $left, $right ) = ( $duplicate->left, $duplicate->right );
141 printf <<'END', $left->file, $left->line, $right->file, $right->line;
142  
143 Possible duplicate code found
144 Left: %s line %d
145 Right: %s line %d
146  
147 END
148 print $duplicate->report;
149 }
150  
151 =cut
152  
153 4 ▶sub BUILD {
Subroutine BUILD: called CC=8 SCAR=24.2
Pod undocumented
154 4 my $self = shift;
155  
156 4 ▶ unless ( $self->noutf8 ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->noutf8) 0 4 4
157 0 eval "use utf8::all";
158 0 ▶ warn $@ if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 0 0
159 }
160  
161 4 my $cache_dir = $self->cache_dir;
162 4 ▶ $self->_set_window(5) unless defined $self->window;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->window 0 4 4
163 4 ▶ $self->_set_threshold(.75) unless defined $self->threshold;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->threshold 0 4 4
164  
165 4 ▶ if ( -d $cache_dir ) {
Branch: 1 branch
BranchTrueFalseTotal
if (-d $cache_dir) { } 0 4 4
166 0 my @cached = File::Find::Rule->file->in($cache_dir);
167 0 unlink $_ for @cached;
168 }
169 else {
170 4 mkdir $cache_dir;
171 }
172 4 for my $dir ( @{ $self->dirs } ) {
173 my @files
174 4 = grep { !/^\./ }
175 File::Find::Rule->file->name( '*.pm', '*.t', '*.pl' )->in($dir);
176  
177 # XXX dups and subdirs?
178 4 push @{ $self->files } => @files;
179 }
180 }
181  
182 4 ▶sub find_dups {
Subroutine find_dups: called CC=8 SCAR=21.6
Pod undocumented
183 4 my $self = shift;
184  
185 4 ▶ printf "Started: %s\n", scalar localtime if $self->verbose;
Branch: 1 branch
BranchTrueFalseTotal
if $self->verbose 0 4 4
186 4 my $start = Benchmark->new;
187 4 $self->_find_dups_called(1);
188 4 my $jobs = $self->jobs;
189  
190 4 ▶ my $fork = Parallel::ForkManager->new( $jobs || 1 );
Condition50%1/2
$jobs || 1
l!l
40
MC/DC0%0/1
$jobs || 1
$jobs
Truth table50%1/2
A
A= $jobs
Aresult
11
00
191 $fork->run_on_finish(
192 6 ▶ sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
193 6 my $duplicates = pop @_;
194 6 push @{ $self->_duplicates } => @$duplicates;
195 }
196 4 );
197  
198 4 my @pairs = $self->_get_pairs_of_files;
199 4 my $progress;
200 4 ▶ $progress = Term::ProgressBar->new(
Branch: 1 branch
BranchTrueFalseTotal
if $self->verbose 0 4 4
201 { count => scalar @pairs,
202 ETA => 'linear',
203 }
204 ) if $self->verbose;
205  
206 4 my $count = 1;
207 4 foreach my $pair (@pairs) {
208 9 ▶ $progress->update( $count++ ) if $self->verbose;
Branch: 1 branch
BranchTrueFalseTotal
if $self->verbose 0 9 9
209 9 ▶ my $pid = $fork->start and next;
Branch: 1 branch
BranchTrueFalseTotal
my $pid = $fork->start and next 6 3 9
210  
211 3 my $duplicates_found = $self->_search_for_dups(@$pair);
212  
213 3 $fork->finish( 0, $duplicates_found );
214 }
215 1 $fork->wait_all_children;
216 1 ▶ if ( $self->verbose ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->verbose) 0 1 1
217 0 printf "Ended: %s\n", scalar localtime;
218 0 my $time = timediff( Benchmark->new, $start );
219 0 print STDERR "Time: ", timestr($time), "\n";
220 }
221 }
222  
223 1 ▶sub duplicates {
Subroutine duplicates: called CC=2 SCAR=7.1
Pod undocumented
224 1 my $self = shift;
225 1 ▶ $self->find_dups unless $self->_find_dups_called;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->_find_dups_called 0 1 1
226 1 return $self->_duplicates;
227 }
228  
229 3 ▶sub _search_for_dups {
Subroutine _search_for_dups: called CC=20 SCAR=34.8
230 3 my ( $self, $first, $second ) = @_;
231 3 my $window = $self->window;
232  
233 3 ▶ my $code1 = $self->_get_text($first) or return [];
Branch: 1 branch
BranchTrueFalseTotal
my $code1 = $self->_get_text($first) or return [] 3 0 3
234 3 ▶ my $code2 = $self->_get_text($second) or return [];
Branch: 1 branch
BranchTrueFalseTotal
my $code2 = $self->_get_text($second) or return [] 3 0 3
235  
236 3 my %in_second = map { $_->{key} => 1 } @$code2;
237  
238 3 my $matches_found = 0;
239 3 my $last_found = 0;
240 3 foreach my $i ( 0 .. $#$code1 ) {
241 127 ▶ if ( $in_second{ $code1->[$i]{key} } ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($in_second{$code1->[$i]{"key"}}) 115 12 127
242 115 ▶ if ( $i == $last_found + 1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i == $last_found + 1) 109 6 115
243 109 $matches_found++;
244 }
245 115 $last_found = $i;
246 }
247 }
248 3 ▶ if ( $matches_found < $window ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($matches_found < $window) 0 3 3
249 0 return [];
250 }
251  
252 # brute force is bad!
253  
254 3 my @duplicates_found;
255 3 ▶ LINE: foreach ( my $i = 0; $i < @$code1 - $window; $i++ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @$code1 - $window) 112 3 115
256 112 ▶ next LINE unless $in_second{ $code1->[$i]{key} };
Branch: 1 branch
BranchTrueFalseTotal
unless $in_second{$code1->[$i]{"key"}} 12 100 112
257  
258 100 my @code1 = @{$code1}[ $i .. $#$code1 ];
259 100 foreach my $j ( 0 .. $#$code2 - $window ) {
260 3168 my @code2 = @{$code2}[ $j .. $#$code2 ];
261 3168 my $matches = 0;
262 3168 my $longest = 0;
263 3168 WINDOW: foreach my $k ( 0 .. $#code1 ) {
264 4498 ▶ if ( $code1[$k]{key} eq $code2[$k]{key} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($code1[$k]{'key'} eq $code2[$k]{'key'}) { } 1400 3098 4498
265 1400 $matches++;
266 1400 my $length1 = length( $code1[$k]{code} );
267 1400 ▶ if ( $length1 > $longest ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($length1 > $longest) 383 1017 1400
268 383 $longest = $length1;
269 }
270 1400 my $length2 = length( $code2[$k]{code} );
271 1400 ▶ if ( $length2 > $longest ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($length2 > $longest) 54 1346 1400
272 54 $longest = $length2;
273 }
274 }
275 else {
276 3098 last WINDOW;
277 }
278 }
279  
280 # if too many lines don't meet our threshold level, don't report
281 # this block of code
282 3168 ▶ if ( $matches >= $window ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($matches >= $window) 73 3095 3168
283 73 ▶ $matches = 0
Branch: 1 branch
BranchTrueFalseTotal
if $self->_match_below_threshold($matches, \@code1) 73 0 73
284 if $self->_match_below_threshold( $matches, \@code1 );
285 }
286 3168 ▶ if ( $matches >= $window ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($matches >= $window) 0 3168 3168
287 0 my $line1 = $code1[0]{line};
288 0 my $line2 = $code2[0]{line};
289  
290 0 my ( $left, $right, $report ) = ( '', '', '' );
291 0 for ( 0 .. $matches - 1 ) {
292 0 $left .= $code1[$_]{code} . "\n";
293 0 $right .= $code2[$_]{code} . "\n";
294 my ( $line1, $line2 )
295 0 = map { chomp; $_ }
296 0 ( $code1[$_]{code}, $code2[$_]{code} );
297 0 $report
298 .= $line1 . ( ' ' x ( $longest - length($line1) ) );
299 0 $report .= " | $line2\n";
300 }
301  
302 # Next duplicate report should start after this chunk of code
303 0 $i += $matches;
304  
305 0 my $ignore = $self->ignore;
306 0 ▶ if ( $ignore and $report =~ /$ignore/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ignore and $report =~ /$ignore/) 0 0 0
Condition0%0/3
$ignore and $report =~ /$ignore/
!ll&&!rl&&r
000
MC/DC0%0/2
$ignore and $report =~ /$ignore/
$ignore $report =~ /$ignore/
Truth table0%0/3
A and B
A= $ignoreB= $report =~ /$ignore/
ABresult
0X0
100
111
307 0 next LINE;
308 }
309 0 push @duplicates_found => Duplicate->new(
310 left => Item->new(
311 file => $first,
312 line => $line1,
313 code => $left,
314 ),
315 right => Item->new(
316 file => $second,
317 line => $line2,
318 code => $right,
319 ),
320 report => $report,
321 );
322 }
323 }
324 }
325 3 return \@duplicates_found;
326 }
327  
328 73 ▶sub _match_below_threshold {
Subroutine _match_below_threshold: called CC=4 SCAR=13.9
329 73 my ( $self, $matches, $code ) = @_;
330  
331 73 ▶ return unless $self->threshold;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->threshold 0 73 73
332  
333 73 my $total = 0;
334 73 for ( 0 .. $matches - 1 ) {
335 1220 ▶ $total++ if $code->[$_]{code} =~ /\w/;
Branch: 1 branch
BranchTrueFalseTotal
if $code->[$_]{"code"} =~ /\w/ 676 544 1220
336 }
337 73 return $self->threshold > $total / $matches;
338 }
339  
340 6 ▶sub _get_text {
Subroutine _get_text: called CC=16 SCAR=33.6
341 6 my ( $self, $file ) = @_;
342  
343 6 my $filename = $file;
344 6 $filename =~ s/\W/_/g;
345 6 $filename = catfile( $self->cache_dir, $filename );
346  
347 6 my $filename_munged = $filename . ".munged";
348 6 my ( @contents, @munged );
349 6 ▶ if ( -f $filename ) {
Branch: 1 branch
BranchTrueFalseTotal
if (-f $filename) { } 3 3 6
350 3 @contents = split /(\n)/ => read_file($filename);
351  
352 # sometimes another fork has already written the $filename, but not
353 # yet written the $filename_munged, so we will wait up to three
354 # seconds for it before trying to read it.
355 # A better ordering of the @pairs might help?
356 3 my $retry = 1;
357 3 ▶ while ( !-f $filename_munged ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (-f $filename_munged) 0 3 3
358 0 sleep 1;
359 0 ▶ last if $retry++ > 3;
Branch: 1 branch
BranchTrueFalseTotal
if $retry++ > 3 0 0 0
360 }
361 3 @munged = split /(\n)/ => read_file($filename_munged);
362 }
363 else {
364 3 my $stderr;
365 3 ▶ try {
Subroutine __ANON__: called CC=1 SCAR=0.0
366 ( undef, $stderr, @contents )
367 3 ▶ = capture {qx($^X -Ilib -MO=CutNPaste $file)};
Subroutine __ANON__: called CC=1 SCAR=0.0
368 0 ▶ } catch {
Subroutine __ANON__: not called CC=1 SCAR=6.9
369 0 warn "Problem when capturing $^X -Ilib -MO=CutNPaste $file: $_";
370 3 };
371 3 ▶ return undef if !@contents; #properly return, so we can avoid undef value as an array ref error
Branch: 1 branch
BranchTrueFalseTotal
unless @contents 0 3 3
372 3 ▶ undef $stderr if $stderr =~ /syntax OK/;
Branch: 1 branch
BranchTrueFalseTotal
if $stderr =~ /syntax OK/ 3 0 3
373 3 ▶ if ( $stderr and !$self->_could_not_deparse->{$file} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($stderr and not $self->_could_not_deparse->{$file}) 0 3 3
Condition33%1/3
$stderr and not $self->_could_not_deparse->{$file}
!ll&&!rl&&r
300
MC/DC0%0/2
$stderr and not $self->_could_not_deparse->{$file}
$stderr not $self->_could_not_deparse->{$file}
Truth table33%1/3
A and not(B)
A= $stderrB= not $self->_could_not_deparse->{$file}
ABresult
0X0
100
111
374 0 ▶ warn "Problem when parsing $file: $stderr"
Branch: 1 branch
BranchTrueFalseTotal
if $self->show_warnings 0 0 0
375 if $self->show_warnings;
376 }
377 3 undef $stderr;
378 3 write_file( $filename, @contents );
379  
380 3 ▶ local $ENV{RENAME_VARS} = $self->renamed_vars || 0;
Condition50%1/2
$self->renamed_vars || 0
l!l
30
MC/DC0%0/1
$self->renamed_vars || 0
$self->renamed_vars
Truth table50%1/2
A
A= $self->renamed_vars
Aresult
11
00
381 3 ▶ local $ENV{RENAME_SUBS} = $self->renamed_subs || 0;
Condition50%1/2
$self->renamed_subs || 0
l!l
30
MC/DC0%0/1
$self->renamed_subs || 0
$self->renamed_subs
Truth table50%1/2
A
A= $self->renamed_subs
Aresult
11
00
382 3 ▶ try {
Subroutine __ANON__: called CC=1 SCAR=0.0
383 ( undef, $stderr, @munged )
384 3 ▶ = capture {qx($^X -Ilib -MO=CutNPaste $file)};
Subroutine __ANON__: called CC=1 SCAR=0.0
385 0 ▶ } catch {
Subroutine __ANON__: not called CC=1 SCAR=6.9
386 0 warn "Problem when capturing $^X -Ilib -MO=CutNPaste $file: $_";
387 3 };
388 3 ▶ return undef if !@munged;
Branch: 1 branch
BranchTrueFalseTotal
unless @munged 0 3 3
389 3 ▶ undef $stderr if $stderr =~ /syntax OK/;
Branch: 1 branch
BranchTrueFalseTotal
if $stderr =~ /syntax OK/ 3 0 3
390 3 ▶ if ( $stderr and !$self->_could_not_deparse->{$file} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($stderr and not $self->_could_not_deparse->{$file}) 0 3 3
Condition33%1/3
$stderr and not $self->_could_not_deparse->{$file}
!ll&&!rl&&r
300
MC/DC0%0/2
$stderr and not $self->_could_not_deparse->{$file}
$stderr not $self->_could_not_deparse->{$file}
Truth table33%1/3
A and not(B)
A= $stderrB= not $self->_could_not_deparse->{$file}
ABresult
0X0
100
111
391 0 ▶ warn "\nProblem when parsing $file: $stderr"
Branch: 1 branch
BranchTrueFalseTotal
if $self->show_warnings 0 0 0
392 if $self->show_warnings;
393 }
394 3 write_file( $filename_munged, @munged );
395 }
396 6 return $self->_add_line_numbers( $file, \@contents, \@munged );
397 }
398  
399 6 ▶sub _add_line_numbers {
Subroutine _add_line_numbers: called CC=5 SCAR=16.4
400 6 my $self = shift;
401 6 my $file = shift;
402 6 ▶ return if $self->_could_not_deparse->{$file};
Branch: 1 branch
BranchTrueFalseTotal
if $self->_could_not_deparse->{$file} 0 6 6
403 6 my $contents = $self->_prefilter(shift);
404 6 my $munged = $self->_prefilter(shift);
405  
406 6 ▶ if ( @$contents != @$munged ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@$contents != @$munged) 0 6 6
407 0 warn <<"END";
408  
409 There was a problem parsing $file. It will be skipped.
410 Try rerunning with show_warnings => 1.
411  
412 END
413 0 $self->_could_not_deparse->{$file} = 1;
414 0 return;
415 }
416 6 my @contents;
417  
418 6 my $line_num = 1;
419 6 foreach my $i ( 0 .. $#$contents ) {
420 426 my ( $line, $munged_line ) = ( $contents->[$i], $munged->[$i] );
421 426 chomp $line;
422 426 chomp $munged_line;
423  
424 426 ▶ if ( $line =~ /^#line\s+([0-9]+)/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($line =~ /^#line\s+([0-9]+)/) 190 236 426
425 190 $line_num = $1;
426 190 next;
427 }
428 236 push @contents => {
429 line => $line_num,
430 key => $self->_make_key($munged_line),
431 code => $line,
432 };
433 236 $line_num++;
434 }
435 6 return $self->_postfilter( \@contents );
436 }
437  
438 12 ▶sub _prefilter {
Subroutine _prefilter: called CC=9 SCAR=24.6
439 12 my ( $self, $contents ) = @_;
440 12 my @contents;
441 12 my %skip = (
442 sub_begin => 0,
443 );
444 12 my $skip = 0;
445  
446 12 ▶ LINE: for ( my $i = 0; $i < @$contents; $i++ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @$contents) 1548 12 1560
447 1548 local $_ = $contents->[$i];
448 1548 ▶ next if /^\s*(?:use|require)\b/; # use/require
Branch: 1 branch
BranchTrueFalseTotal
if /^\s*(?:use|require)\b/ 48 1500 1548
449 1500 ▶ next if /^\s*$/; # blank lines
Branch: 1 branch
BranchTrueFalseTotal
if /^\s*$/ 648 852 1500
450 852 ▶ next if /^#(?!line\s+[0-9]+)/; # comments which aren't line directives
Branch: 1 branch
BranchTrueFalseTotal
if /^#(?!line\s+[0-9]+)/ 0 852 852
451  
452 # Modules which import things create code like this:
453 #
454 # sub BEGIN {
455 # require strict;
456 # do {
457 # 'strict'->import('refs')
458 # };
459 # }
460 #
461 # $skip{sub_begin} filters this out
462  
463 852 ▶ if (/^sub BEGIN \{/) {
Branch: 2 branches
BranchTrueFalseTotal
if (/^sub BEGIN \{/) { } 0 852 852
elsif ($skip{'sub_begin'} and /^}/) { } 0 1704 1704
464 0 $skip{sub_begin} = 1;
465 0 $skip++;
466 }
467 ▶ elsif ( $skip{sub_begin} and /^}/ ) {
Condition33%1/3
$skip{"sub_begin"} and /^}/
!ll&&!rl&&r
85200
MC/DC0%0/2
$skip{"sub_begin"} and /^}/
$skip{"sub_begin"} /^}/
Truth table33%1/3
A and B
A= $skip{"sub_begin"}B= /^}/
ABresult
0X0
100
111
468 0 $skip{sub_begin} = 0;
469 0 $skip--;
470 0 next;
471 }
472  
473 852 ▶ push @contents => $_ unless $skip;
Branch: 1 branch
BranchTrueFalseTotal
unless $skip 852 0 852
474 }
475 12 return \@contents;
476 }
477  
478 6 ▶sub _postfilter {
Subroutine _postfilter: called CC=4 SCAR=16.8
479 6 my ( $self, $contents ) = @_;
480  
481 6 my @contents;
482 6 ▶ INDEX: for ( my $i = 0; $i < @$contents; $i++ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @$contents) 236 6 242
483 236 ▶ if ( $contents->[$i]{code} =~ /^(\s*)BEGIN\s*\{/ ) { # BmEGIN {
Branch: 1 branch
BranchTrueFalseTotal
if ($contents->[$i]{"code"} =~ /^(\s*)BEGIN\s*\{/) 0 236 236
484 0 my $padding = $1;
485 0 ▶ if ( $contents->[ $i + 1 ]{code} =~ /^$padding}/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($$contents[$i + 1]{"code"} =~ /^$padding}/) 0 0 0
486 0 $i++;
487 0 next INDEX;
488 }
489 }
490 236 push @contents => $contents->[$i];
491 }
492 6 return \@contents;
493 }
494  
495 236 ▶sub _make_key {
Subroutine _make_key: called CC=1 SCAR=0.0
496 236 my $self = shift;
497 236 local $_ = shift;
498 236 chomp;
499 236 s/\s//g;
500 236 return $_;
501 }
502  
503 4 ▶sub _get_pairs_of_files {
Subroutine _get_pairs_of_files: called CC=6 SCAR=20.2
504 4 my $self = shift;
505 4 my $files = $self->files;
506 4 my $num_files = @$files;
507 4 my $jobs = $self->jobs;
508  
509 4 my @pairs;
510 4 for my $i ( 0 .. $#$files - 1 ) {
511 8 my $next = $i + 1;
512 8 for my $j ( $next .. $#$files ) {
513 12 push @pairs => [ @{$files}[ $i, $j ] ];
514 }
515 }
516  
517 4 my @left_right;
518 4 ▶ if ( $jobs > 1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($jobs > 1) { } 0 4 4
519 0 my $files_per_job = int( $num_files / $jobs );
520 0 for ( 1 .. $jobs ) {
521 0 ▶ if ( $_ < $jobs ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($_ < $jobs) { } 0 0 0
522 0 push @left_right => splice @pairs, 0, $files_per_job;
523 }
524 else {
525 0 push @left_right => @pairs;
526 }
527 }
528 }
529 else {
530 4 @left_right = @pairs;
531 }
532 4 return @left_right;
533 }
534  
535 5 1;
536 __END__
« B/CutNPaste.pm ↑ Summary Code/CutNPaste/Duplicate.pm »