blib/lib/PPIx/Refactor.pm

filter: Statementstmt 87.7% 43 / 49 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 84.7% 61 / 72 CC 3 SCAR 5.2
CC
3
Cov
85%
dump
6.0
BEGIN
2.0
__ANON__
1.1
CRAP 1.7
↑ Summary
1 package PPIx::Refactor;
2 3 $PPIx::Refactor::VERSION = '0.07';
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->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 ▶ $DB::single=1;
Subroutine __ANON__: not called CC=1 SCAR=1.2
Subroutine _build_element: called CC=1 SCAR=1.2
107 0 $_[0]->doc;
108 },
109 3 );
110  
111 =head3 ppi_find
112  
113 required coderef with which to find the elements of interest
114  
115 =cut
116  
117 3 has ppi_find => (
118 is => 'ro',
119 # isa CodeRef
120 required => 1,
121 );
122  
123  
124 =head3 writer
125  
126 optional coderef with which to rewrite the code.
127  
128 =cut
129  
130 has writer => (
131 is => 'ro',
132 ▶ default => sub {},
Subroutine __ANON__: called CC=1 SCAR=0.0
133 3 );
134  
135 =head3 finds
136  
137 lazy built arrayref of all the elements of interest found
138  
139 =cut
140  
141 has finds => (
142 is => 'ro',
143 lazy => 1,
144 default => sub {
145 2 ▶ my ($self) = @_;
Subroutine __ANON__: called CC=1 SCAR=0.0
146 2 my $find = PPI::Find->new($self->ppi_find);
147 2 my @results = $find->in($self->element);
148 2 return \@results;
149 }
150 3 );
151  
152 =head1 METHODS
153  
154 =head2 $self->rewrite
155  
156 Worker sub that rewrites the code. Operates on what it finds in
157 C<<$self->finds>>
158  
159 =cut
160  
161 sub rewrite {
162 1 ▶ my ($self) = @_;
Subroutine rewrite: called CC=1 SCAR=0.0
Pod documented
163 1 $self->writer->($self->finds);
164 1 $self->element->save($self->file);
165 }
166  
167 =head2 $self->dump($elem, $whitespace);
168  
169 For debugging. Prints a dump of the passed in element. If C<$whitespace>
170 is true it will include whitespace in the dump. Defaults to false.
171  
172 =cut
173  
174 sub dump {
175 0 ▶ my ($self, $doc, $whitespace) = @_;
Subroutine dump: not called CC=2 SCAR=17.9
Pod documented
176 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
177 0 my $dump = PPI::Dumper->new($doc, whitespace => $whitespace);
178 0 $dump->print;
179 }
180  
181  
182 =head1 AUTHOR
183  
184 Kieren Diment, C<< <zarquon at cpan.org> >>
185  
186 =head1 BUGS
187  
188 Please report any bugs or feature requests via github:
189 L<https://github.com/singingfish/PPIx-Refactor/issues>.
190  
191 =head1 SUPPORT
192  
193 Jump on to #web-simple on irc.perl.org
194  
195 =head1 ACKNOWLEDGEMENTS
196  
197  
198 =head1 LICENSE AND COPYRIGHT
199  
200 Copyright 2015 Kieren Diment.
201  
202 This program is free software; you can redistribute it and/or modify it
203 under the same terms as perl itself.
204 =cut
205  
206 3 1;
↑ Summary