Subs/Preprocessor.pm

filter: Statementstmt 99.2% 140 / 141 Branchbran 91.1% 62 / 68 Conditioncond 66.6% 4 / 6 MC/DCmcdc 50.0% 2 / 4 Subroutinesub 100% 20 / 20 Podpod 66.6% 4 / 6 totaltotal 94.6% 232 / 245 CC 43 SCAR 20.1
CC
43
Cov
96%
__ANON__
16.1
__ANON__
9.0
__ANON__
8.0
CRAP 7.5
« Subs.pm ↑ Summary Subs/Postprocessor.pm »
1 package Devel::Examine::Subs::Preprocessor;
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 Symbol qw(delete_package);
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 60 ▶BEGIN {
Subroutine BEGIN: called CC=2 SCAR=7.4
12  
13 # we need to do some trickery for Devel::Trace::Subs due to circular
14 # referencing, which broke CPAN installs. DTS does nothing if not presented,
15 # per this code
16  
17 60 eval {
18 60 require Devel::Trace::Subs;
19 0 import Devel::Trace::Subs qw(trace);
20 };
21  
22 60 ▶ if (! defined &trace){
Branch: 1 branch
BranchTrueFalseTotal
if (not defined &Devel::Examine::Subs::Preprocessor::trace) 60 0 60
23 60 ▶ *trace = sub {};
Subroutine __ANON__: called CC=1 SCAR=0.0
24 }
25 }
26  
27 49 ▶sub new {
Subroutine new: called CC=2 SCAR=6.9
Pod undocumented
28 49 ▶ trace() if $ENV{TRACE};
Branch: 1 branch
BranchTrueFalseTotal
if $ENV{"TRACE"} 16 33 49
29 48 my $self = {};
30 48 bless $self, shift;
31 48 $self->{pre_procs} = $self->_dt;
32 48 return $self;
33 }
34 52 ▶sub _dt {
Subroutine _dt: called CC=2 SCAR=6.9
35  
36 52 ▶ trace() if $ENV{TRACE};
Branch: 1 branch
BranchTrueFalseTotal
if $ENV{"TRACE"} 18 34 52
37  
38 51 my $dt = {
39 module => \&module,
40 inject => \&inject,
41 remove => \&remove,
42 replace => \&replace,
43 _test_bad => \&test_bad,
44 };
45  
46 51 return $dt;
47 }
48 46 ▶sub exists {
Subroutine exists: called CC=3 SCAR=11.0
Pod documented
49 46 ▶ trace() if $ENV{TRACE};
Branch: 1 branch
BranchTrueFalseTotal
if $ENV{"TRACE"} 14 32 46
50 45 my $self = shift;
51 45 my $string = shift;
52 45 ▶ return exists $self->{pre_procs}{$string} ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
exists $self->{'pre_procs'}{$string} ? : 43 2 45
53 }
54 13 ▶sub module {
Subroutine module: called CC=2 SCAR=6.9
Pod documented
55  
56 13 ▶ trace() if $ENV{TRACE};
Branch: 1 branch
BranchTrueFalseTotal
if $ENV{"TRACE"} 3 10 13
57  
58 11 ▶ return sub {
Subroutine __ANON__: called CC=9 SCAR=22.0
59 11 ▶ trace() if $ENV{TRACE};
Branch: 1 branch
BranchTrueFalseTotal
if $ENV{"TRACE"} 1 10 11
60  
61 60 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
62  
63 11 my $p = shift;
64  
65 11 ▶ if (! $p->{module} or $p->{module} eq '') {
Branch: 1 branch
BranchTrueFalseTotal
if (not $p->{"module"} or $p->{"module"} eq "") 3 8 11
Condition66%2/3
not $p->{"module"} or $p->{"module"} eq ""
l!l&&r!l&&!r
308
MC/DC50%1/2
not $p->{"module"} or $p->{"module"} eq ""
not $p->{"module"} $p->{"module"} eq ""
Truth table66%2/3
not(A) or B
A= not $p->{"module"}B= $p->{"module"} eq ""
ABresult
1X1
011
000
66 3 return [];
67 }
68  
69 8 (my $module_file = $p->{module}) =~ s|::|/|g;
70 8 $module_file .= '.pm';
71  
72 8 my $module_is_loaded = 0;
73  
74 8 ▶ if (! $INC{$module_file}){
Branch: 1 branch
BranchTrueFalseTotal
if (not $INC{$module_file}) { } 4 4 8
75 4 my $required_ok = eval {
76 4 require "$module_file";
77 3 1;
78 };
79  
80 4 ▶ if (! $required_ok) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($required_ok) 1 3 4
81 1 die "Problem loading $p->{module}: $@";
82 }
83 }
84 else {
85 4 $module_is_loaded = 1;
86 }
87  
88 7 my $namespace = "$p->{module}::";
89  
90 7 my @subs;
91  
92 7 for my $sub (keys %$namespace){
93 210 ▶ if (defined &{$namespace . $sub}){
Branch: 1 branch
BranchTrueFalseTotal
if (defined &{$namespace . $sub;}) 179 31 210
94 179 push @subs, $sub;
95 }
96 }
97  
98 7 ▶ if (! $module_is_loaded){
Branch: 1 branch
BranchTrueFalseTotal
unless ($module_is_loaded) 3 4 7
99 3 delete_package($p->{module});
100 3 delete $INC{$module_file};
101 }
102  
103 7 return \@subs;
104 12 };
105 }
106 8 ▶sub inject {
Subroutine inject: called CC=2 SCAR=6.9
Pod documented
107  
108 8 ▶ trace() if $ENV{TRACE};
Branch: 1 branch
BranchTrueFalseTotal
if $ENV{"TRACE"} 2 6 8
109  
110 6 ▶ return sub {
Subroutine __ANON__: called CC=16 SCAR=27.8
111  
112 6 ▶ trace() if $ENV{TRACE};
Branch: 1 branch
BranchTrueFalseTotal
if $ENV{"TRACE"} 0 6 6
113  
114 6 my $p = shift;
115  
116 6 my @file_contents = @{ $p->{file_contents} };
117  
118 # after line number
119  
120 6 my $rw = File::Edit::Portable->new;
121  
122 6 ▶ if (defined $p->{line_num}){
Branch: 3 branches
BranchTrueFalseTotal
if (defined $p->{'line_num'}) { } 2 4 6
elsif ($p->{'inject_use'}) { } 1 3 4
elsif ($p->{'inject_after_sub_def'}) { } 3 0 3
123
124 # inject after line number
125  
126 $rw->splice(
127 file => $p->{file},
128 line => $p->{line_num},
129 insert => $p->{code},
130 copy => $p->{copy},
131 2 );
132 }
133 elsif ($p->{inject_use}){
134
135 # inject a use statement
136  
137 1 my $use = qr/use\s+\w+/;
138
139 1 my $index;
140  
141 ($index) = grep {
142 1 $file_contents[$_] =~ $use
143 } 0..$#file_contents;
144  
145 1 ▶ if (!$index) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($index) 1 0 1
146 ($index) = grep {
147 1 $file_contents[$_] =~ /^package\s+\w+/
148 } 0..$#file_contents;
149 1 $index++;
150 }
151  
152 1 ▶ if ($index) {
Branch: 1 branch
BranchTrueFalseTotal
if ($index) 1 0 1
153 $rw->splice(
154 file => $p->{file},
155 line => $index,
156 insert => $p->{inject_use},
157 copy => $p->{copy},
158 1 );
159 }
160 }
161 elsif ($p->{inject_after_sub_def}){
162  
163 # inject code after sub definition
164  
165 3 my $code = $p->{inject_after_sub_def};
166  
167 3 my @new_file;
168  
169 3 my $single_line = qr/
170 sub\s+\w+\s*(?:\(.*?\)\s+)?\{\s*(?!\s*[\S])
171 |
172 sub\s+\{\s*(?!\s*[\S])
173 /x;
174  
175 3 my $multi_line = qr/sub\s+\w+\s*(?![\S])/;
176  
177 3 my $is_multi = 0;
178  
179 3 my $i = -1;
180  
181 3 for my $e (@file_contents){
182  
183 81 $i++;
184  
185 81 my $indent = '';
186  
187 81 my $count = $i;
188 81 $count++;
189  
190 81 ▶ while ($count < @file_contents){
Branch: 1 branch
BranchTrueFalseTotal
if ($count < @file_contents) 92 3 95
191 92 ▶ if ($file_contents[$count] =~ /^(\s*)\S/){
Branch: 1 branch
BranchTrueFalseTotal
if ($file_contents[$count] =~ /^(\s*)\S/) { } 78 14 92
192 78 $indent = $1;
193 78 last;
194 }
195 else {
196 14 $count++;
197 }
198 }
199  
200 81 push @new_file, $e;
201  
202 81 ▶ if ($e =~ $single_line) {
Branch: 2 branches
BranchTrueFalseTotal
if ($e =~ /$single_line/) { } 13 68 81
elsif ($e =~ /$multi_line/) { } 1 67 68
203 13 for (@$code){
204 13 push @new_file, $indent . $_;
205 }
206 }
207 elsif ($e =~ $multi_line) {
208 1 ▶ if ($file_contents[$count] =~ /\s*\{\s*(?!\s*[\S])/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($file_contents[$count] =~ /\s*\{\s*(?!\s*[\S])/) 1 0 1
209 1 $is_multi = 1;
210 1 next;
211 }
212 }
213  
214 80 ▶ if ($is_multi) {
Branch: 1 branch
BranchTrueFalseTotal
if ($is_multi) 1 79 80
215 1 for (@$code) {
216 4 push @new_file, $indent . $_;
217 }
218 1 $is_multi = 0;
219 }
220 }
221 3 $p->{write_file_contents} = \@new_file;
222 }
223 }
224 7 }
225 20 ▶sub replace {
Subroutine replace: called CC=2 SCAR=6.9
Pod undocumented
226  
227 20 ▶ trace() if $ENV{TRACE};
Branch: 1 branch
BranchTrueFalseTotal
if $ENV{"TRACE"} 10 10 20
228  
229 18 ▶ return sub {
Subroutine __ANON__: called CC=8 SCAR=20.8
230  
231 18 ▶ trace() if $ENV{TRACE};
Branch: 1 branch
BranchTrueFalseTotal
if $ENV{"TRACE"} 8 10 18
232  
233 18 my $p = shift;
234 18 my $exec = $p->{exec};
235 18 ▶ my $limit = defined $p->{limit} ? $p->{limit} : -1;
Branch: 1 branch
BranchTrueFalseTotal
defined $p->{'limit'} ? : 10 8 18
236  
237 18 my @file = @{ $p->{file_contents} };
238  
239 10 ▶ if (! $exec || ref $exec ne 'CODE'){
Branch: 1 branch
BranchTrueFalseTotal
if (not $exec or ref $exec ne "CODE") 1 9 10
Condition66%2/3
not $exec or ref $exec ne "CODE"
l!l&&r!l&&!r
109
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
240 1 confess "\nDES::replace() requires 'exec => \$cref param\n";
241 }
242  
243 9 my $lines_changed;
244  
245 9 for (@file){
246 353 my $changed = $exec->($_);
247 353 ▶ if ($changed){
Branch: 1 branch
BranchTrueFalseTotal
if ($changed) 100 253 353
248 100 $lines_changed++;
249 100 $limit--;
250 100 ▶ last if $limit == 0;
Branch: 1 branch
BranchTrueFalseTotal
if $limit == 0 3 97 100
251 }
252 }
253  
254 9 $p->{write_file_contents} = \@file;
255 9 return $lines_changed;
256 }
257 19 }
258 6 ▶sub remove {
Subroutine remove: called CC=2 SCAR=6.9
Pod documented
259  
260 6 ▶ trace() if $ENV{TRACE};
Branch: 1 branch
BranchTrueFalseTotal
if $ENV{"TRACE"} 5 1 6
261  
262 3 ▶ return sub {
Subroutine __ANON__: called CC=4 SCAR=13.9
263  
264 3 ▶ trace() if $ENV{TRACE};
Branch: 1 branch
BranchTrueFalseTotal
if $ENV{"TRACE"} 2 1 3
265
266 3 my $p = shift;
267 3 my @file = @{ $p->{file_contents}};
268  
269 1 my $delete = $p->{delete};
270  
271 1 for my $find (@$delete){
272 1 ▶ while (my ($index) = grep { $file[$_] =~ $find } 0..$#file){
Branch: 1 branch
BranchTrueFalseTotal
if (my($index) = grep({$file[$_] =~ /$find/;} 0 .. $#file)) 1 1 2
273 1 splice @file, $index, 1;
274 }
275 }
276 1 $p->{write_file_contents} = \@file;
277 }
278 5 }
279 1 ▶sub _vim_placeholder {1;}
Subroutine _vim_placeholder: called CC=1 SCAR=0.0
280 60 1;
281 __END__
« Subs.pm ↑ Summary Subs/Postprocessor.pm »