Utils/Traversal.pm

filter: Statementstmt 87.8% 167 / 190 Branchbran 60.3% 70 / 116 Conditioncond 53.8% 42 / 78 MC/DCmcdc 38.1% 16 / 42 Subroutinesub 90.0% 18 / 20 Podpod 100% 7 / 7 totaltotal 70.6% 320 / 453 CC 91 SCAR 32.6
CC
91
Cov
73%
get_previous_module_used_on_same_line
90.0
get_constant_name_elements_from_declaring_statement
59.8
get_next_element_in_same_simple_statement
56.0
CRAP 26.1
↑ Summary Utils/Classification.pm »
1 package PPIx::Utils::Traversal;
2  
3 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 3 ▶use Exporter 'import';
Subroutine BEGIN: called CC=1 SCAR=0.0
6 3 ▶use PPI::Token::Quote::Single;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 3 ▶use PPI::Document::Fragment;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 3 ▶use Scalar::Util 'refaddr';
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 3 ▶use PPIx::Utils::Language qw(precedence_of);
Subroutine BEGIN: called CC=1 SCAR=0.0
11 3 use PPIx::Utils::_Common qw(
12 is_ppi_expression_or_generic_statement
13 is_ppi_simple_statement
14 3 ▶);
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 3 our $VERSION = '0.003';
17  
18 3 our @EXPORT_OK = qw(
19 first_arg parse_arg_list split_nodes_on_comma
20 get_next_element_in_same_simple_statement
21 get_previous_module_used_on_same_line
22 get_constant_name_elements_from_declaring_statement
23 split_ppi_node_by_namespace
24 );
25  
26 3 our %EXPORT_TAGS = (all => [@EXPORT_OK]);
27  
28 # From Perl::Critic::Utils
29 3 my $MIN_PRECEDENCE_TO_TERMINATE_PARENLESS_ARG_LIST =
30 precedence_of( 'not' );
31  
32 sub first_arg {
33 7 ▶ my $elem = shift;
Subroutine first_arg: called CC=5 SCAR=16.2
Pod documented
34 7 my $sib = $elem->snext_sibling();
35 7 ▶ return undef if !$sib;
Branch: 1 branch
BranchTrueFalseTotal
unless $sib 0 7 7
36  
37 7 ▶ if ( $sib->isa('PPI::Structure::List') ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($sib->isa("PPI::Structure::List")) 2 5 7
38  
39 2 my $expr = $sib->schild(0);
40 2 ▶ return undef if !$expr;
Branch: 1 branch
BranchTrueFalseTotal
unless $expr 1 1 2
41 1 ▶ return $expr->isa('PPI::Statement') ? $expr->schild(0) : $expr;
Branch: 1 branch
BranchTrueFalseTotal
$expr->isa('PPI::Statement') ? : 1 0 1
42 }
43  
44 5 return $sib;
45 }
46  
47 sub parse_arg_list {
48 11 ▶ my $elem = shift;
Subroutine parse_arg_list: called CC=11 SCAR=24.9
Pod documented
49 11 my $sib = $elem->snext_sibling();
50 11 ▶ return() if !$sib;
Branch: 1 branch
BranchTrueFalseTotal
unless $sib 0 11 11
51  
52 11 ▶ if ( $sib->isa('PPI::Structure::List') ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($sib->isa('PPI::Structure::List')) { } 3 8 11
53  
54 #Pull siblings from list
55 3 my @list_contents = $sib->schildren();
56 3 ▶ return() if not @list_contents;
Branch: 1 branch
BranchTrueFalseTotal
unless @list_contents 0 3 3
57  
58 3 my @list_expressions;
59 3 foreach my $item (@list_contents) {
60 3 ▶ if (
Branch: 1 branch
BranchTrueFalseTotal
if (is_ppi_expression_or_generic_statement($item)) { } 3 0 3
61 is_ppi_expression_or_generic_statement($item)
62 ) {
63 3 push
64 @list_expressions,
65 split_nodes_on_comma( $item->schildren() );
66 }
67 else {
68 0 push @list_expressions, $item;
69 }
70 }
71  
72 3 return @list_expressions;
73 }
74 else {
75  
76 #Gather up remaining nodes in the statement
77 8 my $iter = $elem;
78 8 my @arg_list = ();
79  
80 8 ▶ while ($iter = $iter->snext_sibling() ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($iter = $iter->snext_sibling) 19 0 19
81 19 ▶ last if $iter->isa('PPI::Token::Structure') and $iter eq ';';
Branch: 1 branch
BranchTrueFalseTotal
if $iter->isa("PPI::Token::Structure") and $iter eq ";" 8 11 19
Condition66%2/3
$iter->isa("PPI::Token::Structure") and $iter eq ";"
!ll&&!rl&&r
1108
MC/DC50%1/2
$iter->isa("PPI::Token::Structure") and $iter eq ";"
$iter->isa("PPI::Token::Structure") $iter eq ";"
Truth table66%2/3
A and B
A= $iter->isa("PPI::Token::Structure")B= $iter eq ";"
ABresult
0X0
100
111
82 11 ▶ last if $iter->isa('PPI::Token::Operator')
Branch: 1 branch
BranchTrueFalseTotal
if $iter->isa("PPI::Token::Operator") and $MIN_PRECEDENCE_TO_TERMINATE_PARENLESS_ARG_LIST <= precedence_of($iter) 0 11 11
Condition66%2/3
$iter->isa("PPI::Token::Operator") and $MIN_PRECEDENCE_TO_TERMINATE_PARENLESS_ARG_LIST <= precedence_of($iter)
!ll&&!rl&&r
920
MC/DC0%0/2
$iter->isa("PPI::Token::Operator") and $MIN_PRECEDENCE_TO_TERMINATE_PARENLESS_ARG_LIST <= precedence_of($iter)
$iter->isa("PPI::Token::Operator") $MIN_PRECEDENCE_TO_TERMINATE_PARENLESS_ARG_LIST <= precedence_of($iter)
Truth table66%2/3
A and B
A= $iter->isa("PPI::Token::Operator")B= $MIN_PRECEDENCE_TO_TERMINATE_PARENLESS_ARG_LIST <= precedence_of($iter)
ABresult
0X0
100
111
83 and $MIN_PRECEDENCE_TO_TERMINATE_PARENLESS_ARG_LIST <=
84 precedence_of( $iter );
85 11 push @arg_list, $iter;
86 }
87 8 return split_nodes_on_comma( @arg_list );
88 }
89 }
90  
91 sub split_nodes_on_comma {
92 11 ▶ my @nodes = @_;
Subroutine split_nodes_on_comma: called CC=8 SCAR=20.9
Pod documented
93  
94 11 my $i = 0;
95 11 my @node_stacks;
96 11 for my $node (@nodes) {
97 26 ▶ if (
Branch: 2 branches
BranchTrueFalseTotal
if ($node->isa('PPI::Token::Operator') and $node eq ',' || $node eq '=>') { } 8 18 26
elsif ($node->isa('PPI::Token::QuoteLike::Words')) { } 7 11 18
Condition66%4/6
$node->isa("PPI::Token::Operator") and $node eq "," || $node eq "=>"
!ll&&!rl&&r
1808
$node eq "," or $node eq "=>"
l!l&&r!l&&!r
620
MC/DC33%1/3
$node->isa("PPI::Token::Operator") and $node eq "," || $node eq "=>"
$node->isa("PPI::Token::Operator") $node eq "," $node eq "=>"
Truth table75%3/4
A and B or C
A= $node->isa("PPI::Token::Operator")B= $node eq ","C= $node eq "=>"
ABCresult
0XX0
1000
11X1
1011
98 $node->isa('PPI::Token::Operator')
99 and ($node eq ',' or $node eq '=>')
100 ) {
101 8 ▶ if (@node_stacks) {
Branch: 1 branch
BranchTrueFalseTotal
if (@node_stacks) 8 0 8
102 8 $i++; #Move forward to next 'node stack'
103 }
104 8 next;
105 } elsif ( $node->isa('PPI::Token::QuoteLike::Words' )) {
106 7 my $section = $node->{sections}->[0];
107 7 my @words = split ' ', substr $node->content, $section->{position}, $section->{size};
108 7 my $loc = $node->location;
109 7 for my $word (@words) {
110 10 my $token = PPI::Token::Quote::Single->new(q{'} . $word . q{'});
111 10 $token->{_location} = $loc;
112 10 push @{ $node_stacks[$i++] }, $token;
113 }
114 7 next;
115 }
116 11 push @{ $node_stacks[$i] }, $node;
117 }
118 11 return @node_stacks;
119 }
120  
121 # From Perl::Critic::Utils::PPI
122 sub get_next_element_in_same_simple_statement {
123 0 ▶ my $element = shift or return undef;
Subroutine get_next_element_in_same_simple_statement: not called CC=7 SCAR=40.3
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
my $element = shift() or return undef 0 0 0
124  
125 0 ▶ while ( $element and (
Branch: 1 branch
BranchTrueFalseTotal
if ($element and !is_ppi_simple_statement($element) || $element->parent && $element->parent->isa("PPI::Structure::List")) 0 0 0
Condition0%0/9
$element and !is_ppi_simple_statement($element) || $element->parent && $element->parent->isa("PPI::Structure::List")
!ll&&!rl&&r
000
not is_ppi_simple_statement($element) or $element->parent and $element->parent->isa("PPI::Structure::List")
l!l&&r!l&&!r
000
$element->parent and $element->parent->isa("PPI::Structure::List")
!ll&&!rl&&r
000
MC/DC0%0/4
$element and !is_ppi_simple_statement($element) || $element->parent && $element->parent->isa("PPI::Structure::List")
$element not is_ppi_simple_statement($element) $element->parent $element->parent->isa("PPI::Structure::List")
Truth table0%0/5
A and not(B) or C and D
A= $elementB= not is_ppi_simple_statement($element)C= $element->parentD= $element->parent->isa("PPI::Structure::List")
ABCDresult
0XXX0
100X0
10100
11XX1
10111
126 not is_ppi_simple_statement( $element )
127 or $element->parent()
128 and $element->parent()->isa( 'PPI::Structure::List' ) ) ) {
129 0 my $next;
130 0 ▶ $next = $element->snext_sibling() and return $next;
Branch: 1 branch
BranchTrueFalseTotal
$next = $element->snext_sibling and return $next 0 0 0
131 0 $element = $element->parent();
132 }
133 0 return undef;
134  
135 }
136  
137 sub get_previous_module_used_on_same_line {
138 0 ▶ my $element = shift or return undef;
Subroutine get_previous_module_used_on_same_line: not called CC=9 SCAR=45.0
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
my $element = shift() or return undef 0 0 0
139  
140 0 ▶ my ( $line ) = @{ $element->location() || []};
Branch: 1 branch
BranchTrueFalseTotal
$element->location or [] 0 0 0
141  
142 0 ▶ while (not is_ppi_simple_statement( $element )) {
Branch: 1 branch
BranchTrueFalseTotal
unless (is_ppi_simple_statement($element)) 0 0 0
143 0 ▶ $element = $element->parent() or return undef;
Branch: 1 branch
BranchTrueFalseTotal
$element = $element->parent or return undef 0 0 0
144 }
145  
146 0 ▶ while ( $element = $element->sprevious_sibling() ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($element = $element->sprevious_sibling) 0 0 0
147 0 ▶ ( @{ $element->location() || []} )[0] == $line or return undef;
Branch: 2 branches
BranchTrueFalseTotal
(@{$element->location or [];})[0] == $line or return undef 0 0 0
$element->location or [] 0 0 0
148 0 ▶ $element->isa( 'PPI::Statement::Include' )
Branch: 1 branch
BranchTrueFalseTotal
$element->isa("PPI::Statement::Include") and return $element->schild(1) 0 0 0
149 and return $element->schild( 1 );
150 }
151  
152 0 return undef;
153 }
154 # End from Perl::Critic::Utils
155  
156 # From PPIx::Utilities::Statement
157 3 my %IS_COMMA = ( q[,] => 1, q[=>] => 1 );
158  
159 sub get_constant_name_elements_from_declaring_statement {
160 4 ▶ my ($element) = @_;
Subroutine get_constant_name_elements_from_declaring_statement: called CC=12 SCAR=40.9
Pod documented
161  
162 4 ▶ return() if not $element;
Branch: 1 branch
BranchTrueFalseTotal
unless $element 0 4 4
163 4 ▶ return() if not $element->isa('PPI::Statement');
Branch: 1 branch
BranchTrueFalseTotal
unless $element->isa("PPI::Statement") 0 4 4
164  
165 4 ▶ if ( $element->isa('PPI::Statement::Include') ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($element->isa('PPI::Statement::Include')) { } 4 0 4
elsif (not $element->specialized and $element->schildren > 2) { } 0 0 0
166 4 my $pragma;
167 4 ▶ if ( $pragma = $element->pragma() and $pragma eq 'constant' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pragma = $element->pragma and $pragma eq "constant") 4 0 4
Condition33%1/3
$pragma = $element->pragma and $pragma eq "constant"
!ll&&!rl&&r
004
MC/DC0%0/2
$pragma = $element->pragma and $pragma eq "constant"
$pragma = $element->pragma $pragma eq "constant"
Truth table33%1/3
A and B
A= $pragma = $element->pragmaB= $pragma eq "constant"
ABresult
0X0
100
111
168 4 return _get_constant_names_from_constant_pragma($element);
169 }
170 ▶ } elsif ( not $element->specialized() and $element->schildren() > 2 ) {
Condition0%0/3
not $element->specialized and $element->schildren > 2
!ll&&!rl&&r
000
MC/DC0%0/2
not $element->specialized and $element->schildren > 2
not $element->specialized $element->schildren > 2
Truth table0%0/3
not(A) and B
A= not $element->specializedB= $element->schildren > 2
ABresult
0X0
100
111
171 0 my $supposed_constant_function = $element->schild(0)->content();
172 0 my $declaring_scope = $element->schild(1)->content();
173  
174 0 ▶ if (
Branch: 1 branch
BranchTrueFalseTotal
if ($supposed_constant_function eq "const" || $supposed_constant_function =~ / \A Readonly \b /x and $declaring_scope eq "our" || $declaring_scope eq "my") 0 0 0
Condition0%0/9
$supposed_constant_function eq "const" || $supposed_constant_function =~ / \A Readonly \b /x and $declaring_scope eq "our" || $declaring_scope eq "my"
!ll&&!rl&&r
000
$supposed_constant_function eq "const" or $supposed_constant_function =~ / \A Readonly \b /x
l!l&&r!l&&!r
000
$declaring_scope eq "our" or $declaring_scope eq "my"
l!l&&r!l&&!r
000
MC/DC0%0/4
$supposed_constant_function eq "const" || $supposed_constant_function =~ / \A Readonly \b /x and $declaring_scope eq "our" || $declaring_scope eq "my"
$supposed_constant_function eq "const" $supposed_constant_function =~ / \A Readonly \b /x $declaring_scope eq "our" $declaring_scope eq "my"
Truth table0%0/7
A or B and C or D
A= $supposed_constant_function eq "const"B= $supposed_constant_function =~ / \A Readonly \b /xC= $declaring_scope eq "our"D= $declaring_scope eq "my"
ABCDresult
00XX0
1X000
01000
1X1X1
1X011
011X1
01011
175 (
176 $supposed_constant_function eq 'const'
177 or $supposed_constant_function =~ m< \A Readonly \b >x
178 )
179 and ($declaring_scope eq 'our' or $declaring_scope eq 'my')
180 ) {
181 0 return ($element->schild(2));
182 }
183 }
184  
185 0 return();
186 }
187  
188 sub _get_constant_names_from_constant_pragma {
189 4 ▶ my ($include) = @_;
Subroutine _get_constant_names_from_constant_pragma: called CC=18 SCAR=30.0
190  
191 4 ▶ my @arguments = $include->arguments() or return();
Branch: 1 branch
BranchTrueFalseTotal
my(@arguments) = $include->arguments or return () 0 4 4
192  
193 4 my $follower = $arguments[0];
194 4 ▶ return() if not defined $follower;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $follower 0 4 4
195  
196 4 ▶ if ($follower->isa('PPI::Token::Operator') && $follower->content eq '+') {
Branch: 1 branch
BranchTrueFalseTotal
if ($follower->isa("PPI::Token::Operator") and $follower->content eq "+") 1 3 4
Condition66%2/3
$follower->isa("PPI::Token::Operator") and $follower->content eq "+"
!ll&&!rl&&r
301
MC/DC50%1/2
$follower->isa("PPI::Token::Operator") and $follower->content eq "+"
$follower->isa("PPI::Token::Operator") $follower->content eq "+"
Truth table66%2/3
A and B
A= $follower->isa("PPI::Token::Operator")B= $follower->content eq "+"
ABresult
0X0
100
111
197 1 $follower = $arguments[1];
198 1 ▶ return() if not defined $follower;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $follower 0 1 1
199 }
200  
201 # We test for a 'PPI::Structure::Block' in the following because some
202 # versions of PPI parse the last element of 'use constant { ONE => 1, TWO
203 # => 2 }' as a block rather than a constructor. As of PPI 1.206, PPI
204 # handles the above correctly, but still blows it on 'use constant 1.16 {
205 # ONE => 1, TWO => 2 }'.
206 4 ▶ if (
Branch: 1 branch
BranchTrueFalseTotal
if ($follower->isa('PPI::Structure::Constructor') or $follower->isa('PPI::Structure::Block')) { } 3 1 4
Condition66%2/3
$follower->isa("PPI::Structure::Constructor") or $follower->isa("PPI::Structure::Block")
l!l&&r!l&&!r
301
MC/DC50%1/2
$follower->isa("PPI::Structure::Constructor") or $follower->isa("PPI::Structure::Block")
$follower->isa("PPI::Structure::Constructor") $follower->isa("PPI::Structure::Block")
Truth table66%2/3
A or B
A= $follower->isa("PPI::Structure::Constructor")B= $follower->isa("PPI::Structure::Block")
ABresult
1X1
011
000
207 $follower->isa( 'PPI::Structure::Constructor' )
208 or $follower->isa( 'PPI::Structure::Block' )
209 ) {
210 3 ▶ my $statement = $follower->schild( 0 ) or return();
Branch: 1 branch
BranchTrueFalseTotal
my $statement = $follower->schild(0) or return () 0 3 3
211 3 ▶ $statement->isa( 'PPI::Statement' ) or return();
Branch: 1 branch
BranchTrueFalseTotal
$statement->isa("PPI::Statement") or return () 0 3 3
212  
213 3 my @elements;
214 3 my $inx = 0;
215 3 foreach my $child ( $statement->schildren() ) {
216 17 ▶ if (not $inx % 2) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($inx % 2) 10 7 17
217 10 ▶ push @{ $elements[ $inx ] ||= [] }, $child;
Condition100%2/2
$elements[$inx] ||= []
l!l
55
MC/DC100%1/1
$elements[$inx] ||= []
$elements[$inx]
Truth table100%2/2
A
A= $elements[$inx]
Aresult
11
00
218 }
219  
220 17 ▶ if ( $IS_COMMA{ $child->content() } ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($IS_COMMA{$child->content}) 7 10 17
221 7 $inx++;
222 }
223 }
224  
225 return map
226 {
227 3 (
228 $_
229 7 ▶ and @{$_} == 2
Branch: 1 branch
BranchTrueFalseTotal
$_ && @{$_;} == 2 && '=>' eq $_->[1]->content && $_->[0]->isa('PPI::Token::Word') ? : 5 2 7
Condition66%6/9
$_ and @{$_;} == 2 and "=>" eq $_->[1]->content and $_->[0]->isa("PPI::Token::Word")
!ll&&!rl&&r
205
$_ and @{$_;} == 2 and "=>" eq $_->[1]->content
!ll&&!rl&&r
205
$_ and @{$_;} == 2
!ll&&!rl&&r
205
MC/DC25%1/4
$_ and @{$_;} == 2 and "=>" eq $_->[1]->content and $_->[0]->isa("PPI::Token::Word")
$_ @{$_;} == 2 "=>" eq $_->[1]->content $_->[0]->isa("PPI::Token::Word")
Truth table40%2/5
A and B and C and D
A= $_B= @{$_;} == 2C= "=>" eq $_->[1]->contentD= $_->[0]->isa("PPI::Token::Word")
ABCDresult
0XXX0
10XX0
110X0
11100
11111
230 and '=>' eq $_->[1]->content()
231 and $_->[0]->isa( 'PPI::Token::Word' )
232 )
233 ? $_->[0]
234 : ()
235 }
236 @elements;
237 } else {
238 1 return ($follower);
239 }
240  
241 0 return ($follower);
242 }
243 # End from PPIx::Utilities::Statement
244  
245 # From PPIx::Utilities::Node
246 sub split_ppi_node_by_namespace {
247 10 ▶ my ($node) = @_;
Subroutine split_ppi_node_by_namespace: called CC=2 SCAR=6.9
Pod documented
248  
249 # Ensure we don't screw up the original.
250 10 $node = $node->clone();
251  
252 # We want to make sure that we have locations prior to things being split
253 # up, if we can, but don't worry about it if we don't.
254 10 eval { $node->location(); };
255  
256 10 ▶ if ( my $single_namespace = _split_ppi_node_by_namespace_single($node) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $single_namespace = _split_ppi_node_by_namespace_single($node)) 2 8 10
257 2 return $single_namespace;
258 }
259  
260 8 my %nodes_by_namespace;
261 8 _split_ppi_node_by_namespace_in_lexical_scope(
262 $node, 'main', undef, \%nodes_by_namespace,
263 );
264  
265 8 return \%nodes_by_namespace;
266 }
267  
268 # Handle the case where there's only one.
269 sub _split_ppi_node_by_namespace_single {
270 10 ▶ my ($node) = @_;
Subroutine _split_ppi_node_by_namespace_single: called CC=7 SCAR=19.5
271  
272 10 my $package_statements = $node->find('PPI::Statement::Package');
273  
274 10 ▶ if ( not $package_statements or not @{$package_statements} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($package_statements and @{$package_statements;}) 1 9 10
Condition66%2/3
$package_statements and @{$package_statements;}
!ll&&!rl&&r
109
MC/DC50%1/2
$package_statements and @{$package_statements;}
$package_statements @{$package_statements;}
Truth table66%2/3
A and B
A= $package_statementsB= @{$package_statements;}
ABresult
0X0
100
111
275 1 return { main => [$node] };
276 }
277  
278 9 ▶ if (@{$package_statements} == 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (@{$package_statements;} == 1) 6 3 9
279 6 my $package_statement = $package_statements->[0];
280 6 my $package_address = refaddr $package_statement;
281  
282 # Yes, child and not schild.
283 6 my $first_child = $node->child(0);
284 6 ▶ if (
Branch: 1 branch
BranchTrueFalseTotal
if ($package_address == builtin::refaddr($node) or $first_child and $package_address == builtin::refaddr($first_child)) 1 5 6
Condition66%4/6
$package_address == builtin::refaddr($node) or $first_child and $package_address == builtin::refaddr($first_child)
l!l&&r!l&&!r
015
$first_child and $package_address == builtin::refaddr($first_child)
!ll&&!rl&&r
051
MC/DC33%1/3
$package_address == builtin::refaddr($node) or $first_child and $package_address == builtin::refaddr($first_child)
$package_address == builtin::refaddr($node) $first_child $package_address == builtin::refaddr($first_child)
Truth table50%2/4
A or B and C
A= $package_address == builtin::refaddr($node)B= $first_childC= $package_address == builtin::refaddr($first_child)
ABCresult
1XX1
0111
00X0
0100
285 $package_address == refaddr $node
286 or $first_child and $package_address == refaddr $first_child
287 ) {
288 1 return { $package_statement->namespace() => [$node] };
289 }
290 }
291  
292 8 return undef;
293 }
294  
295  
296 sub _split_ppi_node_by_namespace_in_lexical_scope {
297 18 ▶ my ($node, $initial_namespace, $initial_fragment, $nodes_by_namespace)
Subroutine _split_ppi_node_by_namespace_in_lexical_scope: called CC=17 SCAR=28.4
298 = @_;
299  
300 18 my %scope_fragments_by_namespace;
301  
302 # I certainly hope a value isn't going to exist at address 0.
303 18 ▶ my $initial_fragment_address = refaddr $initial_fragment || 0;
Condition100%2/2
builtin::refaddr($initial_fragment) || 0
l!l
108
MC/DC100%1/1
builtin::refaddr($initial_fragment) || 0
builtin::refaddr($initial_fragment)
Truth table100%2/2
A
A= builtin::refaddr($initial_fragment)
Aresult
11
00
304 18 my ($namespace, $fragment) = ($initial_namespace, $initial_fragment);
305  
306 18 ▶ if ($initial_fragment) {
Branch: 1 branch
BranchTrueFalseTotal
if ($initial_fragment) 10 8 18
307 10 $scope_fragments_by_namespace{$namespace} = $initial_fragment;
308 }
309  
310 18 foreach my $child ( $node->children() ) {
311 151 ▶ if ( $child->isa('PPI::Statement::Package') ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($child->isa('PPI::Statement::Package')) { } 20 131 151
elsif ($child->isa('PPI::Statement::Compound') or $child->isa('PPI::Statement::Given') or $child->isa('PPI::Statement::When')) { } 10 121 131
312 20 ▶ if ($fragment) {
Branch: 1 branch
BranchTrueFalseTotal
if ($fragment) 19 1 20
313 19 _push_fragment($nodes_by_namespace, $namespace, $fragment);
314  
315 19 undef $fragment;
316 }
317  
318 20 $namespace = $child->namespace();
319 ▶ } elsif (
Condition100%6/6
$child->isa("PPI::Statement::Compound") or $child->isa("PPI::Statement::Given") or $child->isa("PPI::Statement::When")
l!l&&r!l&&!r
64121
$child->isa("PPI::Statement::Compound") or $child->isa("PPI::Statement::Given")
l!l&&r!l&&!r
42125
MC/DC100%3/3
$child->isa("PPI::Statement::Compound") or $child->isa("PPI::Statement::Given") or $child->isa("PPI::Statement::When")
$child->isa("PPI::Statement::Compound") $child->isa("PPI::Statement::Given") $child->isa("PPI::Statement::When")
Truth table100%4/4
A or B or C
A= $child->isa("PPI::Statement::Compound")B= $child->isa("PPI::Statement::Given")C= $child->isa("PPI::Statement::When")
ABCresult
1XX1
01X1
0011
0000
320 $child->isa('PPI::Statement::Compound')
321 or $child->isa('PPI::Statement::Given')
322 or $child->isa('PPI::Statement::When')
323 ) {
324 10 my $block;
325 10 my @components = $child->children();
326 10 ▶ while (not $block and my $component = shift @components) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $block and my $component = shift @components) 44 10 54
Condition66%2/3
not $block and my $component = shift @components
!ll&&!rl&&r
10044
MC/DC50%1/2
not $block and my $component = shift @components
not $block my $component = shift @components
Truth table66%2/3
not(A) and B
A= not $blockB= my $component = shift @components
ABresult
0X0
100
111
327 44 ▶ if ( $component->isa('PPI::Structure::Block') ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($component->isa("PPI::Structure::Block")) 10 34 44
328 10 $block = $component;
329 }
330 }
331  
332 10 ▶ if ($block) {
Branch: 1 branch
BranchTrueFalseTotal
if ($block) 10 0 10
333 10 ▶ if (not $fragment) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($fragment) 1 9 10
334 1 $fragment = _get_fragment_for_split_ppi_node(
335 $nodes_by_namespace,
336 \%scope_fragments_by_namespace,
337 $namespace,
338 );
339 }
340  
341 _split_ppi_node_by_namespace_in_lexical_scope(
342 10 $block, $namespace, $fragment, $nodes_by_namespace,
343 );
344 }
345 }
346  
347 151 $fragment = _get_fragment_for_split_ppi_node(
348 $nodes_by_namespace, \%scope_fragments_by_namespace, $namespace,
349 );
350  
351 151 ▶ if ($initial_fragment_address != refaddr $fragment) {
Branch: 1 branch
BranchTrueFalseTotal
if ($initial_fragment_address != builtin::refaddr($fragment)) 125 26 151
352 # Need to fix these to use exceptions. Thankfully the P::C tests
353 # will insist that this happens.
354 125 ▶ $child->remove() or die 'Could not remove child from parent.';
Branch: 1 branch
BranchTrueFalseTotal
$child->remove or die "Could not remove child from parent." 0 125 125
355 125 ▶ $fragment->add_element($child) or die 'Could not add child to fragment.';
Branch: 1 branch
BranchTrueFalseTotal
$fragment->add_element($child) or die "Could not add child to fragment." 0 125 125
356 }
357 }
358  
359 18 return;
360 }
361  
362 sub _get_fragment_for_split_ppi_node {
363 152 ▶ my ($nodes_by_namespace, $scope_fragments_by_namespace, $namespace) = @_;
Subroutine _get_fragment_for_split_ppi_node: called CC=2 SCAR=6.9
364  
365 152 my $fragment;
366 152 ▶ if ( not $fragment = $scope_fragments_by_namespace->{$namespace} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($fragment = $scope_fragments_by_namespace->{$namespace}) 22 130 152
367 22 $fragment = PPI::Document::Fragment->new();
368 22 $scope_fragments_by_namespace->{$namespace} = $fragment;
369 22 _push_fragment($nodes_by_namespace, $namespace, $fragment);
370 }
371  
372 152 return $fragment;
373 }
374  
375 # Due to $fragment being passed into recursive calls to
376 # _split_ppi_node_by_namespace_in_lexical_scope(), we can end up attempting to
377 # put the same fragment into a namespace's nodes multiple times.
378 sub _push_fragment {
379 41 ▶ my ($nodes_by_namespace, $namespace, $fragment) = @_;
Subroutine _push_fragment: called CC=4 SCAR=13.9
380  
381 41 ▶ my $nodes = $nodes_by_namespace->{$namespace} ||= [];
Condition100%2/2
$nodes_by_namespace->{$namespace} ||= []
l!l
2417
MC/DC100%1/1
$nodes_by_namespace->{$namespace} ||= []
$nodes_by_namespace->{$namespace}
Truth table100%2/2
A
A= $nodes_by_namespace->{$namespace}
Aresult
11
00
382  
383 41 ▶ if (not @{$nodes} or refaddr $nodes->[-1] != refaddr $fragment) {
Branch: 1 branch
BranchTrueFalseTotal
if (not @{$nodes;} or builtin::refaddr($nodes->[-1]) != builtin::refaddr($fragment)) 22 19 41
Condition100%3/3
not @{$nodes;} or builtin::refaddr($nodes->[-1]) != builtin::refaddr($fragment)
l!l&&r!l&&!r
17519
MC/DC100%2/2
not @{$nodes;} or builtin::refaddr($nodes->[-1]) != builtin::refaddr($fragment)
not @{$nodes;} builtin::refaddr($nodes->[-1]) != builtin::refaddr($fragment)
Truth table100%3/3
not(A) or B
A= not @{$nodes;}B= builtin::refaddr($nodes->[-1]) != builtin::refaddr($fragment)
ABresult
1X1
011
000
384 22 push @{$nodes}, $fragment;
385 }
386  
387 41 return;
388 }
389 # End from PPIx::Utilities::Node
390  
391 3 1;
392  
393 =head1 NAME
394  
395 PPIx::Utils::Traversal - Utility functions for traversing PPI documents
396  
397 =head1 SYNOPSIS
398  
399 use PPIx::Utils::Traversal ':all';
400  
401 =head1 DESCRIPTION
402  
403 This package is a component of L<PPIx::Utils> that contains functions for
404 traversal of L<PPI> documents.
405  
406 =head1 FUNCTIONS
407  
408 All functions can be imported by name, or with the tag C<:all>.
409  
410 =head2 first_arg
411  
412 my $first_arg = first_arg($element);
413  
414 Given a L<PPI::Element> that is presumed to be a function call (which
415 is usually a L<PPI::Token::Word>), return the first argument. This is
416 similar of L</parse_arg_list> and follows the same logic. Note that
417 for the code:
418  
419 int($x + 0.5)
420  
421 this function will return just the C<$x>, not the whole expression.
422 This is different from the behavior of L</parse_arg_list>. Another
423 caveat is:
424  
425 int(($x + $y) + 0.5)
426  
427 which returns C<($x + $y)> as a L<PPI::Structure::List> instance.
428  
429 =head2 parse_arg_list
430  
431 my @args = parse_arg_list($element);
432  
433 Given a L<PPI::Element> that is presumed to be a function call (which
434 is usually a L<PPI::Token::Word>), splits the argument expressions
435 into arrays of tokens. Returns a list containing references to each
436 of those arrays. This is useful because parentheses are optional when
437 calling a function, and PPI parses them very differently. So this
438 method is a poor-man's parse tree of PPI nodes. It's not bullet-proof
439 because it doesn't respect precedence. In general, I don't like the
440 way this function works, so don't count on it to be stable (or even
441 present).
442  
443 =head2 split_nodes_on_comma
444  
445 my @args = split_nodes_on_comma(@nodes);
446  
447 This has the same return type as L</parse_arg_list> but expects to be
448 passed the nodes that represent the interior of a list, like:
449  
450 'foo', 1, 2, 'bar'
451  
452 =head2 get_next_element_in_same_simple_statement
453  
454 my $element = get_next_element_in_same_simple_statement($element);
455  
456 Given a L<PPI::Element>, this subroutine returns the next element in
457 the same simple statement as defined by
458 L<PPIx::Utils::Classification/is_ppi_simple_statement>. If no next
459 element can be found, this subroutine simply returns C<undef>.
460  
461 If the $element is undefined or unblessed, we simply return C<undef>.
462  
463 If the $element satisfies
464 L<PPIx::Utils::Classification/is_ppi_simple_statement>, we return
465 C<undef>, B<unless> it has a parent which is a L<PPI::Structure::List>.
466  
467 If the $element is the last significant element in its L<PPI::Node>,
468 we replace it with its parent and iterate again.
469  
470 Otherwise, we return C<< $element->snext_sibling() >>.
471  
472 =head2 get_previous_module_used_on_same_line
473  
474 my $element = get_previous_module_used_on_same_line($element);
475  
476 Given a L<PPI::Element>, returns the L<PPI::Element> representing the
477 name of the module included by the previous C<use> or C<require> on
478 the same line as the $element. If none is found, simply returns
479 C<undef>.
480  
481 For example, with the line
482  
483 use version; our $VERSION = ...;
484  
485 given the L<PPI::Token::Symbol> instance for C<$VERSION>, this will
486 return "version".
487  
488 If the given element is in a C<use> or <require>, the return is from
489 the previous C<use> or C<require> on the line, if any.
490  
491 =head2 get_constant_name_elements_from_declaring_statement
492  
493 my @constants = get_constant_name_elements_from_declaring_statement($statement);
494  
495 Given a L<PPI::Statement>, if the statement is a L<Readonly>, L<ReadonlyX>, or
496 L<Const::Fast> declaration statement or a C<use constant>, returns the names
497 of the things being defined.
498  
499 Given
500  
501 use constant 1.16 FOO => 'bar';
502  
503 this will return the L<PPI::Token::Word> containing C<'FOO'>.
504 Given
505  
506 use constant 1.16 { FOO => 'bar', 'BAZ' => 'burfle' };
507  
508 this will return a list of the L<PPI::Token>s containing C<'FOO'> and C<'BAZ'>.
509 Similarly, given
510  
511 Readonly::Hash my %FOO => ( bar => 'baz' );
512  
513 or
514  
515 const my %FOO => ( bar => 'baz' );
516  
517 this will return the L<PPI::Token::Symbol> containing C<'%FOO'>.
518  
519 =head2 split_ppi_node_by_namespace
520  
521 my $subtrees = split_ppi_node_by_namespace($node);
522  
523 Returns the sub-trees for each namespace in the node as a reference to a hash
524 of references to arrays of L<PPI::Node>s. Say we've got the following code:
525  
526 #!perl
527  
528 my $x = blah();
529  
530 package Foo;
531  
532 my $y = blah_blah();
533  
534 {
535 say 'Whee!';
536  
537 package Bar;
538  
539 something();
540 }
541  
542 thingy();
543  
544 package Baz;
545  
546 da_da_da();
547  
548 package Foo;
549  
550 foreach ( blrfl() ) {
551 ...
552 }
553  
554 Calling this function on a L<PPI::Document> for the above returns a
555 value that looks like this, using multi-line string literals for the
556 actual code parts instead of PPI trees to make this easier to read:
557  
558 {
559 main => [
560 q<
561 #!perl
562  
563 my $x = blah();
564 >,
565 ],
566 Foo => [
567 q<
568 package Foo;
569  
570 my $y = blah_blah();
571  
572 {
573 say 'Whee!';
574  
575 }
576  
577 thingy();
578 >,
579 q<
580 package Foo;
581  
582 foreach ( blrfl() ) {
583 ...
584 }
585 >,
586 ],
587 Bar => [
588 q<
589 package Bar;
590  
591 something();
592 >,
593 ],
594 Baz => [
595 q<
596 package Baz;
597  
598 da_da_da();
599 >,
600 ],
601 }
602  
603 Note that the return value contains copies of the original nodes, and not the
604 original nodes themselves due to the need to handle namespaces that are not
605 file-scoped. (Notice how the first element for "Foo" above differs from the
606 original code.)
607  
608 =head1 BUGS
609  
610 Report any issues on the public bugtracker.
611  
612 =head1 AUTHOR
613  
614 Dan Book <dbook@cpan.org>
615  
616 Code originally from L<Perl::Critic::Utils> by Jeffrey Ryan Thalhammer
617 <jeff@imaginative-software.com>, L<Perl::Critic::Utils::PPI> and
618 L<PPIx::Utilities::Node> by Elliot Shank <perl@galumph.com>, and
619 L<PPIx::Utilities::Statement> by Thomas R. Wyant, III <wyant@cpan.org>
620  
621 =head1 COPYRIGHT AND LICENSE
622  
623 This software is copyright (c) 2005-2011 Imaginative Software Systems,
624 2007-2011 Elliot Shank, 2009-2010 Thomas R. Wyant, III, 2017 Dan Book.
625  
626 This is free software; you can redistribute it and/or modify it under
627 the same terms as the Perl 5 programming language system itself.
628  
629 =head1 SEE ALSO
630  
631 L<Perl::Critic::Utils>, L<Perl::Critic::Utils::PPI>, L<PPIx::Utilities>
↑ Summary Utils/Classification.pm »