blib/lib/PPIx/Refactor.pm

filter: Statementstmt 89.3% 42 / 47 Branchbran 100% 2 / 2 Conditioncond 0.0% 0 / 2 MC/DCmcdc 0.0% 0 / 1 Subroutinesub 87.5% 14 / 16 Podpod 100% 2 / 2 totaltotal 85.7% 60 / 70 SCAR 11.1
CC
3
Cov
86%
dump
6.0
BEGIN
2.0
__ANON__
1.1
CRAP 3.0
↑ Summary
1 package PPIx::Refactor;
2 3 $PPIx::Refactor::VERSION = '0.08';
3  
4 3 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 3 ▶use Path::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 BEGIN {
7 3 ▶ use File::Path;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 3 ▶ use constant { CACHE => '/tmp/ppix-refactor_cache', };
Subroutine BEGIN: called CC=1 SCAR=0.0
9 3 ▶ our $cache = CACHE;
Subroutine BEGIN: called CC=2 SCAR=6.9
10 3 ▶ File::Path::mkpath($cache) unless -e CACHE;
Branch: 1 branch
BranchTrueFalseTotal
unless -e "/tmp/ppix-refactor_cache" 1 2 3
11 }
12  
13 3 ▶use PPI;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 3 ▶use PPI::Cache path => CACHE;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 3 ▶use PPI::Find;
Subroutine BEGIN: called CC=1 SCAR=0.0
16  
17 =head1 NAME
18  
19 PPIx::Refactor - Hooks for refactoring perl via L<PPI>
20  
21 =head1 SYNOPSIS
22  
23 use PPIx::Refactor;
24 my $p = PPIx::Refactor->new(file => '/path/to/perl/code/file.pl',
25 ppi_find => sub {
26 my ($elem, $doc) = @_;
27 return 1 if $elem->class eq 'PPI::Statement::Sub',
28 return 0;
29 }
30 [ writer => \&found ]);
31 my $finds = $p->finds; # for examining them interactively
32 $p->rewrite; # rewrites the file in place. You are using version control yes?
33  
34 =head1 SUMMARY
35  
36 This is a really simple module to make rewriting perl code via L<PPI>
37 debugger friendly and easy. See the test in
38 L<t/refactor.t|https://github.com/singingfish/PPIx-Refactor/blob/master/t/refactor.t>
39 of this distribution for a working example. Pretty much all the real work
40 happens in the coderef you set up in C<< $p->ppi_find >> and C<< $p->writer >>.
41  
42 For an example of a simple script for checking statements in code for being
43 syntactically identical (i.e. a crude copypasta detector) see C<
44 similar_statements.pl > in the examples directory of the distribution.
45  
46 NOTE L<PPI::Cache> is used to store a cached representation of the source
47 parse in C</tmp/pppix-refactor_cache>
48  
49 =head2 RATIONALE
50  
51 Rewriting code via ppi is a fiddly pain. L<PPIx::Refactor> provides a
52 minimal interface so you can concentrate on the fiddlyness and minimise the
53 pain.
54  
55 =head2 TODO
56  
57 Would be nice to specify a rewriter via roles, and it would be nice to have
58 $self in C<< $p->ppi_find >>. On the other hand rewrite/refactoring code like
59 this can either be simple throwaways, or really really complicated. This
60 code is so far optimised for the throwaway case.
61  
62 =cut
63  
64 =head2 ATTRIBUTES
65  
66 =head3 file
67  
68 required string that coerces into a Path::Tiny
69  
70 =cut
71  
72 has file => (
73 is => 'ro',
74 coerce => sub {
75 2 ▶ path($_[0]);
Subroutine __ANON__: called CC=1 SCAR=0.0
76 }
77 3 );
78  
79 =head3 doc
80  
81 lazily built PPI::Document
82  
83 =cut
84  
85 has doc => (
86 is => 'ro',
87 lazy => 1,
88 default => sub {
89 2 ▶ my ($self) = @_;
Subroutine __ANON__: called CC=1 SCAR=0.0
90 2 return PPI::Document::File->new($self->file->stringify);
91 },
92 3 );
93  
94 =head3 element
95  
96 If you're using prior finds (e.g. subroutines you're trying to analyse)
97 you'll want to pass an element into new rather than a doc. Element
98 defaults to the document you passed in.
99  
100 =cut
101  
102 has element => (
103 is => 'ro',
104 lazy => 1,
105 builder => sub {
106 0 ▶ $_[0]->doc;
Subroutine _build_element: not called CC=1 SCAR=1.2
Subroutine __ANON__: called CC=1 SCAR=1.2
107 },
108 3 );
109  
110 =head3 ppi_find
111  
112 required coderef with which to find the elements of interest
113  
114 =cut
115  
116 3 has ppi_find => (
117 is => 'ro',
118 # isa CodeRef
119 required => 1,
120 );
121  
122  
123 =head3 writer
124  
125 optional coderef with which to rewrite the code.
126  
127 =cut
128  
129 has writer => (
130 is => 'ro',
131 ▶ default => sub {},
Subroutine __ANON__: called CC=1 SCAR=0.0
132 3 );
133  
134 =head3 finds
135  
136 lazy built arrayref of all the elements of interest found
137  
138 =cut
139  
140 has finds => (
141 is => 'ro',
142 lazy => 1,
143 default => sub {
144 2 ▶ my ($self) = @_;
Subroutine __ANON__: called CC=1 SCAR=0.0
145 2 my $find = PPI::Find->new($self->ppi_find);
146 2 my @results = $find->in($self->element);
147 2 return \@results;
148 }
149 3 );
150  
151 =head1 METHODS
152  
153 =head2 $self->rewrite
154  
155 Worker sub that rewrites the code. Operates on what it finds in
156 C<<$self->finds>>
157  
158 =cut
159  
160 sub rewrite {
161 1 ▶ my ($self) = @_;
Subroutine rewrite: called CC=1 SCAR=0.0
Pod documented
162 1 $self->writer->($self->finds);
163 1 $self->element->save($self->file);
164 }
165  
166 =head2 $self->dump($elem, $whitespace);
167  
168 For debugging. Prints a dump of the passed in element. If C<$whitespace>
169 is true it will include whitespace in the dump. Defaults to false.
170  
171 =cut
172  
173 sub dump {
174 0 ▶ my ($self, $doc, $whitespace) = @_;
Subroutine dump: not called CC=2 SCAR=17.9
Pod documented
175 0 ▶ $whitespace ||=0;
Condition0%0/2
$whitespace ||= 0
l!l
00
MC/DC0%0/1
$whitespace ||= 0
$whitespace
Truth table0%0/2
A
A= $whitespace
Aresult
11
00
176 0 my $dump = PPI::Dumper->new($doc, whitespace => $whitespace);
177 0 $dump->print;
178 }
179  
180  
181 =head1 AUTHOR
182  
183 Kieren Diment, C<< <zarquon at cpan.org> >>
184  
185 =head1 BUGS
186  
187 Please report any bugs or feature requests via github:
188 L<https://github.com/singingfish/PPIx-Refactor/issues>.
189  
190 =head1 SUPPORT
191  
192 Jump on to #web-simple on irc.perl.org
193  
194 =head1 ACKNOWLEDGEMENTS
195  
196  
197 =head1 LICENSE AND COPYRIGHT
198  
199 Copyright 2015 Kieren Diment.
200  
201 This program is free software; you can redistribute it and/or modify it
202 under the same terms as perl itself.
203 =cut
204  
205 3 1;
↑ Summary