PPI/Document/Normalized.pm

filter: Statementstmt 80.6% 79 / 98 Branchbran 50.0% 32 / 64 Conditioncond 33.3% 9 / 27 MC/DCmcdc 6.2% 1 / 16 Subroutinesub 78.2% 18 / 23 Podpod 100% 4 / 4 totaltotal 61.6% 143 / 232 CC 44 SCAR 24.3
CC
44
Cov
63%
_equal_SCALAR
30.0
_equal_HASH
21.7
_equal_ARRAY
14.6
CRAP 11.3
« PPI/Structure/List.pm ↑ Summary PPI/Document.pm »
1 package PPI::Document::Normalized;
2  
3 =pod
4  
5 =head1 NAME
6  
7 PPI::Document::Normalized - A normalized Perl Document
8  
9 =head1 DESCRIPTION
10  
11 A C<Normalized Document> object is the result of the normalization process
12 contained in the L<PPI::Normal> class. See the documentation for
13 L<PPI::Normal> for more information.
14  
15 The object contains a version stamp and function list for the version
16 of L<PPI::Normal> used to create it, and a processed and delinked
17 L<PPI::Document> object.
18  
19 Typically, the Document object will have been mangled by the normalization
20 process in a way that would make it fatal to try to actually DO anything
21 with it.
22  
23 Put simply, B<never> use the Document object after normalization.
24 B<YOU HAVE BEEN WARNED!>
25  
26 The object is designed the way it is to provide a bias towards false
27 negatives. A comparison between two ::Normalized object will only return
28 true if they were produced by the same version of PPI::Normal, with the
29 same set of normalization functions (in the same order).
30  
31 You may get false negatives if you are caching objects across an upgrade.
32  
33 Please note that this is done for security purposes, as there are many
34 cases in which low layer normalization is likely to be done as part of
35 a code security process, and false positives could be highly dangerous.
36  
37 =head1 METHODS
38  
39 =cut
40  
41 # For convenience (and since this isn't really a public class), import
42 # the methods we will need from Scalar::Util.
43 62 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
44 62 ▶use Scalar::Util qw{refaddr reftype blessed};
Subroutine BEGIN: called CC=1 SCAR=0.0
45 62 ▶use Params::Util qw{_INSTANCE _ARRAY};
Subroutine BEGIN: called CC=1 SCAR=0.0
46 62 ▶use PPI::Util ();
Subroutine BEGIN: called CC=1 SCAR=0.0
47  
48 62 ▶use vars qw{$VERSION};
Subroutine BEGIN: called CC=1 SCAR=0.0
49 BEGIN {
50 62 ▶ $VERSION = '1.236';
Subroutine BEGIN: called CC=1 SCAR=0.0
51 }
52  
53 62 ▶use overload 'bool' => \&PPI::Util::TRUE;
Subroutine BEGIN: called CC=1 SCAR=0.0
54 62 ▶use overload '==' => 'equal';
Subroutine BEGIN: called CC=1 SCAR=0.0
55  
56  
57  
58  
59  
60  
61 #####################################################################
62 # Constructor and Accessors
63  
64 =pod
65  
66 =head2 new
67  
68 The C<new> method is intended for use only by the L<PPI::Normal> class,
69 and to get ::Normalized objects, you are highly recommended to use
70 either that module, or the C<normalized> method of the L<PPI::Document>
71 object itself.
72  
73 =cut
74  
75 sub new {
76 11 ▶ my $class = shift;
Subroutine new: called CC=4 SCAR=14.3
Pod documented
77 11 my %args = @_;
78  
79 # Check the required params
80 11 ▶ my $Document = _INSTANCE($args{Document}, 'PPI::Document') or return undef;
Branch: 1 branch
BranchTrueFalseTotal
my $Document = &_INSTANCE($args{"Document"}, "PPI::Document") or return undef 0 11 11
81 11 ▶ my $version = $args{version} or return undef;
Branch: 1 branch
BranchTrueFalseTotal
my $version = $args{"version"} or return undef 0 11 11
82 11 ▶ my $functions = _ARRAY($args{functions}) or return undef;
Branch: 1 branch
BranchTrueFalseTotal
my $functions = &_ARRAY($args{"functions"}) or return undef 0 11 11
83  
84 # Create the object
85 11 my $self = bless {
86 Document => $Document,
87 version => $version,
88 functions => $functions,
89 }, $class;
90  
91 11 $self;
92 }
93  
94 4 ▶sub _Document { $_[0]->{Document} }
Subroutine _Document: called CC=1 SCAR=0.0
95  
96 =pod
97  
98 =head2 version
99  
100 The C<version> accessor returns the L<PPI::Normal> version used to create
101 the object.
102  
103 =cut
104  
105 5 ▶sub version { $_[0]->{version} }
Subroutine version: called CC=1 SCAR=0.0
Pod documented
106  
107 =pod
108  
109 =head2 functions
110  
111 The C<functions> accessor returns a reference to an array of the
112 normalization functions (in order) that were called when creating
113 the object.
114  
115 =cut
116  
117 5 ▶sub functions { $_[0]->{functions} }
Subroutine functions: called CC=1 SCAR=0.0
Pod documented
118  
119  
120  
121  
122  
123 #####################################################################
124 # Comparison Methods
125  
126 =pod
127  
128 =head2 equal $Normalized
129  
130 The C<equal> method is the primary comparison method, taking another
131 PPI::Document::Normalized object, and checking for equivalence to it.
132  
133 The C<==> operator is also overload to this method, so that you can
134 do something like the following:
135  
136 my $first = PPI::Document->load('first.pl');
137 my $second = PPI::Document->load('second.pl');
138
139 if ( $first->normalized == $second->normalized ) {
140 print "The two documents are equivalent";
141 }
142  
143 Returns true if the normalized documents are equivalent, false if not,
144 or C<undef> if there is an error.
145  
146 =cut
147  
148 sub equal {
149 2 ▶ my $self = shift;
Subroutine equal: called CC=5 SCAR=16.7
Pod documented
150 2 ▶ my $other = _INSTANCE(shift, 'PPI::Document::Normalized') or return undef;
Branch: 1 branch
BranchTrueFalseTotal
my $other = &_INSTANCE(shift(), "PPI::Document::Normalized") or return undef 0 2 2
151  
152 # Prevent multiple concurrent runs
153 2 ▶ return undef if $self->{processing};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"processing"} 0 2 2
154  
155 # Check the version and function list first
156 2 ▶ return '' unless $self->version eq $other->version;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->version eq $other->version 0 2 2
157 2 ▶ $self->_equal_ARRAY( $self->functions, $other->functions ) or return '';
Branch: 1 branch
BranchTrueFalseTotal
$self->_equal_ARRAY($self->functions, $other->functions) or return "" 0 2 2
158  
159 # Do the main comparison run
160 2 $self->{seen} = {};
161 2 my $rv = $self->_equal_blessed( $self->_Document, $other->_Document );
162 2 delete $self->{seen};
163  
164 2 $rv;
165 }
166  
167 # Check that two objects are matched
168 sub _equal_blessed {
169 9 ▶ my ($self, $this, $that) = @_;
Subroutine _equal_blessed: called CC=4 SCAR=14.5
170 9 my ($bthis, $bthat) = (blessed $this, blessed $that);
171 9 ▶ $bthis and $bthat and $bthis eq $bthat or return '';
Branch: 1 branch
BranchTrueFalseTotal
$bthis and $bthat and $bthis eq $bthat or return "" 1 8 9
Condition50%3/6
$bthis and $bthat and $bthis eq $bthat
!ll&&!rl&&r
018
$bthis and $bthat
!ll&&!rl&&r
009
MC/DC33%1/3
$bthis and $bthat and $bthis eq $bthat
$bthis $bthat $bthis eq $bthat
Truth table50%2/4
A and B and C
A= $bthisB= $bthatC= $bthis eq $bthat
ABCresult
0XX0
10X0
1100
1111
172  
173 # Check the object as a reference
174 8 $self->_equal_reference( $this, $that );
175 }
176  
177 # Check that two references match their types
178 sub _equal_reference {
179 11 ▶ my ($self, $this, $that) = @_;
Subroutine _equal_reference: called CC=8 SCAR=24.6
180 11 my ($rthis, $rthat) = (refaddr $this, refaddr $that);
181 11 ▶ $rthis and $rthat or return undef;
Branch: 1 branch
BranchTrueFalseTotal
$rthis and $rthat or return undef 0 11 11
Condition33%1/3
$rthis and $rthat
!ll&&!rl&&r
0011
MC/DC0%0/2
$rthis and $rthat
$rthis $rthat
Truth table33%1/3
A and B
A= $rthisB= $rthat
ABresult
0X0
100
111
182  
183 # If we have seen this before, are the pointing
184 # is it the same one we saw in both sides
185 11 my $seen = $self->{seen}->{$rthis};
186 11 ▶ if ( $seen and $seen ne $rthat ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($seen and $seen ne $rthat) 0 11 11
Condition33%1/3
$seen and $seen ne $rthat
!ll&&!rl&&r
1100
MC/DC0%0/2
$seen and $seen ne $rthat
$seen $seen ne $rthat
Truth table33%1/3
A and B
A= $seenB= $seen ne $rthat
ABresult
0X0
100
111
187 0 return '';
188 }
189  
190 # Check the reference types
191 11 my ($tthis, $tthat) = (reftype $this, reftype $that);
192 11 ▶ $tthis and $tthat and $tthis eq $tthat or return undef;
Branch: 1 branch
BranchTrueFalseTotal
$tthis and $tthat and $tthis eq $tthat or return undef 0 11 11
Condition33%2/6
$tthis and $tthat and $tthis eq $tthat
!ll&&!rl&&r
0011
$tthis and $tthat
!ll&&!rl&&r
0011
MC/DC0%0/3
$tthis and $tthat and $tthis eq $tthat
$tthis $tthat $tthis eq $tthat
Truth table25%1/4
A and B and C
A= $tthisB= $tthatC= $tthis eq $tthat
ABCresult
0XX0
10X0
1100
1111
193  
194 # Check the children of the reference type
195 11 $self->{seen}->{$rthis} = $rthat;
196 11 my $method = "_equal_$tthat";
197 11 my $rv = $self->$method( $this, $that );
198 11 delete $self->{seen}->{$rthis};
199 11 $rv;
200 }
201  
202 # Compare the children of two SCALAR references
203 sub _equal_SCALAR {
204 0 ▶ my ($self, $this, $that) = @_;
Subroutine _equal_SCALAR: not called CC=5 SCAR=34.0
205 0 my ($cthis, $cthat) = ($$this, $$that);
206 0 ▶ return $self->_equal_blessed( $cthis, $cthat ) if blessed $cthis;
Branch: 1 branch
BranchTrueFalseTotal
if builtin::blessed($cthis) 0 0 0
207 0 ▶ return $self->_equal_reference( $cthis, $cthat ) if ref $cthis;
Branch: 1 branch
BranchTrueFalseTotal
if ref $cthis 0 0 0
208 0 ▶ return (defined $cthat and $cthis eq $cthat) if defined $cthis;
Branch: 1 branch
BranchTrueFalseTotal
if defined $cthis 0 0 0
Condition0%0/3
defined $cthat && $cthis eq $cthat
!ll&&!rl&&r
000
MC/DC0%0/2
defined $cthat && $cthis eq $cthat
defined $cthat $cthis eq $cthat
Truth table0%0/3
A && B
A= defined $cthatB= $cthis eq $cthat
ABresult
0X0
100
111
209 0 ! defined $cthat;
210 }
211  
212 # For completeness sake, lets just treat REF as a specialist SCALAR case
213 0 ▶sub _equal_REF { shift->_equal_SCALAR(@_) }
Subroutine _equal_REF: not called CC=1 SCAR=6.9
214  
215 # Compare the children of two ARRAY references
216 sub _equal_ARRAY {
217 5 ▶ my ($self, $this, $that) = @_;
Subroutine _equal_ARRAY: called CC=11 SCAR=26.8
218  
219 # Compare the number of elements
220 5 ▶ scalar(@$this) == scalar(@$that) or return '';
Branch: 1 branch
BranchTrueFalseTotal
scalar @$this == scalar @$that or return "" 0 5 5
221  
222 # Check each element in the array.
223 # Descend depth-first.
224 5 foreach my $i ( 0 .. scalar(@$this) ) {
225 15 my ($cthis, $cthat) = ($this->[$i], $that->[$i]);
226 15 ▶ if ( blessed $cthis ) {
Branch: 3 branches
BranchTrueFalseTotal
if (builtin::blessed($cthis)) { } 7 8 15
elsif (ref $cthis) { } 0 8 8
elsif (defined $cthis) { } 4 4 8
227 7 ▶ return '' unless $self->_equal_blessed( $cthis, $cthat );
Branch: 1 branch
BranchTrueFalseTotal
unless $self->_equal_blessed($cthis, $cthat) 1 6 7
228 } elsif ( ref $cthis ) {
229 0 ▶ return '' unless $self->_equal_reference( $cthis, $cthat );
Branch: 1 branch
BranchTrueFalseTotal
unless $self->_equal_reference($cthis, $cthat) 0 0 0
230 } elsif ( defined $cthis ) {
231 4 ▶ return '' unless (defined $cthat and $cthis eq $cthat);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $cthat and $cthis eq $cthat 0 4 4
Condition33%1/3
defined $cthat and $cthis eq $cthat
!ll&&!rl&&r
004
MC/DC0%0/2
defined $cthat and $cthis eq $cthat
defined $cthat $cthis eq $cthat
Truth table33%1/3
A and B
A= defined $cthatB= $cthis eq $cthat
ABresult
0X0
100
111
232 } else {
233 4 ▶ return '' if defined $cthat;
Branch: 1 branch
BranchTrueFalseTotal
if defined $cthat 0 4 4
234 }
235 }
236  
237 4 1;
238 }
239  
240 # Compare the children of a HASH reference
241 sub _equal_HASH {
242 8 ▶ my ($self, $this, $that) = @_;
Subroutine _equal_HASH: called CC=12 SCAR=30.8
243  
244 # Compare the number of keys
245 8 ▶ return '' unless scalar(keys %$this) == scalar(keys %$that);
Branch: 1 branch
BranchTrueFalseTotal
unless scalar keys %$this == scalar keys %$that 0 8 8
246  
247 # Compare each key, descending depth-first.
248 8 foreach my $k ( keys %$this ) {
249 10 ▶ return '' unless exists $that->{$k};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $that->{$k} 0 10 10
250 10 my ($cthis, $cthat) = ($this->{$k}, $that->{$k});
251 10 ▶ if ( blessed $cthis ) {
Branch: 3 branches
BranchTrueFalseTotal
if (builtin::blessed($cthis)) { } 0 10 10
elsif (ref $cthis) { } 3 7 10
elsif (defined $cthis) { } 7 0 7
252 0 ▶ return '' unless $self->_equal_blessed( $cthis, $cthat );
Branch: 1 branch
BranchTrueFalseTotal
unless $self->_equal_blessed($cthis, $cthat) 0 0 0
253 } elsif ( ref $cthis ) {
254 3 ▶ return '' unless $self->_equal_reference( $cthis, $cthat );
Branch: 1 branch
BranchTrueFalseTotal
unless $self->_equal_reference($cthis, $cthat) 1 2 3
255 } elsif ( defined $cthis ) {
256 7 ▶ return '' unless (defined $cthat and $cthis eq $cthat);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $cthat and $cthis eq $cthat 0 7 7
Condition33%1/3
defined $cthat and $cthis eq $cthat
!ll&&!rl&&r
007
MC/DC0%0/2
defined $cthat and $cthis eq $cthat
defined $cthat $cthis eq $cthat
Truth table33%1/3
A and B
A= defined $cthatB= $cthis eq $cthat
ABresult
0X0
100
111
257 } else {
258 0 ▶ return '' if defined $cthat;
Branch: 1 branch
BranchTrueFalseTotal
if defined $cthat 0 0 0
259 }
260 }
261  
262 7 1;
263 }
264  
265 # We do not support GLOB comparisons
266 sub _equal_GLOB {
267 0 ▶ my ($self, $this, $that) = @_;
Subroutine _equal_GLOB: not called CC=1 SCAR=6.9
268 0 warn('GLOB comparisons are not supported');
269 0 '';
270 }
271  
272 # We do not support CODE comparisons
273 sub _equal_CODE {
274 0 ▶ my ($self, $this, $that) = @_;
Subroutine _equal_CODE: not called CC=1 SCAR=6.9
275 0 refaddr $this == refaddr $that;
276 }
277  
278 # We don't support IO comparisons
279 sub _equal_IO {
280 0 ▶ my ($self, $this, $that) = @_;
Subroutine _equal_IO: not called CC=1 SCAR=6.9
281 0 warn('IO comparisons are not supported');
282 0 '';
283 }
284  
285 sub DESTROY {
286 # Take the screw up Document with us
287 11 ▶ if ( $_[0]->{Document} ) {
Subroutine DESTROY: called CC=2 SCAR=7.1
Branch: 1 branch
BranchTrueFalseTotal
if ($_[0]{"Document"}) 11 0 11
288 11 $_[0]->{Document}->DESTROY;
289 11 delete $_[0]->{Document};
290 }
291 }
292  
293 62 1;
294  
295 =pod
296  
297 =head1 SUPPORT
298  
299 See the L<support section|PPI/SUPPORT> in the main module.
300  
301 =head1 AUTHOR
302  
303 Adam Kennedy E<lt>adamk@cpan.orgE<gt>
304  
305 =head1 COPYRIGHT
306  
307 Copyright 2005 - 2011 Adam Kennedy.
308  
309 This program is free software; you can redistribute
310 it and/or modify it under the same terms as Perl itself.
311  
312 The full text of the license can be found in the
313 LICENSE file included with this module.
314  
315 =cut
316
« PPI/Structure/List.pm ↑ Summary PPI/Document.pm »