Utilities/Node.pm

filter: Statementstmt 100% 88 / 88 Branchbran 91.1% 31 / 34 Conditioncond 84.0% 21 / 25 MC/DCmcdc 71.4% 10 / 14 Subroutinesub 100% 13 / 13 Podpod 100% 1 / 1 totaltotal 93.7% 164 / 175 CC 28 SCAR 22.3
CC
28
Cov
95%
_split_ppi_node_by_namespace_in_lexical_scope
17.1
_split_ppi_node_by_namespace_single
7.1
_push_fragment
4.0
CRAP 9.3
« Utilities/Statement.pm ↑ Summary Utilities.pm »
1 package PPIx::Utilities::Node;
2  
3 1 ▶use 5.006001;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 1 our $VERSION = '1.001000';
8  
9 1 ▶use Readonly;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11  
12 1 ▶use PPI::Document::Fragment 1.208 qw< >;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 1 ▶use Scalar::Util qw< refaddr >;
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15  
16 1 ▶use PPIx::Utilities::Exception::Bug qw< >;
Subroutine BEGIN: called CC=1 SCAR=0.0
17  
18  
19 1 ▶use base 'Exporter';
Subroutine BEGIN: called CC=1 SCAR=0.0
20  
21 1 Readonly::Array our @EXPORT_OK => qw<
22 split_ppi_node_by_namespace
23 >;
24  
25  
26 10 ▶sub split_ppi_node_by_namespace {
Subroutine split_ppi_node_by_namespace: called CC=2 SCAR=6.9
Pod documented
27 10 my ($node) = @_;
28  
29 # Ensure we don't screw up the original.
30 10 $node = $node->clone();
31  
32 # We want to make sure that we have locations prior to things being split
33 # up, if we can, but don't worry about it if we don't.
34 10 eval { $node->location(); }; ## no critic (RequireCheckingReturnValueOfEval)
35  
36 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
37 2 return $single_namespace;
38 } # end if
39  
40 8 my %nodes_by_namespace;
41 8 _split_ppi_node_by_namespace_in_lexical_scope(
42 $node, 'main', undef, \%nodes_by_namespace,
43 );
44  
45 8 return \%nodes_by_namespace;
46 } # end split_ppi_node_by_namespace()
47  
48  
49 # Handle the case where there's only one.
50 10 ▶sub _split_ppi_node_by_namespace_single {
Subroutine _split_ppi_node_by_namespace_single: called CC=7 SCAR=19.5
51 10 my ($node) = @_;
52  
53 10 my $package_statements = $node->find('PPI::Statement::Package');
54  
55 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
56 1 return { main => [$node] };
57 } # end if
58  
59 9 ▶ if (@{$package_statements} == 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (@{$package_statements;} == 1) 6 3 9
60 6 my $package_statement = $package_statements->[0];
61 6 my $package_address = refaddr $package_statement;
62  
63 # Yes, child and not schild.
64 6 my $first_child = $node->child(0);
65 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
66 $package_address == refaddr $node
67 or $first_child and $package_address == refaddr $first_child
68 ) {
69 1 return { $package_statement->namespace() => [$node] };
70 } # end if
71 } # end if
72  
73 8 return;
74 } # end _split_ppi_node_by_namespace_single()
75  
76  
77 18 ▶sub _split_ppi_node_by_namespace_in_lexical_scope {
Subroutine _split_ppi_node_by_namespace_in_lexical_scope: called CC=17 SCAR=28.4
78 18 my ($node, $initial_namespace, $initial_fragment, $nodes_by_namespace)
79 = @_;
80  
81 18 my %scope_fragments_by_namespace;
82  
83 # I certainly hope a value isn't going to exist at address 0.
84 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
85 18 my ($namespace, $fragment) = ($initial_namespace, $initial_fragment);
86  
87 18 ▶ if ($initial_fragment) {
Branch: 1 branch
BranchTrueFalseTotal
if ($initial_fragment) 10 8 18
88 10 $scope_fragments_by_namespace{$namespace} = $initial_fragment;
89 } # end if
90  
91 18 foreach my $child ( $node->children() ) {
92 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
93 20 ▶ if ($fragment) {
Branch: 1 branch
BranchTrueFalseTotal
if ($fragment) 19 1 20
94 19 _push_fragment($nodes_by_namespace, $namespace, $fragment);
95  
96 19 undef $fragment;
97 } # end if
98  
99 20 $namespace = $child->namespace();
100 ▶ } 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
101 $child->isa('PPI::Statement::Compound')
102 or $child->isa('PPI::Statement::Given')
103 or $child->isa('PPI::Statement::When')
104 ) {
105 10 my $block;
106 10 my @components = $child->children();
107 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
108 44 ▶ if ( $component->isa('PPI::Structure::Block') ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($component->isa("PPI::Structure::Block")) 10 34 44
109 10 $block = $component;
110 } # end if
111 } # end while
112  
113 10 ▶ if ($block) {
Branch: 1 branch
BranchTrueFalseTotal
if ($block) 10 0 10
114 10 ▶ if (not $fragment) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($fragment) 1 9 10
115 1 $fragment = _get_fragment_for_split_ppi_node(
116 $nodes_by_namespace,
117 \%scope_fragments_by_namespace,
118 $namespace,
119 );
120 } # end if
121  
122 _split_ppi_node_by_namespace_in_lexical_scope(
123 10 $block, $namespace, $fragment, $nodes_by_namespace,
124 );
125 } # end if
126 } # end if
127  
128 151 $fragment = _get_fragment_for_split_ppi_node(
129 $nodes_by_namespace, \%scope_fragments_by_namespace, $namespace,
130 );
131  
132 151 ▶ if ($initial_fragment_address != refaddr $fragment) {
Branch: 1 branch
BranchTrueFalseTotal
if ($initial_fragment_address != builtin::refaddr($fragment)) 125 26 151
133 # Need to fix these to use exceptions. Thankfully the P::C tests
134 # will insist that this happens.
135 125 ▶ $child->remove()
Branch: 1 branch
BranchTrueFalseTotal
$child->remove or "PPIx::Utilities::Exception::Bug"->throw("Could not remove child from parent.") 125 0 125
136 or PPIx::Utilities::Exception::Bug->throw(
137 'Could not remove child from parent.'
138 );
139 125 ▶ $fragment->add_element($child)
Branch: 1 branch
BranchTrueFalseTotal
$fragment->add_element($child) or "PPIx::Utilities::Exception::Bug"->throw("Could not add child to fragment.") 125 0 125
140 or PPIx::Utilities::Exception::Bug->throw(
141 'Could not add child to fragment.'
142 );
143 } # end if
144 } # end foreach
145  
146 18 return;
147 } # end _split_ppi_node_by_namespace_in_lexical_scope()
148  
149  
150 152 ▶sub _get_fragment_for_split_ppi_node {
Subroutine _get_fragment_for_split_ppi_node: called CC=2 SCAR=6.9
151 152 my ($nodes_by_namespace, $scope_fragments_by_namespace, $namespace) = @_;
152  
153 152 my $fragment;
154 152 ▶ if ( not $fragment = $scope_fragments_by_namespace->{$namespace} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($fragment = $scope_fragments_by_namespace->{$namespace}) 22 130 152
155 22 $fragment = PPI::Document::Fragment->new();
156 22 $scope_fragments_by_namespace->{$namespace} = $fragment;
157 22 _push_fragment($nodes_by_namespace, $namespace, $fragment);
158 } # end if
159  
160 152 return $fragment;
161 } # end _get_fragment_for_split_ppi_node()
162  
163  
164 # Due to $fragment being passed into recursive calls to
165 # _split_ppi_node_by_namespace_in_lexical_scope(), we can end up attempting to
166 # put the same fragment into a namespace's nodes multiple times.
167 41 ▶sub _push_fragment {
Subroutine _push_fragment: called CC=4 SCAR=13.9
168 41 my ($nodes_by_namespace, $namespace, $fragment) = @_;
169  
170 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
171  
172 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
173 22 push @{$nodes}, $fragment;
174 } # end if
175  
176 41 return;
177 } # end _push_fragment()
178  
179  
180 1 1;
181  
182 __END__
« Utilities/Statement.pm ↑ Summary Utilities.pm »