PackageIndexer.pm

filter: Statementstmt 90.0% 153 / 170 Branchbran 84.3% 54 / 64 Conditioncond 48.4% 16 / 33 MC/DCmcdc 26.3% 5 / 19 Subroutinesub 93.7% 15 / 16 Podpod 83.3% 5 / 6 totaltotal 80.5% 248 / 308 CC 53 SCAR 26.1
CC
53
Cov
84%
clean_results
42.0
parse
17.2
merge_results_inplace
16.2
CRAP 13.6
« PackageIndexer/PPI/Util.pm ↑ Summary PackageIndexer/PPI/Inheritance.pm »
1 package File::PackageIndexer;
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.02';
8  
9 9 ▶use PPI;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 9 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 9 require File::PackageIndexer::PPI::Util;
12 9 require File::PackageIndexer::PPI::ClassXSAccessor;
13 9 require File::PackageIndexer::PPI::Inheritance;
14  
15 use Class::XSAccessor
16 9 ▶ accessors => {
Subroutine BEGIN: called CC=1 SCAR=0.0
17 default_package => 'default_package',
18 clean => 'clean',
19 9 };
20  
21 8 ▶sub new {
Subroutine new: called CC=1 SCAR=0.0
Pod documented
22 8 my $class = shift;
23 8 my $self = bless {
24 clean => 1,
25 @_
26 } => $class;
27 8 return $self;
28 }
29  
30 85 ▶sub parse {
Subroutine parse: called CC=17 SCAR=28.4
Pod documented
31 85 my $self = shift;
32 85 my $def_pkg = $self->default_package;
33 85 ▶ $def_pkg = 'main', $self->default_package('main')
Branch: 1 branch
BranchTrueFalseTotal
unless defined $def_pkg 8 77 85
34 if not defined $def_pkg;
35  
36 85 my $doc = shift;
37 85 ▶ if (not ref($doc) or not $doc->isa("PPI::Node")) {
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $doc and $doc->isa("PPI::Node")) 85 0 85
Condition33%1/3
ref $doc and $doc->isa("PPI::Node")
!ll&&!rl&&r
8500
MC/DC0%0/2
ref $doc and $doc->isa("PPI::Node")
ref $doc $doc->isa("PPI::Node")
Truth table33%1/3
A and B
A= ref $docB= $doc->isa("PPI::Node")
ABresult
0X0
100
111
38 85 $doc = PPI::Document->new(\$doc);
39 }
40 85 ▶ if (not ref($doc)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $doc) 0 85 85
41 0 return();
42 }
43
44 85 my $curpkg;
45 85 my $pkgs = {};
46  
47 # TODO: More accessor generators et al
48 # TODO: More inheritance
49 # TODO: package statement scopes
50  
51 85 my $in_scheduled_block = 0;
52 85 my $finder;
53 9 ▶ use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
54 6913 ▶ $finder = sub {
Subroutine __ANON__: called CC=8 SCAR=20.8
55 6913 ▶ return(0) unless $_[1]->isa("PPI::Statement");
Branch: 1 branch
BranchTrueFalseTotal
unless $_[1]->isa("PPI::Statement") 6055 858 6913
56 858 my $statement = $_[1];
57  
58 858 my $class = $statement->class;
59 # BEGIN/CHECK/INIT/UNITCHECK/END:
60 # Recurse and set the block state, then break outer
61 # recursion so we don't process twice
62 858 ▶ if ( $class eq 'PPI::Statement::Scheduled' ) {
Branch: 5 branches
BranchTrueFalseTotal
if ($class eq 'PPI::Statement::Scheduled') { } 15 843 858
elsif ($class eq 'PPI::Statement::Sub') { } 61 782 843
elsif ($class eq 'PPI::Statement::Package') { } 74 708 782
elsif ($class eq 'PPI::Statement::Include') { } 87 621 708
elsif ($statement->find_any(sub { $_[1]->class eq 'PPI::Token::Symbol' and $_[1]->content eq '@ISA'; } )) { } 85 536 621
63 15 my $temp_copy = $in_scheduled_block;
64 15 $in_scheduled_block = $statement->type;
65 15 $statement->find($finder);
66 15 $in_scheduled_block = $temp_copy;
67 15 return undef;
68 }
69 # new sub declaration
70 elsif ( $class eq 'PPI::Statement::Sub' ) {
71 61 my $subname = $statement->name;
72 61 ▶ if (not defined $curpkg) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $curpkg) 22 39 61
73 22 $curpkg = $self->lazy_create_pkg($def_pkg, $pkgs);
74 }
75 61 $curpkg->{subs}->{$subname} = 1;
76 }
77 # new package statement
78 elsif ( $class eq 'PPI::Statement::Package' ) {
79 74 my $namespace = $statement->namespace;
80 74 $curpkg = $self->lazy_create_pkg($namespace, $pkgs);
81 }
82 # use()
83 elsif ( $class eq 'PPI::Statement::Include' ) {
84 87 $self->_handle_includes($statement, $curpkg, $pkgs);
85 }
86 11579 ▶ elsif ( $statement->find_any(sub {$_[1]->class eq "PPI::Token::Symbol" and $_[1]->content eq '@ISA'}) ) {
Subroutine __ANON__: called CC=2 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
$_[1]->class eq "PPI::Token::Symbol" and $_[1]->content eq "\@ISA" 1222 10357 11579
87 85 File::PackageIndexer::PPI::Inheritance::handle_isa($self, $statement, $curpkg, $pkgs, $in_scheduled_block);
88 }
89 85 };
90  
91 # run it
92 85 $doc->find($finder);
93  
94 85 foreach my $token ( $doc->tokens ) {
95 # find Class->method and __PACKAGE__->method
96 7450 my ($callee, $methodname) = File::PackageIndexer::PPI::Util::is_class_method_call($token);
97  
98 7450 ▶ if ($callee and $methodname =~ /^(?:mk_(?:[rw]o_)?accessors)$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($callee and $methodname =~ /^(?:mk_(?:[rw]o_)?accessors)$/) 9 7441 7450
Condition100%3/3
$callee and $methodname =~ /^(?:mk_(?:[rw]o_)?accessors)$/
!ll&&!rl&&r
743839
MC/DC100%2/2
$callee and $methodname =~ /^(?:mk_(?:[rw]o_)?accessors)$/
$callee $methodname =~ /^(?:mk_(?:[rw]o_)?accessors)$/
Truth table100%3/3
A and B
A= $calleeB= $methodname =~ /^(?:mk_(?:[rw]o_)?accessors)$/
ABresult
0X0
100
111
99 # resolve __PACKAGE__ to current package
100 9 ▶ if ($callee eq '__PACKAGE__') {
Branch: 1 branch
BranchTrueFalseTotal
if ($callee eq "__PACKAGE__") 3 6 9
101 3 ▶ $callee = defined($curpkg) ? $curpkg->{name} : $def_pkg;
Branch: 1 branch
BranchTrueFalseTotal
defined $curpkg ? : 3 0 3
102 }
103  
104 9 my $args = $token->snext_sibling->snext_sibling->snext_sibling; # class->op->method->structure
105 9 ▶ if (defined $args and $args->isa("PPI::Structure::List")) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $args and $args->isa("PPI::Structure::List")) 9 0 9
Condition33%1/3
defined $args and $args->isa("PPI::Structure::List")
!ll&&!rl&&r
009
MC/DC0%0/2
defined $args and $args->isa("PPI::Structure::List")
defined $args $args->isa("PPI::Structure::List")
Truth table33%1/3
A and B
A= defined $argsB= $args->isa("PPI::Structure::List")
ABresult
0X0
100
111
106 9 my $list = File::PackageIndexer::PPI::Util::list_structure_to_array($args);
107 9 ▶ if (@$list) {
Branch: 1 branch
BranchTrueFalseTotal
if (@$list) 8 1 9
108 8 my $pkg = $self->lazy_create_pkg($callee, $pkgs);
109 8 $pkg->{subs}{$_} = 1 for @$list;
110 }
111 }
112  
113 }
114 }
115  
116  
117 # prepend unshift()d inheritance to the
118 # compile-time ISA, then append the push()d
119 # inheritance
120 85 foreach my $pkgname (keys %$pkgs) {
121 101 my $pkg = $pkgs->{$pkgname};
122  
123 101 my $isa = [ @{$pkg->{begin_isa}} ];
124 101 ▶ if ($pkg->{isa_cleared_at_runtime}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pkg->{"isa_cleared_at_runtime"}) 10 91 101
125 10 $isa = [];
126 }
127  
128 101 unshift @$isa, @{ $pkg->{isa_unshift} };
129 101 push @$isa, @{ $pkg->{isa_push} };
130  
131 101 ▶ if ($self->clean) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->clean) 85 16 101
132 85 delete $pkg->{begin_isa};
133 85 delete $pkg->{isa_unshift};
134 85 delete $pkg->{isa_push};
135 85 delete $pkg->{isa_cleared_at_runtime};
136 85 delete $pkg->{isa_cleared_at_compiletime};
137 }
138  
139 101 $pkg->{isa} = $isa;
140 }
141  
142 85 return $pkgs;
143 }
144  
145 # generate empty, new package struct
146 135 ▶sub lazy_create_pkg {
Subroutine lazy_create_pkg: called CC=2 SCAR=6.9
Pod undocumented
147 135 my $self = shift;
148 135 my $p_name = shift;
149 135 my $pkgs = shift;
150 135 ▶ return $pkgs->{$p_name} if exists $pkgs->{$p_name};
Branch: 1 branch
BranchTrueFalseTotal
if exists $pkgs->{$p_name} 34 101 135
151 101 $pkgs->{$p_name} = {
152 name => $p_name,
153 subs => {},
154 isa_unshift => [], # usa entries unshifted at run-time
155 isa_push => [], # isa entries pushed at run-time
156 begin_isa => [], # temporary storage for compile-time inheritance, will be deleted before returning from parse()
157 };
158 101 return $pkgs->{$p_name};
159 }
160  
161  
162 # try to deduce info from module loads
163 87 ▶sub _handle_includes {
Subroutine _handle_includes: called CC=5 SCAR=16.1
164 87 my $self = shift;
165 87 my $statement = shift;
166 87 my $curpkg = shift;
167 87 my $pkgs = shift;
168  
169 return
170 87 ▶ if $statement->type ne 'use'
Branch: 1 branch
BranchTrueFalseTotal
if $statement->type ne "use" or not defined $statement->module 3 84 87
Condition66%2/3
$statement->type ne "use" or not defined $statement->module
l!l&&r!l&&!r
3084
MC/DC50%1/2
$statement->type ne "use" or not defined $statement->module
$statement->type ne "use" not defined $statement->module
Truth table66%2/3
A or not(B)
A= $statement->type ne "use"B= not defined $statement->module
ABresult
1X1
011
000
171 or not defined $statement->module;
172  
173 84 my $module = $statement->module;
174  
175 84 ▶ if ($module =~ /^Class::XSAccessor(?:::Array)?$/) {
Branch: 2 branches
BranchTrueFalseTotal
if ($module =~ /^Class::XSAccessor(?:::Array)?$/) { } 31 53 84
elsif ($module =~ /^(?:base|parent)$/) { } 44 9 53
176 31 File::PackageIndexer::PPI::ClassXSAccessor::handle_class_xsaccessor($self, $statement, $curpkg, $pkgs);
177 }
178 elsif ($module =~ /^(?:base|parent)$/) {
179 44 File::PackageIndexer::PPI::Inheritance::handle_base($self, $statement, $curpkg, $pkgs);
180 }
181  
182 # TODO: handle other generating modules loaded via use
183
184 # TODO: Elsewhere, we need to handle Class->import()!
185 }
186  
187  
188 7 ▶sub merge_results {
Subroutine merge_results: called CC=4 SCAR=14.1
Pod documented
189 7 my @results = @_;
190 7 ▶ shift @results while @results and !ref($results[0]) || ref($results[0]) eq 'File::PackageIndexer';
Branch: 1 branch
BranchTrueFalseTotal
@results and !ref($results[0]) || ref $results[0] eq "File::PackageIndexer" and do { shift @results } 7 7 14
Condition66%4/6
@results and !ref($results[0]) || ref $results[0] eq "File::PackageIndexer"
!ll&&!rl&&r
077
not ref $results[0] or ref $results[0] eq "File::PackageIndexer"
l!l&&r!l&&!r
707
MC/DC33%1/3
@results and !ref($results[0]) || ref $results[0] eq "File::PackageIndexer"
@results not ref $results[0] ref $results[0] eq "File::PackageIndexer"
Truth table50%2/4
A and not(B) or C
A= @resultsB= not ref $results[0]C= ref $results[0] eq "File::PackageIndexer"
ABCresult
0XX0
1000
11X1
1011
191 7 return merge_results_inplace({}, @results);
192 }
193  
194 14 ▶sub merge_results_inplace {
Subroutine merge_results_inplace: called CC=16 SCAR=27.9
Pod documented
195 14 my @results = @_;
196 14 ▶ shift @results while @results and !ref($results[0]) || ref($results[0]) eq 'File::PackageIndexer';
Branch: 1 branch
BranchTrueFalseTotal
@results and !ref($results[0]) || ref $results[0] eq "File::PackageIndexer" and do { shift @results } 7 14 21
Condition66%4/6
@results and !ref($results[0]) || ref $results[0] eq "File::PackageIndexer"
!ll&&!rl&&r
0147
not ref $results[0] or ref $results[0] eq "File::PackageIndexer"
l!l&&r!l&&!r
7014
MC/DC33%1/3
@results and !ref($results[0]) || ref $results[0] eq "File::PackageIndexer"
@results not ref $results[0] ref $results[0] eq "File::PackageIndexer"
Truth table50%2/4
A and not(B) or C
A= @resultsB= not ref $results[0]C= ref $results[0] eq "File::PackageIndexer"
ABCresult
0XX0
1000
11X1
1011
197  
198 14 ▶ return @results if @results == 1;
Branch: 1 branch
BranchTrueFalseTotal
if @results == 1 0 14 14
199  
200 # check that the user used things right
201 14 foreach my $r (@results) {
202 35 foreach my $pkg (values %$r) {
203 58 ▶ if (not exists $pkg->{begin_isa}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $pkg->{"begin_isa"}) 0 58 58
204 0 croak("Can't merge results that have been cleaned. Set the 'clean' option of the parser to a false value to disable cleaning of the result structures. Also RTFM.");
205 }
206 }
207 }
208  
209 14 my $res = shift(@results);
210 14 foreach my $in (@results) {
211  
212 21 foreach my $pkgname (keys %$in) {
213 37 my $inpkg = $in->{$pkgname};
214 37 ▶ if (not exists $res->{$pkgname}) {
Branch: 1 branch
BranchTrueFalseTotal
if (not exists $res->{$pkgname}) { } 29 8 37
215 29 $res->{$pkgname} = $inpkg;
216 }
217 # merge!
218 else {
219 8 my $pkg = $res->{$pkgname};
220  
221 # handle compile time isa
222 8 ▶ if ($inpkg->{isa_cleared_at_compiletime}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($inpkg->{'isa_cleared_at_compiletime'}) { } 4 4 8
223 4 $pkg->{begin_isa} = [@{$inpkg->{begin_isa}}];
224 4 $pkg->{isa_cleared_at_compiletime} = 1;
225 }
226 else {
227 4 push @{$pkg->{begin_isa}}, @{$inpkg->{begin_isa}};
228 }
229  
230 # handle run-time isa
231 8 ▶ if ($inpkg->{isa_cleared_at_runtime}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($inpkg->{'isa_cleared_at_runtime'}) { } 2 6 8
232 2 $pkg->{isa_unshift} = [@{$inpkg->{isa_unshift}}];
233 2 $pkg->{isa_push} = [@{$inpkg->{isa_push}}];
234 2 ▶ $pkg->{isa_cleared_at_runtime} ||= $inpkg->{isa_cleared_at_runtime};
Condition33%1/3
$pkg->{"isa_cleared_at_runtime"} ||= $inpkg->{"isa_cleared_at_runtime"}
l!l&&r!l&&!r
020
MC/DC0%0/2
$pkg->{"isa_cleared_at_runtime"} ||= $inpkg->{"isa_cleared_at_runtime"}
$pkg->{"isa_cleared_at_runtime"} $inpkg->{"isa_cleared_at_runtime"}
Truth table33%1/3
A ||= B
A= $pkg->{"isa_cleared_at_runtime"}B= $inpkg->{"isa_cleared_at_runtime"}
ABresult
1X1
011
000
235 }
236 else {
237 6 unshift @{$pkg->{isa_unshift}}, @{$inpkg->{isa_unshift}};
238 6 push @{$pkg->{isa_push}}, @{$inpkg->{isa_push}};
239 }
240  
241 # finalize isa
242 8 my $isa = [];
243 8 @$isa = @{$pkg->{begin_isa}};
244 8 ▶ if ($pkg->{isa_cleared_at_runtime}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pkg->{"isa_cleared_at_runtime"}) 2 6 8
245 2 $isa = [];
246 }
247  
248 8 unshift @$isa, @{ $pkg->{isa_unshift} };
249 8 push @$isa, @{ $pkg->{isa_push} };
250  
251 8 $pkg->{isa} = $isa;
252  
253 # merge subs
254 8 my $subs = $pkg->{subs};
255 8 foreach my $insub (keys %{$inpkg->{subs}}) {
256 4 $subs->{$insub} = 1;
257 }
258  
259 } # end merge
260  
261 } # end foreach packages
262 } # end foreach @results
263
264 14 return $res;
265 }
266  
267  
268 0 ▶sub clean_results {
Subroutine clean_results: not called CC=6 SCAR=37.4
Pod documented
269 0 my @results = @_;
270 0 ▶ shift @results while @results and !ref($results[0]) || ref($results[0]) eq 'File::PackageIndexer';
Branch: 1 branch
BranchTrueFalseTotal
@results and !ref($results[0]) || ref $results[0] eq "File::PackageIndexer" and do { shift @results } 0 0 0
Condition0%0/6
@results and !ref($results[0]) || ref $results[0] eq "File::PackageIndexer"
!ll&&!rl&&r
000
not ref $results[0] or ref $results[0] eq "File::PackageIndexer"
l!l&&r!l&&!r
000
MC/DC0%0/3
@results and !ref($results[0]) || ref $results[0] eq "File::PackageIndexer"
@results not ref $results[0] ref $results[0] eq "File::PackageIndexer"
Truth table0%0/4
A and not(B) or C
A= @resultsB= not ref $results[0]C= ref $results[0] eq "File::PackageIndexer"
ABCresult
0XX0
1000
11X1
1011
271  
272 0 ▶ return({}) if not @results;
Branch: 1 branch
BranchTrueFalseTotal
unless @results 0 0 0
273 0 my $in = $results[0];
274  
275 0 my $res = {};
276 0 foreach my $pkgname (keys %{$in}) {
277 0 my $inpkg = $in->{$pkgname};
278 0 my $pkg = $res->{$pkgname} = {};
279 0 $pkg->{subs} = {%{$inpkg->{subs}}};
280 0 $pkg->{isa} = [@{$inpkg->{isa}}];
281 0 $pkg->{name} = $inpkg->{name};
282 }
283
284 0 return $res;
285 }
286  
287 9 1;
288  
289 __END__
« PackageIndexer/PPI/Util.pm ↑ Summary PackageIndexer/PPI/Inheritance.pm »