StricterSubs/Utils.pm

filter: Statementstmt 94.5% 192 / 203 Branchbran 80.6% 79 / 98 Conditioncond 69.7% 30 / 43 MC/DCmcdc 44.0% 11 / 25 Subroutinesub 96.7% 30 / 31 Podpod 100% 12 / 12 totaltotal 85.9% 354 / 412 CC 73 SCAR 17.5
CC
73
Cov
88%
_smells_like_filehandle
12.1
_smells_like_label
8.9
_parse_export_list
8.2
CRAP 5.7
↑ Summary Policy/Modules/RequireExplicitInclusion.pm »
1 package Perl::Critic::StricterSubs::Utils;
2  
3 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 1 ▶use base 'Exporter';
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 1 ▶use Carp qw(croak);
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 1 ▶use List::MoreUtils qw( any );
Subroutine BEGIN: called CC=1 SCAR=0.0
11 1 ▶use Perl::Critic::Utils qw(
Subroutine BEGIN: called CC=1 SCAR=0.0
12 :characters
13 :severities
14 &first_arg
15 &hashify
16 &is_function_call
17 &is_perl_builtin
18 &words_from_string
19 1 );
20  
21 #-----------------------------------------------------------------------------
22  
23 1 our $VERSION = '0.08';
24  
25 #-----------------------------------------------------------------------------
26  
27 1 our @EXPORT_OK = qw{
28 &find_exported_subroutine_names
29 &find_declared_subroutine_names
30 &find_declared_constant_names
31 &find_imported_subroutine_names
32 &find_subroutine_calls
33 &get_all_subs_from_list_of_symbols
34 &get_package_names_from_include_statements
35 &get_package_names_from_package_statements
36 &get_include_statements
37 &parse_literal_list
38 &parse_quote_words
39 &parse_simple_list
40 };
41  
42 #-----------------------------------------------------------------------------
43  
44 12 ▶sub parse_simple_list {
Subroutine parse_simple_list: called CC=3 SCAR=11.0
Pod documented
45 12 my ($list_node) = @_;
46  
47 # Per RT 36783, lists may contain qw{...} strings as well as words. We
48 # don't need to look for nested lists because they are of interest only
49 # for their contents, which we get by looking for them directly.
50 20 my @strings = map { $_->string() }
51 12 ▶ @{ $list_node->find( 'PPI::Token::Quote' ) || [] };
Condition100%2/2
$list_node->find('PPI::Token::Quote') || []
l!l
102
MC/DC100%1/1
$list_node->find('PPI::Token::Quote') || []
$list_node->find('PPI::Token::Quote')
Truth table100%2/2
A
A= $list_node->find('PPI::Token::Quote')
Aresult
11
00
52 1 push @strings, map { parse_quote_words( $_ ) }
53 12 ▶ @{ $list_node->find( 'PPI::Token::QuoteLike::Words' ) || [] };
Condition100%2/2
$list_node->find('PPI::Token::QuoteLike::Words') || []
l!l
111
MC/DC100%1/1
$list_node->find('PPI::Token::QuoteLike::Words') || []
$list_node->find('PPI::Token::QuoteLike::Words')
Truth table100%2/2
A
A= $list_node->find('PPI::Token::QuoteLike::Words')
Aresult
11
00
54  
55 12 return @strings; #Just hoping that these are single words
56 }
57  
58 #-----------------------------------------------------------------------------
59  
60 0 ▶sub parse_literal_list {
Subroutine parse_literal_list: not called CC=2 SCAR=17.9
Pod documented
61 0 my (@nodes) = @_;
62 0 my @string_elems = grep { $_->isa('PPI::Token::Quote') } @nodes;
63 0 ▶ return if not @string_elems;
Branch: 1 branch
BranchTrueFalseTotal
unless @string_elems 0 0 0
64  
65 0 my @strings = map { $_->string() } @string_elems;
66 0 return @strings; #Just hoping that these are single words
67 }
68  
69 #-----------------------------------------------------------------------------
70  
71 16 ▶sub parse_quote_words {
Subroutine parse_quote_words: called CC=2 SCAR=7.4
Pod documented
72 16 my ($qw_elem) = @_;
73 16 my ($word_string) = ( $qw_elem =~ m{\A qw \s* . (.*) .\z}msx );
74 16 ▶ my @words = words_from_string( $word_string || $EMPTY );
Condition33%1/3
$word_string || $Perl::Critic::StricterSubs::Utils::EMPTY
l!l&&r!l&&!r
1600
MC/DC0%0/2
$word_string || $Perl::Critic::StricterSubs::Utils::EMPTY
$word_string $Perl::Critic::StricterSubs::Utils::EMPTY
Truth table33%1/3
A || B
A= $word_stringB= $Perl::Critic::StricterSubs::Utils::EMPTY
ABresult
1X1
011
000
75 16 return @words;
76 }
77  
78 #-----------------------------------------------------------------------------
79  
80 56 ▶sub get_package_names_from_include_statements {
Subroutine get_package_names_from_include_statements: called CC=1 SCAR=0.0
Pod documented
81 56 my $doc = shift;
82  
83 56 return map { $_->module() } get_include_statements( $doc );
84 }
85  
86 #-----------------------------------------------------------------------------
87  
88 28 ▶sub get_package_names_from_package_statements {
Subroutine get_package_names_from_package_statements: called CC=2 SCAR=6.9
Pod documented
89 28 my $doc = shift;
90  
91 28 my $statements = $doc->find( 'PPI::Statement::Package' );
92 28 ▶ return () if not $statements;
Branch: 1 branch
BranchTrueFalseTotal
unless $statements 24 4 28
93  
94 4 return map { $_->namespace() } @{$statements};
95 }
96  
97 #-----------------------------------------------------------------------------
98  
99 56 ▶sub get_include_statements {
Subroutine get_include_statements: called CC=2 SCAR=6.9
Pod documented
100 56 my $doc = shift;
101  
102 56 my $statements = $doc->find( \&_wanted_include_statement );
103  
104 56 ▶ return $statements ? @{$statements} : ();
Branch: 1 branch
BranchTrueFalseTotal
$statements ? : 33 23 56
105 }
106  
107 #-----------------------------------------------------------------------------
108  
109 2888 ▶sub _wanted_include_statement {
Subroutine _wanted_include_statement: called CC=5 SCAR=16.2
110 2888 my (undef, $element) = @_;
111  
112 2888 ▶ return 0 if not $element->isa('PPI::Statement::Include');
Branch: 1 branch
BranchTrueFalseTotal
unless $element->isa("PPI::Statement::Include") 2826 62 2888
113  
114 # This will block out file names, e.g. require 'Foo.pm';
115 62 ▶ return 0 if not $element->module();
Branch: 1 branch
BranchTrueFalseTotal
unless $element->module 5 57 62
116  
117 # Skip 'no' as in 'no strict'
118 57 my $include_type = $element->type();
119 57 ▶ return 0 if $include_type ne 'use' && $include_type ne 'require';
Branch: 1 branch
BranchTrueFalseTotal
if $include_type ne "use" and $include_type ne "require" 0 57 57
Condition66%2/3
$include_type ne "use" and $include_type ne "require"
!ll&&!rl&&r
39180
MC/DC0%0/2
$include_type ne "use" and $include_type ne "require"
$include_type ne "use" $include_type ne "require"
Truth table66%2/3
A and B
A= $include_type ne "use"B= $include_type ne "require"
ABresult
0X0
100
111
120  
121 57 return 1;
122 }
123  
124 #-----------------------------------------------------------------------------
125  
126 10 ▶sub _find_exported_names {
Subroutine _find_exported_names: called CC=5 SCAR=16.1
127 10 my ($doc, @export_types) = @_;
128  
129 10 ▶ @export_types = @export_types ?
Branch: 1 branch
BranchTrueFalseTotal
@export_types ? : 0 10 10
130 @export_types : qw{@EXPORT @EXPORT_OK};
131  
132 10 my @all_exports;
133 10 for my $export_type( @export_types ) {
134  
135 20 my $export_assignment = _find_export_assignment( $doc, $export_type );
136 20 ▶ next if not $export_assignment;
Branch: 1 branch
BranchTrueFalseTotal
unless $export_assignment 8 12 20
137  
138 12 my @exports = _parse_export_list( $export_assignment );
139 12 foreach (@exports) { s/ \A & //xms; } # Strip all sub sigils
140 12 push @all_exports, @exports;
141 }
142  
143 10 return @all_exports;
144 }
145  
146 #-----------------------------------------------------------------------------
147  
148 10 ▶sub find_exported_subroutine_names {
Subroutine find_exported_subroutine_names: called CC=1 SCAR=0.0
Pod documented
149 10 my ($doc, @export_types) = @_;
150  
151 10 my @exports = _find_exported_names( $doc, @export_types );
152 10 return get_all_subs_from_list_of_symbols( @exports );
153 }
154  
155 #-----------------------------------------------------------------------------
156  
157 34 ▶sub find_declared_subroutine_names {
Subroutine find_declared_subroutine_names: called CC=3 SCAR=11.0
Pod documented
158 34 my ($doc) = @_;
159 34 my $sub_nodes = $doc->find('PPI::Statement::Sub');
160 34 ▶ return if not $sub_nodes;
Branch: 1 branch
BranchTrueFalseTotal
unless $sub_nodes 27 7 34
161  
162 7 my @sub_names = map { $_->name() } @{ $sub_nodes };
163 7 for ( @sub_names ) {
164 9 s{\A .*::}{}mxs; # Remove leading package name
165 }
166  
167 7 return @sub_names;
168 }
169  
170 #-----------------------------------------------------------------------------
171  
172 28 ▶sub find_imported_subroutine_names {
Subroutine find_imported_subroutine_names: called CC=2 SCAR=6.9
Pod documented
173 28 my ($doc) = @_;
174  
175 28 my $includes_ref = $doc->find('PPI::Statement::Include');
176 28 ▶ return if not $includes_ref;
Branch: 1 branch
BranchTrueFalseTotal
unless $includes_ref 12 16 28
177  
178 16 my @use_stmnts = grep { $_->type() eq 'use' } @{ $includes_ref };
179  
180 my @imported_symbols =
181 16 map { _get_imports_from_use_statements($_) } @use_stmnts;
182  
183 16 my @imported_sub_names =
184 get_all_subs_from_list_of_symbols( @imported_symbols );
185  
186 16 return @imported_sub_names;
187 }
188  
189 #-----------------------------------------------------------------------------
190  
191 28 ▶sub _get_imports_from_use_statements {
Subroutine _get_imports_from_use_statements: called CC=7 SCAR=19.5
192 28 my ($use_stmnt) = @_;
193  
194 # In a typical C<use> statement, the first child is "use", and the
195 # second child is the package name (a bareword). Everything after
196 # that (except the trailing semi-colon) is part of the import
197 # arguments.
198  
199 28 my @schildren = $use_stmnt->schildren();
200 28 my @import_args = @schildren[2 .. $#schildren - 1];
201  
202 28 my $first_import_arg = $import_args[0];
203 28 ▶ return if not defined $first_import_arg;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $first_import_arg 6 22 28
204  
205 # RT 43310 is a pathological case, which shows we can't simply look at the
206 # first token after the module name to tell what to do. So we iterate over
207 # the entire argument list, scavenging what we recognize, and hoping the
208 # rest is structure (commas and such).
209 22 my @result;
210 22 foreach my $import_rqst ( @import_args ) {
211  
212 37 ▶ defined $import_rqst
Branch: 1 branch
BranchTrueFalseTotal
defined $import_rqst or next 37 0 37
213 or next;
214  
215 37 ▶ if ( $import_rqst->isa( 'PPI::Token::QuoteLike::Words' ) ) {
Branch: 3 branches
BranchTrueFalseTotal
if ($import_rqst->isa('PPI::Token::QuoteLike::Words')) { } 8 29 37
elsif ($import_rqst->isa('PPI::Structure::List')) { } 7 22 29
elsif ($import_rqst->isa('PPI::Token::Quote')) { } 11 11 22
216  
217 8 push @result, parse_quote_words( $import_rqst );
218  
219 } elsif ( $import_rqst->isa( 'PPI::Structure::List' ) ) {
220  
221 7 push @result, parse_simple_list ( $import_rqst );
222  
223 } elsif ( $import_rqst->isa( 'PPI::Token::Quote' ) ) {
224  
225 11 push @result, $import_rqst->string();
226  
227 }
228  
229 }
230  
231 22 return @result;
232  
233 }
234  
235 #-----------------------------------------------------------------------------
236  
237 34 ▶sub find_declared_constant_names {
Subroutine find_declared_constant_names: called CC=6 SCAR=18.0
Pod documented
238 34 my ($doc) = @_;
239  
240 34 my $constant_pragmas_ref = $doc->find( \&_is_constant_pragma );
241 34 ▶ return if not $constant_pragmas_ref;
Branch: 1 branch
BranchTrueFalseTotal
unless $constant_pragmas_ref 31 3 34
242 3 my @declared_constants;
243  
244 3 for my $constant_pragma ( @{$constant_pragmas_ref} ) {
245  
246 #######################################################
247 # Constant pragmas typically look like one of these:
248 # use constant (AVAGADRO => 6.02*10^23); # With parens
249 # use constant PI => 3.1415927; # Without parens
250 # use constant {FOO => 1, BAR => 1} # Block form
251 #######################################################
252  
253 7 my $pragma_bareword = $constant_pragma->schild(1);
254 7 my $sibling = $pragma_bareword->snext_sibling();
255  
256 7 ▶ if ( defined $sibling && $sibling->isa('PPI::Structure::Constructor') ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $sibling and $sibling->isa('PPI::Structure::Constructor')) { } 2 5 7
Condition66%2/3
defined $sibling and $sibling->isa("PPI::Structure::Constructor")
!ll&&!rl&&r
052
MC/DC50%1/2
defined $sibling and $sibling->isa("PPI::Structure::Constructor")
defined $sibling $sibling->isa("PPI::Structure::Constructor")
Truth table66%2/3
A and B
A= defined $siblingB= $sibling->isa("PPI::Structure::Constructor")
ABresult
0X0
100
111
257 # Parse the multi-constant block form...
258 2 push @declared_constants, _get_keys_of_hash($sibling);
259 }
260 else {
261 # Parse the single-constant declaration
262 5 ▶ my $constant_name = first_arg( $pragma_bareword ) || next;
Condition50%1/2
first_arg($pragma_bareword) || (next)
l!l
50
MC/DC0%0/1
first_arg($pragma_bareword) || (next)
first_arg($pragma_bareword)
Truth table50%1/2
A
A= first_arg($pragma_bareword)
Aresult
11
00
263 5 push @declared_constants, $constant_name->content();
264 }
265  
266 }
267 3 return @declared_constants;
268 }
269  
270 #-----------------------------------------------------------------------------
271  
272 2 ▶sub _get_keys_of_hash {
Subroutine _get_keys_of_hash: called CC=3 SCAR=11.2
273 2 my ($block_or_list_node) = @_;
274 2 ▶ return if not defined $block_or_list_node;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $block_or_list_node 0 2 2
275  
276 2 ▶ my $fat_commas = $block_or_list_node->find( \&_is_fat_comma )
Branch: 1 branch
BranchTrueFalseTotal
my $fat_commas = $block_or_list_node->find(\&Perl::Critic::StricterSubs::Utils::_is_fat_comma) or return 2 0 2
277 or return;
278  
279 2 my @keys = map { $_->sprevious_sibling() } @{$fat_commas};
280 2 return @keys;
281 }
282  
283 #-----------------------------------------------------------------------------
284  
285 39 ▶sub _is_fat_comma {
Subroutine _is_fat_comma: called CC=2 SCAR=6.9
286 39 my( undef, $elem) = @_;
287 39 ▶ return $elem->isa('PPI::Token::Operator')
Condition100%3/3
$elem->isa('PPI::Token::Operator') && $elem eq $Perl::Critic::StricterSubs::Utils::FATCOMMA
!ll&&!rl&&r
3045
MC/DC100%2/2
$elem->isa('PPI::Token::Operator') && $elem eq $Perl::Critic::StricterSubs::Utils::FATCOMMA
$elem->isa('PPI::Token::Operator') $elem eq $Perl::Critic::StricterSubs::Utils::FATCOMMA
Truth table100%3/3
A && B
A= $elem->isa('PPI::Token::Operator')B= $elem eq $Perl::Critic::StricterSubs::Utils::FATCOMMA
ABresult
0X0
100
111
288 && $elem eq $FATCOMMA;
289 }
290  
291 #-----------------------------------------------------------------------------
292  
293 2092 ▶sub _is_constant_pragma {
Subroutine _is_constant_pragma: called CC=3 SCAR=11.0
294 2092 my (undef, $elem) = @_;
295  
296 2092 ▶ return $elem->isa('PPI::Statement::Include')
Condition83%5/6
$elem->isa('PPI::Statement::Include') && $elem->pragma eq 'constant' && $elem->type eq 'use'
!ll&&!rl&&r
208507
$elem->isa("PPI::Statement::Include") and $elem->pragma eq "constant"
!ll&&!rl&&r
2052337
MC/DC66%2/3
$elem->isa('PPI::Statement::Include') && $elem->pragma eq 'constant' && $elem->type eq 'use'
$elem->isa("PPI::Statement::Include") $elem->pragma eq "constant" $elem->type eq 'use'
Truth table75%3/4
A and B && C
A= $elem->isa("PPI::Statement::Include")B= $elem->pragma eq "constant"C= $elem->type eq 'use'
ABCresult
0XX0
10X0
1100
1111
297 && $elem->pragma() eq 'constant'
298 && $elem->type() eq 'use';
299 }
300  
301 #-----------------------------------------------------------------------------
302  
303 33 ▶sub find_subroutine_calls {
Subroutine find_subroutine_calls: called CC=2 SCAR=6.9
Pod documented
304 33 my ($doc) = @_;
305  
306 33 my $sub_calls_ref = $doc->find( \&_is_subroutine_call );
307 33 ▶ return if not $sub_calls_ref;
Branch: 1 branch
BranchTrueFalseTotal
unless $sub_calls_ref 5 28 33
308 28 return @{$sub_calls_ref};
309 }
310  
311 #-----------------------------------------------------------------------------
312  
313 2204 ▶sub _is_subroutine_call {
Subroutine _is_subroutine_call: called CC=8 SCAR=20.8
314 2204 my (undef, $elem) = @_;
315  
316 2204 ▶ if ( $elem->isa('PPI::Token::Word') ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($elem->isa('PPI::Token::Word')) { } 265 1939 2204
elsif ($elem->isa('PPI::Token::Symbol')) { } 89 1850 1939
317  
318 265 ▶ return 0 if is_perl_builtin( $elem );
Branch: 1 branch
BranchTrueFalseTotal
if is_perl_builtin($elem) 92 173 265
319 173 ▶ return 0 if _smells_like_filehandle( $elem );
Branch: 1 branch
BranchTrueFalseTotal
if _smells_like_filehandle($elem) 26 147 173
320 147 ▶ return 0 if _smells_like_label( $elem );
Branch: 1 branch
BranchTrueFalseTotal
if _smells_like_label($elem) 5 142 147
321 142 ▶ return 1 if is_function_call( $elem );
Branch: 1 branch
BranchTrueFalseTotal
if is_function_call($elem) 78 64 142
322  
323 }
324 elsif ($elem->isa('PPI::Token::Symbol')) {
325  
326 89 ▶ return 1 if $elem->symbol_type eq q{&};
Branch: 1 branch
BranchTrueFalseTotal
if $elem->symbol_type eq "&" 29 60 89
327 }
328  
329 1974 return 0;
330 }
331  
332 #-----------------------------------------------------------------------------
333  
334 1 my %functions_that_take_filehandles =
335 hashify( qw(
336 binmode
337 close
338 eof
339 fileno
340 flock
341 getc
342 open
343 print
344 printf
345 read
346 seek
347 select
348 sysopen
349 sysread
350 sysseek
351 syswrite
352 tell
353 truncate
354 write
355 ) );
356  
357  
358 1 my %functions_that_take_dirhandles =
359 hashify( qw(
360 closedir
361 opendir
362 readdir
363 rewinddir
364 seekdir
365 telldir
366 ) );
367  
368 1 my %functions_that_take_handleish_things = (
369 %functions_that_take_filehandles,
370 %functions_that_take_dirhandles,
371 );
372  
373 173 ▶sub _smells_like_filehandle {
Subroutine _smells_like_filehandle: called CC=11 SCAR=24.9
374 173 my ($elem) = @_;
375 173 ▶ return if not $elem;
Branch: 1 branch
BranchTrueFalseTotal
unless $elem 0 173 173
376  
377 #--------------------------------------------------------------------
378 # This handles calls *without* parens, for example:
379 # open HANDLE, $path;
380 # print HANDLE 'Hello World';
381 # close HANDLE;
382  
383 173 ▶ if ( my $left_sib = $elem->sprevious_sibling ){
Branch: 1 branch
BranchTrueFalseTotal
if (my $left_sib = $elem->sprevious_sibling) 77 96 173
384 77 ▶ return exists $functions_that_take_handleish_things{ $left_sib }
Condition66%2/3
exists $functions_that_take_handleish_things{$left_sib} && is_function_call($left_sib)
!ll&&!rl&&r
63014
MC/DC50%1/2
exists $functions_that_take_handleish_things{$left_sib} && is_function_call($left_sib)
exists $functions_that_take_handleish_things{$left_sib} is_function_call($left_sib)
Truth table66%2/3
A && B
A= exists $functions_that_take_handleish_things{$left_sib}B= is_function_call($left_sib)
ABresult
0X0
100
111
385 && is_function_call( $left_sib );
386 }
387  
388 #--------------------------------------------------------------------
389 # This handles calls *with* parens, for example:
390 # open( HANDLE, $path );
391 # print( HANDLE 'Hello World' );
392 # close( HANDLE );
393 #
394 # Or this case (Conway-style):
395 # print {HANDLE} 'Hello World';
396  
397 96 ▶ my $expression = $elem->parent() || return;
Condition50%1/2
$elem->parent || (return)
l!l
960
MC/DC0%0/1
$elem->parent || (return)
$elem->parent
Truth table50%1/2
A
A= $elem->parent
Aresult
11
00
398 96 ▶ my $enclosing_node = $expression->parent() || return;
Condition50%1/2
$expression->parent || (return)
l!l
960
MC/DC0%0/1
$expression->parent || (return)
$expression->parent
Truth table50%1/2
A
A= $expression->parent
Aresult
11
00
399  
400 96 ▶ return if ! ( $enclosing_node->isa('PPI::Structure::List')
Branch: 1 branch
BranchTrueFalseTotal
unless $enclosing_node->isa("PPI::Structure::List") or $enclosing_node->isa("PPI::Structure::Block") 74 22 96
Condition100%3/3
$enclosing_node->isa("PPI::Structure::List") or $enclosing_node->isa("PPI::Structure::Block")
l!l&&r!l&&!r
15774
MC/DC100%2/2
$enclosing_node->isa("PPI::Structure::List") or $enclosing_node->isa("PPI::Structure::Block")
$enclosing_node->isa("PPI::Structure::List") $enclosing_node->isa("PPI::Structure::Block")
Truth table100%3/3
A or B
A= $enclosing_node->isa("PPI::Structure::List")B= $enclosing_node->isa("PPI::Structure::Block")
ABresult
1X1
011
000
401 || $enclosing_node->isa('PPI::Structure::Block') );
402  
403 22 ▶ return if $enclosing_node->schild(0) != $expression;
Branch: 1 branch
BranchTrueFalseTotal
if $enclosing_node->schild(0) != $expression 1 21 22
404  
405 21 ▶ if ( my $left_uncle = $enclosing_node->sprevious_sibling ){
Branch: 1 branch
BranchTrueFalseTotal
if (my $left_uncle = $enclosing_node->sprevious_sibling) 21 0 21
406 21 ▶ return exists $functions_that_take_handleish_things{ $left_uncle }
Condition66%2/3
exists $functions_that_take_handleish_things{$left_uncle} && is_function_call($left_uncle)
!ll&&!rl&&r
9012
MC/DC50%1/2
exists $functions_that_take_handleish_things{$left_uncle} && is_function_call($left_uncle)
exists $functions_that_take_handleish_things{$left_uncle} is_function_call($left_uncle)
Truth table66%2/3
A && B
A= exists $functions_that_take_handleish_things{$left_uncle}B= is_function_call($left_uncle)
ABresult
0X0
100
111
407 && is_function_call( $left_uncle );
408 }
409  
410 0 return;
411 }
412  
413 #-----------------------------------------------------------------------------
414  
415 1 my %functions_that_take_labels =
416 hashify( qw( last next redo ) );
417  
418 # The following is cribbed shamelessly from _looks_like_filehandle. TRW
419  
420 147 ▶sub _smells_like_label {
Subroutine _smells_like_label: called CC=8 SCAR=21.8
421 147 my ($elem) = @_;
422 147 ▶ return if not $elem;
Branch: 1 branch
BranchTrueFalseTotal
unless $elem 0 147 147
423  
424 #--------------------------------------------------------------------
425 # This handles calls *without* parens, for example:
426 # next FOO
427 # last BAR
428 # redo BAZ
429  
430 147 ▶ if ( my $left_sib = $elem->sprevious_sibling ){
Branch: 1 branch
BranchTrueFalseTotal
if (my $left_sib = $elem->sprevious_sibling) 63 84 147
431 63 return exists $functions_that_take_labels{ $left_sib };
432 }
433  
434 #--------------------------------------------------------------------
435 # This handles calls *with* parens, for example:
436 # next ( FOO )
437 # last ( BAR )
438 # redo ( BAZ )
439 #
440 # The above actually work, at least under 5.6.2 and 5.14.2.
441 # next { FOO }
442 # does _not_ work under those Perls, so we don't check for it.
443  
444 84 ▶ my $expression = $elem->parent() || return;
Condition50%1/2
$elem->parent || (return)
l!l
840
MC/DC0%0/1
$elem->parent || (return)
$elem->parent
Truth table50%1/2
A
A= $elem->parent
Aresult
11
00
445 84 ▶ my $enclosing_node = $expression->parent() || return;
Condition50%1/2
$expression->parent || (return)
l!l
840
MC/DC0%0/1
$expression->parent || (return)
$expression->parent
Truth table50%1/2
A
A= $expression->parent
Aresult
11
00
446  
447 84 ▶ return if ! ( $enclosing_node->isa('PPI::Structure::List') );
Branch: 1 branch
BranchTrueFalseTotal
unless $enclosing_node->isa("PPI::Structure::List") 78 6 84
448  
449 6 ▶ return if $enclosing_node->schild(0) != $expression;
Branch: 1 branch
BranchTrueFalseTotal
if $enclosing_node->schild(0) != $expression 0 6 6
450  
451 6 ▶ if ( my $left_uncle = $enclosing_node->sprevious_sibling ){
Branch: 1 branch
BranchTrueFalseTotal
if (my $left_uncle = $enclosing_node->sprevious_sibling) 6 0 6
452 6 return exists $functions_that_take_labels{ $left_uncle };
453 }
454  
455 0 return;
456 }
457  
458 #-----------------------------------------------------------------------------
459  
460 26 ▶sub get_all_subs_from_list_of_symbols {
Subroutine get_all_subs_from_list_of_symbols: called CC=2 SCAR=6.9
Pod documented
461 26 my @symbols = @_;
462  
463 26 my @sub_names = grep { m/\A [&\w]/mxs } @symbols;
464 26 for (@sub_names) { s/\A &//mxs; } # Remove optional sigil
465  
466 26 return @sub_names;
467 }
468  
469 #-----------------------------------------------------------------------------
470  
471 20 ▶sub _find_export_assignment {
Subroutine _find_export_assignment: called CC=3 SCAR=11.0
472 20 my ($doc, $export_type) = @_;
473  
474 20 my $wanted = _make_assignment_finder( $export_type );
475 20 my $export_assignments = $doc->find( $wanted );
476 20 ▶ return if not $export_assignments;
Branch: 1 branch
BranchTrueFalseTotal
unless $export_assignments 8 12 20
477  
478 croak qq{Found multiple $export_type lists\n}
479 12 ▶ if @{$export_assignments} > 1;
Branch: 1 branch
BranchTrueFalseTotal
if @{$export_assignments;} > 1 0 12 12
480  
481 12 return $export_assignments->[0];
482 }
483  
484 #-----------------------------------------------------------------------------
485  
486 20 ▶sub _make_assignment_finder {
Subroutine _make_assignment_finder: called CC=1 SCAR=0.0
487 20 my ($wanted_symbol) = @_;
488  
489 #############################################################
490 # This function returns a callback functiaon that is suitable
491 # for use with the PPI::Node::find() method. It will find
492 # all the occurances of the $wanted_symbol where the symbol
493 # is on the immediate left-hand side of the assignment operator.
494 ##############################################################
495  
496 1082 ▶ my $finder = sub {
Subroutine __ANON__: called CC=6 SCAR=18.0
497  
498 1082 my (undef, $elem) = @_;
499  
500 1082 ▶ return 0 if not $elem->isa('PPI::Token::Symbol');
Branch: 1 branch
BranchTrueFalseTotal
unless $elem->isa("PPI::Token::Symbol") 1056 26 1082
501 26 ▶ return 0 if $elem ne $wanted_symbol;
Branch: 1 branch
BranchTrueFalseTotal
if $elem ne $wanted_symbol 13 13 26
502  
503 # Check if symbol is on left-hand side of assignment
504 13 ▶ my $next_sib = $elem->snext_sibling() || return 0;
Condition50%1/2
$elem->snext_sibling || (return 0)
l!l
130
MC/DC0%0/1
$elem->snext_sibling || (return 0)
$elem->snext_sibling
Truth table50%1/2
A
A= $elem->snext_sibling
Aresult
11
00
505 13 ▶ return 0 if not $next_sib->isa('PPI::Token::Operator');
Branch: 1 branch
BranchTrueFalseTotal
unless $next_sib->isa("PPI::Token::Operator") 1 12 13
506 12 ▶ return 0 if $next_sib ne q{=};
Branch: 1 branch
BranchTrueFalseTotal
if $next_sib ne "=" 0 12 12
507  
508 12 return 1;
509 20 };
510  
511 20 return $finder;
512 }
513  
514 #-----------------------------------------------------------------------------
515  
516 12 ▶sub _parse_export_list {
Subroutine _parse_export_list: called CC=7 SCAR=21.1
517 12 my ($export_symbol) = @_;
518  
519 # First element after the symbol should be "="
520 12 my $snext_sibling = $export_symbol->snext_sibling();
521 12 ▶ return if not $snext_sibling;
Branch: 1 branch
BranchTrueFalseTotal
unless $snext_sibling 0 12 12
522  
523  
524 # Gather up remaining elements
525 12 my @left_hand_side;
526 12 ▶ while ( $snext_sibling = $snext_sibling->snext_sibling() ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($snext_sibling = $snext_sibling->snext_sibling) 24 12 36
527 24 push @left_hand_side, $snext_sibling;
528 }
529  
530 # Did we get any?
531 12 ▶ return if not @left_hand_side;
Branch: 1 branch
BranchTrueFalseTotal
unless @left_hand_side 0 12 12
532  
533  
534 #Now parse the rest based on type of first element
535 12 my $first_element = $left_hand_side[0];
536 12 ▶ return parse_quote_words( $first_element )
Branch: 1 branch
BranchTrueFalseTotal
if $first_element->isa("PPI::Token::QuoteLike::Words") 7 5 12
537 if $first_element->isa('PPI::Token::QuoteLike::Words');
538  
539 5 ▶ return parse_simple_list( $first_element )
Branch: 1 branch
BranchTrueFalseTotal
if $first_element->isa("PPI::Structure::List") 5 0 5
540 if $first_element->isa('PPI::Structure::List');
541  
542 0 ▶ return parse_literal_list( @left_hand_side )
Branch: 1 branch
BranchTrueFalseTotal
if $first_element->isa("PPI::Token::Quote") 0 0 0
543 if $first_element->isa('PPI::Token::Quote');
544  
545  
546 0 return; #Don't know what do do!
547 }
548  
549 #-----------------------------------------------------------------------------
550  
551 1 1;
552  
553 __END__
↑ Summary Policy/Modules/RequireExplicitInclusion.pm »