blib/lib/PPIx/EditorTools/Outline.pm

filter: Statementstmt 93.2% 111 / 119 Branchbran 81.2% 52 / 64 Conditioncond 58.3% 7 / 12 MC/DCmcdc 12.5% 1 / 8 Subroutinesub 100% 15 / 15 Podpod 100% 1 / 1 totaltotal 85.3% 187 / 219 SCAR 37.1
CC
38
Cov
87%
find
23.3
__ANON__
8.2
__ANON__
5.0
CRAP 41.0
« blib/lib/PPIx/EditorTools/RenameVariable.pm ↑ Summary blib/lib/PPIx/EditorTools/Lexer.pm »
1 package PPIx::EditorTools::Outline;
2 3 our $AUTHORITY = 'cpan:YANICK';
3 # ABSTRACT: Collect use pragmata, modules, subroutiones, methods, attributes
4 3 $PPIx::EditorTools::Outline::VERSION = '0.20';
5 3 ▶use 5.008;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 3 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 3 ▶use Try::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 3 ▶use base 'PPIx::EditorTools';
Subroutine BEGIN: called CC=1 SCAR=0.0
11 3 ▶use Class::XSAccessor accessors => {};
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 3 ▶use PPI;
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 sub find {
16 9 ▶ my ( $self, %args ) = @_;
Subroutine find: called CC=21 SCAR=31.5
Pod documented
17 9 $self->process_doc(%args);
18  
19 9 my $ppi = $self->ppi;
20  
21 9 ▶ return [] unless defined $ppi;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $ppi 0 9 9
22 9 $ppi->index_locations;
23  
24 # Search for interesting things
25 9 require PPI::Find;
26  
27 # TODO things not very discriptive
28 my @things = PPI::Find->new(
29 sub {
30  
31 # This is a fairly ugly search
32 1133 ▶ return 1 if ref $_[0] eq 'PPI::Statement::Package';
Subroutine __ANON__: called CC=5 SCAR=16.1
Branch: 1 branch
BranchTrueFalseTotal
if ref $_[0] eq "PPI::Statement::Package" 2 1131 1133
33 1131 ▶ return 1 if ref $_[0] eq 'PPI::Statement::Include';
Branch: 1 branch
BranchTrueFalseTotal
if ref $_[0] eq "PPI::Statement::Include" 17 1114 1131
34 1114 ▶ return 1 if ref $_[0] eq 'PPI::Statement::Sub';
Branch: 1 branch
BranchTrueFalseTotal
if ref $_[0] eq "PPI::Statement::Sub" 6 1108 1114
35 1108 ▶ return 1 if ref $_[0] eq 'PPI::Statement';
Branch: 1 branch
BranchTrueFalseTotal
if ref $_[0] eq "PPI::Statement" 38 1070 1108
36 }
37 9 )->in($ppi);
38  
39 # Define a flag indicating that further Method::Signature/Moose check should run
40 9 my $check_alternate_sub_decls = 0;
41  
42 # Build the outline structure from the search results
43 9 my @outline = ();
44 9 my $cur_pkg = {};
45 9 my $not_first_one = 0;
46 9 foreach my $thing (@things) {
47 63 ▶ if ( ref $thing eq 'PPI::Statement::Package' ) {
Branch: 4 branches
BranchTrueFalseTotal
if (ref $thing eq 'PPI::Statement::Package') { } 2 61 63
elsif (ref $thing eq 'PPI::Statement::Include') { } 17 44 61
elsif (ref $thing eq 'PPI::Statement::Sub') { } 6 38 44
elsif (ref $thing eq 'PPI::Statement') { } 38 0 38
48 2 ▶ if ($not_first_one) {
Branch: 1 branch
BranchTrueFalseTotal
if ($not_first_one) 0 2 2
49 0 ▶ if ( not $cur_pkg->{name} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($cur_pkg->{"name"}) 0 0 0
50 0 $cur_pkg->{name} = 'main';
51 }
52 0 push @outline, $cur_pkg;
53 0 $cur_pkg = {};
54 }
55 2 $not_first_one = 1;
56 2 $cur_pkg->{name} = $thing->namespace;
57 2 $cur_pkg->{line} = $thing->location->[0];
58 } elsif ( ref $thing eq 'PPI::Statement::Include' ) {
59 17 ▶ next if $thing->type eq 'no';
Branch: 1 branch
BranchTrueFalseTotal
if $thing->type eq "no" 0 17 17
60 17 ▶ if ( $thing->pragma ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($thing->pragma) { } 9 8 17
elsif ($thing->module) { } 7 1 8
61 9 push @{ $cur_pkg->{pragmata} }, { name => $thing->pragma, line => $thing->location->[0] };
62 } elsif ( $thing->module ) {
63 7 push @{ $cur_pkg->{modules} }, { name => $thing->module, line => $thing->location->[0] };
64 7 ▶ unless ($check_alternate_sub_decls) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($check_alternate_sub_decls) 7 0 7
65 $check_alternate_sub_decls = 1
66 7 ▶ if grep { $thing->module eq $_ } (
Branch: 1 branch
BranchTrueFalseTotal
if grep {$thing->module eq $_;} "Method::Signatures", "MooseX::Declare", "MooseX::Method::Signatures", "Moose::Role", "Moose" 5 2 7
67 'Method::Signatures',
68 'MooseX::Declare',
69 'MooseX::Method::Signatures',
70 'Moose::Role',
71 'Moose',
72 );
73 }
74 }
75 } elsif ( ref $thing eq 'PPI::Statement::Sub' ) {
76 6 push @{ $cur_pkg->{methods} }, { name => $thing->name, line => $thing->location->[0] };
77 } elsif ( ref $thing eq 'PPI::Statement' ) {
78  
79 # last resort, let's analyse further down...
80 38 my $node1 = $thing->first_element;
81 38 my $node2 = $thing->child(2);
82  
83 38 ▶ next unless defined $node2;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $node2 3 35 38
84  
85 # Tests for has followed by new line
86 try {
87 3 ▶ no warnings 'exiting'; # suppress warning Exiting eval via next
Subroutine BEGIN: called CC=1 SCAR=0.0
88 35 ▶ if ( defined $node2->{content} ) {
Subroutine __ANON__: called CC=3 SCAR=11.7
Branch: 1 branch
BranchTrueFalseTotal
if (defined $node2->{"content"}) 33 2 35
89 33 ▶ if ( $node2->{content} =~ /\n/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($node2->{"content"} =~ /\n/) 0 33 33
90 0 next;
91 }
92 }
93 35 };
94  
95 # Moose attribute declaration
96 35 ▶ if ( $node1->isa('PPI::Token::Word') && $node1->content eq 'has' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($node1->isa("PPI::Token::Word") and $node1->content eq "has") 17 18 35
Condition66%2/3
$node1->isa("PPI::Token::Word") and $node1->content eq "has"
!ll&&!rl&&r
01817
MC/DC50%1/2
$node1->isa("PPI::Token::Word") and $node1->content eq "has"
$node1->isa("PPI::Token::Word") $node1->content eq "has"
Truth table66%2/3
A and B
A= $node1->isa("PPI::Token::Word")B= $node1->content eq "has"
ABresult
0X0
100
111
97  
98 # p $_[1]->next_sibling->isa('PPI::Token::Whitespace');
99 17 $self->_Moo_Attributes( $node2, $cur_pkg, $thing );
100 17 next;
101 }
102  
103 # MooseX::POE event declaration
104 18 ▶ if ( $node1->isa('PPI::Token::Word') && $node1->content eq 'event' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($node1->isa("PPI::Token::Word") and $node1->content eq "event") 0 18 18
Condition33%1/3
$node1->isa("PPI::Token::Word") and $node1->content eq "event"
!ll&&!rl&&r
0180
MC/DC0%0/2
$node1->isa("PPI::Token::Word") and $node1->content eq "event"
$node1->isa("PPI::Token::Word") $node1->content eq "event"
Truth table33%1/3
A and B
A= $node1->isa("PPI::Token::Word")B= $node1->content eq "event"
ABresult
0X0
100
111
105 0 push @{ $cur_pkg->{events} }, { name => $node2->content, line => $thing->location->[0] };
106 0 next;
107 }
108 }
109 }
110  
111 9 ▶ if ($check_alternate_sub_decls) {
Branch: 1 branch
BranchTrueFalseTotal
if ($check_alternate_sub_decls) 5 4 9
112 $ppi->find(
113 sub {
114 886 ▶ $_[1]->isa('PPI::Token::Word') or return 0;
Subroutine __ANON__: called CC=8 SCAR=21.0
Branch: 1 branch
BranchTrueFalseTotal
$_[1]->isa("PPI::Token::Word") or return 0 757 129 886
115 129 ▶ $_[1]->content =~ /^(?:func|method|before|after|around|override|augment|class|role)\z/ or return 0;
Branch: 1 branch
BranchTrueFalseTotal
$_[1]->content =~ /^(?:func|method|before|after|around|override|augment|class|role)\z/ or return 0 110 19 129
116 19 ▶ $_[1]->next_sibling->isa('PPI::Token::Whitespace') or return 0;
Branch: 1 branch
BranchTrueFalseTotal
$_[1]->next_sibling->isa("PPI::Token::Whitespace") or return 0 0 19 19
117 19 ▶ my $sib_content = $_[1]->next_sibling->next_sibling->content or return 0;
Branch: 1 branch
BranchTrueFalseTotal
my $sib_content = $_[1]->next_sibling->next_sibling->content or return 0 0 19 19
118  
119 19 my $name = eval $sib_content;
120  
121 # if eval() failed for whatever reason, default to original trimmed original token
122 19 ▶ $name ||= ( $sib_content =~ m/^\b(\w+)\b/ )[0];
Condition66%2/3
$name ||= ($sib_content =~ /^\b(\w+)\b/)[0]
l!l&&r!l&&!r
5140
MC/DC0%0/2
$name ||= ($sib_content =~ /^\b(\w+)\b/)[0]
$name ($sib_content =~ /^\b(\w+)\b/)[0]
Truth table66%2/3
A ||= B
A= $nameB= ($sib_content =~ /^\b(\w+)\b/)[0]
ABresult
1X1
011
000
123  
124 19 ▶ return 0 unless defined $name;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $name 0 19 19
125  
126 # test for MooseX::Declare class, role
127 19 ▶ if ( $_[1]->content =~ m/(class|role)/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($_[1]->content =~ /(class|role)/) 4 15 19
128 4 $self->_Moo_PkgName( $cur_pkg, $sib_content, $_[1] );
129 4 return 1; # break out so we don't write Package name as method
130 }
131  
132 15 push @{ $cur_pkg->{methods} }, { name => $name, line => $_[1]->line_number };
133  
134 15 return 1;
135 }
136 5 );
137 }
138  
139 9 ▶ if ( not $cur_pkg->{name} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($cur_pkg->{"name"}) 4 5 9
140 4 $cur_pkg->{name} = 'main';
141 }
142  
143 9 push @outline, $cur_pkg;
144  
145 9 return \@outline;
146 }
147  
148 ########
149 # Composed Method, internal, Moose Attributes
150 # cleans moose attributes up, and single lines them.
151 # only runs if PPI finds has
152 # prefix all vars with ma_ otherwise same name
153 ########
154 sub _Moo_Attributes {
155 17 ▶ my ( $self, $ma_node2, $ma_cur_pkg, $ma_thing ) = @_;
Subroutine _Moo_Attributes: called CC=4 SCAR=13.9
156  
157 17 my $line_num = $ma_thing->location->[0];
158  
159 17 ▶ if ( $ma_node2->content =~ /[\n|;]/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ma_node2->content =~ /[\n|;]/) 1 16 17
160 1 return;
161 }
162  
163 16 my $attrs = eval $ma_node2->content;
164  
165 # if eval() failed for whatever reason, default to original token
166 16 ▶ $attrs ||= $ma_node2->content;
Condition66%2/3
$attrs ||= $ma_node2->content
l!l&&r!l&&!r
1240
MC/DC0%0/2
$attrs ||= $ma_node2->content
$attrs $ma_node2->content
Truth table66%2/3
A ||= B
A= $attrsB= $ma_node2->content
ABresult
1X1
011
000
167  
168 16 ▶ if ( ref $attrs eq 'ARRAY' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $attrs eq 'ARRAY') { } 2 14 16
169 10 map { push @{ $ma_cur_pkg->{attributes} }, { name => $_, line => $line_num, } }
170 2 grep {defined} @{$attrs};
171  
172 } else {
173  
174 14 push @{ $ma_cur_pkg->{attributes} },
175 {
176 name => $attrs,
177 line => $line_num,
178 };
179 }
180 16 return;
181 }
182  
183 ########
184 # Composed Method, internal, Moose Pakage Name
185 # write first Class or Role as Package Name if none
186 # prefix all vars with mpn_ otherwise same name
187 ########
188 sub _Moo_PkgName {
189 4 ▶ my ( $self, $mpn_cur_pkg, $mpn_sib_content, $mpn_ppi_tuple ) = @_;
Subroutine _Moo_PkgName: called CC=2 SCAR=6.9
190 4 ▶ if ( $mpn_cur_pkg->{name} ) { return 1; } # break if we have a pkg name
Branch: 1 branch
BranchTrueFalseTotal
if ($mpn_cur_pkg->{"name"}) 1 3 4
191 # add to outline
192 3 $mpn_cur_pkg->{name} = $mpn_sib_content; # class or role name
193 3 $mpn_cur_pkg->{line} = $mpn_ppi_tuple->line_number; # class or role location
194 3 return 1;
195 }
196  
197 3 1;
198  
199 =pod
200  
201 =encoding UTF-8
202  
203 =head1 NAME
204  
205 PPIx::EditorTools::Outline - Collect use pragmata, modules, subroutiones, methods, attributes
206  
207 =head1 VERSION
208  
209 version 0.20
210  
211 =head1 SYNOPSIS
212  
213 my $outline = PPIx::EditorTools::Outline->new->find(
214 code => "package TestPackage;\nsub x { 1;\n"
215 );
216 print Dumper $outline;
217  
218 =head1 DESCRIPTION
219  
220 Return a list of pragmatas, modules, methods, attributes of a C<PPI::Document>.
221  
222 =head1 METHODS
223  
224 =over 4
225  
226 =item * new()
227  
228 Constructor. Generally shouldn't be called with any arguments.
229  
230 =item * find()
231  
232 find( ppi => PPI::Document $ppi )
233 or
234 find( code => Str $code )
235  
236 Accepts either a C<PPI::Document> to process or a string containing
237 the code (which will be converted into a C<PPI::Document>) to process.
238 Return a reference to a hash.
239  
240 =back
241  
242 =head2 Internal Methods
243  
244 =over 4
245  
246 =item * _Moo_Attributes
247  
248 =item * _Moo_PkgName
249  
250 =back
251  
252 =head1 SEE ALSO
253  
254 This class inherits from C<PPIx::EditorTools>.
255 Also see L<App::EditorTools>, L<Padre>, and L<PPI>.
256  
257 =head1 AUTHORS
258  
259 =over 4
260  
261 =item *
262  
263 Steffen Mueller C<smueller@cpan.org>
264  
265 =item *
266  
267 Mark Grimes C<mgrimes@cpan.org>
268  
269 =item *
270  
271 Ahmad M. Zawawi <ahmad.zawawi@gmail.com>
272  
273 =item *
274  
275 Gabor Szabo <gabor@szabgab.com>
276  
277 =item *
278  
279 Yanick Champoux <yanick@cpan.org>
280  
281 =back
282  
283 =head1 COPYRIGHT AND LICENSE
284  
285 This software is copyright (c) 2017, 2014, 2012 by The Padre development team as listed in Padre.pm..
286  
287 This is free software; you can redistribute it and/or modify it under
288 the same terms as the Perl 5 programming language system itself.
289  
290 =cut
291  
292 __END__
293  
294  
295  
296  
297 # Copyright 2008-2012 The Padre development team as listed in Padre.pm.
298 # LICENSE
299 # This program is free software; you can redistribute it and/or
300 # modify it under the same terms as Perl 5 itself.
« blib/lib/PPIx/EditorTools/RenameVariable.pm ↑ Summary blib/lib/PPIx/EditorTools/Lexer.pm »