PackageIndexer/PPI/Util.pm

filter: Statementstmt 88.3% 121 / 137 Branchbran 72.5% 74 / 102 Conditioncond 70.5% 36 / 51 MC/DCmcdc 51.7% 15 / 29 Subroutinesub 85.7% 12 / 14 Podpod 0.0% 0 / 9 totaltotal 75.4% 258 / 342 CC 70 SCAR 30.0
CC
70
Cov
80%
_hash_constructor_to_structure
36.2
_array_constructor_to_structure
21.5
is_method_call
11.6
CRAP 20.1
« PackageIndexer/PPI/ClassXSAccessor.pm ↑ Summary PackageIndexer.pm »
1 package File::PackageIndexer::PPI::Util;
2  
3 9 ▶use 5.008001;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 9 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 9 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 9 our $VERSION = '0.01';
8  
9 # try to regenerate a hash or array struct from
10 # a PPI::Structure::Constructor (anon hash/array constr.)
11 51 ▶sub constructor_to_structure {
Subroutine constructor_to_structure: called CC=5 SCAR=16.8
Pod undocumented
12 51 my $token = shift;
13
14 51 ▶ return() unless $token->isa("PPI::Structure::Constructor");
Branch: 1 branch
BranchTrueFalseTotal
unless $token->isa("PPI::Structure::Constructor") 0 51 51
15  
16 51 my $start = $token->start;
17 51 ▶ return() unless $start->isa("PPI::Token::Structure");
Branch: 1 branch
BranchTrueFalseTotal
unless $start->isa("PPI::Token::Structure") 0 51 51
18 51 ▶ if ($start->content eq '{') {
Branch: 2 branches
BranchTrueFalseTotal
if ($start->content eq '{') { } 34 17 51
elsif ($start->content eq '[') { } 17 0 17
19 34 return _hash_constructor_to_structure($token);
20 }
21 elsif ($start->content eq '[') {
22 17 return _array_constructor_to_structure($token);
23 }
24 0 return();
25 }
26  
27 0 ▶sub list_structure_to_hash {
Subroutine list_structure_to_hash: not called CC=1 SCAR=6.9
Pod undocumented
28 0 my $token = shift;
29
30 #return() unless $token->isa("PPI::Structure::List");
31  
32 0 return _hash_constructor_to_structure($token);
33 0 return();
34 }
35  
36 117 ▶sub list_structure_to_array {
Subroutine list_structure_to_array: called CC=1 SCAR=0.4
Pod undocumented
37 117 my $token = shift;
38
39 #return() unless $token->isa("PPI::Structure::List");
40  
41 117 return _array_constructor_to_structure($token);
42 0 return();
43 }
44  
45 34 ▶sub _hash_constructor_to_structure {
Subroutine _hash_constructor_to_structure: called CC=28 SCAR=35.9
46 34 my $hash = shift;
47  
48 34 my $struct = {};
49
50 34 my $state = 'key';
51 34 my $key;
52  
53 # either use the children or walk the siblings
54 34 ▶ my $use_siblings = $hash->can('schildren') ? 0 : 1;
Branch: 1 branch
BranchTrueFalseTotal
$hash->can('schildren') ? : 34 0 34
55 34 ▶ my @children = $use_siblings ? () : $hash->schildren();
Branch: 1 branch
BranchTrueFalseTotal
$use_siblings ? : 0 34 34
56 34 ▶ my $token = $use_siblings ? $hash : undef;
Branch: 1 branch
BranchTrueFalseTotal
$use_siblings ? : 0 34 34
57 34 while (1) {
58 242 ▶ if (@children) {
Branch: 2 branches
BranchTrueFalseTotal
if (@children) { } 208 34 242
elsif ($use_siblings) { } 0 34 34
59 208 $token = shift @children;
60 }
61 elsif ($use_siblings) {
62 0 $token = $token->snext_sibling();
63 0 ▶ last if not defined $token or not ref($token);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $token and ref $token 0 0 0
Condition0%0/3
defined $token and ref $token
!ll&&!rl&&r
000
MC/DC0%0/2
defined $token and ref $token
defined $token ref $token
Truth table0%0/3
A and B
A= defined $tokenB= ref $token
ABresult
0X0
100
111
64 }
65 else {
66 34 last;
67 }
68  
69 # flatten
70 208 ▶ if ($token->isa("PPI::Statement") or $token->isa("PPI::Structure::List")) {
Branch: 1 branch
BranchTrueFalseTotal
if ($token->isa("PPI::Statement") or $token->isa("PPI::Structure::List")) 34 174 208
Condition66%2/3
$token->isa("PPI::Statement") or $token->isa("PPI::Structure::List")
l!l&&r!l&&!r
340174
MC/DC50%1/2
$token->isa("PPI::Statement") or $token->isa("PPI::Structure::List")
$token->isa("PPI::Statement") $token->isa("PPI::Structure::List")
Truth table66%2/3
A or B
A= $token->isa("PPI::Statement")B= $token->isa("PPI::Structure::List")
ABresult
1X1
011
000
71 34 my @ch = $token->schildren();
72  
73 # pop trailing commas
74 34 ▶ while (@ch and $ch[-1]->isa("PPI::Token::Operator") || $ch[-1]->content =~ /^(?:,|=>)$/) {
Branch: 1 branch
BranchTrueFalseTotal
if (@ch and $ch[-1]->isa("PPI::Token::Operator") || $ch[-1]->content =~ /^(?:,|=>)$/) 3 34 37
Condition66%4/6
@ch and $ch[-1]->isa("PPI::Token::Operator") || $ch[-1]->content =~ /^(?:,|=>)$/
!ll&&!rl&&r
0343
$ch[-1]->isa("PPI::Token::Operator") or $ch[-1]->content =~ /^(?:,|=>)$/
l!l&&r!l&&!r
3034
MC/DC33%1/3
@ch and $ch[-1]->isa("PPI::Token::Operator") || $ch[-1]->content =~ /^(?:,|=>)$/
@ch $ch[-1]->isa("PPI::Token::Operator") $ch[-1]->content =~ /^(?:,|=>)$/
Truth table50%2/4
A and B or C
A= @chB= $ch[-1]->isa("PPI::Token::Operator")C= $ch[-1]->content =~ /^(?:,|=>)$/
ABCresult
0XX0
1000
11X1
1011
75 3 pop(@ch);
76 }
77  
78 34 unshift @children, @ch;
79 34 next;
80 }
81  
82 # semicolon
83 174 ▶ return($struct)
Branch: 1 branch
BranchTrueFalseTotal
if $token->isa("PPI::Token::Structure") and $token->content eq ";" 0 174 174
Condition33%1/3
$token->isa("PPI::Token::Structure") and $token->content eq ";"
!ll&&!rl&&r
17400
MC/DC0%0/2
$token->isa("PPI::Token::Structure") and $token->content eq ";"
$token->isa("PPI::Token::Structure") $token->content eq ";"
Truth table33%1/3
A and B
A= $token->isa("PPI::Token::Structure")B= $token->content eq ";"
ABresult
0X0
100
111
84 if $token->isa("PPI::Token::Structure")
85 and $token->content eq ';';
86  
87 # special case: qw()
88 174 ▶ if ( ($state eq 'key' or $state eq 'value')
Branch: 1 branch
BranchTrueFalseTotal
if ($state eq "key" || $state eq "value" and $token->isa("PPI::Token::QuoteLike::Words")) 4 170 174
Condition100%6/6
$state eq "key" || $state eq "value" and $token->isa("PPI::Token::QuoteLike::Words")
!ll&&!rl&&r
701004
$state eq "key" or $state eq "value"
l!l&&r!l&&!r
525270
MC/DC100%3/3
$state eq "key" || $state eq "value" and $token->isa("PPI::Token::QuoteLike::Words")
$state eq "key" $state eq "value" $token->isa("PPI::Token::QuoteLike::Words")
Truth table100%5/5
A or B and C
A= $state eq "key"B= $state eq "value"C= $token->isa("PPI::Token::QuoteLike::Words")
ABCresult
00X0
1X00
0100
1X11
0111
89 and $token->isa("PPI::Token::QuoteLike::Words") )
90 {
91 4 my @values = qw_to_list($token);
92  
93 # emulate the state flip flop to end up in a consistent state afterwards
94 4 foreach my $v (@values) {
95 8 ▶ if ($state eq 'key') {
Branch: 1 branch
BranchTrueFalseTotal
if ($state eq 'key') { } 4 4 8
96 4 $key = $v;
97 4 $state = 'value';
98 }
99 else {
100 4 $struct->{$key} = $v;
101 4 $key = undef;
102 4 $state = 'key';
103 }
104 }
105 4 $state = 'comma';
106 4 next;
107 } # end special case 'qw'
108  
109 170 ▶ if ($state eq 'key') {
Branch: 3 branches
BranchTrueFalseTotal
if ($state eq 'key') { } 50 120 170
elsif ($state eq 'comma') { } 70 50 120
elsif ($state eq 'value') { } 50 0 50
110 50 my $keyname = get_keyname($token);
111 50 ▶ return() if not defined $keyname;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $keyname 0 50 50
112 50 $key = $keyname;
113 50 $state = 'comma';
114 }
115 elsif ($state eq 'comma') {
116 70 ▶ return() unless $token->isa("PPI::Token::Operator");
Branch: 1 branch
BranchTrueFalseTotal
unless $token->isa("PPI::Token::Operator") 0 70 70
117 70 ▶ return() unless $token->content =~ /^(?:,|=>)$/; # are there other valid comma-likes?
Branch: 1 branch
BranchTrueFalseTotal
unless $token->content =~ /^(?:,|=>)$/ 0 70 70
118 70 ▶ $state = defined($key) ? 'value' : 'key';
Branch: 1 branch
BranchTrueFalseTotal
defined $key ? : 52 18 70
119 }
120 elsif ($state eq 'value') {
121 50 my $value = token_to_string($token);
122 50 ▶ return() unless defined $value;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $value 0 50 50
123 50 $struct->{$key} = $value;
124 50 $key = undef;
125 50 $state = 'comma';
126 }
127 else {
128 0 die "Sanity check: Unknown state!";
129 }
130 }
131  
132 34 return($struct);
133 }
134  
135 134 ▶sub _array_constructor_to_structure {
Subroutine _array_constructor_to_structure: called CC=21 SCAR=30.7
136 134 my $array = shift;
137  
138 134 my $struct = [];
139
140 134 my $state = 'elem';
141  
142 # either use the children or walk the siblings
143 134 ▶ my $use_siblings = $array->can('schildren') ? 0 : 1;
Branch: 1 branch
BranchTrueFalseTotal
$array->can('schildren') ? : 26 108 134
144 134 ▶ my @children = $use_siblings ? () : $array->schildren();
Branch: 1 branch
BranchTrueFalseTotal
$use_siblings ? : 108 26 134
145 134 ▶ my $token = $use_siblings ? $array : undef;
Branch: 1 branch
BranchTrueFalseTotal
$use_siblings ? : 108 26 134
146 134 while (1) {
147 546 ▶ if (@children) {
Branch: 2 branches
BranchTrueFalseTotal
if (@children) { } 200 346 546
elsif ($use_siblings) { } 320 26 346
148 200 $token = shift @children;
149 }
150 elsif ($use_siblings) {
151 320 $token = $token->snext_sibling();
152 320 ▶ last if not defined $token or not ref($token);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $token and ref $token 30 290 320
Condition66%2/3
defined $token and ref $token
!ll&&!rl&&r
030290
MC/DC50%1/2
defined $token and ref $token
defined $token ref $token
Truth table66%2/3
A and B
A= defined $tokenB= ref $token
ABresult
0X0
100
111
153 }
154 else {
155 26 last;
156 }
157  
158 # flatten
159 490 ▶ if ($token->isa("PPI::Statement") or $token->isa("PPI::Structure::List")) {
Branch: 1 branch
BranchTrueFalseTotal
if ($token->isa("PPI::Statement") or $token->isa("PPI::Structure::List")) 101 389 490
Condition100%3/3
$token->isa("PPI::Statement") or $token->isa("PPI::Structure::List")
l!l&&r!l&&!r
6140389
MC/DC100%2/2
$token->isa("PPI::Statement") or $token->isa("PPI::Structure::List")
$token->isa("PPI::Statement") $token->isa("PPI::Structure::List")
Truth table100%3/3
A or B
A= $token->isa("PPI::Statement")B= $token->isa("PPI::Structure::List")
ABresult
1X1
011
000
160 101 my @ch = $token->schildren();
161  
162 # pop trailing commas
163 101 ▶ while (@ch and $ch[-1]->isa("PPI::Token::Operator") || $ch[-1]->content =~ /^(?:,|=>)$/) {
Branch: 1 branch
BranchTrueFalseTotal
if (@ch and $ch[-1]->isa("PPI::Token::Operator") || $ch[-1]->content =~ /^(?:,|=>)$/) 6 101 107
Condition83%5/6
@ch and $ch[-1]->isa("PPI::Token::Operator") || $ch[-1]->content =~ /^(?:,|=>)$/
!ll&&!rl&&r
4976
$ch[-1]->isa("PPI::Token::Operator") or $ch[-1]->content =~ /^(?:,|=>)$/
l!l&&r!l&&!r
6097
MC/DC66%2/3
@ch and $ch[-1]->isa("PPI::Token::Operator") || $ch[-1]->content =~ /^(?:,|=>)$/
@ch $ch[-1]->isa("PPI::Token::Operator") $ch[-1]->content =~ /^(?:,|=>)$/
Truth table75%3/4
A and B or C
A= @chB= $ch[-1]->isa("PPI::Token::Operator")C= $ch[-1]->content =~ /^(?:,|=>)$/
ABCresult
0XX0
1000
11X1
1011
164 6 pop(@ch);
165 }
166  
167 101 unshift @children, @ch;
168 101 next;
169 }
170  
171 # semicolon
172 last
173 389 ▶ if $token->isa("PPI::Token::Structure")
Branch: 1 branch
BranchTrueFalseTotal
if $token->isa("PPI::Token::Structure") and $token->content eq ";" 78 311 389
Condition66%2/3
$token->isa("PPI::Token::Structure") and $token->content eq ";"
!ll&&!rl&&r
311078
MC/DC50%1/2
$token->isa("PPI::Token::Structure") and $token->content eq ";"
$token->isa("PPI::Token::Structure") $token->content eq ";"
Truth table66%2/3
A and B
A= $token->isa("PPI::Token::Structure")B= $token->content eq ";"
ABresult
0X0
100
111
174 and $token->content eq ';';
175  
176 311 ▶ if ($state eq 'elem') {
Branch: 2 branches
BranchTrueFalseTotal
if ($state eq 'elem') { } 219 92 311
elsif ($state eq 'comma') { } 92 0 92
177 219 ▶ if ($token->isa("PPI::Token::QuoteLike::Words")) {
Branch: 1 branch
BranchTrueFalseTotal
if ($token->isa('PPI::Token::QuoteLike::Words')) { } 32 187 219
178 32 my @values = qw_to_list($token);
179 32 push @{$struct}, @values;
180 }
181 else {
182 187 my $value = token_to_string($token);
183 187 ▶ last unless defined $value;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $value 0 187 187
184 187 push @{$struct}, $value;
185 }
186 219 $state = 'comma';
187 }
188 elsif ($state eq 'comma') {
189 92 ▶ last unless $token->isa("PPI::Token::Operator");
Branch: 1 branch
BranchTrueFalseTotal
unless $token->isa("PPI::Token::Operator") 0 92 92
190 92 ▶ last unless $token->content =~ /^(?:,|=>)$/; # are there other valid comma-likes?
Branch: 1 branch
BranchTrueFalseTotal
unless $token->content =~ /^(?:,|=>)$/ 0 92 92
191 92 $state = 'elem';
192 }
193 else {
194 0 die "Sanity check: Unknown state!";
195 }
196 }
197  
198 134 return($struct);
199 }
200  
201 # best guess at turning a qw() into a real list
202 36 ▶sub qw_to_list {
Subroutine qw_to_list: called CC=2 SCAR=7.0
Pod undocumented
203 36 my $token = shift;
204 36 ▶ return() if not $token->isa("PPI::Token::QuoteLike::Words");
Branch: 1 branch
BranchTrueFalseTotal
unless $token->isa("PPI::Token::QuoteLike::Words") 0 36 36
205  
206 # FIXME This breaks PPI encapsulation, but there seems to be no API!
207 36 my $string = substr($token->content, $token->{sections}[0]{position}, $token->{sections}[0]{size});
208 36 $string =~ s/^\s+//;
209 36 $string =~ s/\s+$//;
210 36 return split /\s+/, $string;
211 }
212  
213 # best guess at turning a token into the string it represents
214 237 ▶sub token_to_string {
Subroutine token_to_string: called CC=4 SCAR=14.1
Pod undocumented
215 237 my $token = shift;
216 237 ▶ if ($token->isa("PPI::Token::Quote")) {
Branch: 2 branches
BranchTrueFalseTotal
if ($token->isa('PPI::Token::Quote')) { } 164 73 237
elsif ($token->isa('PPI::Token::HereDoc')) { } 0 73 73
217 164 ▶ return($token->can('literal') ? $token->literal : $token->string);
Branch: 1 branch
BranchTrueFalseTotal
$token->can('literal') ? : 112 52 164
218 }
219 elsif ($token->isa("PPI::Token::HereDoc")) {
220 0 return join '', $token->heredoc;
221 }
222 else {
223 73 return $token->content;
224 }
225 }
226  
227 # Given a PPI token, try to interpret it as a quoted "key" or word (re fat comma)
228 129 ▶sub get_keyname {
Subroutine get_keyname: called CC=4 SCAR=14.6
Pod undocumented
229 129 my $token = shift;
230 129 ▶ return() unless $token->isa("PPI::Token");
Branch: 1 branch
BranchTrueFalseTotal
unless $token->isa("PPI::Token") 0 129 129
231 129 ▶ return $token->content if $token->isa("PPI::Token::Word"); # likely followed by a =>
Branch: 1 branch
BranchTrueFalseTotal
if $token->isa("PPI::Token::Word") 127 2 129
232 2 ▶ return $token->string if $token->isa("PPI::Token::Quote");
Branch: 1 branch
BranchTrueFalseTotal
if $token->isa("PPI::Token::Quote") 2 0 2
233 0 return(); # TODO: what else makes sense here?
234 }
235  
236 7450 ▶sub is_class_method_call {
Subroutine is_class_method_call: called CC=2 SCAR=6.9
Pod undocumented
237 7450 my $token = shift;
238 7450 ▶ if ($token->isa("PPI::Token::Word")) {
Branch: 1 branch
BranchTrueFalseTotal
if ($token->isa("PPI::Token::Word")) 1156 6294 7450
239 1156 return is_method_call($token);
240 }
241 6294 return();
242 }
243  
244 0 ▶sub is_instance_method_call {
Subroutine is_instance_method_call: not called CC=2 SCAR=17.9
Pod undocumented
245 0 my $token = shift;
246 0 ▶ if ($token->isa("PPI::Token::Symbol")) {
Branch: 1 branch
BranchTrueFalseTotal
if ($token->isa("PPI::Token::Symbol")) 0 0 0
247 0 return is_method_call($token);
248 }
249 0 return();
250 }
251  
252 1156 ▶sub is_method_call {
Subroutine is_method_call: called CC=10 SCAR=24.5
Pod undocumented
253 1156 my $token = shift;
254 1156 ▶ return() unless $token->isa("PPI::Token::Word") or $token->isa("PPI::Token::Symbol");
Branch: 1 branch
BranchTrueFalseTotal
unless $token->isa("PPI::Token::Word") or $token->isa("PPI::Token::Symbol") 0 1156 1156
Condition33%1/3
$token->isa("PPI::Token::Word") or $token->isa("PPI::Token::Symbol")
l!l&&r!l&&!r
115600
MC/DC0%0/2
$token->isa("PPI::Token::Word") or $token->isa("PPI::Token::Symbol")
$token->isa("PPI::Token::Word") $token->isa("PPI::Token::Symbol")
Truth table33%1/3
A or B
A= $token->isa("PPI::Token::Word")B= $token->isa("PPI::Token::Symbol")
ABresult
1X1
011
000
255  
256 1156 my $next = $token->snext_sibling();
257 return()
258 1156 ▶ unless $next
Branch: 1 branch
BranchTrueFalseTotal
unless $next and $token->content =~ /^[\w:]+$/ and $next->isa("PPI::Token::Operator") and $next->content eq "->" 1144 12 1156
Condition100%9/9
$next and $token->content =~ /^[\w:]+$/ and $next->isa("PPI::Token::Operator") and $next->content eq "->"
!ll&&!rl&&r
99015412
$next and $token->content =~ /^[\w:]+$/ and $next->isa("PPI::Token::Operator")
!ll&&!rl&&r
54936166
$next and $token->content =~ /^[\w:]+$/
!ll&&!rl&&r
5311102
MC/DC100%4/4
$next and $token->content =~ /^[\w:]+$/ and $next->isa("PPI::Token::Operator") and $next->content eq "->"
$next $token->content =~ /^[\w:]+$/ $next->isa("PPI::Token::Operator") $next->content eq "->"
Truth table100%5/5
A and B and C and D
A= $nextB= $token->content =~ /^[\w:]+$/C= $next->isa("PPI::Token::Operator")D= $next->content eq "->"
ABCDresult
0XXX0
10XX0
110X0
11100
11111
259 and $token->content =~ /^[\w:]+$/
260 and $next->isa("PPI::Token::Operator")
261 and $next->content eq '->';
262  
263 12 my $third = $next->snext_sibling();
264 return()
265 12 ▶ unless defined $third;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $third 0 12 12
266  
267 12 ▶ if ( $third->isa("PPI::Token::Word") or $third->isa("PPI::Token::Symbol") ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($third->isa("PPI::Token::Word") or $third->isa("PPI::Token::Symbol")) 12 0 12
Condition33%1/3
$third->isa("PPI::Token::Word") or $third->isa("PPI::Token::Symbol")
l!l&&r!l&&!r
1200
MC/DC0%0/2
$third->isa("PPI::Token::Word") or $third->isa("PPI::Token::Symbol")
$third->isa("PPI::Token::Word") $third->isa("PPI::Token::Symbol")
Truth table33%1/3
A or B
A= $third->isa("PPI::Token::Word")B= $third->isa("PPI::Token::Symbol")
ABresult
1X1
011
000
268 12 return($token->content(), $third->content());
269 }
270 0 return();
271 }
272  
273  
274 9 1;
275  
276 __END__
« PackageIndexer/PPI/ClassXSAccessor.pm ↑ Summary PackageIndexer.pm »