PPI/Node.pm

filter: Statementstmt 86.9% 214 / 246 Branchbran 77.6% 104 / 134 Conditioncond 75.0% 9 / 12 MC/DCmcdc 62.5% 5 / 8 Subroutinesub 87.5% 35 / 40 Podpod 94.7% 18 / 19 totaltotal 83.8% 385 / 459 CC 77 SCAR 21.0
CC
77
Cov
83%
_wanted
25.8
find
11.1
find_first
11.1
CRAP 8.2
« PPI/Tokenizer.pm ↑ Summary PPI/Token/Number/Exp.pm »
1 package PPI::Node;
2  
3 =pod
4  
5 =head1 NAME
6  
7 PPI::Node - Abstract PPI Node class, an Element that can contain other Elements
8  
9 =head1 INHERITANCE
10  
11 PPI::Node
12 isa PPI::Element
13  
14 =head1 SYNOPSIS
15  
16 # Create a typical node (a Document in this case)
17 my $Node = PPI::Document->new;
18
19 # Add an element to the node( in this case, a token )
20 my $Token = PPI::Token::Word->new('my');
21 $Node->add_element( $Token );
22
23 # Get the elements for the Node
24 my @elements = $Node->children;
25
26 # Find all the barewords within a Node
27 my $barewords = $Node->find( 'PPI::Token::Word' );
28
29 # Find by more complex criteria
30 my $my_tokens = $Node->find( sub { $_[1]->content eq 'my' } );
31
32 # Remove all the whitespace
33 $Node->prune( 'PPI::Token::Whitespace' );
34
35 # Remove by more complex criteria
36 $Node->prune( sub { $_[1]->content eq 'my' } );
37  
38 =head1 DESCRIPTION
39  
40 The C<PPI::Node> class provides an abstract base class for the Element
41 classes that are able to contain other elements L<PPI::Document>,
42 L<PPI::Statement>, and L<PPI::Structure>.
43  
44 As well as those listed below, all of the methods that apply to
45 L<PPI::Element> objects also apply to C<PPI::Node> objects.
46  
47 =head1 METHODS
48  
49 =cut
50  
51 62 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
52 62 ▶use Carp ();
Subroutine BEGIN: called CC=1 SCAR=0.0
53 62 ▶use Scalar::Util qw{refaddr};
Subroutine BEGIN: called CC=1 SCAR=0.0
54 62 ▶use List::MoreUtils ();
Subroutine BEGIN: called CC=1 SCAR=0.0
55 62 ▶use Params::Util qw{_INSTANCE _CLASS _CODELIKE _NUMBER};
Subroutine BEGIN: called CC=1 SCAR=0.0
56 62 ▶use PPI::Element ();
Subroutine BEGIN: called CC=1 SCAR=0.0
57  
58 62 ▶use vars qw{$VERSION @ISA *_PARENT};
Subroutine BEGIN: called CC=1 SCAR=0.0
59 BEGIN {
60 62 ▶ $VERSION = '1.236';
Subroutine BEGIN: called CC=1 SCAR=0.0
61 62 @ISA = 'PPI::Element';
62 62 *_PARENT = *PPI::Element::_PARENT;
63 }
64  
65  
66  
67  
68  
69 #####################################################################
70 # The basic constructor
71  
72 sub new {
73 15660 ▶ my $class = ref $_[0] || $_[0];
Subroutine new: called CC=2 SCAR=8.1
Pod undocumented
Condition33%1/3
ref $_[0] || $_[0]
l!l&&r!l&&!r
0156600
MC/DC0%0/2
ref $_[0] || $_[0]
ref $_[0] $_[0]
Truth table33%1/3
A || B
A= ref $_[0]B= $_[0]
ABresult
1X1
011
000
74 15660 bless { children => [] }, $class;
75 }
76  
77  
78  
79  
80  
81 #####################################################################
82 # PDOM Methods
83  
84 =pod
85  
86 =head2 scope
87  
88 The C<scope> method returns true if the node represents a lexical scope
89 boundary, or false if it does not.
90  
91 =cut
92  
93 ### XS -> PPI/XS.xs:_PPI_Node__scope 0.903+
94 sub scope() { '' }
95  
96 =pod
97  
98 =head2 add_element $Element
99  
100 The C<add_element> method adds a L<PPI::Element> object to the end of a
101 C<PPI::Node>. Because Elements maintain links to their parent, an
102 Element can only be added to a single Node.
103  
104 Returns true if the L<PPI::Element> was added. Returns C<undef> if the
105 Element was already within another Node, or the method is not passed
106 a L<PPI::Element> object.
107  
108 =cut
109  
110 sub add_element {
111 1 ▶ my $self = shift;
Subroutine add_element: called CC=3 SCAR=11.2
Pod documented
112  
113 # Check the element
114 1 ▶ my $Element = _INSTANCE(shift, 'PPI::Element') or return undef;
Branch: 1 branch
BranchTrueFalseTotal
my $Element = &_INSTANCE(shift(), "PPI::Element") or return undef 0 1 1
115 1 ▶ $_PARENT{refaddr $Element} and return undef;
Branch: 1 branch
BranchTrueFalseTotal
$PPI::Node::_PARENT{builtin::refaddr($Element)} and return undef 0 1 1
116  
117 # Add the argument to the elements
118 1 push @{$self->{children}}, $Element;
119 Scalar::Util::weaken(
120 1 $_PARENT{refaddr $Element} = $self
121 );
122  
123 1 1;
124 }
125  
126 # In a typical run profile, add_element is the number 1 resource drain.
127 # This is a highly optimised unsafe version, for internal use only.
128 sub __add_element {
129 Scalar::Util::weaken(
130 0 ▶ $_PARENT{refaddr $_[1]} = $_[0]
Subroutine __add_element: not called CC=1 SCAR=6.9
131 );
132 0 push @{$_[0]->{children}}, $_[1];
133 }
134  
135 =pod
136  
137 =head2 elements
138  
139 The C<elements> method accesses all child elements B<structurally> within
140 the C<PPI::Node> object. Note that in the base of the L<PPI::Structure>
141 classes, this C<DOES> include the brace tokens at either end of the
142 structure.
143  
144 Returns a list of zero or more L<PPI::Element> objects.
145  
146 Alternatively, if called in the scalar context, the C<elements> method
147 returns a count of the number of elements.
148  
149 =cut
150  
151 sub elements {
152 2 ▶ if ( wantarray ) {
Subroutine elements: called CC=2 SCAR=8.4
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
if (wantarray) { } 2 0 2
153 2 return @{$_[0]->{children}};
154 } else {
155 0 return scalar @{$_[0]->{children}};
156 }
157 }
158  
159 =pod
160  
161 =head2 first_element
162  
163 The C<first_element> method accesses the first element structurally within
164 the C<PPI::Node> object. As for the C<elements> method, this does include
165 the brace tokens for L<PPI::Structure> objects.
166  
167 Returns a L<PPI::Element> object, or C<undef> if for some reason the
168 C<PPI::Node> object does not contain any elements.
169  
170 =cut
171  
172 # Normally the first element is also the first child
173 sub first_element {
174 15 ▶ $_[0]->{children}->[0];
Subroutine first_element: called CC=1 SCAR=0.0
Pod documented
175 }
176  
177 =pod
178  
179 =head2 last_element
180  
181 The C<last_element> method accesses the last element structurally within
182 the C<PPI::Node> object. As for the C<elements> method, this does include
183 the brace tokens for L<PPI::Structure> objects.
184  
185 Returns a L<PPI::Element> object, or C<undef> if for some reason the
186 C<PPI::Node> object does not contain any elements.
187  
188 =cut
189  
190 # Normally the last element is also the last child
191 sub last_element {
192 11 ▶ $_[0]->{children}->[-1];
Subroutine last_element: called CC=1 SCAR=0.0
Pod documented
193 }
194  
195 =pod
196  
197 =head2 children
198  
199 The C<children> method accesses all child elements lexically within the
200 C<PPI::Node> object. Note that in the case of the L<PPI::Structure>
201 classes, this does B<NOT> include the brace tokens at either end of the
202 structure.
203  
204 Returns a list of zero of more L<PPI::Element> objects.
205  
206 Alternatively, if called in the scalar context, the C<children> method
207 returns a count of the number of lexical children.
208  
209 =cut
210  
211 # In the default case, this is the same as for the elements method
212 sub children {
213 1986 ▶ wantarray ? @{$_[0]->{children}} : scalar @{$_[0]->{children}};
Subroutine children: called CC=2 SCAR=8.1
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 1986 0 1986
214 }
215  
216 =pod
217  
218 =head2 schildren
219  
220 The C<schildren> method is really just a convenience, the significant-only
221 variation of the normal C<children> method.
222  
223 In list context, returns a list of significant children. In scalar context,
224 returns the number of significant children.
225  
226 =cut
227  
228 sub schildren {
229 60714 ▶ return grep { $_->significant } @{$_[0]->{children}} if wantarray;
Subroutine schildren: called CC=4 SCAR=13.9
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
if wantarray 30231 30483 60714
230 30483 my $count = 0;
231 30483 foreach ( @{$_[0]->{children}} ) {
232 105843 ▶ $count++ if $_->significant;
Branch: 1 branch
BranchTrueFalseTotal
if $_->significant 68623 37220 105843
233 }
234 30483 return $count;
235 }
236  
237 =pod
238  
239 =head2 child $index
240  
241 The C<child> method accesses a child L<PPI::Element> object by its
242 position within the Node.
243  
244 Returns a L<PPI::Element> object, or C<undef> if there is no child
245 element at that node.
246  
247 =cut
248  
249 sub child {
250 856 ▶ my ( $self, $index ) = @_;
Subroutine child: called CC=2 SCAR=6.9
Pod documented
251 856 ▶ PPI::Exception->throw( "method child() needs an index" )
Branch: 1 branch
BranchTrueFalseTotal
unless defined &_NUMBER($index) 2 854 856
252 if not defined _NUMBER $index;
253 854 $self->{children}->[$index];
254 }
255  
256 =pod
257  
258 =head2 schild $index
259  
260 The lexical structure of the Perl language ignores 'insignificant' items,
261 such as whitespace and comments, while L<PPI> treats these items as valid
262 tokens so that it can reassemble the file at any time. Because of this,
263 in many situations there is a need to find an Element within a Node by
264 index, only counting lexically significant Elements.
265  
266 The C<schild> method returns a child Element by index, ignoring
267 insignificant Elements. The index of a child Element is specified in the
268 same way as for a normal array, with the first Element at index 0, and
269 negative indexes used to identify a "from the end" position.
270  
271 =cut
272  
273 sub schild {
274 133833 ▶ my $self = shift;
Subroutine schild: called CC=8 SCAR=20.8
Pod documented
275 133833 my $idx = 0 + shift;
276 133833 my $el = $self->{children};
277 133833 ▶ if ( $idx < 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($idx < 0) { } 22916 110917 133833
278 22916 my $cursor = 0;
279 22916 ▶ while ( exists $el->[--$cursor] ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $$el[--$cursor]) 23672 1412 25084
280 23672 ▶ return $el->[$cursor] if $el->[$cursor]->significant and ++$idx >= 0;
Branch: 1 branch
BranchTrueFalseTotal
if $el->[$cursor]->significant and ++$idx >= 0 21504 2168 23672
Condition100%3/3
$el->[$cursor]->significant and ++$idx >= 0
!ll&&!rl&&r
1014115421504
MC/DC100%2/2
$el->[$cursor]->significant and ++$idx >= 0
$el->[$cursor]->significant ++$idx >= 0
Truth table100%3/3
A and B
A= $el->[$cursor]->significantB= ++$idx >= 0
ABresult
0X0
100
111
281 }
282 } else {
283 110917 my $cursor = -1;
284 110917 ▶ while ( exists $el->[++$cursor] ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $$el[++$cursor]) 149094 1772 150866
285 149094 ▶ return $el->[$cursor] if $el->[$cursor]->significant and --$idx < 0;
Branch: 1 branch
BranchTrueFalseTotal
if $el->[$cursor]->significant and --$idx < 0 109145 39949 149094
Condition100%3/3
$el->[$cursor]->significant and --$idx < 0
!ll&&!rl&&r
2115118798109145
MC/DC100%2/2
$el->[$cursor]->significant and --$idx < 0
$el->[$cursor]->significant --$idx < 0
Truth table100%3/3
A and B
A= $el->[$cursor]->significantB= --$idx < 0
ABresult
0X0
100
111
286 }
287 }
288 3184 undef;
289 }
290  
291 =pod
292  
293 =head2 contains $Element
294  
295 The C<contains> method is used to determine if another L<PPI::Element>
296 object is logically "within" a C<PPI::Node>. For the special case of the
297 brace tokens at either side of a L<PPI::Structure> object, they are
298 generally considered "within" a L<PPI::Structure> object, even if they are
299 not actually in the elements for the L<PPI::Structure>.
300  
301 Returns true if the L<PPI::Element> is within us, false if not, or C<undef>
302 on error.
303  
304 =cut
305  
306 sub contains {
307 34 ▶ my $self = shift;
Subroutine contains: called CC=4 SCAR=14.1
Pod documented
308 34 ▶ my $Element = _INSTANCE(shift, 'PPI::Element') or return undef;
Branch: 1 branch
BranchTrueFalseTotal
my $Element = &_INSTANCE(shift(), "PPI::Element") or return undef 5 29 34
309  
310 # Iterate up the Element's parent chain until we either run out
311 # of parents, or get to ourself.
312 29 ▶ while ( $Element = $Element->parent ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($Element = $Element->parent) 79 0 79
313 79 ▶ return 1 if refaddr($self) == refaddr($Element);
Branch: 1 branch
BranchTrueFalseTotal
if builtin::refaddr($self) == builtin::refaddr($Element) 29 50 79
314 }
315  
316 0 '';
317 }
318  
319 =pod
320  
321 =head2 find $class | \&wanted
322  
323 The C<find> method is used to search within a code tree for
324 L<PPI::Element> objects that meet a particular condition.
325  
326 To specify the condition, the method can be provided with either a simple
327 class name (full or shortened), or a C<CODE>/function reference.
328  
329 # Find all single quotes in a Document (which is a Node)
330 $Document->find('PPI::Quote::Single');
331
332 # The same thing with a shortened class name
333 $Document->find('Quote::Single');
334
335 # Anything more elaborate, we go with the sub
336 $Document->find( sub {
337 # At the top level of the file...
338 $_[1]->parent == $_[0]
339 and (
340 # ...find all comments and POD
341 $_[1]->isa('PPI::Token::Pod')
342 or
343 $_[1]->isa('PPI::Token::Comment')
344 )
345 } );
346  
347 The function will be passed two arguments, the top-level C<PPI::Node>
348 you are searching in and the current L<PPI::Element> that the condition
349 is testing.
350  
351 The anonymous function should return one of three values. Returning true
352 indicates a condition match, defined-false (C<0> or C<''>) indicates
353 no-match, and C<undef> indicates no-match and no-descend.
354  
355 In the last case, the tree walker will skip over anything below the
356 C<undef>-returning element and move on to the next element at the same
357 level.
358  
359 To halt the entire search and return C<undef> immediately, a condition
360 function should throw an exception (i.e. C<die>).
361  
362 Note that this same wanted logic is used for all methods documented to
363 have a C<\&wanted> parameter, as this one does.
364  
365 The C<find> method returns a reference to an array of L<PPI::Element>
366 objects that match the condition, false (but defined) if no Elements match
367 the condition, or C<undef> if you provide a bad condition, or an error
368 occurs during the search process.
369  
370 In the case of a bad condition, a warning will be emitted as well.
371  
372 =cut
373  
374 sub find {
375 5120 ▶ my $self = shift;
Subroutine find: called CC=11 SCAR=24.1
Pod documented
376 5120 ▶ my $wanted = $self->_wanted(shift) or return undef;
Branch: 1 branch
BranchTrueFalseTotal
my $wanted = $self->_wanted(shift()) or return undef 3 5117 5120
377  
378 # Use a queue based search, rather than a recursive one
379 5117 my @found;
380 5117 my @queue = @{$self->{children}};
381 5117 my $ok = eval {
382 5117 ▶ while ( @queue ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@queue) 336345 5117 341462
383 336345 my $Element = shift @queue;
384 336345 my $rv = &$wanted( $self, $Element );
385 336345 ▶ push @found, $Element if $rv;
Branch: 1 branch
BranchTrueFalseTotal
if $rv 16677 319668 336345
386  
387 # Support "don't descend on undef return"
388 336345 ▶ next unless defined $rv;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $rv 0 336345 336345
389  
390 # Skip if the Element doesn't have any children
391 336345 ▶ next unless $Element->isa('PPI::Node');
Branch: 1 branch
BranchTrueFalseTotal
unless $Element->isa("PPI::Node") 278553 57792 336345
392  
393 # Depth-first keeps the queue size down and provides a
394 # better logical order.
395 57792 ▶ if ( $Element->isa('PPI::Structure') ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($Element->isa('PPI::Structure')) { } 21124 36668 57792
396 21124 ▶ unshift @queue, $Element->finish if $Element->finish;
Branch: 1 branch
BranchTrueFalseTotal
if $Element->finish 21117 7 21124
397 21124 unshift @queue, @{$Element->{children}};
398 21124 ▶ unshift @queue, $Element->start if $Element->start;
Branch: 1 branch
BranchTrueFalseTotal
if $Element->start 21124 0 21124
399 } else {
400 36668 unshift @queue, @{$Element->{children}};
401 }
402 }
403 5117 1;
404 };
405 5117 ▶ if ( !$ok ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($ok) 0 5117 5117
406 # Caught exception thrown from the wanted function
407 0 return undef;
408 }
409  
410 5117 ▶ @found ? \@found : '';
Branch: 1 branch
BranchTrueFalseTotal
@found ? : 4323 794 5117
411 }
412  
413 =pod
414  
415 =head2 find_first $class | \&wanted
416  
417 If the normal C<find> method is like a grep, then C<find_first> is
418 equivalent to the L<List::Util> C<first> function.
419  
420 Given an element class or a wanted function, it will search depth-first
421 through a tree until it finds something that matches the condition,
422 returning the first Element that it encounters.
423  
424 See the C<find> method for details on the format of the search condition.
425  
426 Returns the first L<PPI::Element> object that matches the condition, false
427 if nothing matches the condition, or C<undef> if given an invalid condition,
428 or an error occurs.
429  
430 =cut
431  
432 sub find_first {
433 2064 ▶ my $self = shift;
Subroutine find_first: called CC=11 SCAR=24.1
Pod documented
434 2064 ▶ my $wanted = $self->_wanted(shift) or return undef;
Branch: 1 branch
BranchTrueFalseTotal
my $wanted = $self->_wanted(shift()) or return undef 0 2064 2064
435  
436 # Use the same queue-based search as for ->find
437 2064 my @queue = @{$self->{children}};
438 2064 my $rv;
439 2064 my $ok = eval {
440 # The defined() here prevents a ton of calls to PPI::Util::TRUE
441 2064 ▶ while ( @queue ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@queue) 701514 2029 703543
442 701514 my $Element = shift @queue;
443 701514 my $element_rv = $wanted->( $self, $Element );
444 701514 ▶ if ( $element_rv ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($element_rv) 35 701479 701514
445 35 $rv = $Element;
446 35 last;
447 }
448  
449 # Support "don't descend on undef return"
450 701479 ▶ next if !defined $element_rv;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $element_rv 21729 679750 701479
451  
452 # Skip if the Element doesn't have any children
453 679750 ▶ next if !$Element->isa('PPI::Node');
Branch: 1 branch
BranchTrueFalseTotal
unless $Element->isa("PPI::Node") 571771 107979 679750
454  
455 # Depth-first keeps the queue size down and provides a
456 # better logical order.
457 107979 ▶ if ( $Element->isa('PPI::Structure') ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($Element->isa('PPI::Structure')) { } 40816 67163 107979
458 40816 ▶ unshift @queue, $Element->finish if defined($Element->finish);
Branch: 1 branch
BranchTrueFalseTotal
if defined $Element->finish 40806 10 40816
459 40816 unshift @queue, @{$Element->{children}};
460 40816 ▶ unshift @queue, $Element->start if defined($Element->start);
Branch: 1 branch
BranchTrueFalseTotal
if defined $Element->start 40816 0 40816
461 } else {
462 67163 unshift @queue, @{$Element->{children}};
463 }
464 }
465 2064 1;
466 };
467 2064 ▶ if ( !$ok ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($ok) 0 2064 2064
468 # Caught exception thrown from the wanted function
469 0 return undef;
470 }
471  
472 2064 ▶ $rv or '';
Branch: 1 branch
BranchTrueFalseTotal
$rv or "" 2029 35 2064
473 }
474  
475 =pod
476  
477 =head2 find_any $class | \&wanted
478  
479 The C<find_any> method is a short-circuiting true/false method that behaves
480 like the normal C<find> method, but returns true as soon as it finds any
481 Elements that match the search condition.
482  
483 See the C<find> method for details on the format of the search condition.
484  
485 Returns true if any Elements that match the condition can be found, false if
486 not, or C<undef> if given an invalid condition, or an error occurs.
487  
488 =cut
489  
490 sub find_any {
491 2028 ▶ my $self = shift;
Subroutine find_any: called CC=2 SCAR=6.9
Pod documented
492 2028 my $rv = $self->find_first(@_);
493 2028 ▶ $rv ? 1 : $rv; # false or undef
Branch: 1 branch
BranchTrueFalseTotal
$rv ? : 3 2025 2028
494 }
495  
496 =pod
497  
498 =head2 remove_child $Element
499  
500 If passed a L<PPI::Element> object that is a direct child of the Node,
501 the C<remove_element> method will remove the C<Element> intact, along
502 with any of its children. As such, this method acts essentially as a
503 'cut' function.
504  
505 If successful, returns the removed element. Otherwise, returns C<undef>.
506  
507 =cut
508  
509 sub remove_child {
510 66 ▶ my $self = shift;
Subroutine remove_child: called CC=3 SCAR=11.0
Pod documented
511 66 ▶ my $child = _INSTANCE(shift, 'PPI::Element') or return undef;
Branch: 1 branch
BranchTrueFalseTotal
my $child = &_INSTANCE(shift(), "PPI::Element") or return undef 0 66 66
512  
513 # Find the position of the child
514 66 my $key = refaddr $child;
515 my $p = List::MoreUtils::firstidx {
516 201 ▶ refaddr $_ == $key
Subroutine __ANON__: called CC=1 SCAR=0.0
517 66 } @{$self->{children}};
518 66 ▶ return undef if $p == -1;
Branch: 1 branch
BranchTrueFalseTotal
if $p == -1 1 65 66
519  
520 # Splice it out, and remove the child's parent entry
521 65 splice( @{$self->{children}}, $p, 1 );
522 65 delete $_PARENT{refaddr $child};
523  
524 65 $child;
525 }
526  
527 =pod
528  
529 =head2 prune $class | \&wanted
530  
531 The C<prune> method is used to strip L<PPI::Element> objects out of a code
532 tree. The argument is the same as for the C<find> method, either a class
533 name, or an anonymous subroutine which returns true/false. Any Element
534 that matches the class|wanted will be deleted from the code tree, along
535 with any of its children.
536  
537 The C<prune> method returns the number of C<Element> objects that matched
538 and were removed, B<non-recursively>. This might also be zero, so avoid a
539 simple true/false test on the return false of the C<prune> method. It
540 returns C<undef> on error, which you probably B<should> test for.
541  
542 =cut
543  
544 sub prune {
545 30 ▶ my $self = shift;
Subroutine prune: called CC=8 SCAR=21.1
Pod documented
546 30 ▶ my $wanted = $self->_wanted(shift) or return undef;
Branch: 1 branch
BranchTrueFalseTotal
my $wanted = $self->_wanted(shift()) or return undef 0 30 30
547  
548 # Use a depth-first queue search
549 30 my $pruned = 0;
550 30 my @queue = $self->children;
551 30 my $ok = eval {
552 30 ▶ while ( my $element = shift @queue ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $element = shift @queue) 269 30 299
553 269 my $rv = &$wanted( $self, $element );
554 269 ▶ if ( $rv ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rv) 62 207 269
555 # Delete the child
556 62 ▶ $element->delete or return undef;
Branch: 1 branch
BranchTrueFalseTotal
$element->delete or return undef 0 62 62
557 62 $pruned++;
558 62 next;
559 }
560  
561 # Support the undef == "don't descend"
562 207 ▶ next unless defined $rv;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $rv 0 207 207
563  
564 207 ▶ if ( _INSTANCE($element, 'PPI::Node') ) {
Branch: 1 branch
BranchTrueFalseTotal
if (&_INSTANCE($element, "PPI::Node")) 43 164 207
565 # Depth-first keeps the queue size down
566 43 unshift @queue, $element->children;
567 }
568 }
569 30 1;
570 };
571 30 ▶ if ( !$ok ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($ok) 0 30 30
572 # Caught exception thrown from the wanted function
573 0 return undef;
574 }
575  
576 30 $pruned;
577 }
578  
579 # This method is likely to be very heavily used, so take
580 # it slowly and carefully.
581 ### NOTE: Renaming this function or changing either to self will probably
582 ### break File::Find::Rule::PPI
583 sub _wanted {
584 7214 ▶ my $either = shift;
Subroutine _wanted: called CC=15 SCAR=32.5
585 7214 ▶ my $it = defined($_[0]) ? shift : do {
Branch: 1 branch
BranchTrueFalseTotal
defined $_[0] ? : 7213 1 7214
586 1 ▶ Carp::carp('Undefined value passed as search condition') if $^W;
Branch: 1 branch
BranchTrueFalseTotal
if $^W 0 1 1
587 1 return undef;
588 };
589  
590 # Has the caller provided a wanted function directly
591 7213 ▶ return $it if _CODELIKE($it);
Branch: 1 branch
BranchTrueFalseTotal
if &_CODELIKE($it) 3629 3584 7213
592 3584 ▶ if ( ref $it ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $it) 1 3583 3584
593 # No other ref types are supported
594 1 ▶ Carp::carp('Illegal non-CODE reference passed as search condition') if $^W;
Branch: 1 branch
BranchTrueFalseTotal
if $^W 0 1 1
595 1 return undef;
596 }
597  
598 # The first argument should be an Element class, possibly in shorthand
599 3583 ▶ $it = "PPI::$it" unless substr($it, 0, 5) eq 'PPI::';
Branch: 1 branch
BranchTrueFalseTotal
unless substr($it, 0, 5) eq "PPI::" 1486 2097 3583
600 3583 ▶ unless ( _CLASS($it) and $it->isa('PPI::Element') ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (&_CLASS($it) and $it->isa("PPI::Element")) 1 3582 3583
Condition66%2/3
&_CLASS($it) and $it->isa("PPI::Element")
!ll&&!rl&&r
013582
MC/DC50%1/2
&_CLASS($it) and $it->isa("PPI::Element")
&_CLASS($it) $it->isa("PPI::Element")
Truth table66%2/3
A and B
A= &_CLASS($it)B= $it->isa("PPI::Element")
ABresult
0X0
100
111
601 # We got something, but it isn't an element
602 1 ▶ Carp::carp("Cannot create search condition for '$it': Not a PPI::Element") if $^W;
Branch: 1 branch
BranchTrueFalseTotal
if $^W 0 1 1
603 1 return undef;
604 }
605  
606 # Create the class part of the wanted function
607 3582 my $wanted_class = "\n\treturn '' unless \$_[1]->isa('$it');";
608  
609 # Have we been given a second argument to check the content
610 3582 my $wanted_content = '';
611 3582 ▶ if ( defined $_[0] ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $_[0]) 0 3582 3582
612 0 my $content = shift;
613 0 ▶ if ( ref $content eq 'Regexp' ) {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $content eq 'Regexp') { } 0 0 0
elsif (ref $content) { } 0 0 0
614 0 $content = "$content";
615 } elsif ( ref $content ) {
616 # No other ref types are supported
617 0 ▶ Carp::carp("Cannot create search condition for '$it': Not a PPI::Element") if $^W;
Branch: 1 branch
BranchTrueFalseTotal
if $^W 0 0 0
618 0 return undef;
619 } else {
620 0 $content = quotemeta $content;
621 }
622  
623 # Complete the content part of the wanted function
624 0 $wanted_content .= "\n\treturn '' unless defined \$_[1]->{content};";
625 0 $wanted_content .= "\n\treturn '' unless \$_[1]->{content} =~ /$content/;";
626 }
627  
628 # Create the complete wanted function
629 3582 my $code = "sub {"
630 . $wanted_class
631 . $wanted_content
632 . "\n\t1;"
633 . "\n}";
634  
635 # Compile the wanted function
636 3582 $code = eval $code;
637 3582 ▶ (ref $code eq 'CODE') ? $code : undef;
Branch: 1 branch
BranchTrueFalseTotal
ref $code eq 'CODE' ? : 3582 0 3582
638 }
639  
640  
641  
642  
643  
644 ####################################################################
645 # PPI::Element overloaded methods
646  
647 sub tokens {
648 71278 ▶ map { $_->tokens } @{$_[0]->{children}};
Subroutine tokens: called CC=1 SCAR=0.0
Pod documented
649 }
650  
651 ### XS -> PPI/XS.xs:_PPI_Element__content 0.900+
652 sub content {
653 84524 ▶ join '', map { $_->content } @{$_[0]->{children}};
Subroutine content: called CC=1 SCAR=0.0
Pod documented
654 }
655  
656 # Clone as normal, but then go down and relink all the _PARENT entries
657 sub clone {
658 6 ▶ my $self = shift;
Subroutine clone: called CC=1 SCAR=0.0
Pod documented
659 6 my $clone = $self->SUPER::clone;
660 6 $clone->__link_children;
661 6 $clone;
662 }
663  
664 sub location {
665 7849 ▶ my $self = shift;
Subroutine location: called CC=2 SCAR=7.1
Pod documented
666 7849 ▶ my $first = $self->{children}->[0] or return undef;
Branch: 1 branch
BranchTrueFalseTotal
my $first = $self->{"children"}[0] or return undef 0 7849 7849
667 7849 $first->location;
668 }
669  
670  
671  
672  
673  
674 #####################################################################
675 # Internal Methods
676  
677 sub DESTROY {
678 91304 ▶ local $_;
Subroutine DESTROY: called CC=4 SCAR=13.9
679 91304 ▶ if ( $_[0]->{children} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($_[0]{"children"}) 15683 75621 91304
680 15683 my @queue = $_[0];
681 15683 ▶ while ( defined($_ = shift @queue) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($_ = shift @queue)) 375275 15683 390958
682 375275 ▶ unshift @queue, @{delete $_->{children}} if $_->{children};
Branch: 1 branch
BranchTrueFalseTotal
if $_->{"children"} 91284 283991 375275
683  
684 # Remove all internal/private weird crosslinking so that
685 # the cascading DESTROY calls will get called properly.
686 375275 %$_ = ();
687 }
688 }
689  
690 # Remove us from our parent node as normal
691 91304 delete $_PARENT{refaddr $_[0]};
692 }
693  
694 # Find the position of a child
695 sub __position {
696 0 ▶ my $key = refaddr $_[1];
Subroutine __position: not called CC=1 SCAR=6.9
697 0 ▶ List::MoreUtils::firstidx { refaddr $_ == $key } @{$_[0]->{children}};
Subroutine __ANON__: not called CC=1 SCAR=6.9
698 }
699  
700 # Insert one or more elements before a child
701 sub __insert_before_child {
702 2 ▶ my $self = shift;
Subroutine __insert_before_child: called CC=2 SCAR=6.9
703 2 my $key = refaddr shift;
704 my $p = List::MoreUtils::firstidx {
705 8 ▶ refaddr $_ == $key
Subroutine __ANON__: called CC=1 SCAR=0.0
706 2 } @{$self->{children}};
707 2 foreach ( @_ ) {
708 Scalar::Util::weaken(
709 2 $_PARENT{refaddr $_} = $self
710 );
711 }
712 2 splice( @{$self->{children}}, $p, 0, @_ );
713 2 1;
714 }
715  
716 # Insert one or more elements after a child
717 sub __insert_after_child {
718 2 ▶ my $self = shift;
Subroutine __insert_after_child: called CC=2 SCAR=6.9
719 2 my $key = refaddr shift;
720 my $p = List::MoreUtils::firstidx {
721 6 ▶ refaddr $_ == $key
Subroutine __ANON__: called CC=1 SCAR=0.0
722 2 } @{$self->{children}};
723 2 foreach ( @_ ) {
724 Scalar::Util::weaken(
725 2 $_PARENT{refaddr $_} = $self
726 );
727 }
728 2 splice( @{$self->{children}}, $p + 1, 0, @_ );
729 2 1;
730 }
731  
732 # Replace a child
733 sub __replace_child {
734 0 ▶ my $self = shift;
Subroutine __replace_child: not called CC=2 SCAR=17.9
735 0 my $key = refaddr shift;
736 my $p = List::MoreUtils::firstidx {
737 0 ▶ refaddr $_ == $key
Subroutine __ANON__: not called CC=1 SCAR=6.9
738 0 } @{$self->{children}};
739 0 foreach ( @_ ) {
740 Scalar::Util::weaken(
741 0 $_PARENT{refaddr $_} = $self
742 );
743 }
744 0 splice( @{$self->{children}}, $p, 1, @_ );
745 0 1;
746 }
747  
748 # Create PARENT links for an entire tree.
749 # Used when cloning or thawing.
750 sub __link_children {
751 12 ▶ my $self = shift;
Subroutine __link_children: called CC=7 SCAR=19.5
752  
753 # Relink all our children ( depth first )
754 12 my @queue = ( $self );
755 12 ▶ while ( my $Node = shift @queue ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $Node = shift @queue) 33 12 45
756 # Link our immediate children
757 33 foreach my $Element ( @{$Node->{children}} ) {
758 Scalar::Util::weaken(
759 117 $_PARENT{refaddr($Element)} = $Node
760 );
761 117 ▶ unshift @queue, $Element if $Element->isa('PPI::Node');
Branch: 1 branch
BranchTrueFalseTotal
if $Element->isa("PPI::Node") 21 96 117
762 }
763  
764 # If it's a structure, relink the open/close braces
765 33 ▶ next unless $Node->isa('PPI::Structure');
Branch: 1 branch
BranchTrueFalseTotal
unless $Node->isa("PPI::Structure") 29 4 33
766 Scalar::Util::weaken(
767 4 ▶ $_PARENT{refaddr($Node->start)} = $Node
Branch: 1 branch
BranchTrueFalseTotal
if $Node->start 4 0 4
768 ) if $Node->start;
769 Scalar::Util::weaken(
770 4 ▶ $_PARENT{refaddr($Node->finish)} = $Node
Branch: 1 branch
BranchTrueFalseTotal
if $Node->finish 4 0 4
771 ) if $Node->finish;
772 }
773  
774 12 1;
775 }
776  
777 62 1;
778  
779 =pod
780  
781 =head1 TO DO
782  
783 - Move as much as possible to L<PPI::XS>
784  
785 =head1 SUPPORT
786  
787 See the L<support section|PPI/SUPPORT> in the main module.
788  
789 =head1 AUTHOR
790  
791 Adam Kennedy E<lt>adamk@cpan.orgE<gt>
792  
793 =head1 COPYRIGHT
794  
795 Copyright 2001 - 2011 Adam Kennedy.
796  
797 This program is free software; you can redistribute
798 it and/or modify it under the same terms as Perl itself.
799  
800 The full text of the license can be found in the
801 LICENSE file included with this module.
802  
803 =cut
« PPI/Tokenizer.pm ↑ Summary PPI/Token/Number/Exp.pm »