PPI/Document/Normalized.pm

filter: Statementstmt 81.4% 79 / 97 Branchbran 53.2% 33 / 62 Conditioncond 35.4% 11 / 31 MC/DCmcdc 5.5% 1 / 18 Subroutinesub 76.1% 16 / 21 Podpod 100% 4 / 4 totaltotal 61.8% 144 / 233 CC 45 SCAR 23.8
CC
45
Cov
65%
_equal_SCALAR
30.0
_equal_HASH
16.4
_equal_ARRAY
14.6
CRAP 10.8
« PPI/Token/Number.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 63 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
44 63 ▶use Scalar::Util qw{refaddr reftype blessed};
Subroutine BEGIN: called CC=1 SCAR=0.0
45 63 ▶use Params::Util qw{_INSTANCE _ARRAY};
Subroutine BEGIN: called CC=1 SCAR=0.0
46 63 ▶use PPI::Util ();
Subroutine BEGIN: called CC=1 SCAR=0.0
47  
48 63 our $VERSION = '1.260'; # VERSION
49  
50 63 ▶use overload 'bool' => \&PPI::Util::TRUE;
Subroutine BEGIN: called CC=1 SCAR=0.0
51 63 ▶use overload '==' => 'equal';
Subroutine BEGIN: called CC=1 SCAR=0.0
52  
53  
54  
55  
56  
57  
58 #####################################################################
59 # Constructor and Accessors
60  
61 =pod
62  
63 =head2 new
64  
65 The C<new> method is intended for use only by the L<PPI::Normal> class,
66 and to get ::Normalized objects, you are highly recommended to use
67 either that module, or the C<normalized> method of the L<PPI::Document>
68 object itself.
69  
70 =cut
71  
72 sub new {
73 11 ▶ my $class = shift;
Subroutine new: called CC=3 SCAR=11.2
Pod documented
74 11 my %args = @_;
75  
76 # Check the required params
77 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
78 11 my $version = $args{version};
79 11 ▶ my $functions = _ARRAY($args{functions}) or return undef;
Branch: 1 branch
BranchTrueFalseTotal
my $functions = &_ARRAY($args{"functions"}) or return undef 0 11 11
80  
81 # Create the object
82 11 my $self = bless {
83 Document => $Document,
84 version => $version,
85 functions => $functions,
86 }, $class;
87  
88 11 $self;
89 }
90  
91 4 ▶sub _Document { $_[0]->{Document} }
Subroutine _Document: called CC=1 SCAR=0.0
92  
93 =pod
94  
95 =head2 version
96  
97 The C<version> accessor returns the L<PPI::Normal> version used to create
98 the object.
99  
100 =cut
101  
102 5 ▶sub version { $_[0]->{version} }
Subroutine version: called CC=1 SCAR=0.0
Pod documented
103  
104 =pod
105  
106 =head2 functions
107  
108 The C<functions> accessor returns a reference to an array of the
109 normalization functions (in order) that were called when creating
110 the object.
111  
112 =cut
113  
114 5 ▶sub functions { $_[0]->{functions} }
Subroutine functions: called CC=1 SCAR=0.0
Pod documented
115  
116  
117  
118  
119  
120 #####################################################################
121 # Comparison Methods
122  
123 =pod
124  
125 =head2 equal $Normalized
126  
127 The C<equal> method is the primary comparison method, taking another
128 PPI::Document::Normalized object, and checking for equivalence to it.
129  
130 The C<==> operator is also overload to this method, so that you can
131 do something like the following:
132  
133 my $first = PPI::Document->load('first.pl');
134 my $second = PPI::Document->load('second.pl');
135
136 if ( $first->normalized == $second->normalized ) {
137 print "The two documents are equivalent";
138 }
139  
140 Returns true if the normalized documents are equivalent, false if not,
141 or C<undef> if there is an error.
142  
143 =cut
144  
145 sub equal {
146 2 ▶ my $self = shift;
Subroutine equal: called CC=7 SCAR=20.6
Pod documented
147 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
148  
149 # Prevent multiple concurrent runs
150 2 ▶ return undef if $self->{processing};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"processing"} 0 2 2
151  
152 # Check the version and function list first
153 2 ▶ my $v1 = $self->version || "undef";
Condition50%1/2
$self->version || 'undef'
l!l
20
MC/DC0%0/1
$self->version || 'undef'
$self->version
Truth table50%1/2
A
A= $self->version
Aresult
11
00
154 2 ▶ my $v2 = $other->version || "undef";
Condition50%1/2
$other->version || 'undef'
l!l
20
MC/DC0%0/1
$other->version || 'undef'
$other->version
Truth table50%1/2
A
A= $other->version
Aresult
11
00
155 2 ▶ return '' if $v1 ne $v2;
Branch: 1 branch
BranchTrueFalseTotal
if $v1 ne $v2 0 2 2
156 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
157  
158 # Do the main comparison run
159 2 $self->{seen} = {};
160 2 my $rv = $self->_equal_blessed( $self->_Document, $other->_Document );
161 2 delete $self->{seen};
162  
163 2 $rv;
164 }
165  
166 # Check that two objects are matched
167 sub _equal_blessed {
168 9 ▶ my ($self, $this, $that) = @_;
Subroutine _equal_blessed: called CC=4 SCAR=14.5
169 9 my ($bthis, $bthat) = (blessed $this, blessed $that);
170 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
171  
172 # Check the object as a reference
173 8 $self->_equal_reference( $this, $that );
174 }
175  
176 # Check that two references match their types
177 sub _equal_reference {
178 11 ▶ my ($self, $this, $that) = @_;
Subroutine _equal_reference: called CC=8 SCAR=24.6
179 11 my ($rthis, $rthat) = (refaddr $this, refaddr $that);
180 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
181  
182 # If we have seen this before, are the pointing
183 # is it the same one we saw in both sides
184 11 my $seen = $self->{seen}->{$rthis};
185 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
186 0 return '';
187 }
188  
189 # Check the reference types
190 11 my ($tthis, $tthat) = (reftype $this, reftype $that);
191 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
192  
193 # Check the children of the reference type
194 11 $self->{seen}->{$rthis} = $rthat;
195 11 my $method = "_equal_$tthat";
196 11 my $rv = $self->$method( $this, $that );
197 11 delete $self->{seen}->{$rthis};
198 11 $rv;
199 }
200  
201 # Compare the children of two SCALAR references
202 sub _equal_SCALAR {
203 0 ▶ my ($self, $this, $that) = @_;
Subroutine _equal_SCALAR: not called CC=5 SCAR=34.0
204 0 my ($cthis, $cthat) = ($$this, $$that);
205 0 ▶ return $self->_equal_blessed( $cthis, $cthat ) if blessed $cthis;
Branch: 1 branch
BranchTrueFalseTotal
if builtin::blessed($cthis) 0 0 0
206 0 ▶ return $self->_equal_reference( $cthis, $cthat ) if ref $cthis;
Branch: 1 branch
BranchTrueFalseTotal
if ref $cthis 0 0 0
207 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
208 0 ! defined $cthat;
209 }
210  
211 # For completeness sake, lets just treat REF as a specialist SCALAR case
212 0 ▶sub _equal_REF { shift->_equal_SCALAR(@_) }
Subroutine _equal_REF: not called CC=1 SCAR=6.9
213  
214 # Compare the children of two ARRAY references
215 sub _equal_ARRAY {
216 5 ▶ my ($self, $this, $that) = @_;
Subroutine _equal_ARRAY: called CC=11 SCAR=26.8
217  
218 # Compare the number of elements
219 5 ▶ scalar(@$this) == scalar(@$that) or return '';
Branch: 1 branch
BranchTrueFalseTotal
scalar @$this == scalar @$that or return "" 0 5 5
220  
221 # Check each element in the array.
222 # Descend depth-first.
223 5 foreach my $i ( 0 .. scalar(@$this) ) {
224 15 my ($cthis, $cthat) = ($this->[$i], $that->[$i]);
225 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
226 7 ▶ return '' unless $self->_equal_blessed( $cthis, $cthat );
Branch: 1 branch
BranchTrueFalseTotal
unless $self->_equal_blessed($cthis, $cthat) 1 6 7
227 } elsif ( ref $cthis ) {
228 0 ▶ return '' unless $self->_equal_reference( $cthis, $cthat );
Branch: 1 branch
BranchTrueFalseTotal
unless $self->_equal_reference($cthis, $cthat) 0 0 0
229 } elsif ( defined $cthis ) {
230 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
231 } else {
232 4 ▶ return '' if defined $cthat;
Branch: 1 branch
BranchTrueFalseTotal
if defined $cthat 0 4 4
233 }
234 }
235  
236 4 1;
237 }
238  
239 # Compare the children of a HASH reference
240 sub _equal_HASH {
241 8 ▶ my ($self, $this, $that) = @_;
Subroutine _equal_HASH: called CC=12 SCAR=28.0
242  
243 # Compare the number of keys
244 8 ▶ return '' unless scalar(keys %$this) == scalar(keys %$that);
Branch: 1 branch
BranchTrueFalseTotal
unless scalar keys %$this == scalar keys %$that 0 8 8
245  
246 # Compare each key, descending depth-first.
247 8 foreach my $k ( keys %$this ) {
248 12 ▶ return '' unless exists $that->{$k};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $that->{$k} 0 12 12
249 12 my ($cthis, $cthat) = ($this->{$k}, $that->{$k});
250 12 ▶ if ( blessed $cthis ) {
Branch: 3 branches
BranchTrueFalseTotal
if (builtin::blessed($cthis)) { } 0 12 12
elsif (ref $cthis) { } 3 9 12
elsif (defined $cthis) { } 7 2 9
251 0 ▶ return '' unless $self->_equal_blessed( $cthis, $cthat );
Branch: 1 branch
BranchTrueFalseTotal
unless $self->_equal_blessed($cthis, $cthat) 0 0 0
252 } elsif ( ref $cthis ) {
253 3 ▶ return '' unless $self->_equal_reference( $cthis, $cthat );
Branch: 1 branch
BranchTrueFalseTotal
unless $self->_equal_reference($cthis, $cthat) 1 2 3
254 } elsif ( defined $cthis ) {
255 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
256 } else {
257 2 ▶ return '' if defined $cthat;
Branch: 1 branch
BranchTrueFalseTotal
if defined $cthat 0 2 2
258 }
259 }
260  
261 7 1;
262 }
263  
264 # We do not support GLOB comparisons
265 sub _equal_GLOB {
266 0 ▶ my ($self, $this, $that) = @_;
Subroutine _equal_GLOB: not called CC=1 SCAR=6.9
267 0 warn('GLOB comparisons are not supported');
268 0 '';
269 }
270  
271 # We do not support CODE comparisons
272 sub _equal_CODE {
273 0 ▶ my ($self, $this, $that) = @_;
Subroutine _equal_CODE: not called CC=1 SCAR=6.9
274 0 refaddr $this == refaddr $that;
275 }
276  
277 # We don't support IO comparisons
278 sub _equal_IO {
279 0 ▶ my ($self, $this, $that) = @_;
Subroutine _equal_IO: not called CC=1 SCAR=6.9
280 0 warn('IO comparisons are not supported');
281 0 '';
282 }
283  
284 sub DESTROY {
285 # Take the screw up Document with us
286 11 ▶ if ( $_[0]->{Document} ) {
Subroutine DESTROY: called CC=2 SCAR=7.1
Branch: 1 branch
BranchTrueFalseTotal
if ($_[0]{"Document"}) 11 0 11
287 11 $_[0]->{Document}->DESTROY;
288 11 delete $_[0]->{Document};
289 }
290 }
291  
292 63 1;
293  
294 =pod
295  
296 =head1 SUPPORT
297  
298 See the L<support section|PPI/SUPPORT> in the main module.
299  
300 =head1 AUTHOR
301  
302 Adam Kennedy E<lt>adamk@cpan.orgE<gt>
303  
304 =head1 COPYRIGHT
305  
306 Copyright 2005 - 2011 Adam Kennedy.
307  
308 This program is free software; you can redistribute
309 it and/or modify it under the same terms as Perl itself.
310  
311 The full text of the license can be found in the
312 LICENSE file included with this module.
313  
314 =cut
315
« PPI/Token/Number.pm ↑ Summary PPI/Document.pm »