Policy/Subroutines/ProhibitCallsToUnexportedSubs.pm

filter: Statementstmt 92.0% 92 / 100 Branchbran 61.5% 16 / 26 Conditioncond 50.0% 1 / 2 MC/DCmcdc 0.0% 0 / 1 Subroutinesub 90.4% 19 / 21 Podpod 83.3% 5 / 6 totaltotal 85.2% 133 / 156 CC 16 SCAR 11.0
CC
16
Cov
85%
new
6.4
violates
5.0
_parse_subroutine_call
3.0
CRAP 3.0
« Policy/Subroutines/ProhibitExportingUndeclaredSubs.pm ↑ Summary Policy/Subroutines/ProhibitCallsToUndeclaredSubs.pm »
1 package Perl::Critic::Policy::Subroutines::ProhibitCallsToUnexportedSubs;
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 PPI::Document;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use File::PathList;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 1 ▶use Perl::Critic::Utils qw(
Subroutine BEGIN: called CC=1 SCAR=0.0
11 :characters
12 :severities
13 &hashify
14 &is_function_call
15 &is_perl_builtin
16 &is_qualified_name
17 &policy_short_name
18 1 );
19  
20 1 ▶use Perl::Critic::StricterSubs::Utils qw{
Subroutine BEGIN: called CC=1 SCAR=0.0
21 &find_exported_subroutine_names
22 &find_subroutine_calls
23 1 };
24  
25 #-----------------------------------------------------------------------------
26  
27 1 our $VERSION = '0.08';
28  
29 #-----------------------------------------------------------------------------
30  
31 1 my $CONFIG_PATH_SPLIT_REGEX = qr/ \s* [|] \s* /xms;
32  
33 #-----------------------------------------------------------------------------
34  
35 0 ▶sub supported_parameters {
Subroutine supported_parameters: not called CC=1 SCAR=6.9
Pod undocumented
36 0 return qw( at_inc_prefix use_standard_at_inc at_inc_suffix );
37 }
38  
39 18 ▶sub default_severity { return $SEVERITY_HIGH }
Subroutine default_severity: called CC=1 SCAR=0.0
Pod documented
40 0 ▶sub default_themes { return qw( strictersubs bugs ) }
Subroutine default_themes: not called CC=1 SCAR=6.9
Pod documented
41 5 ▶sub applies_to { return 'PPI::Document' }
Subroutine applies_to: called CC=1 SCAR=0.0
Pod documented
42  
43 #-----------------------------------------------------------------------------
44  
45 5 ▶sub new {
Subroutine new: called CC=6 SCAR=18.6
Pod documented
46 5 my ( $class, %config ) = @_;
47 5 my $self = bless {}, $class;
48  
49 5 my @at_inc_prefix;
50 my @at_inc_suffix;
51  
52 5 ▶ if ( defined $config{at_inc_prefix} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $config{"at_inc_prefix"}) 5 0 5
53 @at_inc_prefix =
54 5 split $CONFIG_PATH_SPLIT_REGEX, $config{at_inc_prefix};
55 }
56 5 ▶ if ( defined $config{at_inc_suffix} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $config{"at_inc_suffix"}) 0 5 5
57 @at_inc_prefix =
58 0 split $CONFIG_PATH_SPLIT_REGEX, $config{at_inc_suffix};
59 }
60  
61 5 ▶ my $use_standard_at_inc = $config{use_standard_at_inc} // 1;
Condition50%1/2
$config{"use_standard_at_inc"} // 1
l!l
05
MC/DC0%0/1
$config{"use_standard_at_inc"} // 1
$config{"use_standard_at_inc"}
Truth table50%1/2
A
A= $config{"use_standard_at_inc"}
Aresult
11
00
62  
63 5 my @inc = @at_inc_prefix;
64 5 ▶ if ($use_standard_at_inc) {
Branch: 1 branch
BranchTrueFalseTotal
if ($use_standard_at_inc) 5 0 5
65 5 push @inc, @INC;
66 }
67 5 push @inc, @at_inc_suffix;
68  
69 5 ▶ die policy_short_name(__PACKAGE__), " has no directories in its module search path.\n"
Branch: 1 branch
BranchTrueFalseTotal
unless @inc 0 5 5
70 if not @inc;
71  
72  
73 5 $self->{_inc} = File::PathList->new( paths => \@inc, cache => 1 );
74 5 $self->{_exports_by_package} = {};
75 5 return $self;
76 }
77  
78 #-----------------------------------------------------------------------------
79  
80 4 ▶sub _get_inc {
Subroutine _get_inc: called CC=1 SCAR=0.0
81 4 my $self = shift;
82 4 return $self->{_inc};
83 }
84  
85 28 ▶sub _get_exports_by_package {
Subroutine _get_exports_by_package: called CC=1 SCAR=0.0
86 28 my $self = shift;
87 28 return $self->{_exports_by_package};
88 }
89  
90 #-----------------------------------------------------------------------------
91  
92 5 ▶sub violates {
Subroutine violates: called CC=5 SCAR=16.1
Pod documented
93 5 my ($self, undef, $doc) = @_;
94  
95 5 my @violations;
96 5 my $expl = q{Violates encapsulation};
97  
98 5 for my $sub_call ( find_subroutine_calls($doc) ) {
99 29 ▶ next if not is_qualified_name( $sub_call );
Branch: 1 branch
BranchTrueFalseTotal
unless is_qualified_name($sub_call) 0 29 29
100  
101 29 my ($package, $sub_name) = _parse_subroutine_call( $sub_call );
102 29 ▶ next if _is_builtin_package( $package );
Branch: 1 branch
BranchTrueFalseTotal
if _is_builtin_package($package) 5 24 29
103  
104 24 my $exports = $self->_get_exports_for_package( $package );
105 24 ▶ if ( not exists $exports->{ $sub_name } ){
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $exports->{$sub_name}) 18 6 24
106  
107 18 my $desc = qq{Subroutine "$sub_name" not exported by "$package"};
108 18 push @violations, $self->violation( $desc, $expl, $sub_call );
109 }
110  
111 }
112  
113 5 return @violations;
114 }
115  
116 #-----------------------------------------------------------------------------
117  
118 29 ▶sub _parse_subroutine_call {
Subroutine _parse_subroutine_call: called CC=3 SCAR=11.1
119 29 my ($sub_call) = @_;
120 29 ▶ return if not $sub_call;
Branch: 1 branch
BranchTrueFalseTotal
unless $sub_call 0 29 29
121  
122 29 my $sub_name = $EMPTY;
123 29 my $package_name = $EMPTY;
124  
125 29 ▶ if ($sub_call =~ m/ \A &? (.*) :: ([^:]+) \z /xms) {
Branch: 1 branch
BranchTrueFalseTotal
if ($sub_call =~ / \A &? (.*) :: ([^:]+) \z /msx) 29 0 29
126 29 $package_name = $1;
127 29 $sub_name = $2;
128 }
129  
130 29 return ($package_name, $sub_name);
131 }
132  
133  
134 #-----------------------------------------------------------------------------
135  
136 24 ▶sub _get_exports_for_package {
Subroutine _get_exports_for_package: called CC=3 SCAR=11.0
137 24 my ( $self, $package_name ) = @_;
138  
139 24 my $exports = $self->_get_exports_by_package()->{$package_name};
140 24 ▶ if (not $exports) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($exports) 4 20 24
141 4 $exports = {};
142  
143 4 my $file_name =
144 $self->_get_file_name_for_package_name( $package_name );
145  
146 4 ▶ if ($file_name) {
Branch: 1 branch
BranchTrueFalseTotal
if ($file_name) 4 0 4
147 4 $exports =
148 { hashify( _get_exports_from_file( $file_name ) ) };
149 }
150  
151 4 $self->_get_exports_by_package()->{$package_name} = $exports;
152 }
153  
154 24 return $exports;
155 }
156  
157 #-----------------------------------------------------------------------------
158  
159 4 ▶sub _get_exports_from_file {
Subroutine _get_exports_from_file: called CC=2 SCAR=7.9
160 4 my ($file_name) = @_;
161  
162 4 my $doc = PPI::Document->new($file_name);
163 4 ▶ if (not $doc) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($doc) 0 4 4
164 0 my $pname = policy_short_name(__PACKAGE__);
165 0 die "$pname: could not parse $file_name: $PPI::Document::errstr\n";
166 }
167  
168 4 return find_exported_subroutine_names( $doc );
169 }
170  
171 #-----------------------------------------------------------------------------
172  
173 4 ▶sub _get_file_name_for_package_name {
Subroutine _get_file_name_for_package_name: called CC=1 SCAR=0.0
174 4 my ($self, $package_name) = @_;
175  
176 4 my $partial_path = $package_name;
177 4 $partial_path =~ s{::}{/}xmsg;
178 4 $partial_path .= '.pm';
179  
180 4 my $full_path = $self->_find_file_in_at_INC( $partial_path );
181 4 return $full_path;
182 }
183  
184 #-----------------------------------------------------------------------------
185  
186 4 ▶sub _find_file_in_at_INC { ## no critic (NamingConventions::Capitalization)
Subroutine _find_file_in_at_INC: called CC=2 SCAR=8.1
187 4 my ($self, $partial_path) = @_;
188  
189 4 my $inc = $self->_get_inc();
190 4 my $full_path = $inc->find_file( $partial_path );
191  
192 4 ▶ if (not $full_path) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($full_path) 0 4 4
193 #TODO reinstate Elliot's error message here.
194 0 my $policy_name = policy_short_name( __PACKAGE__ );
195 0 warn qq{$policy_name: Cannot find source file "$partial_path"\n};
196 0 return;
197 }
198  
199 4 return $full_path;
200 }
201  
202 #-----------------------------------------------------------------------------
203  
204 1 my %BUILTIN_PACKAGES = hashify( qw(CORE CORE::GLOBAL UNIVERSAL main), $EMPTY );
205  
206 29 ▶sub _is_builtin_package {
Subroutine _is_builtin_package: called CC=1 SCAR=0.0
207 29 my ($package_name) = @_;
208 29 return exists $BUILTIN_PACKAGES{$package_name};
209 }
210  
211 #-----------------------------------------------------------------------------
212  
213 1 1;
214  
215 __END__
« Policy/Subroutines/ProhibitExportingUndeclaredSubs.pm ↑ Summary Policy/Subroutines/ProhibitCallsToUndeclaredSubs.pm »