Policy/Modules/RequireExplicitInclusion.pm

filter: Statementstmt 93.8% 123 / 131 Branchbran 80.5% 29 / 36 Conditioncond 85.7% 30 / 35 MC/DCmcdc 72.2% 13 / 18 Subroutinesub 96.6% 29 / 30 Podpod 80.0% 4 / 5 totaltotal 89.4% 228 / 255 CC 33 SCAR 12.7
CC
33
Cov
90%
_find_violations
8.0
_parse_modules
6.2
_handle_module_specification
5.1
CRAP 3.6
« StricterSubs/Utils.pm ↑ Summary Policy/Subroutines/ProhibitExportingUndeclaredSubs.pm »
1 package Perl::Critic::Policy::Modules::RequireExplicitInclusion;
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 List::MoreUtils qw( any );
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use Readonly;
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_class_name
15 &is_function_call
16 &is_perl_builtin
17 &is_qualified_name
18 &policy_short_name
19 1 );
20  
21 1 ▶use Perl::Critic::Exception::Configuration::Option::Policy::ParameterValue qw(
Subroutine BEGIN: called CC=1 SCAR=0.0
22 throw_policy_value
23 1 );
24  
25 1 ▶use Perl::Critic::StricterSubs::Utils qw(
Subroutine BEGIN: called CC=1 SCAR=0.0
26 &get_package_names_from_include_statements
27 &get_package_names_from_package_statements
28 1 );
29  
30 #-----------------------------------------------------------------------------
31  
32 1 our $VERSION = '0.08';
33  
34 1 my $expl =
35 'Without importing a package, it is unlikely that references to things inside it even exist.';
36  
37 #-----------------------------------------------------------------------------
38  
39 28 ▶sub supported_parameters {
Subroutine supported_parameters: called CC=1 SCAR=0.0
Pod undocumented
40 return (
41 {
42 28 name => 'ignore_modules',
43 description => 'The names of modules to ignore if a violation is found',
44 default_string => q{},
45 parser => \&_parse_modules,
46 },
47 );
48 }
49 27 ▶sub default_severity { return $SEVERITY_HIGH }
Subroutine default_severity: called CC=1 SCAR=0.0
Pod documented
50 0 ▶sub default_themes { return qw( strictersubs bugs ) }
Subroutine default_themes: not called CC=1 SCAR=6.9
Pod documented
51 28 ▶sub applies_to { return 'PPI::Document' }
Subroutine applies_to: called CC=1 SCAR=0.0
Pod documented
52  
53 #-----------------------------------------------------------------------------
54  
55 1 Readonly my $MODULE_NAME_REGEX =>
56 qr{
57 \b
58 [[:alpha:]_]
59 (?:
60 (?: \w | :: )*
61 \w
62 )?
63 \b
64 }xms;
65 1 Readonly my $REGULAR_EXPRESSION_REGEX => qr{ [/] ( [^/]+ ) [/] }xms;
66  
67 # It's kind of unfortunate that I had to put capturing parentheses in the
68 # component regexes above, because they're not visible here and so make
69 # figuring out the positions of captures hard. Too bad we can't make the
70 # minimum perl version 5.10. :]
71 1 Readonly my $MODULES_REGEX =>
72 qr{
73 \A
74 \s*
75 (?:
76 ( $MODULE_NAME_REGEX )
77 | $REGULAR_EXPRESSION_REGEX
78 )
79 \s*
80 }xms;
81  
82 #-----------------------------------------------------------------------------
83  
84 28 ▶sub _parse_modules {
Subroutine _parse_modules: called CC=6 SCAR=18.3
85 28 my ($self, $parameter, $config_string) = @_;
86  
87 28 ▶ my $module_specifications = $config_string // $parameter->get_default_string();
Condition66%2/3
$config_string // $parameter->get_default_string
l!l&&r!l&&!r
2260
MC/DC0%0/2
$config_string // $parameter->get_default_string
$config_string $parameter->get_default_string
Truth table66%2/3
A // B
A= $config_stringB= $parameter->get_default_string
ABresult
1X1
011
000
88  
89 28 ▶ return if not $module_specifications;
Branch: 1 branch
BranchTrueFalseTotal
unless $module_specifications 26 2 28
90 2 ▶ return if $module_specifications =~ m{ \A \s* \z }xms;
Branch: 1 branch
BranchTrueFalseTotal
if $module_specifications =~ / \A \s* \z /msx 0 2 2
91  
92 2 ▶ while ( $module_specifications =~ s{ $MODULES_REGEX }{}xms ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($module_specifications =~ s/ $MODULES_REGEX //msx) 2 2 4
93 2 my ($module, $regex_string) = ($1, $2);
94  
95 2 $self->_handle_module_specification(
96 module => $module,
97 regex_string => $regex_string,
98 option_name => 'ignore_modules',
99 option_value => $config_string,
100 );
101 }
102  
103 2 ▶ if ($module_specifications) {
Branch: 1 branch
BranchTrueFalseTotal
if ($module_specifications) 0 2 2
104 0 throw_policy_value
105 policy => $self->get_short_name(),
106 option_name => 'ignore_modules',
107 option_value => $config_string,
108 message_suffix =>
109 qq{contains unparseable data: "$module_specifications"};
110 }
111  
112 2 return;
113 }
114  
115  
116 2 ▶sub _handle_module_specification {
Subroutine _handle_module_specification: called CC=5 SCAR=16.3
117 2 my ($self, %arguments) = @_;
118  
119 2 ▶ if ( my $regex_string = $arguments{regex_string} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $regex_string = $arguments{'regex_string'}) { } 1 1 2
120 # These are module name patterns (e.g. /Acme/)
121 1 my $actual_regex;
122  
123 1 eval { $actual_regex = qr/$regex_string/; 1 } ## no critic (ExtendedFormatting, LineBoundaryMatching, DotMatchAnything)
124 or throw_policy_value
125 policy => $self->get_short_name(),
126 option_name => $arguments{option_name},
127 option_value => $arguments{option_value},
128 1 ▶ message_suffix =>
Branch: 1 branch
BranchTrueFalseTotal
eval { do { $actual_regex = qr/$regex_string/; 1 } } or throw_policy_value("policy", $self->get_short_name, "option_name", $arguments{"option_name"}, "option_value", $arguments{"option_value"}, "message_suffix", qq[contains an invalid regular expression: "$regex_string"]) 1 0 1
129 qq{contains an invalid regular expression: "$regex_string"};
130  
131 # Can't use a hash due to stringification, so this is an AoA.
132 1 ▶ $self->{_ignore_modules_regexes} ||= [];
Condition50%1/2
$self->{"_ignore_modules_regexes"} ||= []
l!l
01
MC/DC0%0/1
$self->{"_ignore_modules_regexes"} ||= []
$self->{"_ignore_modules_regexes"}
Truth table50%1/2
A
A= $self->{"_ignore_modules_regexes"}
Aresult
11
00
133  
134 push
135 1 @{ $self->{_ignore_modules_regexes} },
136 $actual_regex;
137 }
138 else {
139 # These are literal module names (e.g. Acme::Foo)
140 1 ▶ $self->{_ignore_modules} ||= {};
Condition50%1/2
$self->{"_ignore_modules"} ||= {}
l!l
01
MC/DC0%0/1
$self->{"_ignore_modules"} ||= {}
$self->{"_ignore_modules"}
Truth table50%1/2
A
A= $self->{"_ignore_modules"}
Aresult
11
00
141 1 $self->{_ignore_modules}{ $arguments{module} } = undef;
142 }
143  
144 2 return;
145 }
146  
147 #-----------------------------------------------------------------------------
148  
149 28 ▶sub violates {
Subroutine violates: called CC=3 SCAR=13.2
Pod documented
150 28 my ($self, undef, $doc) = @_;
151  
152 28 my @declared_packages = get_package_names_from_package_statements($doc);
153  
154 28 ▶ if ( @declared_packages > 1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@declared_packages > 1) 0 28 28
155 0 ▶ my $fname = $doc->filename() || 'unknown';
Condition0%0/2
$doc->filename || 'unknown'
l!l
00
MC/DC0%0/1
$doc->filename || 'unknown'
$doc->filename
Truth table0%0/2
A
A= $doc->filename
Aresult
11
00
156 0 my $pname = policy_short_name(__PACKAGE__);
157 0 warn qq{$pname: Cannot cope with multiple packages in file "$fname"\n};
158 0 return;
159 }
160  
161 28 my @included_packages = get_package_names_from_include_statements($doc);
162 28 my @builtin_packages = ( qw(main UNIVERSAL CORE CORE::GLOBAL utf8), $EMPTY );
163  
164 28 my %all_packages =
165 hashify( @declared_packages, @included_packages, @builtin_packages );
166  
167 28 my @violations = (
168 $self->_find_subroutine_call_violations( $doc, \%all_packages ),
169 $self->_find_class_method_call_violations( $doc, \%all_packages ),
170 $self->_find_symbol_violations( $doc, \%all_packages ),
171 );
172  
173 28 return @violations;
174 }
175  
176 #-----------------------------------------------------------------------------
177  
178 28 ▶sub _find_qualified_subroutine_calls {
Subroutine _find_qualified_subroutine_calls: called CC=2 SCAR=6.9
179 28 my $doc = shift;
180  
181 my $calls =
182 $doc->find(
183 1088 ▶ sub {
Subroutine __ANON__: called CC=3 SCAR=11.0
184 1088 my (undef, $elem) = @_;
185  
186 return
187 1088 ▶ $elem->isa('PPI::Token::Word')
Condition100%6/6
$elem->isa('PPI::Token::Word') && is_qualified_name($elem->content) && is_function_call($elem)
!ll&&!rl&&r
10561715
$elem->isa("PPI::Token::Word") and is_qualified_name($elem->content)
!ll&&!rl&&r
92912732
MC/DC100%3/3
$elem->isa('PPI::Token::Word') && is_qualified_name($elem->content) && is_function_call($elem)
$elem->isa("PPI::Token::Word") is_qualified_name($elem->content) is_function_call($elem)
Truth table100%4/4
A and B && C
A= $elem->isa("PPI::Token::Word")B= is_qualified_name($elem->content)C= is_function_call($elem)
ABCresult
0XX0
10X0
1100
1111
188 && is_qualified_name( $elem->content() )
189 && is_function_call( $elem );
190  
191 }
192 28 );
193  
194 28 ▶ return @{$calls} if $calls;
Branch: 1 branch
BranchTrueFalseTotal
if $calls 6 22 28
195 22 return;
196 }
197  
198 #-----------------------------------------------------------------------------
199  
200 28 ▶sub _find_class_method_calls {
Subroutine _find_class_method_calls: called CC=2 SCAR=6.9
201 28 my $doc = shift;
202  
203 my $calls =
204 $doc->find(
205 1088 ▶ sub {
Subroutine __ANON__: called CC=4 SCAR=13.9
206 1088 my (undef, $elem) = @_;
207  
208 return
209 1088 ▶ $elem->isa('PPI::Token::Word')
Condition100%9/9
$elem->isa('PPI::Token::Word') && is_class_name($elem) && !is_perl_builtin($elem) && '__PACKAGE__' ne $elem->content
!ll&&!rl&&r
1066121
$elem->isa("PPI::Token::Word") and is_class_name($elem) and not is_perl_builtin($elem)
!ll&&!rl&&r
1065122
$elem->isa("PPI::Token::Word") and is_class_name($elem)
!ll&&!rl&&r
92913623
MC/DC100%4/4
$elem->isa('PPI::Token::Word') && is_class_name($elem) && !is_perl_builtin($elem) && '__PACKAGE__' ne $elem->content
$elem->isa("PPI::Token::Word") is_class_name($elem) not is_perl_builtin($elem) '__PACKAGE__' ne $elem->content
Truth table100%5/5
A and B and not(C) && D
A= $elem->isa("PPI::Token::Word")B= is_class_name($elem)C= not is_perl_builtin($elem)D= '__PACKAGE__' ne $elem->content
ABCDresult
0XXX0
10XX0
110X0
11100
11111
210 && is_class_name( $elem )
211 && !is_perl_builtin( $elem )
212 && '__PACKAGE__' ne $elem->content(); # RT 43314, 44609
213 # From a design standpoint we should filter later, but
214 # the violation code is generic. The patch included with
215 # 44609, or adding '__PACKAGE__ to @builtin_packages,
216 # would have also allowed, willy-nilly,
217 # __PACKAGE__::foo() or $__PACKAGE__::foo, neither of
218 # which is correct. So I just hid __PACKAGE__->foo() from
219 # the violation logic. Mea culpa! Tom Wyant
220 }
221 28 );
222  
223 28 ▶ return @{$calls} if $calls;
Branch: 1 branch
BranchTrueFalseTotal
if $calls 7 21 28
224 21 return;
225 }
226  
227 #-----------------------------------------------------------------------------
228  
229 28 ▶sub _find_qualified_symbols {
Subroutine _find_qualified_symbols: called CC=2 SCAR=6.9
230 28 my $doc = shift;
231  
232 my $symbols =
233 $doc->find(
234 1088 ▶ sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
235 1088 my (undef, $elem) = @_;
236  
237 return
238 1088 ▶ $elem->isa('PPI::Token::Symbol')
Condition100%3/3
$elem->isa('PPI::Token::Symbol') && is_qualified_name($elem->canonical)
!ll&&!rl&&r
1050632
MC/DC100%2/2
$elem->isa('PPI::Token::Symbol') && is_qualified_name($elem->canonical)
$elem->isa('PPI::Token::Symbol') is_qualified_name($elem->canonical)
Truth table100%3/3
A && B
A= $elem->isa('PPI::Token::Symbol')B= is_qualified_name($elem->canonical)
ABresult
0X0
100
111
239 && is_qualified_name( $elem->canonical() );
240 }
241 28 );
242  
243 28 ▶ return @{$symbols} if $symbols;
Branch: 1 branch
BranchTrueFalseTotal
if $symbols 18 10 28
244 10 return;
245 }
246  
247 #-----------------------------------------------------------------------------
248  
249 21 ▶sub _extract_package_from_class_method_call {
Subroutine _extract_package_from_class_method_call: called CC=1 SCAR=0.0
250  
251 # Class method calls look like "Foo::Bar->baz()"
252 # So the package name will be the entire word,
253 # which should be everything to the left of "->"
254  
255 21 my $word = shift;
256  
257 # Remove trailing double colon, which is allowed and can be used for
258 # disambiguation.
259 21 $word =~ s/::$//xms;
260  
261 21 return $word;
262 }
263  
264 #-----------------------------------------------------------------------------
265  
266 15 ▶sub _extract_package_from_subroutine_call {
Subroutine _extract_package_from_subroutine_call: called CC=2 SCAR=7.6
267  
268 # Subroutine calls look like "Foo::Bar::baz()"
269 # So the package name will be everything up
270 # to (but not including) the last "::".
271  
272 15 my $word = shift;
273 15 ▶ if ($word->content() =~ m/\A ( .* ) :: [^:]* \z/xms) {
Branch: 1 branch
BranchTrueFalseTotal
if ($word->content =~ /\A ( .* ) :: [^:]* \z/msx) 15 0 15
274 15 return $1;
275 }
276  
277 0 return;
278 }
279  
280 #-----------------------------------------------------------------------------
281  
282 32 ▶sub _extract_package_from_symbol {
Subroutine _extract_package_from_symbol: called CC=2 SCAR=7.6
283  
284 # Qualified symbols look like "$Foo::Bar::baz"
285 # So the package name will be everything between
286 # the sigil and the last "::".
287  
288 32 my $symbol = shift;
289 32 ▶ if ($symbol->canonical() =~ m/\A [\$*@%&] ( .* ) :: [^:]+ \z/xms) {
Branch: 1 branch
BranchTrueFalseTotal
if ($symbol->canonical =~ /\A [\$*\@%&] ( .* ) :: [^:]+ \z/msx) 32 0 32
290 32 return $1;
291 }
292  
293 0 return;
294 }
295  
296 #-----------------------------------------------------------------------------
297  
298 84 ▶sub _find_violations {
Subroutine _find_violations: called CC=8 SCAR=20.8
299  
300 84 my ($self, $doc, $included_packages, $finder, $package_extractor) = @_;
301 84 my @violations;
302  
303 84 for my $call ( $finder->( $doc ) ) {
304 68 my $package = $package_extractor->( $call );
305 68 ▶ next if exists $included_packages->{ $package };
Branch: 1 branch
BranchTrueFalseTotal
if exists $included_packages->{$package} 34 34 68
306 34 ▶ next if $self->_is_ignored_module( $package );
Branch: 1 branch
BranchTrueFalseTotal
if $self->_is_ignored_module($package) 2 32 34
307  
308 32 ▶ next if exists $self->{_ignore_modules}->{ $package };
Branch: 1 branch
BranchTrueFalseTotal
if exists $self->{"_ignore_modules"}{$package} 2 30 32
309  
310 30 ▶ if ( not ( $call eq 'STDIN' || $call eq 'STDOUT' || $call eq 'STDERR' ) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($call eq "STDIN" or $call eq "STDOUT" or $call eq "STDERR") 27 3 30
Condition100%6/6
$call eq "STDIN" or $call eq "STDOUT" or $call eq "STDERR"
l!l&&r!l&&!r
2127
$call eq "STDIN" or $call eq "STDOUT"
l!l&&r!l&&!r
1128
MC/DC100%3/3
$call eq "STDIN" or $call eq "STDOUT" or $call eq "STDERR"
$call eq "STDIN" $call eq "STDOUT" $call eq "STDERR"
Truth table100%4/4
A or B or C
A= $call eq "STDIN"B= $call eq "STDOUT"C= $call eq "STDERR"
ABCresult
1XX1
01X1
0011
0000
311 27 my $desc = qq{Use of "$call" without including "$package"};
312 27 push @violations, $self->violation( $desc, $expl, $call );
313 }
314 }
315  
316 84 return @violations;
317 }
318  
319  
320 34 ▶sub _is_ignored_module {
Subroutine _is_ignored_module: called CC=4 SCAR=13.9
321 34 my ($self, $package) = @_;
322  
323 34 ▶ my $ignore_hash = ($self->{_ignore_modules} //= {});
Condition100%2/2
$self->{"_ignore_modules"} //= {}
l!l
2410
MC/DC100%1/1
$self->{"_ignore_modules"} //= {}
$self->{"_ignore_modules"}
Truth table100%2/2
A
A= $self->{"_ignore_modules"}
Aresult
11
00
324 34 ▶ return if $ignore_hash->{ $package };
Branch: 1 branch
BranchTrueFalseTotal
if $ignore_hash->{$package} 0 34 34
325  
326 34 ▶ if ( my $ignore_regex_list = $self->{_ignore_modules_regexes} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $ignore_regex_list = $self->{"_ignore_modules_regexes"}) 2 32 34
327 2 ▶ return any { $package =~ /$_/smx } @{$ignore_regex_list};
Subroutine __ANON__: called CC=1 SCAR=0.0
328 }
329  
330 32 return 0;
331 }
332  
333 #-----------------------------------------------------------------------------
334  
335 28 ▶sub _find_subroutine_call_violations {
Subroutine _find_subroutine_call_violations: called CC=1 SCAR=0.0
336 28 my ($self, $doc, $packages) = @_;
337 28 my $finder = \&_find_qualified_subroutine_calls;
338 28 my $extractor = \&_extract_package_from_subroutine_call;
339 28 return $self->_find_violations( $doc, $packages, $finder, $extractor );
340 }
341  
342 #-----------------------------------------------------------------------------
343  
344 28 ▶sub _find_class_method_call_violations {
Subroutine _find_class_method_call_violations: called CC=1 SCAR=0.0
345 28 my ($self, $doc, $packages) = @_;
346 28 my $finder = \&_find_class_method_calls;
347 28 my $extractor = \&_extract_package_from_class_method_call;
348 28 return $self->_find_violations( $doc, $packages, $finder, $extractor );
349 }
350  
351 #-----------------------------------------------------------------------------
352  
353 28 ▶sub _find_symbol_violations {
Subroutine _find_symbol_violations: called CC=1 SCAR=0.0
354 28 my ($self, $doc, $packages) = @_;
355 28 my $finder = \&_find_qualified_symbols;
356 28 my $extractor = \&_extract_package_from_symbol;
357 28 return $self->_find_violations( $doc, $packages, $finder, $extractor );
358 }
359  
360 #-----------------------------------------------------------------------------
361  
362 1 1;
363  
364 __END__
« StricterSubs/Utils.pm ↑ Summary Policy/Subroutines/ProhibitExportingUndeclaredSubs.pm »