blib/lib/Acme/PPIx/MetaSyntactic.pm

filter: Statementstmt 88.2% 98 / 111 Branchbran 59.0% 26 / 44 Conditioncond 42.4% 14 / 33 MC/DCmcdc 14.2% 3 / 21 Subroutinesub 86.9% 20 / 23 Podpod n/a totaltotal 69.4% 161 / 232 SCAR 41.8
CC
38
Cov
73%
_relabel_variables
39.6
_relabel_subs
27.9
_build_local_subs
6.2
CRAP 65.2
↑ Summary
1 package Acme::PPIx::MetaSyntactic;
2  
3 2 ▶use 5.010001;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 2 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 2 ▶use namespace::sweep;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 2 ▶no warnings qw( void once uninitialized numeric );
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 BEGIN {
9 2 ▶ $Acme::PPIx::MetaSyntactic::AUTHORITY = 'cpan:TOBYINK';
Subroutine BEGIN: called CC=1 SCAR=0.0
10 2 $Acme::PPIx::MetaSyntactic::VERSION = '0.003';
11 }
12  
13 2 ▶use Acme::MetaSyntactic;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 2 ▶use Perl::Critic::Utils qw( is_perl_builtin is_function_call );
Subroutine BEGIN: called CC=1 SCAR=0.0
15 2 ▶use PPI;
Subroutine BEGIN: called CC=1 SCAR=0.0
16  
17 2 ▶use Types::Standard -types;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 2 ▶use Type::Utils;
Subroutine BEGIN: called CC=1 SCAR=0.0
19  
20 2 my $Document = class_type Document => { class => "PPI::Document" };
21 2 my $MetaSyntactic = class_type MetaSyntactic => { class => "Acme::MetaSyntactic" };
22 2 my $TruthTable = declare TruthTable => as Map[Str, Bool];
23  
24 2 coerce $Document,
25 from ScalarRef[Str], q { "PPI::Document"->new($_) },
26 from Str, q { "PPI::Document"->new($_) },
27 from FileHandle, q { do { local $/; my $c = <$_>; "PPI::Document"->new(\$c) } },
28 from ArrayRef[Str], q { do { my $c = join "\n", map { chomp(my $l = $_); $l } @$_; "PPI::Document"->new(\$c) } },
29 ;
30  
31 2 coerce $MetaSyntactic,
32 from Str, q { "Acme::MetaSyntactic"->new($_) },
33 ;
34  
35 2 coerce $TruthTable,
36 from ArrayRef[Str], q { +{ map +($_, 1), @$_ } },
37 ;
38  
39 2 has document => (
40 is => "ro",
41 isa => $Document,
42 coerce => $Document->coercion,
43 required => 1,
44 trigger => 1,
45 );
46  
47 2 has theme => (
48 is => "lazy",
49 isa => $MetaSyntactic,
50 coerce => $MetaSyntactic->coercion,
51 );
52  
53 2 has local_subs => (
54 is => "lazy",
55 isa => $TruthTable,
56 coerce => $TruthTable->coercion,
57 );
58  
59 2 has names => (
60 is => "lazy",
61 isa => Map[Str, Str],
62 );
63  
64 2 has already_used => (
65 is => "lazy",
66 isa => $TruthTable,
67 coerce => $TruthTable->coercion,
68 init_arg => undef,
69 );
70  
71 sub _get_name
72 {
73 4 ▶ my $self = shift;
Subroutine _get_name: called CC=2 SCAR=7.0
74 4 my $name = $self->theme->name;
75 4 my $i = undef;
76 4 my $used = $self->already_used;
77 4 ▶ $i++ while $used->{"$name$i"};
Branch: 1 branch
BranchTrueFalseTotal
$$used{"$name$i"} and do { ++$i } 0 4 4
78 4 $used->{"$name$i"} = 1;
79 4 return "$name$i";
80 }
81  
82 sub _build_theme
83 {
84 1 ▶ my $self = shift;
Subroutine _build_theme: called CC=1 SCAR=0.0
85 1 return $MetaSyntactic->new("haddock");
86 }
87  
88 sub _build_local_subs
89 {
90 1 ▶ my $self = shift;
Subroutine _build_local_subs: called CC=6 SCAR=18.3
91 1 my %r;
92
93 1 ▶ for my $word (@{ $self->document->find("PPI::Token::Word") || [] })
Branch: 1 branch
BranchTrueFalseTotal
$self->document->find("PPI::Token::Word") or [] 0 1 1
94 {
95 8 ▶ $r{$word} = 1 if $word->sprevious_sibling eq "sub";
Branch: 1 branch
BranchTrueFalseTotal
if $word->sprevious_sibling eq "sub" 0 8 8
96 8 ▶ $r{$word} = 1 if $word->sprevious_sibling eq "constant" && $word->sprevious_sibling->sprevious_sibling eq "use";
Branch: 1 branch
BranchTrueFalseTotal
if $word->sprevious_sibling eq "constant" and $word->sprevious_sibling->sprevious_sibling eq "use" 1 7 8
Condition66%2/3
$word->sprevious_sibling eq "constant" and $word->sprevious_sibling->sprevious_sibling eq "use"
!ll&&!rl&&r
701
MC/DC50%1/2
$word->sprevious_sibling eq "constant" and $word->sprevious_sibling->sprevious_sibling eq "use"
$word->sprevious_sibling eq "constant" $word->sprevious_sibling->sprevious_sibling eq "use"
Truth table66%2/3
A and B
A= $word->sprevious_sibling eq "constant"B= $word->sprevious_sibling->sprevious_sibling eq "use"
ABresult
0X0
100
111
97 }
98
99 1 return \%r;
100 }
101  
102 sub _build_names
103 {
104 1 ▶ my $self = shift;
Subroutine _build_names: called CC=1 SCAR=0.0
105 1 return +{};
106 }
107  
108 sub _build_already_used
109 {
110 1 ▶ my $self = shift;
Subroutine _build_already_used: called CC=1 SCAR=0.0
111 return +{
112 1 map +($_, 1), values %{ $self->names },
113 };
114 }
115  
116 sub _trigger_document
117 {
118 1 ▶ my $self = shift;
Subroutine _trigger_document: called CC=1 SCAR=0.0
119 1 $self->_relabel_subs;
120 1 $self->_relabel_variables;
121 1 return;
122 }
123  
124 sub _relabel_subs
125 {
126 1 ▶ my $self = shift;
Subroutine _relabel_subs: called CC=16 SCAR=33.3
127 1 my $ls = $self->local_subs;
128 1 my $n = $self->names;
129
130 1 ▶ for my $word (@{ $self->document->find("PPI::Token::Word")||[] })
Branch: 1 branch
BranchTrueFalseTotal
$self->document->find("PPI::Token::Word") or [] 0 1 1
131 {
132 8 ▶ next if is_perl_builtin($word);
Branch: 1 branch
BranchTrueFalseTotal
if is_perl_builtin($word) 4 4 8
133
134 # Function to preserve original case of variable.
135 my $case =
136 1 ▶ ($word eq uc $word) ? sub { uc $_[0] } :
Subroutine __ANON__: called CC=1 SCAR=0.0
137 4 ▶ ($word eq lc $word) ? sub { lc $_[0] } : sub { $_[0] };
Subroutine __ANON__: not called CC=1 SCAR=0.8
Branch: 2 branches
BranchTrueFalseTotal
$word eq uc $word ? : 2 2 4
$word eq lc $word ? : 2 0 2
138
139 4 ▶ if ($word->sprevious_sibling eq "sub" and $ls->{$word})
Branch: 3 branches
BranchTrueFalseTotal
if ($word->sprevious_sibling eq 'sub' and $ls->{$word}) { } 0 4 4
elsif ($word->sprevious_sibling eq 'constant' and $word->sprevious_sibling->sprevious_sibling eq 'use' and $ls->{$word}) { } 1 3 4
elsif (is_function_call($word) and $ls->{$word}) { } 1 4 5
Condition33%1/3
$word->sprevious_sibling eq "sub" and $ls->{$word}
!ll&&!rl&&r
400
MC/DC0%0/2
$word->sprevious_sibling eq "sub" and $ls->{$word}
$word->sprevious_sibling eq "sub" $ls->{$word}
Truth table33%1/3
A and B
A= $word->sprevious_sibling eq "sub"B= $ls->{$word}
ABresult
0X0
100
111
140 {
141 0 ▶ $word->set_content($n->{$word} ||= $case->($self->_get_name));
Condition0%0/3
$n->{$word} ||= &$case($self->_get_name)
l!l&&r!l&&!r
000
MC/DC0%0/2
$n->{$word} ||= &$case($self->_get_name)
$n->{$word} &$case($self->_get_name)
Truth table0%0/3
A ||= B
A= $n->{$word}B= &$case($self->_get_name)
ABresult
1X1
011
000
142 }
143 ▶ elsif ($word->sprevious_sibling eq "constant" && $word->sprevious_sibling->sprevious_sibling eq "use" and $ls->{$word})
Condition66%4/6
$word->sprevious_sibling eq "constant" and $word->sprevious_sibling->sprevious_sibling eq "use" and $ls->{$word}
!ll&&!rl&&r
301
$word->sprevious_sibling eq "constant" and $word->sprevious_sibling->sprevious_sibling eq "use"
!ll&&!rl&&r
301
MC/DC33%1/3
$word->sprevious_sibling eq "constant" and $word->sprevious_sibling->sprevious_sibling eq "use" and $ls->{$word}
$word->sprevious_sibling eq "constant" $word->sprevious_sibling->sprevious_sibling eq "use" $ls->{$word}
Truth table50%2/4
A and B and C
A= $word->sprevious_sibling eq "constant"B= $word->sprevious_sibling->sprevious_sibling eq "use"C= $ls->{$word}
ABCresult
0XX0
10X0
1100
1111
144 {
145 1 ▶ $word->set_content($n->{$word} ||= $case->($self->_get_name));
Condition33%1/3
$n->{$word} ||= &$case($self->_get_name)
l!l&&r!l&&!r
010
MC/DC0%0/2
$n->{$word} ||= &$case($self->_get_name)
$n->{$word} &$case($self->_get_name)
Truth table33%1/3
A ||= B
A= $n->{$word}B= &$case($self->_get_name)
ABresult
1X1
011
000
146 }
147 ▶ elsif (is_function_call($word) and $ls->{$word})
Condition66%2/3
is_function_call($word) and $ls->{$word}
!ll&&!rl&&r
201
MC/DC50%1/2
is_function_call($word) and $ls->{$word}
is_function_call($word) $ls->{$word}
Truth table66%2/3
A and B
A= is_function_call($word)B= $ls->{$word}
ABresult
0X0
100
111
148 {
149 1 ▶ $word->set_content($n->{$word} ||= $case->($self->_get_name));
Condition33%1/3
$n->{$word} ||= &$case($self->_get_name)
l!l&&r!l&&!r
100
MC/DC0%0/2
$n->{$word} ||= &$case($self->_get_name)
$n->{$word} &$case($self->_get_name)
Truth table33%1/3
A ||= B
A= $n->{$word}B= &$case($self->_get_name)
ABresult
1X1
011
000
150 }
151 }
152
153 1 return;
154 }
155  
156 sub _relabel_variables
157 {
158 1 ▶ my $self = shift;
Subroutine _relabel_variables: called CC=16 SCAR=36.8
159 1 my $ls = $self->local_subs;
160 1 my $n = $self->names;
161
162 my $VariableFinder = sub {
163 90 ▶ $_[1]->isa("PPI::Token::Symbol") or $_[1]->isa("PPI::Token::ArrayIndex");
Subroutine __ANON__: called CC=2 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
$_[1]->isa("PPI::Token::Symbol") or $_[1]->isa("PPI::Token::ArrayIndex") 83 7 90
164 1 };
165
166 1 ▶ for my $word (@{ $self->document->find($VariableFinder) || [] })
Branch: 1 branch
BranchTrueFalseTotal
$self->document->find($VariableFinder) or [] 0 1 1
167 {
168 7 ▶ next if $word->isa("PPI::Token::Magic");
Branch: 1 branch
BranchTrueFalseTotal
if $word->isa("PPI::Token::Magic") 0 7 7
169
170 # Function to preserve original case of variable.
171 my $case =
172 0 ▶ ($word eq uc $word) ? sub { uc $_[0] } :
Subroutine __ANON__: not called CC=1 SCAR=6.9
173 7 ▶ ($word eq lc $word) ? sub { lc $_[0] } : sub { $_[0] };
Subroutine __ANON__: not called CC=1 SCAR=0.8
Branch: 2 branches
BranchTrueFalseTotal
$word eq uc $word ? : 0 7 7
$word eq lc $word ? : 7 0 7
174
175 # Separate sigil from the rest of the variable name.
176 7 (my $sigil = "$word") =~ s/(\w.*)$//g;
177 7 my $rest = $1;
178
179 7 ▶ if ($word->isa("PPI::Token::Symbol"))
Branch: 2 branches
BranchTrueFalseTotal
if ($word->isa('PPI::Token::Symbol')) { } 7 0 7
elsif ($word->isa('PPI::Token::ArrayIndex')) { } 0 0 0
180 {
181 7 ▶ $n->{$word->symbol} ||= $case->($self->_get_name);
Condition66%2/3
$$n{$word->symbol} ||= &$case($self->_get_name)
l!l&&r!l&&!r
430
MC/DC0%0/2
$$n{$word->symbol} ||= &$case($self->_get_name)
$$n{$word->symbol} &$case($self->_get_name)
Truth table66%2/3
A ||= B
A= $$n{$word->symbol}B= &$case($self->_get_name)
ABresult
1X1
011
000
182 7 $word->set_content($sigil . $n->{$word->symbol});
183 }
184 elsif ($word->isa("PPI::Token::ArrayIndex")) # like $#foo
185 {
186 0 ▶ $n->{"\@$rest"} ||= $case->($self->_get_name);
Condition0%0/3
$$n{"\@$rest"} ||= &$case($self->_get_name)
l!l&&r!l&&!r
000
MC/DC0%0/2
$$n{"\@$rest"} ||= &$case($self->_get_name)
$$n{"\@$rest"} &$case($self->_get_name)
Truth table0%0/3
A ||= B
A= $$n{"\@$rest"}B= &$case($self->_get_name)
ABresult
1X1
011
000
187 0 $word->set_content($sigil . $n->{"\@$rest"});
188 }
189 }
190
191 1 ▶ for my $qq (@{ $self->document->find("PPI::Token::Quote") || [] })
Branch: 1 branch
BranchTrueFalseTotal
$self->document->find("PPI::Token::Quote") or [] 0 1 1
192 {
193 # A string that "co-incidentally" happens to have the name as a locally
194 # defined sub. This might be a __PACKAGE__->can("foo"), so change it!
195 #
196 1 ▶ if ($ls->{$qq->string})
Branch: 2 branches
BranchTrueFalseTotal
if ($$ls{$qq->string}) { } 0 1 1
elsif ($qq->isa('PPI::Token::Quote::Double') or $qq->isa('PPI::Token::Quote::Interpolate')) { } 1 0 1
197 {
198 0 my $txt = "$qq";
199 0 $txt =~ s/${\quotemeta($qq->string)}/$n->{$qq->string}/eg;
200 0 $qq->set_content($txt);
201 }
202
203 # An interpolated string. We'll do our best to find any variables
204 # within it and rename them, but PPI doesn't really look inside
205 # interpolated strings (yet?).
206 #
207 ▶ elsif ($qq->isa("PPI::Token::Quote::Double") or $qq->isa("PPI::Token::Quote::Interpolate"))
Condition33%1/3
$qq->isa("PPI::Token::Quote::Double") or $qq->isa("PPI::Token::Quote::Interpolate")
l!l&&r!l&&!r
100
MC/DC0%0/2
$qq->isa("PPI::Token::Quote::Double") or $qq->isa("PPI::Token::Quote::Interpolate")
$qq->isa("PPI::Token::Quote::Double") $qq->isa("PPI::Token::Quote::Interpolate")
Truth table33%1/3
A or B
A= $qq->isa("PPI::Token::Quote::Double")B= $qq->isa("PPI::Token::Quote::Interpolate")
ABresult
1X1
011
000
208 {
209 1 my $txt = "$qq";
210 1 ▶ $txt =~ s/([\$\@]\w+)/$n->{$1}?substr($1,0,1).$n->{$1}:$1/eg;
Branch: 1 branch
BranchTrueFalseTotal
$n->{$1} ? : 0 0 0
211 1 $qq->set_content($txt);
212 }
213 }
214 }
215  
216 2 1;
217  
218 __END__
219  
220 =pod
221  
222 =encoding utf-8
223  
224 =head1 NAME
225  
226 Acme::PPIx::MetaSyntactic - rename functions and variables in a PPI::Document using Acme::MetaSyntactic
227  
228 =head1 SYNOPSIS
229  
230 my $acme = "Acme::PPIx::MetaSyntactic"->new(document => \<<'END');
231 use v5.010;
232 use constant PLACE => "World";
233
234 sub join_spaces {
235 return join " ", @_;
236 }
237
238 my @greetings = qw(Hello);
239
240 say join_spaces($greetings[0], PLACE);
241 END
242
243 say $acme->document;
244  
245 Example output:
246  
247 use v5.010;
248 use constant VULTURE => "World";
249
250 sub fraud {
251 return join " ", @_;
252 }
253
254 my @gang_of_thieves = qw(Hello);
255
256 say fraud($gang_of_thieves[0], VULTURE);
257  
258 =head1 DESCRIPTION
259  
260 This module uses L<PPI> to parse some Perl source code, find all the
261 variables and function names defined in it, and reassign them random names
262 using L<Acme::MetaSyntactic>.
263  
264 =head2 Constructor
265  
266 This module is object-oriented, though there's really very little reason
267 for it to be.
268  
269 =over
270  
271 =item C<< new(%attributes) >>
272  
273 Moose-style constructor.
274  
275 =back
276  
277 =head2 Attributes
278  
279 All attributes are read-only.
280  
281 =over
282  
283 =item C<< document >>
284  
285 The L<PPI::Document> that will be munged.
286  
287 Can be coerced from a C<< Str >> (filename), C<< ScalarRef[Str] >> (string
288 of Perl source), C<< ArrayRef[Str] >> (lines of Perl source) or
289 C<< FileHandle >>.
290  
291 Required.
292  
293 Once the C<document> attribute has been set, a trigger automatically runs
294 the relabelling.
295  
296 =item C<< theme >>
297  
298 The L<Acme::MetaSyntactic> object that will be used to obtain new names.
299 If your source code is more than a couple of lines; choose one that provides
300 a large selection of names.
301  
302 Can be coerced from C<< Str >> (theme name).
303  
304 Defaults to the C<< "haddock" >> theme.
305  
306 =item C<< local_subs >>
307  
308 HashRef where the keys are the names of subs which are considered locally
309 defined (i.e. not Perl built-ins, and not imported) and thus available for
310 relabelling. Values are expected to all be C<< "1" >>.
311  
312 Can be coerced from C<< ArrayRef[Str] >>.
313  
314 Defaults to a list built by scanning the C<document> with PPI.
315  
316 =item C<< names >>
317  
318 HashRef mapping old names to new names. This will be populated by the
319 relabelling process, but you may supply some initial values.
320  
321 Defaults to empty hashref.
322  
323 =item C<< already_used >>
324  
325 HashRef keeping track of names already used in remapping, to avoid renaming
326 two variables the same thing.
327  
328 Defaults to a hashref populated from C<names>.
329  
330 This attribute cannot be provided to the constructor.
331  
332 =back
333  
334 =head1 BUGS
335  
336 Please report any bugs to
337 L<http://rt.cpan.org/Dist/Display.html?Queue=Acme-PPIx-MetaSyntactic>.
338  
339 =head1 SEE ALSO
340  
341 L<PPI>, L<Acme::MetaSyntactic>, L<Acme::MetaSyntactic::RefactorCode>.
342  
343 =head1 AUTHOR
344  
345 Toby Inkster E<lt>tobyink@cpan.orgE<gt>.
346  
347 =head1 COPYRIGHT AND LICENCE
348  
349 This software is copyright (c) 2013 by Toby Inkster.
350  
351 This is free software; you can redistribute it and/or modify it under
352 the same terms as the Perl 5 programming language system itself.
353  
354 =head1 DISCLAIMER OF WARRANTIES
355  
356 THIS PACKAGE IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR IMPLIED
357 WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
358 MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
↑ Summary