Policy/Subroutines/ProhibitCallsToUndeclaredSubs.pm

filter: Statementstmt 94.5% 52 / 55 Branchbran 91.6% 11 / 12 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 84.6% 11 / 13 Podpod 83.3% 5 / 6 totaltotal 91.8% 79 / 86 CC 10 SCAR 9.7
CC
10
Cov
94%
violates
5.0
new
3.0
_is_exempt_subroutine
3.0
CRAP 2.6
« Policy/Subroutines/ProhibitCallsToUnexportedSubs.pm ↑ Summary StricterSubs.pm »
1 package Perl::Critic::Policy::Subroutines::ProhibitCallsToUndeclaredSubs;
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 1 ▶use base 'Perl::Critic::Policy';
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 1 ▶use Perl::Critic::StricterSubs::Utils qw(
Subroutine BEGIN: called CC=1 SCAR=0.0
8 &find_declared_constant_names
9 &find_declared_subroutine_names
10 &find_imported_subroutine_names
11 &find_subroutine_calls
12 &get_package_names_from_include_statements
13 1 );
14  
15 1 ▶use Perl::Critic::Utils qw(
Subroutine BEGIN: called CC=1 SCAR=0.0
16 :severities
17 &hashify
18 &is_qualified_name
19 &words_from_string
20 1 );
21  
22 #-----------------------------------------------------------------------------
23  
24 1 our $VERSION = '0.08';
25  
26 #-----------------------------------------------------------------------------
27  
28 0 ▶sub supported_parameters { return qw(exempt_subs) }
Subroutine supported_parameters: not called CC=1 SCAR=6.9
Pod undocumented
29 19 ▶sub default_severity { return $SEVERITY_HIGH }
Subroutine default_severity: called CC=1 SCAR=0.0
Pod documented
30 0 ▶sub default_themes { return qw( strictersubs bugs ) }
Subroutine default_themes: not called CC=1 SCAR=6.9
Pod documented
31 28 ▶sub applies_to { return 'PPI::Document' }
Subroutine applies_to: called CC=1 SCAR=0.0
Pod documented
32  
33 #-----------------------------------------------------------------------------
34  
35 28 ▶sub new {
Subroutine new: called CC=3 SCAR=11.0
Pod documented
36 28 my ( $class, %args ) = @_;
37 28 my $self = bless {}, $class;
38 28 $self->{_exempt_subs} = {};
39  
40 28 ▶ if (defined $args{exempt_subs} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $args{"exempt_subs"}) 5 23 28
41 5 for my $qualified_sub ( words_from_string( $args{exempt_subs} ) ){
42 10 my ($package, $sub_name) = _parse_sub_name( $qualified_sub );
43 10 $self->{_exempt_subs}->{$package}->{$sub_name} = 1;
44 }
45 }
46  
47 28 return $self;
48 }
49  
50 #-----------------------------------------------------------------------------
51  
52 10 ▶sub _parse_sub_name {
Subroutine _parse_sub_name: called CC=2 SCAR=7.4
53  
54 10 my $full_name = shift;
55  
56 10 ▶ if ( $full_name =~ m/\A ( .+ ) :: ([^:]+) \z/xms ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($full_name =~ /\A ( .+ ) :: ([^:]+) \z/msx) { } 10 0 10
57  
58 10 my ($package_name, $sub_name) = ($1, $2);
59 10 return ($package_name, $sub_name);
60 }
61 else {
62  
63 0 die qq{Sub name "$full_name" must be fully qualifed.\n};
64 }
65 }
66  
67 #-----------------------------------------------------------------------------
68  
69 69 ▶sub _is_exempt_subroutine {
Subroutine _is_exempt_subroutine: called CC=3 SCAR=11.0
70  
71 69 my ($self, $sub_name, $included_packages) = @_;
72 69 for my $package ( @{$included_packages} ) {
73 90 ▶ return 1 if exists $self->{_exempt_subs}->{$package}->{$sub_name};
Branch: 1 branch
BranchTrueFalseTotal
if exists $self->{"_exempt_subs"}{$package}{$sub_name} 5 85 90
74 }
75  
76 64 return;
77 }
78  
79 #-----------------------------------------------------------------------------
80  
81 28 ▶sub violates {
Subroutine violates: called CC=5 SCAR=16.1
Pod documented
82  
83 28 my ($self, undef, $doc) = @_;
84  
85 28 my @declared_constants = find_declared_constant_names( $doc );
86 28 my @declared_sub_names = find_declared_subroutine_names( $doc );
87 28 my @imported_sub_names = find_imported_subroutine_names( $doc );
88  
89 28 my %defined_sub_names = hashify(@declared_sub_names,
90 @imported_sub_names,
91 @declared_constants);
92  
93 28 my @included_packages = get_package_names_from_include_statements( $doc );
94  
95 28 my @violations;
96 28 for my $elem ( find_subroutine_calls($doc) ){
97  
98 78 ▶ next if is_qualified_name( $elem );
Branch: 1 branch
BranchTrueFalseTotal
if is_qualified_name($elem) 9 69 78
99 69 ▶ next if $self->_is_exempt_subroutine( $elem, \@included_packages );
Branch: 1 branch
BranchTrueFalseTotal
if $self->_is_exempt_subroutine($elem, \@included_packages) 5 64 69
100  
101 64 my ( $name ) = ( $elem =~ m{&?(\w+)}mxs );
102 64 ▶ if ( not exists $defined_sub_names{$name} ){
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $defined_sub_names{$name}) 19 45 64
103 19 my $expl = q{This might be a major bug};
104 19 my $desc = qq{Subroutine "$elem" is neither declared nor explicitly imported};
105 19 push @violations, $self->violation($desc, $expl, $elem);
106 }
107 }
108  
109 28 return @violations;
110 }
111  
112 #-----------------------------------------------------------------------------
113  
114 1 1;
115  
116 __END__
« Policy/Subroutines/ProhibitCallsToUnexportedSubs.pm ↑ Summary StricterSubs.pm »