Subs/Engine.pm

filter: Statementstmt 99.5% 209 / 210 Branchbran 86.9% 80 / 92 Conditioncond 70.0% 14 / 20 MC/DCmcdc 53.8% 7 / 13 Subroutinesub 100% 30 / 30 Podpod 88.8% 8 / 9 totaltotal 93.0% 348 / 374 CC 68 SCAR 21.1
CC
68
Cov
94%
__ANON__
20.1
__ANON__
11.0
__ANON__
11.0
CRAP 8.2
↑ Summary Subs.pm »
1 package Devel::Examine::Subs::Engine;
2 60 ▶use 5.008;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 60 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 60 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 60 our $VERSION = '1.74';
7  
8 60 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 60 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 60 ▶use Devel::Examine::Subs;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 60 ▶use Devel::Examine::Subs::Sub;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 60 ▶use File::Copy;
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 60 ▶BEGIN {
Subroutine BEGIN: called CC=2 SCAR=7.4
15  
16 # we need to do some trickery for Devel::Trace::Subs due to circular
17 # referencing, which broke CPAN installs. DTS does nothing if not presented,
18 # per this code
19  
20 60 eval {
21 60 require Devel::Trace::Subs;
22 0 import Devel::Trace::Subs qw(trace);
23 };
24  
25 60 ▶ if (! defined &trace){
Branch: 1 branch
BranchTrueFalseTotal
if (not defined &Devel::Examine::Subs::Engine::trace) 60 0 60
26 60 ▶ *trace = sub {};
Subroutine __ANON__: called CC=1 SCAR=0.0
27 }
28 }
29  
30 197 ▶sub new {
Subroutine new: called CC=2 SCAR=6.9
Pod undocumented
31
32 197 ▶ trace() if $ENV{TRACE};
Branch: 1 branch
BranchTrueFalseTotal
if $ENV{"TRACE"} 5 192 197
33  
34 196 my $self = {};
35 196 bless $self, shift;
36  
37 196 $self->{engines} = $self->_dt;
38  
39 196 return $self;
40 }
41 201 ▶sub _dt {
Subroutine _dt: called CC=2 SCAR=6.9
42
43 201 ▶ trace() if $ENV{TRACE};
Branch: 1 branch
BranchTrueFalseTotal
if $ENV{"TRACE"} 7 194 201
44  
45 200 my $dt = {
46 all => \&all,
47 has => \&has,
48 missing => \&missing,
49 lines => \&lines,
50 objects => \&objects,
51 search_replace => \&search_replace,
52 inject_after => \&inject_after,
53 dt_test => \&dt_test,
54 _test => \&_test,
55 _test_bad => \&_test_bad,
56 };
57  
58 200 return $dt;
59 }
60 192 ▶sub exists {
Subroutine exists: called CC=3 SCAR=11.0
Pod documented
61 192 ▶ trace() if $ENV{TRACE};
Branch: 1 branch
BranchTrueFalseTotal
if $ENV{"TRACE"} 3 189 192
62 191 my $self = shift;
63 191 my $string = shift;
64 191 ▶ return exists $self->{engines}{$string} ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
exists $self->{'engines'}{$string} ? : 189 2 191
65 }
66 5 ▶sub _test {
Subroutine _test: called CC=2 SCAR=7.0
67 5 ▶ trace() if $ENV{TRACE};
Branch: 1 branch
BranchTrueFalseTotal
if $ENV{"TRACE"} 2 3 5
68 4 ▶ return sub {
Subroutine __ANON__: called CC=2 SCAR=7.2
69 4 ▶ trace() if $ENV{TRACE};
Branch: 1 branch
BranchTrueFalseTotal
if $ENV{"TRACE"} 0 4 4
70 4 return {a => 1};
71 4 };
72 }
73 100 ▶sub all {
Subroutine all: called CC=2 SCAR=6.9
Pod documented
74
75 100 ▶ trace() if $ENV{TRACE};
Branch: 1 branch
BranchTrueFalseTotal
if $ENV{"TRACE"} 2 98 100
76  
77 98 ▶ return sub {
Subroutine __ANON__: called CC=3 SCAR=11.0
78
79 98 ▶ trace() if $ENV{TRACE};
Branch: 1 branch
BranchTrueFalseTotal
if $ENV{"TRACE"} 0 98 98
80  
81 98 my $p = shift;
82 98 my $struct = shift;
83  
84 98 my $file = $p->{file};
85  
86 98 my @subs;
87  
88 98 for my $name (@{ $p->{order} }){
89 496 push @subs, grep {$name eq $_} keys %{ $struct->{$file}{subs} };
90 }
91  
92 98 return \@subs;
93 99 };
94 }
95 37 ▶sub has {
Subroutine has: called CC=2 SCAR=7.0
Pod documented
96
97 37 ▶ trace() if $ENV{TRACE};
Branch: 1 branch
BranchTrueFalseTotal
if $ENV{"TRACE"} 3 34 37
98  
99 35 ▶ return sub {
Subroutine __ANON__: called CC=4 SCAR=14.0
100
101 35 ▶ trace() if $ENV{TRACE};
Branch: 1 branch
BranchTrueFalseTotal
if $ENV{"TRACE"} 1 34 35
102
103 35 my $p = shift;
104 35 my $struct = shift;
105  
106 35 ▶ return [] if ! $struct;
Branch: 1 branch
BranchTrueFalseTotal
unless $struct 0 35 35
107  
108 35 my $file = (keys %$struct)[0];
109  
110 35 my @has = keys %{ $struct->{$file}{subs} };
111  
112 35 ▶ return \@has || [];
Condition50%1/2
\@has || []
l!l
350
MC/DC0%0/1
\@has || []
\@has
Truth table50%1/2
A
A= \@has
Aresult
11
00
113 36 };
114 }
115 14 ▶sub missing {
Subroutine missing: called CC=2 SCAR=6.9
Pod documented
116
117 14 ▶ trace() if $ENV{TRACE};
Branch: 1 branch
BranchTrueFalseTotal
if $ENV{"TRACE"} 2 12 14
118  
119 12 ▶ return sub {
Subroutine __ANON__: called CC=11 SCAR=24.0
120
121 12 ▶ trace() if $ENV{TRACE};
Branch: 1 branch
BranchTrueFalseTotal
if $ENV{"TRACE"} 0 12 12
122  
123 12 my $p = shift;
124 12 my $struct = shift;
125  
126 12 my $search = $p->{search};
127  
128 12 ▶ if ($search && ! $p->{regex}){
Branch: 1 branch
BranchTrueFalseTotal
if ($search and not $p->{"regex"}) 1 11 12
Condition100%3/3
$search and not $p->{"regex"}
!ll&&!rl&&r
291
MC/DC100%2/2
$search and not $p->{"regex"}
$search not $p->{"regex"}
Truth table100%3/3
A and not(B)
A= $searchB= not $p->{"regex"}
ABresult
0X0
100
111
129 1 $search = "\Q$search";
130 }
131
132 12 ▶ return [] if not $search;
Branch: 1 branch
BranchTrueFalseTotal
unless $search 2 10 12
133  
134 10 my @missing;
135  
136 10 for my $file (keys %$struct){
137 10 for my $sub (keys %{$struct->{$file}{subs}}){
138 104 my @code = @{$struct->{$file}{subs}{$sub}{code}};
139  
140 104 my @clean;
141  
142 104 for (@code){
143 515 ▶ push @clean, $_ if $_;
Branch: 1 branch
BranchTrueFalseTotal
if $_ 497 18 515
144 }
145  
146 104 ▶ if (! grep {/$search/ and $_} @clean){
Branch: 1 branch
BranchTrueFalseTotal
unless (grep {/$search/ and $_;} @clean) 69 35 104
Condition66%2/3
/$search/ && $_
!ll&&!rl&&r
460037
MC/DC50%1/2
/$search/ && $_
/$search/ $_
Truth table66%2/3
A && B
A= /$search/B= $_
ABresult
0X0
100
111
147 69 push @missing, $sub;
148 }
149 }
150 }
151 10 return \@missing;
152 13 };
153 }
154 7 ▶sub lines {
Subroutine lines: called CC=2 SCAR=6.9
Pod documented
155
156 7 ▶ trace() if $ENV{TRACE};
Branch: 1 branch
BranchTrueFalseTotal
if $ENV{"TRACE"} 2 5 7
157  
158 5 ▶ return sub {
Subroutine __ANON__: called CC=5 SCAR=16.1
159
160 5 ▶ trace() if $ENV{TRACE};
Branch: 1 branch
BranchTrueFalseTotal
if $ENV{"TRACE"} 0 5 5
161
162 5 my $p = shift;
163 5 my $struct = shift;
164  
165 5 my %return;
166  
167 5 for my $file (keys %$struct){
168 5 for my $sub (keys %{$struct->{$file}{subs}}){
169 37 my $line_num = $struct->{$file}{subs}{$sub}{start};
170 37 my @code = @{$struct->{$file}{subs}{$sub}{code}};
171 37 for my $line (@code){
172 125 $line_num++;
173 125 push @{$return{$sub}}, {$line_num => $line};
174 }
175 }
176 }
177 5 return \%return;
178 6 };
179 }
180 22 ▶sub objects {
Subroutine objects: called CC=2 SCAR=6.9
Pod documented
181
182 22 ▶ trace() if $ENV{TRACE};
Branch: 1 branch
BranchTrueFalseTotal
if $ENV{"TRACE"} 2 20 22
183  
184 # uses 'subs' post_processor
185  
186 20 ▶ return sub {
Subroutine __ANON__: called CC=6 SCAR=18.0
187
188 20 ▶ trace() if $ENV{TRACE};
Branch: 1 branch
BranchTrueFalseTotal
if $ENV{"TRACE"} 0 20 20
189  
190 20 my $p = shift;
191 20 my $struct = shift;
192  
193  
194 20 ▶ return if not ref($struct) eq 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $struct eq "ARRAY" 0 20 20
195  
196 20 my ($des_sub, %obj_hash, @obj_array);
197  
198 20 for my $sub (@$struct){
199 $des_sub
200 187 = Devel::Examine::Subs::Sub->new($sub, $sub->{name});
201  
202 187 ▶ if ($p->{objects_in_hash}){
Branch: 1 branch
BranchTrueFalseTotal
if ($p->{'objects_in_hash'}) { } 72 115 187
203 72 $obj_hash{$sub->{name}} = $des_sub;
204 }
205 else {
206 115 push @obj_array, $des_sub;
207 }
208 }
209  
210 20 ▶ if ($p->{objects_in_hash}){
Branch: 1 branch
BranchTrueFalseTotal
if ($p->{'objects_in_hash'}) { } 10 10 20
211 10 return \%obj_hash;
212 }
213 else {
214 10 return \@obj_array;
215 }
216 21 };
217 }
218 8 ▶sub search_replace {
Subroutine search_replace: called CC=2 SCAR=6.9
Pod documented
219  
220 8 ▶ trace() if $ENV{TRACE};
Branch: 1 branch
BranchTrueFalseTotal
if $ENV{"TRACE"} 2 6 8
221  
222 6 ▶ return sub {
Subroutine __ANON__: called CC=11 SCAR=24.0
223  
224 6 ▶ trace() if $ENV{TRACE};
Branch: 1 branch
BranchTrueFalseTotal
if $ENV{"TRACE"} 0 6 6
225  
226 6 my $p = shift;
227 6 my $struct = shift;
228  
229 6 my $file = $p->{file};
230 6 my $exec = $p->{exec};
231  
232 6 my @file_contents;
233  
234 6 ▶ if ($p->{file_contents}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($p->{"file_contents"}) 5 1 6
235 5 @file_contents = @{ $p->{file_contents} };
236 }
237  
238 6 ▶ if (! $file){
Branch: 1 branch
BranchTrueFalseTotal
unless ($file) 1 5 6
239 1 confess
240 "\nDevel::Examine::Subs::Engine::search_replace " .
241 "speaking:\n" .
242 "can't use search_replace engine without specifying a " .
243 "file\n\n";
244 }
245  
246 5 ▶ if (! $exec || ref($exec) ne 'CODE'){
Branch: 1 branch
BranchTrueFalseTotal
if (not $exec or ref $exec ne "CODE") 2 3 5
Condition66%2/3
not $exec or ref $exec ne "CODE"
l!l&&r!l&&!r
203
MC/DC50%1/2
not $exec or ref $exec ne "CODE"
not $exec ref $exec ne "CODE"
Truth table66%2/3
not(A) or B
A= not $execB= ref $exec ne "CODE"
ABresult
1X1
011
000
247 2 confess
248 "\nDevel::Examine::Subs::Engine::search_replace " .
249 " speaking:\n" .
250 "can't use search_replace engine without specifying" .
251 "a substitution regex code reference\n\n";
252 }
253  
254 3 my @changed_lines;
255
256 3 for my $sub (@$struct){
257  
258 33 my $start_line = $sub->start;
259 33 my $end_line = $sub->end;
260  
261 33 my $line_num = 0;
262  
263 33 for my $line (@file_contents){
264  
265 1080 $line_num++;
266  
267 1080 ▶ next if $line_num < $start_line;
Branch: 1 branch
BranchTrueFalseTotal
if $line_num < $start_line 885 195 1080
268 195 ▶ last if $line_num > $end_line;
Branch: 1 branch
BranchTrueFalseTotal
if $line_num > $end_line 30 165 195
269  
270 165 my $orig = $line;
271  
272 165 my $replaced = $exec->($line);
273  
274 165 ▶ if ($replaced) {
Branch: 1 branch
BranchTrueFalseTotal
if ($replaced) 15 150 165
275 15 push @changed_lines, [$orig, $line];
276 }
277 }
278 }
279  
280 3 $p->{write_file_contents} = \@file_contents;
281  
282 3 return \@changed_lines;
283 7 };
284 }
285 12 ▶sub inject_after {
Subroutine inject_after: called CC=2 SCAR=6.9
Pod documented
286
287 12 ▶ trace() if $ENV{TRACE};
Branch: 1 branch
BranchTrueFalseTotal
if $ENV{"TRACE"} 2 10 12
288  
289 10 ▶ return sub {
Subroutine __ANON__: called CC=20 SCAR=30.0
290
291 10 ▶ trace() if $ENV{TRACE};
Branch: 1 branch
BranchTrueFalseTotal
if $ENV{"TRACE"} 0 10 10
292  
293 10 my $p = shift;
294 10 my $struct = shift;
295  
296 10 my $search = $p->{search};
297  
298 10 ▶ if ($search && !$p->{regex}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($search and not $p->{"regex"}) 1 9 10
Condition100%3/3
$search and not $p->{"regex"}
!ll&&!rl&&r
181
MC/DC100%2/2
$search and not $p->{"regex"}
$search not $p->{"regex"}
Truth table100%3/3
A and not(B)
A= $searchB= not $p->{"regex"}
ABresult
0X0
100
111
299 1 $search = "\Q$search";
300 }
301  
302 10 my $code = $p->{code};
303  
304 10 ▶ if (!$search) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($search) 1 9 10
305 1 confess
306 "\nDevel::Examine::Subs::Engine::inject_after speaking:\n" .
307 "can't use inject_after engine without specifying a " .
308 "search term\n\n";
309 }
310 9 ▶ if (!$code) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($code) 1 8 9
311 1 confess
312 "\nDevel::Examine::Subs::Engine::inject_after speaking:\n" .
313 "can't use inject_after engine without code to inject\n\n";
314  
315 }
316  
317 8 my @file_contents = @{$p->{file_contents}};
318 8 my @processed;
319 8 my $added_lines = 0;
320 8 my @subs;
321  
322 8 for my $sub (@$struct) {
323 20 push @subs, $sub->name;
324 }
325  
326 8 my $des = Devel::Examine::Subs->new(file => $p->{file});
327 8 my $subs_hash = $des->objects(objects_in_hash => 1, include => \@subs);
328  
329 my @sorted_subs = sort {
330 8 $subs_hash->{$a}->start <=> $subs_hash->{$b}->start
331 } keys %$subs_hash;
332  
333 8 for (@sorted_subs){
334  
335 20 my $sub = $subs_hash->{$_};
336  
337 20 ▶ my $num_injects = defined $p->{injects} ? $p->{injects} : 1;
Branch: 1 branch
BranchTrueFalseTotal
defined $p->{'injects'} ? : 10 10 20
338  
339 20 push @processed, $sub->name;
340  
341 20 my $start_line = $sub->start;
342 20 my $end_line = $sub->end;
343  
344 20 $start_line += $added_lines;
345 20 $end_line += $added_lines;
346  
347 20 my $line_num = 0;
348 20 my $new_lines = 0; # don't search added lines
349  
350 20 for my $line (@file_contents){
351 754 $line_num++;
352 754 ▶ if ($line_num < $start_line){
Branch: 1 branch
BranchTrueFalseTotal
if ($line_num < $start_line) 670 84 754
353 670 next;
354 }
355 84 ▶ if ($line_num > $end_line){
Branch: 1 branch
BranchTrueFalseTotal
if ($line_num > $end_line) 1 83 84
356 1 last;
357 }
358  
359 83 ▶ if ($line =~ /$search/ && ! $new_lines){
Branch: 1 branch
BranchTrueFalseTotal
if ($line =~ /$search/ and not $new_lines) 24 59 83
Condition66%2/3
$line =~ /$search/ and not $new_lines
!ll&&!rl&&r
59024
MC/DC50%1/2
$line =~ /$search/ and not $new_lines
$line =~ /$search/ not $new_lines
Truth table66%2/3
A and not(B)
A= $line =~ /$search/B= not $new_lines
ABresult
0X0
100
111
360  
361 24 my $location = $line_num;
362  
363 24 my $indent = '';
364  
365 24 ▶ if (! $p->{no_indent}){
Branch: 1 branch
BranchTrueFalseTotal
unless ($p->{"no_indent"}) 24 0 24
366 24 ▶ if ($line =~ /^(\s+)/ && $1){
Branch: 1 branch
BranchTrueFalseTotal
if ($line =~ /^(\s+)/ and $1) 24 0 24
Condition33%1/3
$line =~ /^(\s+)/ and $1
!ll&&!rl&&r
0024
MC/DC0%0/2
$line =~ /^(\s+)/ and $1
$line =~ /^(\s+)/ $1
Truth table33%1/3
A and B
A= $line =~ /^(\s+)/B= $1
ABresult
0X0
100
111
367 24 $indent = $1;
368 }
369 }
370 24 for (@$code){
371 39 splice @file_contents, $location++, 0, $indent . $_;
372 39 $new_lines++;
373 39 $added_lines++;
374 }
375  
376 # stop injecting after N search finds
377  
378 24 $num_injects--;
379 24 ▶ if ($num_injects == 0){
Branch: 1 branch
BranchTrueFalseTotal
if ($num_injects == 0) 19 5 24
380 19 last;
381 }
382  
383 }
384 64 ▶ $new_lines-- if $new_lines != 0;
Branch: 1 branch
BranchTrueFalseTotal
if $new_lines != 0 5 59 64
385 }
386 }
387 8 $p->{write_file_contents} = \@file_contents;
388 8 return \@processed;
389 11 };
390 }
391 1 ▶sub _vim_placeholder {1;}
Subroutine _vim_placeholder: called CC=1 SCAR=0.0
392 60 1;
393  
394 __END__
↑ Summary Subs.pm »