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% 14 / 14 Podpod 75.0% 3 / 4 totaltotal 88.2% 105 / 119 CC 16 SCAR 14.5
CC
16
Cov
90%
register
7.3
process
6.1
new
5.0
CRAP 4.3
« PPI/Token/Structure.pm ↑ Summary PPI/Token/Number/Version.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 63 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
38 63 ▶use Carp ();
Subroutine BEGIN: called CC=1 SCAR=0.0
39 63 ▶use List::Util 1.33 ();
Subroutine BEGIN: called CC=1 SCAR=0.0
40 63 ▶use PPI::Util '_Document';
Subroutine BEGIN: called CC=1 SCAR=0.0
41 63 ▶use PPI::Document::Normalized ();
Subroutine BEGIN: called CC=1 SCAR=0.0
42 63 ▶use PPI::Normal::Standard ();
Subroutine BEGIN: called CC=1 SCAR=0.0
43 63 ▶use PPI::Singletons '%LAYER';
Subroutine BEGIN: called CC=1 SCAR=0.0
44  
45 63 our $VERSION = '1.260'; # VERSION
46  
47 # With the registration mechanism in place, load in the main set of
48 # normalization methods to initialize the store.
49 63 PPI::Normal::Standard->import;
50  
51  
52  
53  
54 #####################################################################
55 # Configuration
56  
57 =pod
58  
59 =head2 register $function => $layer, ...
60  
61 The C<register> method is used by normalization method providers to
62 tell the normalization engines which functions need to be run, and
63 in which layer they apply.
64  
65 Provide a set of key/value pairs, where the key is the full name of the
66 function (in string form), and the value is the layer (see description
67 of the layers above) in which it should be run.
68  
69 Returns true if all functions are registered, or C<undef> on error.
70  
71 =cut
72  
73 sub register {
74 65 ▶ my $class = shift;
Subroutine register: called CC=7 SCAR=19.9
Pod documented
75 65 ▶ while ( @_ ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 317 64 381
76 # Check the function
77 317 my $function = shift;
78 SCOPE: {
79 63 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
80 317 ▶ 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 317 317
Condition33%1/3
defined $function and defined &{"$function";}
!ll&&!rl&&r
00317
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
81 or Carp::croak("Bad function name provided to PPI::Normal");
82 }
83  
84 # Has it already been added?
85 317 ▶ 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 316 317
86 1 return 1;
87 }
88  
89 # Check the layer to add it to
90 316 my $layer = shift;
91 316 ▶ 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 316 316
Condition33%1/3
defined $layer and $layer =~ /^(?:1|2)$/
!ll&&!rl&&r
00316
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
92 or Carp::croak("Bad layer provided to PPI::Normal");
93  
94 # Add to the layer data store
95 316 push @{ $LAYER{$layer} }, $function;
96 }
97  
98 64 1;
99 }
100  
101  
102  
103  
104  
105 #####################################################################
106 # Constructor and Accessors
107  
108 =pod
109  
110 =head2 new
111  
112 my $level_1 = PPI::Normal->new;
113 my $level_2 = PPI::Normal->new(2);
114  
115 Creates a new normalization object, to which Document objects
116 can be passed to be normalized.
117  
118 Of course, what you probably REALLY want is just to call
119 L<PPI::Document>'s C<normalize> method.
120  
121 Takes an optional single parameter of the normalisation layer
122 to use, which at this time can be either "1" or "2".
123  
124 Returns a new C<PPI::Normal> object, or C<undef> on error.
125  
126 =cut
127  
128 sub new {
129 15 ▶ my $class = shift;
Subroutine new: called CC=5 SCAR=16.1
Pod documented
130 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
131 (defined $_[0] and ! ref $_[0] and $_[0] =~ /^[12]$/) ? shift : return undef
132 : 1;
133  
134 # Create the object
135 9 my $object = bless {
136 layer => $layer,
137 }, $class;
138  
139 9 $object;
140 }
141  
142 =pod
143  
144 =head1 layer
145  
146 The C<layer> accessor returns the normalisation layer of the object.
147  
148 =cut
149  
150 14 ▶sub layer { $_[0]->{layer} }
Subroutine layer: called CC=1 SCAR=0.0
Pod undocumented
151  
152  
153  
154  
155  
156 #####################################################################
157 # Main Methods
158  
159 =pod
160  
161 =head2 process
162  
163 The C<process> method takes anything that can be converted to a
164 L<PPI::Document> (object, SCALAR ref, filename), loads it and
165 applies the normalisation process to the document.
166  
167 Returns a L<PPI::Document::Normalized> object, or C<undef> on error.
168  
169 =cut
170  
171 sub process {
172 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
173  
174 # PPI::Normal objects are reusable, but not re-entrant
175 11 ▶ return undef if $self->{Document};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"Document"} 0 11 11
176  
177 # Get or create the document
178 11 ▶ $self->{Document} = _Document(shift) or return undef;
Branch: 1 branch
BranchTrueFalseTotal
$self->{"Document"} = _Document(shift()) or return undef 0 11 11
179  
180 # Work out what functions we need to call
181 11 my @functions = map { @{ $LAYER{$_} } } ( 1 .. $self->layer );
182  
183 # Execute each function
184 11 foreach my $function ( @functions ) {
185 63 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
186 34 &{"$function"}( $self->{Document} );
187 }
188  
189 # Create the normalized Document object
190 my $Normalized = PPI::Document::Normalized->new(
191 Document => $self->{Document},
192 11 ▶ version => __PACKAGE__->VERSION,
Branch: 1 branch
BranchTrueFalseTotal
my $Normalized = "PPI::Document::Normalized"->new("Document", $self->{"Document"}, "version", __PACKAGE__->VERSION, "functions", \@functions) or return undef 0 11 11
193 functions => \@functions,
194 ) or return undef;
195  
196 # Done, clean up
197 11 delete $self->{Document};
198 11 return $Normalized;
199 }
200  
201 63 1;
202  
203 =pod
204  
205 =head1 NOTES
206  
207 The following normalisation layers are implemented. When writing
208 plugins, you should register each transformation function with the
209 appropriate layer.
210  
211 =head2 Layer 1 - Insignificant Data Removal
212  
213 The basic step common to all normalization, layer 1 scans through the
214 Document and removes all whitespace, comments, POD, and anything else
215 that returns false for its C<significant> method.
216  
217 It also checks each Element and removes known-useless sub-element
218 metadata such as the Element's physical position in the file.
219  
220 =head2 Layer 2 - Significant Element Removal
221  
222 After the removal of the insignificant data, Layer 2 removed larger, more
223 complex, and superficially "significant" elements, that can be removed
224 for the purposes of normalisation.
225  
226 Examples from this layer include pragmas, now-useless statement
227 separators (since the PDOM tree is holding statement elements), and
228 several other minor bits and pieces.
229  
230 =head2 Layer 3 - TO BE COMPLETED
231  
232 This version of the forward-port of the Perl::Compare functionality
233 to the 0.900+ API of PPI only implements Layer 1 and 2 at this time.
234  
235 =head1 TO DO
236  
237 - Write the other 4-5 layers :)
238  
239 =head1 SUPPORT
240  
241 See the L<support section|PPI/SUPPORT> in the main module.
242  
243 =head1 AUTHOR
244  
245 Adam Kennedy E<lt>adamk@cpan.orgE<gt>
246  
247 =head1 COPYRIGHT
248  
249 Copyright 2005 - 2011 Adam Kennedy.
250  
251 This program is free software; you can redistribute
252 it and/or modify it under the same terms as Perl itself.
253  
254 The full text of the license can be found in the
255 LICENSE file included with this module.
256  
257 =cut
« PPI/Token/Structure.pm ↑ Summary PPI/Token/Number/Version.pm »