blib/lib/PPIx/EditorTools/RenameVariable.pm

filter: Statementstmt 90.5% 96 / 106 Branchbran 70.3% 38 / 54 Conditioncond 62.9% 17 / 27 MC/DCmcdc 12.5% 2 / 16 Subroutinesub 100% 11 / 11 Podpod 100% 1 / 1 totaltotal 76.7% 165 / 215 SCAR 38.5
CC
37
Cov
81%
rename
29.7
__ANON__
11.2
_from_camel_case
4.0
CRAP 46.8
« blib/lib/PPIx/EditorTools.pm ↑ Summary blib/lib/PPIx/EditorTools/Outline.pm »
1 package PPIx::EditorTools::RenameVariable;
2 8 our $AUTHORITY = 'cpan:YANICK';
3 # ABSTRACT: Lexically replace a variable name in Perl code
4 8 $PPIx::EditorTools::RenameVariable::VERSION = '0.20';
5 8 ▶use 5.008;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 8 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 8 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 8 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 8 ▶use base 'PPIx::EditorTools';
Subroutine BEGIN: called CC=1 SCAR=0.0
11 8 ▶use Class::XSAccessor;
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13  
14 sub rename {
15 12 ▶ my ( $self, %args ) = @_;
Subroutine rename: called CC=22 SCAR=33.9
Pod documented
16 12 $self->process_doc(%args);
17 12 ▶ my $column = $args{column} || croak "column required";
Condition33%1/3
$args{'column'} || croak('column required')
l!l&&r!l&&!r
1200
MC/DC0%0/2
$args{'column'} || croak('column required')
$args{'column'} croak('column required')
Truth table33%1/3
A || B
A= $args{'column'}B= croak('column required')
ABresult
1X1
011
000
18 12 ▶ my $line = $args{line} || croak "line required";
Condition33%1/3
$args{'line'} || croak('line required')
l!l&&r!l&&!r
1200
MC/DC0%0/2
$args{'line'} || croak('line required')
$args{'line'} croak('line required')
Truth table33%1/3
A || B
A= $args{'line'}B= croak('line required')
ABresult
1X1
011
000
19 12 my $location = [ $line, $column ];
20 12 my $replacement = $args{replacement};
21 12 ▶ if ( ( $args{to_camel_case} or $args{from_camel_case} )
Branch: 2 branches
BranchTrueFalseTotal
if ($args{'to_camel_case'} || $args{'from_camel_case'} and defined $replacement) { } 0 12 12
elsif (not $args{'to_camel_case'} || $args{'from_camel_case'} || defined $replacement) { } 0 12 12
Condition83%5/6
$args{"to_camel_case"} || $args{"from_camel_case"} and defined $replacement
!ll&&!rl&&r
750
$args{"to_camel_case"} or $args{"from_camel_case"}
l!l&&r!l&&!r
327
MC/DC0%0/3
$args{"to_camel_case"} || $args{"from_camel_case"} and defined $replacement
$args{"to_camel_case"} $args{"from_camel_case"} defined $replacement
Truth table60%3/5
A or B and C
A= $args{"to_camel_case"}B= $args{"from_camel_case"}C= defined $replacement
ABCresult
00X0
1X00
0100
1X11
0111
22 and defined $replacement )
23 {
24 0 croak("Can't accept both replacement name and to_camel_case/from_camel_case");
25 ▶ } elsif ( not $args{to_camel_case}
Condition83%5/6
$args{'to_camel_case'} || $args{'from_camel_case'} || defined $replacement
l!l&&r!l&&!r
570
$args{"to_camel_case"} or $args{"from_camel_case"}
l!l&&r!l&&!r
327
MC/DC0%0/3
$args{'to_camel_case'} || $args{'from_camel_case'} || defined $replacement
$args{"to_camel_case"} $args{"from_camel_case"} defined $replacement
Truth table75%3/4
A or B || C
A= $args{"to_camel_case"}B= $args{"from_camel_case"}C= defined $replacement
ABCresult
1XX1
01X1
0011
0000
26 and not $args{from_camel_case}
27 and not defined $replacement )
28 {
29 0 croak("Need either 'replacement' or to/from_camel_case options");
30 }
31  
32 12 my $doc = $self->ppi;
33 12 my $token = PPIx::EditorTools::find_token_at_location( $doc, $location );
34  
35 12 ▶ die "no token found" unless defined $token;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $token 0 12 12
36  
37 12 my $declaration = PPIx::EditorTools::find_variable_declaration($token);
38 12 ▶ die "no declaration" unless defined $declaration;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $declaration 0 12 12
39  
40 12 $doc->index_locations;
41  
42 12 my $scope = $declaration;
43 12 ▶ while ( not $scope->isa('PPI::Document')
Branch: 1 branch
BranchTrueFalseTotal
unless ($scope->isa("PPI::Document") or $scope->isa("PPI::Structure::Block")) 12 12 24
Condition66%2/3
$scope->isa("PPI::Document") or $scope->isa("PPI::Structure::Block")
l!l&&r!l&&!r
01212
MC/DC50%1/2
$scope->isa("PPI::Document") or $scope->isa("PPI::Structure::Block")
$scope->isa("PPI::Document") $scope->isa("PPI::Structure::Block")
Truth table66%2/3
A or B
A= $scope->isa("PPI::Document")B= $scope->isa("PPI::Structure::Block")
ABresult
1X1
011
000
44 and not $scope->isa('PPI::Structure::Block') )
45 {
46 12 $scope = $scope->parent;
47 }
48  
49 12 my $varname = $token->symbol;
50 12 ▶ if ( not defined $replacement ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $replacement) 5 7 12
51 5 ▶ if ( $args{from_camel_case} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($args{'from_camel_case'}) { } 2 3 5
52 2 $replacement = _from_camel_case( $varname, $args{ucfirst} );
53 } else { # $args{to_camel_case}
54 3 $replacement = _to_camel_case( $varname, $args{ucfirst} );
55 }
56 5 ▶ if ( $varname eq $replacement ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($varname eq $replacement) 0 5 5
57 0 return PPIx::EditorTools::ReturnObject->new(
58 ppi => $doc,
59 element => $token
60 );
61 }
62 }
63  
64 #warn "VARNAME: $varname";
65  
66 # TODO: This could be part of PPI somehow?
67 # The following string of hacks is simply for finding symbols in quotelikes and regexes
68 12 my $type = substr( $varname, 0, 1 );
69 12 ▶ my $brace = $type eq '@' ? '[' : ( $type eq '%' ? '{' : '' );
Branch: 2 branches
BranchTrueFalseTotal
$type eq '@' ? : 0 12 12
$type eq '%' ? : 3 9 12
70  
71 12 my @patterns;
72 12 ▶ if ( $type eq '@' or $type eq '%' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($type eq '@' or $type eq '%') { } 3 9 12
Condition66%2/3
$type eq "\@" or $type eq "%"
l!l&&r!l&&!r
039
MC/DC50%1/2
$type eq "\@" or $type eq "%"
$type eq "\@" $type eq "%"
Truth table66%2/3
A or B
A= $type eq "\@"B= $type eq "%"
ABresult
1X1
011
000
73 3 my $accessv = $varname;
74 3 $accessv =~ s/^\Q$type\E/\$/;
75 3 @patterns = (
76 quotemeta( _curlify($varname) ),
77 quotemeta($varname),
78 quotemeta($accessv) . '(?=' . quotemeta($brace) . ')',
79 );
80 3 ▶ if ( $type eq '%' ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($type eq '%') { } 3 0 3
elsif ($type eq '@') { } 0 0 0
81 3 my $slicev = $varname;
82 3 $slicev =~ s/^\%/\@/;
83 3 push @patterns, quotemeta($slicev) . '(?=' . quotemeta($brace) . ')';
84 } elsif ( $type eq '@' ) {
85 0 my $indexv = $varname;
86 0 $indexv =~ s/^\@/\$\#/;
87 0 push @patterns, quotemeta($indexv);
88 }
89 } else {
90 9 @patterns = (
91 quotemeta( _curlify($varname) ),
92 quotemeta($varname) . "(?![\[\{])"
93 );
94 }
95 12 my %unique;
96 12 my $finder_regexp = '(?:' . join( '|', grep { !$unique{$_}++ } @patterns ) . ')';
97  
98 12 $finder_regexp = qr/$finder_regexp/; # used to find symbols in quotelikes and regexes
99 #warn $finder_regexp;
100  
101 12 $replacement =~ s/^\W+//;
102  
103 $scope->find(
104 sub {
105 729 ▶ my $node = $_[1];
Subroutine __ANON__: called CC=9 SCAR=24.1
106 729 ▶ if ( $node->isa("PPI::Token::Symbol") ) {
Branch: 3 branches
BranchTrueFalseTotal
if ($node->isa('PPI::Token::Symbol')) { } 69 660 729
elsif ($type eq '@' and $node->isa('PPI::Token::ArrayIndex')) { } 0 660 660
elsif ($node->isa('PPI::Token')) { } 558 102 660
107 69 ▶ return 0 unless $node->symbol eq $varname;
Branch: 1 branch
BranchTrueFalseTotal
unless $node->symbol eq $varname 41 28 69
108  
109 # TODO do this without breaking encapsulation!
110 28 $node->{content} = substr( $node->content(), 0, 1 ) . $replacement;
111 }
112  
113 # This used to be a simple "if". Patrickas: "[elsif] resolves this
114 # issue but it may introduce other bugs since I am not sure I
115 # understand the code that follows it."
116 # See Padre trac ticket #655 for the full comment. Remove this
117 # comment if there are new bugs resulting from this change.
118 ▶ elsif ( $type eq '@' and $node->isa("PPI::Token::ArrayIndex") ) { # $#foo
Condition33%1/3
$type eq "\@" and $node->isa("PPI::Token::ArrayIndex")
!ll&&!rl&&r
66000
MC/DC0%0/2
$type eq "\@" and $node->isa("PPI::Token::ArrayIndex")
$type eq "\@" $node->isa("PPI::Token::ArrayIndex")
Truth table33%1/3
A and B
A= $type eq "\@"B= $node->isa("PPI::Token::ArrayIndex")
ABresult
0X0
100
111
119 0 ▶ return 0
Branch: 1 branch
BranchTrueFalseTotal
unless substr($node->content, 2) eq substr($varname, 1) 0 0 0
120 unless substr( $node->content, 2 ) eq substr( $varname, 1 );
121  
122 # TODO do this without breaking encapsulation!
123 0 $node->{content} = '$#' . $replacement;
124 } elsif ( $node->isa("PPI::Token") ) { # the case of potential quotelikes and regexes
125 558 my $str = $node->content;
126 558 ▶ if ($str =~ s{($finder_regexp)([\[\{]?)}<
Branch: 1 branch
BranchTrueFalseTotal
if ($str =~ s(($finder_regexp)([\[\{]?))[if ($1 =~ tr/{//) { substr($1, 0, $1 =~ tr/#// + 1) . "{$replacement}$2"; } else { substr($1, 0, $1 =~ tr/#// + 1) . "$replacement$2"; } ;]eg) 7 551 558
127 7 ▶ if ($1 =~ tr/{//) { substr($1, 0, ($1=~tr/#//)+1) . "{$replacement}$2" }
Branch: 1 branch
BranchTrueFalseTotal
if ($1 =~ tr/{//) { } 7 0 7
128 0 else { substr($1, 0, ($1=~tr/#//)+1) . "$replacement$2" }
129 >ge
130 )
131 {
132  
133 # TODO do this without breaking encapsulation!
134 7 $node->{content} = $str;
135 }
136 }
137 688 return 0;
138 },
139 12 );
140  
141 12 return PPIx::EditorTools::ReturnObject->new(
142 ppi => $doc,
143 element => $token,
144 );
145 }
146  
147 # converts a variable name to camel case and optionally converts the
148 # first character to upper case
149 sub _to_camel_case {
150 39 ▶ my $var = shift;
Subroutine _to_camel_case: called CC=4 SCAR=13.9
151 39 my $ucfirst = shift;
152 39 my $prefix;
153 39 ▶ if ( $var =~ s/^(\W*_)// ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($var =~ s/^(\W*_)//) 6 33 39
154 6 $prefix = $1;
155 }
156 39 $var =~ s/_([[:alpha:]])/\u$1/g;
157 39 ▶ $var =~ s/^([^[:alpha:]]*)([[:alpha:]])/$1\u$2/ if $ucfirst;
Branch: 1 branch
BranchTrueFalseTotal
if $ucfirst 19 20 39
158 39 ▶ $var = $prefix . $var if defined $prefix;
Branch: 1 branch
BranchTrueFalseTotal
if defined $prefix 6 33 39
159 39 return $var;
160 }
161  
162 sub _from_camel_case {
163 56 ▶ my $var = shift;
Subroutine _from_camel_case: called CC=4 SCAR=13.9
164 56 my $ucfirst = shift;
165 56 my $prefix;
166 56 ▶ if ( $var =~ s/^(\W*_?)// ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($var =~ s/^(\W*_?)//) 56 0 56
167 56 $prefix = $1;
168 }
169 56 ▶ if ($ucfirst) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ucfirst) { } 28 28 56
170 28 $var = lcfirst($var);
171 28 $var =~ s/([[:upper:]])/_\u$1/g;
172 28 $var =~ s/^([^[:alpha:]]*)([[:alpha:]])/$1\u$2/;
173 } else {
174 28 $var =~ s/^([^[:alpha:]]*)([[:alpha:]])/$1\l$2/;
175 28 $var =~ s/([[:upper:]])/_\l$1/g;
176 }
177 56 ▶ $var = $prefix . $var if defined $prefix;
Branch: 1 branch
BranchTrueFalseTotal
if defined $prefix 56 0 56
178 56 return $var;
179 }
180  
181  
182 sub _curlify {
183 12 ▶ my $var = shift;
Subroutine _curlify: called CC=2 SCAR=7.6
184 12 ▶ if ( $var =~ s/^([\$\@\%])(.+)$/${1}{$2}/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($var =~ s/^([\$\@\%])(.+)$/$1{$2}/) 12 0 12
185 12 return ($var);
186 }
187 0 return ();
188 }
189  
190 8 1;
191  
192 __END__
193  
194 =pod
195  
196 =encoding UTF-8
197  
198 =head1 NAME
199  
200 PPIx::EditorTools::RenameVariable - Lexically replace a variable name in Perl code
201  
202 =head1 VERSION
203  
204 version 0.20
205  
206 =head1 SYNOPSIS
207  
208 my $munged = PPIx::EditorTools::RenameVariable->new->rename(
209 code => $code,
210 line => 15,
211 column => 13,
212 replacement => 'stuff',
213 );
214 my $code_as_strig = $munged->code;
215 my $code_as_ppi = $munged->ppi;
216 my $location = $munged->element->location;
217  
218 =head1 DESCRIPTION
219  
220 This module will lexically replace a variable name.
221  
222 =head1 METHODS
223  
224 =over 4
225  
226 =item new()
227  
228 Constructor. Generally shouldn't be called with any arguments.
229  
230 =item rename( ppi => PPI::Document $ppi, line => Int, column => Int, replacement => Str )
231  
232 =item rename( code => Str $code, line => Int, column => Int, replacement => Str )
233  
234 =item rename( code => Str $code, line => Int, column => Int, to_camel_case => Bool, [ucfirst => Bool] )
235  
236 =item rename( code => Str $code, line => Int, column => Int, from_camel_case => Bool, [ucfirst => Bool] )
237  
238 Accepts either a C<PPI::Document> to process or a string containing
239 the code (which will be converted into a C<PPI::Document>) to process.
240 Renames the variable found at line, column with that supplied in the C<replacement>
241 parameter and returns a C<PPIx::EditorTools::ReturnObject> with the
242 new code available via the C<ppi> or C<code> accessors, as a
243 C<PPI::Document> or C<string>, respectively. The C<PPI::Token> found at
244 line, column is available via the C<element> accessor.
245  
246 Instead of specifying an explicit replacement variable name, you may
247 choose to use the C<to_camel_case> or C<from_camel_case> options that automatically
248 convert to/from camelCase. In that mode, the C<ucfirst> option will force
249 uppercasing of the first letter.
250  
251 You can not specify a replacement name and use the C<to/from_camel_case>
252 options.
253  
254 Croaks with a "no token" exception if no token is found at the location.
255 Croaks with a "no declaration" exception if unable to find the declaration.
256  
257 =back
258  
259 =head1 SEE ALSO
260  
261 This class inherits from C<PPIx::EditorTools>.
262 Also see L<App::EditorTools>, L<Padre>, and L<PPI>.
263  
264 =head1 AUTHORS
265  
266 =over 4
267  
268 =item *
269  
270 Steffen Mueller C<smueller@cpan.org>
271  
272 =item *
273  
274 Mark Grimes C<mgrimes@cpan.org>
275  
276 =item *
277  
278 Ahmad M. Zawawi <ahmad.zawawi@gmail.com>
279  
280 =item *
281  
282 Gabor Szabo <gabor@szabgab.com>
283  
284 =item *
285  
286 Yanick Champoux <yanick@cpan.org>
287  
288 =back
289  
290 =head1 COPYRIGHT AND LICENSE
291  
292 This software is copyright (c) 2017, 2014, 2012 by The Padre development team as listed in Padre.pm..
293  
294 This is free software; you can redistribute it and/or modify it under
295 the same terms as the Perl 5 programming language system itself.
296  
297 =cut
« blib/lib/PPIx/EditorTools.pm ↑ Summary blib/lib/PPIx/EditorTools/Outline.pm »