blib/lib/Ref/Util/Rewriter.pm

filter: Statementstmt 90.7% 118 / 130 Branchbran 69.3% 43 / 62 Conditioncond 72.7% 8 / 11 MC/DCmcdc 57.1% 4 / 7 Subroutinesub 100% 14 / 14 Podpod 100% 3 / 3 totaltotal 83.7% 190 / 227 CC 43 SCAR 29.3
CC
43
Cov
83%
rewrite_doc
28.7
_handle_eval
19.3
rewrite_file
2.0
CRAP 18.8
↑ Summary
1 package Ref::Util::Rewriter;
2 # ABSTRACT: Rewrite your code to use Ref::Util
3  
4 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 2 ▶use PPI;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 2 ▶use Safe::Isa;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 2 ▶use Exporter qw< import >;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 2 ▶use List::Util qw< first >;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 2 our @EXPORT_OK = qw< rewrite_string rewrite_file >;
13  
14 2 my %reftype_to_reffunc = (
15 SCALAR => 'is_scalarref',
16 ARRAY => 'is_arrayref',
17 HASH => 'is_hashref',
18 CODE => 'is_coderef',
19 Regexp => 'is_regexpref',
20 GLOB => 'is_globref',
21 IO => 'is_ioref',
22 REF => 'is_refref',
23 );
24  
25 17 ▶sub rewrite_string {
Subroutine rewrite_string: called CC=1 SCAR=0.0
Pod documented
26 17 my $string = shift;
27 17 my $res = rewrite_doc( PPI::Document->new(\$string) );
28 17 return $res;
29 }
30  
31 1 ▶sub rewrite_file {
Subroutine rewrite_file: called CC=2 SCAR=7.0
Pod documented
32 1 my $file = shift;
33 1 my $content = rewrite_doc( PPI::Document->new($file) );
34  
35 1 ▶ open my $fh, '>', $file
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, ">", $file or die "Failed to open file ${file}: $!" 1 0 1
36 or die "Failed to open file $file: $!";
37 1 print {$fh} $content;
38 1 close $fh;
39  
40 1 return $content;
41 }
42  
43 18 ▶sub rewrite_doc {
Subroutine rewrite_doc: called CC=27 SCAR=33.6
Pod documented
44 18 ▶ my $doc = shift or die;
Branch: 1 branch
BranchTrueFalseTotal
my $doc = shift() or die 18 0 18
45 18 my $all_statements = $doc->find('PPI::Statement');
46 18 ▶ $all_statements = [] unless defined $all_statements;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $all_statements 0 18 18
47  
48 18 my @cond_ops = qw<or || and &&>;
49 18 my @new_statements;
50  
51 ALL_STATEMENTS:
52 34 foreach my $statement ( @{$all_statements} ) {
53 # if there's an "if()" statement, it appears as a Compound statement
54 # and then each internal statement appears again,
55 # causing duplication in results
56 59 ▶ $statement->$_isa('PPI::Statement::Compound')
Branch: 1 branch
BranchTrueFalseTotal
$statement->$Ref::Util::Rewriter::_isa("PPI::Statement::Compound") and next 2 57 59
57 and next;
58  
59 57 _handle_eval($statement);
60  
61 # find the 'ref' functions
62 521 ▶ my $ref_subs = $statement->find( sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
63 521 ▶ $_[1]->isa('PPI::Token::Word') && $_[1]->content eq 'ref'
Branch: 1 branch
BranchTrueFalseTotal
$_[1]->isa("PPI::Token::Word") and $_[1]->content eq "ref" 73 448 521
64 57 ▶ }) or next;
Branch: 1 branch
BranchTrueFalseTotal
my $ref_subs = $statement->find(sub { $_[1]->isa("PPI::Token::Word") and $_[1]->content eq "ref"; } ) or next 16 41 57
65  
66 16 my $statement_def;
67  
68 REF_STATEMENT:
69 16 foreach my $ref_sub ( @{$ref_subs} ) {
70 # we want to pick up everything until we find a delimiter
71 # effectively telling us we ended the parameters to "ref"
72 16 my $sib = $ref_sub;
73 16 my ( @func_args, $reffunc_doc, @rest_of_tokens );
74  
75 16 my @siblings_to_remove;
76  
77 16 ▶ while ( $sib = $sib->next_sibling ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($sib = $sib->next_sibling) 89 6 95
78 # end of statement/expression
79 89 my $content = $sib->content;
80  
81 89 push @siblings_to_remove, $sib;
82 89 ▶ $content eq ';' and last;
Branch: 1 branch
BranchTrueFalseTotal
$content eq ";" and last 10 79 89
83  
84 # we might already have a statement
85 # in this case collect all the rest of the tokens
86 # (this could be in two separate loops)
87 79 ▶ if ($statement_def) {
Branch: 1 branch
BranchTrueFalseTotal
if ($statement_def) 20 59 79
88 20 push @rest_of_tokens, $sib;
89 20 next;
90 }
91  
92 # reasons to stop
93 59 ▶ if ( ! $statement_def && $sib->$_isa('PPI::Token::Operator') ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $statement_def and $sib->$Ref::Util::Rewriter::_isa('PPI::Token::Operator')) { } 16 43 59
Condition66%2/3
not $statement_def and $sib->$Ref::Util::Rewriter::_isa("PPI::Token::Operator")
!ll&&!rl&&r
04316
MC/DC50%1/2
not $statement_def and $sib->$Ref::Util::Rewriter::_isa("PPI::Token::Operator")
not $statement_def $sib->$Ref::Util::Rewriter::_isa("PPI::Token::Operator")
Truth table66%2/3
not(A) and B
A= not $statement_defB= $sib->$Ref::Util::Rewriter::_isa("PPI::Token::Operator")
ABresult
0X0
100
111
94 # comparison operators
95 16 ▶ if ( $content eq 'eq' || $content eq 'ne' ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($content eq 'eq' or $content eq 'ne') { } 15 1 16
elsif (&first(sub { $content eq $_; } , @cond_ops)) { } 1 0 1
Condition66%2/3
$content eq "eq" or $content eq "ne"
l!l&&r!l&&!r
1501
MC/DC50%1/2
$content eq "eq" or $content eq "ne"
$content eq "eq" $content eq "ne"
Truth table66%2/3
A or B
A= $content eq "eq"B= $content eq "ne"
ABresult
1X1
011
000
96 # "ARRAY" vs. $foo (which has "ARRAY" as value)
97 # we also move $sib to next significant sibling
98 15 my $val_token = $sib = $sib->snext_sibling;
99 15 ▶ my $val_str = $val_token->$_isa('PPI::Token::Quote')
Branch: 1 branch
BranchTrueFalseTotal
$val_token->$Ref::Util::Rewriter::_isa('PPI::Token::Quote') ? : 15 0 15
100 ? $val_token->string
101 : $val_token->content;
102  
103 15 my $func = $reftype_to_reffunc{$val_str};
104 15 ▶ if ( !$func ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($func) 0 15 15
105 0 warn "Error: no match for $val_str\n";
106 0 next REF_STATEMENT;
107 }
108  
109 15 push @siblings_to_remove, $sib;
110  
111 15 $statement_def = [ $func, \@func_args, '' ];
112 1 ▶ } elsif ( first { $content eq $_ } @cond_ops ) {
Subroutine __ANON__: called CC=1 SCAR=0.0
113 # is_ref
114  
115 # @func_args will now contain spaces too,
116 # which we will need to take out,
117 # in order to add them after the is_ref()
118 # reason those spaces don't appear in is_ref()
119 # we created is because we clean the function up
120 1 my $spaces_count = 0;
121 1 foreach my $idx ( reverse 0 .. $#func_args ) {
122 2 ▶ $func_args[$idx]->$_isa('PPI::Token::Whitespace')
Branch: 1 branch
BranchTrueFalseTotal
$func_args[$idx]->$Ref::Util::Rewriter::_isa('PPI::Token::Whitespace') ? : 1 1 2
123 ? $spaces_count++
124 : last;
125 }
126  
127 # we should add these *and* the cond op
128 # to the statement
129 # technically we can just add them at the end
130 # but it seems easier to stick them as strings
131 # and have them parsed
132 # (wish i understood PPI better)
133  
134 $statement_def = [
135 1 'is_ref',
136 \@func_args,
137 ' ' x $spaces_count . $content,
138 ];
139 } else {
140 0 warn "Warning: unknown operator: $sib\n";
141 0 next REF_STATEMENT;
142 }
143 } else {
144 # otherwise, collect it as a parameter
145 43 push @func_args, $sib;
146 }
147 }
148  
149 # skip when failed (error or warnings should appear from above)
150 16 ▶ $statement_def or next;
Branch: 1 branch
BranchTrueFalseTotal
$statement_def or next 16 0 16
151  
152 16 my ( $func_name, $func_args, $rest ) = @{$statement_def};
153 16 $rest .= $_ for @rest_of_tokens;
154 16 ▶ $sib && $sib->content eq ';'
Branch: 1 branch
BranchTrueFalseTotal
$sib and $sib->content eq ";" and $rest .= ';' 10 6 16
Condition66%2/3
$sib and $sib->content eq ";"
!ll&&!rl&&r
6010
MC/DC50%1/2
$sib and $sib->content eq ";"
$sib $sib->content eq ";"
Truth table66%2/3
A and B
A= $sibB= $sib->content eq ";"
ABresult
0X0
100
111
155 and $rest .= ';';
156  
157 16 $reffunc_doc = _create_statement(
158 $func_name, $func_args, $rest
159 );
160  
161 # prevent garbage collection
162 # FIXME: turn this into an interation that finds weaken
163 # objects and unweakens them (Scalar::Util::unweaken)
164 16 push @new_statements, $reffunc_doc;
165  
166 16 my $new_statement = ( $reffunc_doc->children )[0];
167  
168 # remove as much as we can
169 16 foreach my $element ( @siblings_to_remove ) {
170 104 $element->remove;
171 }
172  
173 # remove the trailing space to avoid duplicate spaces
174 16 ▶ if ( my $next = $ref_sub->next_sibling ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $next = $ref_sub->next_sibling) 15 1 16
175 15 ▶ $next->remove if $next->isa('PPI::Token::Whitespace');
Branch: 1 branch
BranchTrueFalseTotal
if $next->isa("PPI::Token::Whitespace") 15 0 15
176 }
177  
178 16 foreach my $element ( $new_statement->children ) {
179 64 my $insert = $ref_sub->insert_before( $element );
180 }
181  
182 16 $ref_sub->remove;
183  
184 # update statements... to avoid PPI issues when moving elements...
185 # this is very ugly... but probably the best solution
186 16 $all_statements = $doc->find('PPI::Statement');
187 16 goto ALL_STATEMENTS;
188 }
189 }
190  
191 18 return "$doc";
192 }
193  
194  
195 57 ▶sub _handle_eval {
Subroutine _handle_eval: called CC=13 SCAR=29.6
196 57 my $statement = shift;
197 521 ▶ my $evals = $statement->find( sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
198 521 ▶ $_[1]->isa('PPI::Token::Word') && $_[1]->content eq 'eval';
Branch: 1 branch
BranchTrueFalseTotal
$_[1]->isa("PPI::Token::Word") and $_[1]->content eq "eval" 73 448 521
199 57 ▶ }) || [];
Condition100%2/2
$statement->find(sub { $_[1]->isa('PPI::Token::Word') and $_[1]->content eq 'eval'; } ) || []
l!l
354
MC/DC100%1/1
$statement->find(sub { $_[1]->isa('PPI::Token::Word') and $_[1]->content eq 'eval'; } ) || []
$statement->find(sub { $_[1]->isa('PPI::Token::Word') and $_[1]->content eq 'eval'; } )
Truth table100%2/2
A
A= $statement->find(sub { $_[1]->isa('PPI::Token::Word') and $_[1]->content eq 'eval'; } )
Aresult
11
00
200  
201 57 foreach my $eval ( @{$evals} ) {
202 3 my $sib = $eval;
203  
204 3 ▶ while ( $sib = $sib->next_sibling ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($sib = $sib->next_sibling) 6 0 6
205 6 ▶ if ( $sib->isa('PPI::Token::Quote') ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($sib->isa("PPI::Token::Quote")) 3 3 6
206 3 ▶ last unless $sib->content =~ qr{\bref\b}; # shortcut
Branch: 1 branch
BranchTrueFalseTotal
unless $sib->content =~ qr/\bref\b/ 0 3 3
207  
208 # '' - PPI::Token::Quote::Single
209 # "q{}" - PPI::Token::Quote::Literal
210 # "" - PPI::Token::Quote::Double
211 # "qq{}" - PPI::Token::Quote::Interpolate
212 3 my ( $content, $after, $before );
213  
214 3 ▶ if ( $sib->isa('PPI::Token::Quote::Single') ) {
Branch: 4 branches
BranchTrueFalseTotal
if ($sib->isa('PPI::Token::Quote::Single')) { } 0 3 3
elsif ($sib->isa('PPI::Token::Quote::Double')) { } 1 2 3
elsif ($sib->isa('PPI::Token::Quote::Literal')) { } 2 0 2
elsif ($sib->isa('PPI::Token::Quote::Interpolate')) { } 0 0 0
215 0 $after = $before = q{'};
216 0 $content = $sib->literal;
217 } elsif ( $sib->isa('PPI::Token::Quote::Double') ) {
218 1 $after = $before = q{"};
219 1 $content = $sib->string;
220 } elsif ( $sib->isa('PPI::Token::Quote::Literal') ) {
221 2 $before = 'q{';
222 2 $after = '}';
223  
224 2 ▶ if ( $sib->content =~ qr{^q(.).*(.)$} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($sib->content =~ qr/^q(.).*(.)$/) 2 0 2
225 2 $before = 'q' . $1;
226 2 $after = $2;
227 }
228  
229 2 $content = $sib->literal;
230 } elsif ( $sib->isa('PPI::Token::Quote::Interpolate') ) {
231 0 $before = 'qq{';
232 0 $after = '}';
233  
234 0 ▶ if ( $sib->string =~ qr{^q(.).*(.)$} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($sib->string =~ qr/^q(.).*(.)$/) 0 0 0
235 0 $before = $1;
236 0 $after = $2;
237 }
238  
239 0 $content = $sib->string;
240 }
241  
242 $content
243 3 ▶ or next;
Branch: 1 branch
BranchTrueFalseTotal
$content or next 3 0 3
244  
245 # FIXME: this is very ugly....
246 # FIXME need to escape for literals but the idea is there
247 3 $sib->{'content'}
248 = $before . rewrite_string($content) . $after;
249  
250 # Idea:
251 # my $p = PPI::Token::Quote::Double->new( rewrite_string($content) );
252 # $sib->insert_before( $p );
253 # $sib->delete;
254 3 last;
255 }
256 }
257  
258 }
259 }
260  
261 16 ▶sub _create_statement {
Subroutine _create_statement: called CC=2 SCAR=6.9
262 16 my ( $func, $args, $rest ) = @_;
263 16 my $args_str = join '', @{$args};
264 16 $args_str =~ s/^\s+//;
265 16 $args_str =~ s/\s+$//;
266 16 $args_str =~ s/^\(+//;
267 16 $args_str =~ s/\)+$//;
268 16 ▶ defined $rest or $rest = '';
Branch: 1 branch
BranchTrueFalseTotal
defined $rest or $rest = "" 16 0 16
269 16 return PPI::Document::Fragment->new(\"$func($args_str)$rest");
270 }
271  
272 2 1;
273  
274 __END__
↑ Summary