PPI/Node.pm

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