PPI/Normal.pm

filter: Statementstmt 100% 62 / 62 Branchbran 75.0% 15 / 20 Conditioncond 66.6% 8 / 12 MC/DCmcdc 42.8% 3 / 7 Subroutinesub 100% 15 / 15 Podpod 75.0% 3 / 4 totaltotal 88.3% 106 / 120 CC 16 SCAR 14.3
CC
16
Cov
90%
register
7.3
process
6.1
new
5.0
CRAP 4.2
« PPI/Cache.pm ↑ Summary PPI/Exception.pm »
1 package PPI::Normal;
2  
3 =pod
4  
5 =head1 NAME
6  
7 PPI::Normal - Normalize Perl Documents
8  
9 =head2 DESCRIPTION
10  
11 Perl Documents, as created by PPI, are typically filled with all sorts of
12 mess such as whitespace and comments and other things that don't effect
13 the actual meaning of the code.
14  
15 In addition, because there is more than one way to do most things, and the
16 syntax of Perl itself is quite flexible, there are many ways in which the
17 "same" code can look quite different.
18  
19 PPI::Normal attempts to resolve this by providing a variety of mechanisms
20 and algorithms to "normalize" Perl Documents, and determine a sort of base
21 form for them (although this base form will be a memory structure, and
22 not something that can be turned back into Perl source code).
23  
24 The process itself is quite complex, and so for convenience and
25 extensibility it has been separated into a number of layers. At a later
26 point, it will be possible to write Plugin classes to insert additional
27 normalization steps into the various different layers.
28  
29 In addition, you can choose to do the normalization only as deep as a
30 particular layer, depending on aggressively you want the normalization
31 process to be.
32  
33 =head1 METHODS
34  
35 =cut
36  
37 62 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
38 62 ▶use Carp ();
Subroutine BEGIN: called CC=1 SCAR=0.0
39 62 ▶use List::Util 1.33 ();
Subroutine BEGIN: called CC=1 SCAR=0.0
40 62 ▶use PPI::Util '_Document';
Subroutine BEGIN: called CC=1 SCAR=0.0
41 62 ▶use PPI::Document::Normalized ();
Subroutine BEGIN: called CC=1 SCAR=0.0
42  
43 62 ▶use vars qw{$VERSION %LAYER};
Subroutine BEGIN: called CC=1 SCAR=0.0
44 BEGIN {
45 62 ▶ $VERSION = '1.236';
Subroutine BEGIN: called CC=1 SCAR=0.0
46  
47 # Registered function store
48 62 %LAYER = (
49 1 => [],
50 2 => [],
51 );
52 }
53  
54  
55  
56  
57  
58 #####################################################################
59 # Configuration
60  
61 =pod
62  
63 =head2 register $function => $layer, ...
64  
65 The C<register> method is used by normalization method providers to
66 tell the normalization engines which functions need to be run, and
67 in which layer they apply.
68  
69 Provide a set of key/value pairs, where the key is the full name of the
70 function (in string form), and the value is the layer (see description
71 of the layers above) in which it should be run.
72  
73 Returns true if all functions are registered, or C<undef> on error.
74  
75 =cut
76  
77 sub register {
78 64 ▶ my $class = shift;
Subroutine register: called CC=7 SCAR=19.9
Pod documented
79 64 ▶ while ( @_ ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 312 63 375
80 # Check the function
81 312 my $function = shift;
82 SCOPE: {
83 62 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
84 312 ▶ defined $function and defined &{"$function"}
Branch: 1 branch
BranchTrueFalseTotal
defined $function and defined &{"$function";} or Carp::croak("Bad function name provided to PPI::Normal") 0 312 312
Condition33%1/3
defined $function and defined &{"$function";}
!ll&&!rl&&r
00312
MC/DC0%0/2
defined $function and defined &{"$function";}
defined $function defined &{"$function";}
Truth table33%1/3
A and B
A= defined $functionB= defined &{"$function";}
ABresult
0X0
100
111
85 or Carp::croak("Bad function name provided to PPI::Normal");
86 }
87  
88 # Has it already been added?
89 312 ▶ if ( List::Util::any { $_ eq $function } map @{$_}, values %LAYER ) {
Subroutine __ANON__: called CC=1 SCAR=0.0
Branch: 1 branch
BranchTrueFalseTotal
if (&List::Util::any(sub { $_ eq $function; } , map(@{$_;}, values %PPI::Normal::LAYER))) 1 311 312
90 1 return 1;
91 }
92  
93 # Check the layer to add it to
94 311 my $layer = shift;
95 311 ▶ defined $layer and $layer =~ /^(?:1|2)$/
Branch: 1 branch
BranchTrueFalseTotal
defined $layer and $layer =~ /^(?:1|2)$/ or Carp::croak("Bad layer provided to PPI::Normal") 0 311 311
Condition33%1/3
defined $layer and $layer =~ /^(?:1|2)$/
!ll&&!rl&&r
00311
MC/DC0%0/2
defined $layer and $layer =~ /^(?:1|2)$/
defined $layer $layer =~ /^(?:1|2)$/
Truth table33%1/3
A and B
A= defined $layerB= $layer =~ /^(?:1|2)$/
ABresult
0X0
100
111
96 or Carp::croak("Bad layer provided to PPI::Normal");
97  
98 # Add to the layer data store
99 311 push @{ $LAYER{$layer} }, $function;
100 }
101  
102 63 1;
103 }
104  
105 # With the registration mechanism in place, load in the main set of
106 # normalization methods to initialize the store.
107 62 ▶use PPI::Normal::Standard;
Subroutine BEGIN: called CC=1 SCAR=0.0
108  
109  
110  
111  
112  
113 #####################################################################
114 # Constructor and Accessors
115  
116 =pod
117  
118 =head2 new
119  
120 my $level_1 = PPI::Normal->new;
121 my $level_2 = PPI::Normal->new(2);
122  
123 Creates a new normalization object, to which Document objects
124 can be passed to be normalized.
125  
126 Of course, what you probably REALLY want is just to call
127 L<PPI::Document>'s C<normalize> method.
128  
129 Takes an optional single parameter of the normalisation layer
130 to use, which at this time can be either "1" or "2".
131  
132 Returns a new C<PPI::Normal> object, or C<undef> on error.
133  
134 =cut
135  
136 sub new {
137 15 ▶ my $class = shift;
Subroutine new: called CC=5 SCAR=16.1
Pod documented
138 15 ▶ my $layer = @_ ?
Branch: 2 branches
BranchTrueFalseTotal
@_ ? : 10 5 15
defined $_[0] && !ref($_[0]) && $_[0] =~ /^[12]$/ ? : 4 6 10
Condition100%6/6
defined $_[0] and not ref $_[0] and $_[0] =~ /^[12]$/
!ll&&!rl&&r
424
defined $_[0] and not ref $_[0]
!ll&&!rl&&r
136
MC/DC100%3/3
defined $_[0] and not ref $_[0] and $_[0] =~ /^[12]$/
defined $_[0] not ref $_[0] $_[0] =~ /^[12]$/
Truth table100%4/4
A and not(B) and C
A= defined $_[0]B= not ref $_[0]C= $_[0] =~ /^[12]$/
ABCresult
0XX0
10X0
1100
1111
139 (defined $_[0] and ! ref $_[0] and $_[0] =~ /^[12]$/) ? shift : return undef
140 : 1;
141  
142 # Create the object
143 9 my $object = bless {
144 layer => $layer,
145 }, $class;
146  
147 9 $object;
148 }
149  
150 =pod
151  
152 =head1 layer
153  
154 The C<layer> accessor returns the normalisation layer of the object.
155  
156 =cut
157  
158 14 ▶sub layer { $_[0]->{layer} }
Subroutine layer: called CC=1 SCAR=0.0
Pod undocumented
159  
160  
161  
162  
163  
164 #####################################################################
165 # Main Methods
166  
167 =pod
168  
169 =head2 process
170  
171 The C<process> method takes anything that can be converted to a
172 L<PPI::Document> (object, SCALAR ref, filename), loads it and
173 applies the normalisation process to the document.
174  
175 Returns a L<PPI::Document::Normalized> object, or C<undef> on error.
176  
177 =cut
178  
179 sub process {
180 11 ▶ my $self = ref $_[0] ? shift : shift->new;
Subroutine process: called CC=6 SCAR=18.0
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] ? : 7 4 11
181  
182 # PPI::Normal objects are reusable, but not re-entrant
183 11 ▶ return undef if $self->{Document};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"Document"} 0 11 11
184  
185 # Get or create the document
186 11 ▶ $self->{Document} = _Document(shift) or return undef;
Branch: 1 branch
BranchTrueFalseTotal
$self->{"Document"} = _Document(shift()) or return undef 0 11 11
187  
188 # Work out what functions we need to call
189 11 my @functions = map { @{ $LAYER{$_} } } ( 1 .. $self->layer );
190  
191 # Execute each function
192 11 foreach my $function ( @functions ) {
193 62 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
194 34 &{"$function"}( $self->{Document} );
195 }
196  
197 # Create the normalized Document object
198 my $Normalized = PPI::Document::Normalized->new(
199 Document => $self->{Document},
200 11 ▶ version => $VERSION,
Branch: 1 branch
BranchTrueFalseTotal
my $Normalized = "PPI::Document::Normalized"->new("Document", $self->{"Document"}, "version", $PPI::Normal::VERSION, "functions", \@functions) or return undef 0 11 11
201 functions => \@functions,
202 ) or return undef;
203  
204 # Done, clean up
205 11 delete $self->{Document};
206 11 return $Normalized;
207 }
208  
209 62 1;
210  
211 =pod
212  
213 =head1 NOTES
214  
215 The following normalisation layers are implemented. When writing
216 plugins, you should register each transformation function with the
217 appropriate layer.
218  
219 =head2 Layer 1 - Insignificant Data Removal
220  
221 The basic step common to all normalization, layer 1 scans through the
222 Document and removes all whitespace, comments, POD, and anything else
223 that returns false for its C<significant> method.
224  
225 It also checks each Element and removes known-useless sub-element
226 metadata such as the Element's physical position in the file.
227  
228 =head2 Layer 2 - Significant Element Removal
229  
230 After the removal of the insignificant data, Layer 2 removed larger, more
231 complex, and superficially "significant" elements, that can be removed
232 for the purposes of normalisation.
233  
234 Examples from this layer include pragmas, now-useless statement
235 separators (since the PDOM tree is holding statement elements), and
236 several other minor bits and pieces.
237  
238 =head2 Layer 3 - TO BE COMPLETED
239  
240 This version of the forward-port of the Perl::Compare functionality
241 to the 0.900+ API of PPI only implements Layer 1 and 2 at this time.
242  
243 =head1 TO DO
244  
245 - Write the other 4-5 layers :)
246  
247 =head1 SUPPORT
248  
249 See the L<support section|PPI/SUPPORT> in the main module.
250  
251 =head1 AUTHOR
252  
253 Adam Kennedy E<lt>adamk@cpan.orgE<gt>
254  
255 =head1 COPYRIGHT
256  
257 Copyright 2005 - 2011 Adam Kennedy.
258  
259 This program is free software; you can redistribute
260 it and/or modify it under the same terms as Perl itself.
261  
262 The full text of the license can be found in the
263 LICENSE file included with this module.
264  
265 =cut
« PPI/Cache.pm ↑ Summary PPI/Exception.pm »