blib/lib/Perl/Critic/Policy/PreferredModules.pm

filter: Statementstmt 93.7% 347 / 370 Branchbran 76.9% 160 / 208 Conditioncond 64.2% 45 / 70 MC/DCmcdc 35.7% 15 / 42 Subroutinesub 91.9% 57 / 62 Podpod 75.0% 3 / 4 totaltotal 82.9% 627 / 756 CC 150 SCAR 21.2
CC
150
Cov
85%
_tag_name_before
56.0
_parse_export_tags
30.0
_violates_include
13.3
CRAP 8.4
↑ Summary
1 package Perl::Critic::Policy::PreferredModules;
2  
3 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 2 ▶use parent 'Perl::Critic::Policy';
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 2 ▶use Perl::Critic::Utils qw{ :severities :classification :ppi $SEVERITY_MEDIUM $TRUE $FALSE };
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 2 ▶use Perl::Critic::Exception::AggregateConfiguration ();
Subroutine BEGIN: called CC=1 SCAR=0.0
11 2 ▶use Perl::Critic::Exception::Configuration::Generic ();
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 2 ▶use Config::INI::Reader ();
Subroutine BEGIN: called CC=1 SCAR=0.0
14 2 ▶use PPI::Document ();
Subroutine BEGIN: called CC=1 SCAR=0.0
15 2 ▶use File::Spec ();
Subroutine BEGIN: called CC=1 SCAR=0.0
16  
17 44 ▶sub supported_parameters {
Subroutine supported_parameters: called CC=1 SCAR=0.0
Pod undocumented
18 return (
19 {
20 44 name => 'config',
21 description => 'Config::INI file listing recommendations.',
22 behavior => 'string',
23 },
24 );
25 }
26  
27 2 ▶use constant default_severity => $SEVERITY_MEDIUM;
Subroutine BEGIN: called CC=1 SCAR=0.0
28 2 ▶use constant applies_to => qw{ PPI::Statement::Include PPI::Token::Word };
Subroutine BEGIN: called CC=1 SCAR=0.0
29  
30 2 ▶use constant optional_config_attributes => qw{ prefer reason severity message for };
Subroutine BEGIN: called CC=1 SCAR=0.0
31  
32 2 ▶use constant builtin_config_attributes => qw{ prefer reason severity };
Subroutine BEGIN: called CC=1 SCAR=0.0
33  
34 # `[perl/rand]` prefers a module over the builtin function `rand`
35 2 ▶use constant BUILTIN_SECTION => qr{\A perl \s* / \s* (\S+) \z}x;
Subroutine BEGIN: called CC=1 SCAR=0.0
36  
37 2 our $VERSION = '0.005'; # VERSION
38 # ABSTRACT: Provide custom package recommendations
39  
40 42 ▶sub initialize_if_enabled {
Subroutine initialize_if_enabled: called CC=1 SCAR=0.0
Pod documented
41 42 my ( $self, $config ) = @_;
42  
43 42 $self->{_is_enabled} = !! $self->_parse_config( $self->_config_file($config) );
44  
45 31 return $TRUE;
46 }
47  
48 48 ▶sub _config_file {
Subroutine _config_file: called CC=3 SCAR=11.2
49 48 my ( $self, $config ) = @_;
50  
51 48 ▶ $config //= $self->__get_config;
Condition66%2/3
$config //= $self->__get_config
l!l&&r!l&&!r
4260
MC/DC0%0/2
$config //= $self->__get_config
$config $self->__get_config
Truth table66%2/3
A //= B
A= $configB= $self->__get_config
ABresult
1X1
011
000
52  
53 48 ▶ my $cfg_file = $config->get('config') // '';
Condition50%1/2
$config->get("config") // ""
l!l
480
MC/DC0%0/1
$config->get("config") // ""
$config->get("config")
Truth table50%1/2
A
A= $config->get("config")
Aresult
11
00
54 48 $cfg_file =~ s{^~}{$ENV{HOME}};
55  
56 48 return $cfg_file;
57 }
58  
59 # Resolving a `prefer` module for a builtin means loading it, so such a
60 # configuration is only honoured when unsafe policies are allowed.
61 6 ▶sub is_safe {
Subroutine is_safe: called CC=2 SCAR=6.9
Pod documented
62 6 my ($self) = @_;
63  
64 6 ▶ return $self->_loads_modules ? $FALSE : $TRUE;
Branch: 1 branch
BranchTrueFalseTotal
$self->_loads_modules ? : 4 2 6
65 }
66  
67 6 ▶sub _loads_modules {
Subroutine _loads_modules: called CC=3 SCAR=11.3
68 6 my ($self) = @_;
69  
70 # Perl::Critic asks before initialize_if_enabled has parsed anything, so
71 # take a look at the configuration ourselves. Errors are left for
72 # _parse_config to report at the usual point.
73 6 ▶ my $builtins = $self->{_cfg_preferred_builtins} // $self->_scan_builtin_sections;
Condition33%1/3
$self->{"_cfg_preferred_builtins"} // $self->_scan_builtin_sections
l!l&&r!l&&!r
060
MC/DC0%0/2
$self->{"_cfg_preferred_builtins"} // $self->_scan_builtin_sections
$self->{"_cfg_preferred_builtins"} $self->_scan_builtin_sections
Truth table33%1/3
A // B
A= $self->{"_cfg_preferred_builtins"}B= $self->_scan_builtin_sections
ABresult
1X1
011
000
74  
75 6 ▶ return ( grep { defined $_->{prefer} } values %$builtins ) ? $TRUE : $FALSE;
Branch: 1 branch
BranchTrueFalseTotal
grep({defined $_->{'prefer'};} values %$builtins) ? : 4 2 6
76 }
77  
78 6 ▶sub _scan_builtin_sections {
Subroutine _scan_builtin_sections: called CC=5 SCAR=16.2
79 6 my ($self) = @_;
80  
81 6 ▶ my $raw = eval { $self->_read_config } or return {};
Branch: 1 branch
BranchTrueFalseTotal
my $raw = eval { do { $self->_read_config } } or return {} 6 0 6
82 6 ▶ my $cfg = $raw->{config} or return {};
Branch: 1 branch
BranchTrueFalseTotal
my $cfg = $raw->{"config"} or return {} 6 0 6
83  
84 6 my %builtins;
85 6 foreach my $pkg ( keys %$cfg ) {
86 8 ▶ next unless my ($function) = $pkg =~ BUILTIN_SECTION;
Branch: 1 branch
BranchTrueFalseTotal
unless my($function) = $pkg =~ m[\A perl \s* / \s* (\S+) \z]x 3 5 8
87 5 $builtins{$function} = $cfg->{$pkg};
88 }
89  
90 6 return \%builtins;
91 }
92  
93 # Read and parse the INI file, once. is_safe() needs it before
94 # initialize_if_enabled does, and both are given the same answer. Problems are
95 # recorded rather than thrown, so the caller decides when to complain.
96 48 ▶sub _read_config {
Subroutine _read_config: called CC=8 SCAR=20.8
97 48 my ( $self, $cfg_file ) = @_;
98  
99 48 ▶ $cfg_file //= $self->_config_file;
Condition66%2/3
$cfg_file //= $self->_config_file
l!l&&r!l&&!r
4260
MC/DC0%0/2
$cfg_file //= $self->_config_file
$cfg_file $self->_config_file
Truth table66%2/3
A //= B
A= $cfg_fileB= $self->_config_file
ABresult
1X1
011
000
100  
101 48 my $cached = $self->{_raw_config};
102 48 ▶ return $cached if $cached && $cached->{file} eq $cfg_file;
Branch: 1 branch
BranchTrueFalseTotal
if $cached and $cached->{"file"} eq $cfg_file 5 43 48
Condition66%2/3
$cached and $cached->{"file"} eq $cfg_file
!ll&&!rl&&r
4305
MC/DC50%1/2
$cached and $cached->{"file"} eq $cfg_file
$cached $cached->{"file"} eq $cfg_file
Truth table66%2/3
A and B
A= $cachedB= $cached->{"file"} eq $cfg_file
ABresult
0X0
100
111
103  
104 43 my $raw = $self->{_raw_config} = { file => $cfg_file };
105  
106 43 ▶ return $raw if !length $cfg_file;
Branch: 1 branch
BranchTrueFalseTotal
unless length $cfg_file 0 43 43
107  
108 43 ▶ if ( !-e $cfg_file ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (-e $cfg_file) 1 42 43
109 1 $raw->{error} = qq[config file '$cfg_file' does not exist.];
110 1 return $raw;
111 }
112  
113 # slurp the file rather than using `read_file` for compat with Test::MockFile
114 42 my $content;
115 {
116 42 local $/;
117 42 ▶ open my $fh, '<', $cfg_file or do {
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, "<", $cfg_file or do { $raw->{"error"} = "Cannot open config file '${cfg_file}': $!"; return $raw } 41 1 42
118 1 $raw->{error} = qq[Cannot open config file '$cfg_file': $!];
119 1 return $raw;
120 };
121 41 $content = <$fh>;
122 }
123  
124 41 my $parsed;
125 41 ▶ if ( eval { $parsed = Config::INI::Reader->read_string($content); 1 } ) {
Branch: 1 branch
BranchTrueFalseTotal
if (eval { do { $parsed = "Config::INI::Reader"->read_string($content); 1 } }) 40 1 41
126 40 $raw->{config} = $parsed;
127 40 return $raw;
128 }
129 1 $raw->{error} = qq[Invalid configuration file $cfg_file];
130  
131 1 return $raw;
132 }
133  
134 3 ▶sub _throw {
Subroutine _throw: called CC=2 SCAR=7.2
135 3 my ( $self, $msg ) = @_;
136  
137 3 ▶ Perl::Critic::Exception::Configuration::Generic->throw(
Condition50%1/2
$msg // "Unknown Error"
l!l
30
MC/DC0%0/1
$msg // "Unknown Error"
$msg
Truth table50%1/2
A
A= $msg
Aresult
11
00
138 message => __PACKAGE__ . ' ' . ( $msg // 'Unknown Error' ),
139 );
140 }
141  
142 # Collect a configuration error rather than dying on the spot, so a single run
143 # can report every problem in the file at once.
144 10 ▶sub _add_exception {
Subroutine _add_exception: called CC=3 SCAR=11.3
145 10 my ( $self, $msg ) = @_;
146  
147 10 ▶ $self->{_config_errors} //= Perl::Critic::Exception::AggregateConfiguration->new();
Condition66%2/3
$self->{"_config_errors"} //= "Perl::Critic::Exception::AggregateConfiguration"->new
l!l&&r!l&&!r
280
MC/DC0%0/2
$self->{"_config_errors"} //= "Perl::Critic::Exception::AggregateConfiguration"->new
$self->{"_config_errors"} "Perl::Critic::Exception::AggregateConfiguration"->new
Truth table66%2/3
A //= B
A= $self->{"_config_errors"}B= "Perl::Critic::Exception::AggregateConfiguration"->new
ABresult
1X1
011
000
148  
149 $self->{_config_errors}->add_exception(
150 10 ▶ Perl::Critic::Exception::Configuration::Generic->new(
Condition50%1/2
$msg // "Unknown Error"
l!l
100
MC/DC0%0/1
$msg // "Unknown Error"
$msg
Truth table50%1/2
A
A= $msg
Aresult
11
00
151 message => __PACKAGE__ . ' ' . ( $msg // 'Unknown Error' ),
152 )
153 );
154  
155 10 return;
156 }
157  
158 39 ▶sub _throw_collected_exceptions {
Subroutine _throw_collected_exceptions: called CC=3 SCAR=11.4
159 39 my ($self) = @_;
160  
161 39 ▶ my $errors = delete $self->{_config_errors} or return;
Branch: 1 branch
BranchTrueFalseTotal
my $errors = delete $self->{"_config_errors"} or return 8 31 39
162  
163 8 ▶ die $errors if $errors->has_exceptions();
Branch: 1 branch
BranchTrueFalseTotal
if $errors->has_exceptions 8 0 8
164  
165 0 return;
166 }
167  
168 42 ▶sub _parse_config {
Subroutine _parse_config: called CC=8 SCAR=20.8
169 42 my ( $self, $cfg_file ) = @_;
170  
171 42 my $raw = $self->_read_config($cfg_file);
172  
173 # A file we cannot read or parse at all is fatal on the spot; only the
174 # per-package settings below are collected and reported together.
175 42 ▶ $self->_throw( $raw->{error} ) if $raw->{error};
Branch: 1 branch
BranchTrueFalseTotal
if $raw->{"error"} 3 39 42
176  
177 39 ▶ my $preferred_cfg = $raw->{config} or return;
Branch: 1 branch
BranchTrueFalseTotal
my $preferred_cfg = $raw->{"config"} or return 39 0 39
178  
179 39 my ( %modules, %builtins );
180  
181 # Config::INI uses '_' for the root/default section — skip it
182 39 delete $preferred_cfg->{'_'};
183  
184 39 foreach my $pkg ( sort keys %$preferred_cfg ) {
185 52 my $setup = $preferred_cfg->{$pkg};
186  
187 52 ▶ if ( $pkg eq 'perl' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pkg eq "perl") 1 51 52
188 1 $self->_add_exception(
189 "Invalid configuration - use a '[perl/<function>]' section to prefer a module over a builtin function");
190 1 next;
191 }
192  
193 51 ▶ if ( my ($function) = $pkg =~ BUILTIN_SECTION ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my($function) = $pkg =~ m[\A perl \s* / \s* (\S+) \z]x) 15 36 51
194 15 $self->_validate_settings( $pkg, $setup, builtin_config_attributes() );
195 15 $builtins{$function} = $setup;
196 15 next;
197 }
198  
199 36 $self->_validate_settings( $pkg, $setup, optional_config_attributes() );
200  
201 36 ▶ if ( defined $setup->{for} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $setup->{"for"}) 4 32 36
202 4 my @functions = grep { length } split m{[\s,]+}, $setup->{for};
203 4 ▶ if ( !@functions ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@functions) 1 3 4
204 1 $self->_add_exception("Invalid configuration - Package '$pkg' has an empty 'for' list");
205 }
206 4 $setup->{_for_functions} = \@functions;
207 }
208  
209 36 $modules{$pkg} = $setup;
210 }
211  
212 39 $self->_throw_collected_exceptions();
213  
214 31 $self->{_cfg_preferred_modules} = \%modules;
215 31 $self->{_cfg_preferred_builtins} = \%builtins;
216  
217 31 return 1;
218 }
219  
220 51 ▶sub _validate_settings {
Subroutine _validate_settings: called CC=5 SCAR=16.1
221 51 my ( $self, $pkg, $setup, @valid_opts ) = @_;
222  
223 51 my %is_valid = map { $_ => 1 } @valid_opts;
224  
225 51 foreach my $opt ( keys %$setup ) {
226 67 ▶ next if $is_valid{$opt};
Branch: 1 branch
BranchTrueFalseTotal
if $is_valid{$opt} 63 4 67
227 4 $self->_add_exception("Invalid configuration - Package '$pkg' is using an unknown setting '$opt'");
228 }
229  
230 51 ▶ if ( defined $setup->{severity} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $setup->{"severity"}) 9 42 51
231 9 my $sev = $setup->{severity};
232 9 ▶ if ( $sev !~ /\A[1-5]\z/ ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($sev =~ /\A[1-5]\z/) 4 5 9
233 4 $self->_add_exception("Invalid configuration - Package '$pkg' has invalid severity '$sev' (must be 1-5)");
234 }
235 }
236  
237 51 return;
238 }
239  
240 61 ▶sub _is_string_token {
Subroutine _is_string_token: called CC=2 SCAR=6.9
241 61 my ($token) = @_;
242  
243 61 ▶ return $token->isa('PPI::Token::QuoteLike::Words') || $token->isa('PPI::Token::Quote');
Condition100%3/3
$token->isa('PPI::Token::QuoteLike::Words') || $token->isa('PPI::Token::Quote')
l!l&&r!l&&!r
16243
MC/DC100%2/2
$token->isa('PPI::Token::QuoteLike::Words') || $token->isa('PPI::Token::Quote')
$token->isa('PPI::Token::QuoteLike::Words') $token->isa('PPI::Token::Quote')
Truth table100%3/3
A || B
A= $token->isa('PPI::Token::QuoteLike::Words')B= $token->isa('PPI::Token::Quote')
ABresult
1X1
011
000
244 }
245  
246 # Extract the real module from a `use if CONDITION, 'Module::Name', IMPORT_LIST`
247 # statement: after the first comma, the next token is the module name.
248 7 ▶sub _extract_use_if_module {
Subroutine _extract_use_if_module: called CC=8 SCAR=20.9
249 7 my ( $self, $elem ) = @_;
250  
251 7 my $found_comma;
252 7 for my $child ( $elem->children ) {
253 73 ▶ if ( $child->isa('PPI::Token::Operator') && $child->content eq ',' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($child->isa("PPI::Token::Operator") and $child->content eq ",") 7 66 73
Condition100%3/3
$child->isa("PPI::Token::Operator") and $child->content eq ","
!ll&&!rl&&r
6247
MC/DC100%2/2
$child->isa("PPI::Token::Operator") and $child->content eq ","
$child->isa("PPI::Token::Operator") $child->content eq ","
Truth table100%3/3
A and B
A= $child->isa("PPI::Token::Operator")B= $child->content eq ","
ABresult
0X0
100
111
254 7 $found_comma = 1;
255 7 next;
256 }
257 66 ▶ next unless $found_comma;
Branch: 1 branch
BranchTrueFalseTotal
unless $found_comma 52 14 66
258 14 ▶ next if $child->isa('PPI::Token::Whitespace');
Branch: 1 branch
BranchTrueFalseTotal
if $child->isa("PPI::Token::Whitespace") 7 7 14
259  
260 7 ▶ if ( $child->isa('PPI::Token::Quote') ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($child->isa("PPI::Token::Quote")) 6 1 7
261 6 return $child->string;
262 }
263 1 ▶ if ( $child->isa('PPI::Token::Word') ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($child->isa("PPI::Token::Word")) 1 0 1
264 1 return $child->content;
265 }
266  
267 0 return;
268 }
269  
270 0 return;
271 }
272  
273 # Return the list of names explicitly imported by an include statement,
274 # e.g. `use Foo qw{ bar baz }` or `use Foo 'bar', 'baz'`.
275 17 ▶sub _imported_names {
Subroutine _imported_names: called CC=7 SCAR=19.8
276 17 my ( $self, $elem ) = @_;
277  
278 17 my @names;
279  
280 17 foreach my $arg ( $elem->arguments ) {
281 my @tokens =
282 $arg->isa('PPI::Node')
283 0 ▶ ? @{ $arg->find( sub { _is_string_token( $_[1] ) } ) || [] }
Subroutine __ANON__: not called CC=1 SCAR=0.2
Branch: 1 branch
BranchTrueFalseTotal
$arg->isa('PPI::Node') ? : 1 13 14
Condition50%1/2
$arg->find(sub { _is_string_token($_[1]); } ) || []
l!l
01
MC/DC0%0/1
$arg->find(sub { _is_string_token($_[1]); } ) || []
$arg->find(sub { _is_string_token($_[1]); } )
Truth table50%1/2
A
A= $arg->find(sub { _is_string_token($_[1]); } )
Aresult
11
00
284 : ($arg);
285  
286 14 foreach my $token (@tokens) {
287 13 ▶ next unless _is_string_token($token);
Branch: 1 branch
BranchTrueFalseTotal
unless _is_string_token($token) 0 13 13
288 13 ▶ push @names, $token->isa('PPI::Token::QuoteLike::Words') ? $token->literal : $token->string;
Branch: 1 branch
BranchTrueFalseTotal
$token->isa('PPI::Token::QuoteLike::Words') ? : 12 1 13
289 }
290 }
291  
292 17 return @names;
293 }
294  
295 # True for `use Foo ()`, which imports nothing, but not for a bare `use Foo`,
296 # which pulls in whatever the module exports by default.
297 8 ▶sub _has_empty_import_list {
Subroutine _has_empty_import_list: called CC=4 SCAR=14.1
298 8 my ( $self, $elem ) = @_;
299  
300 8 my @args = $elem->arguments;
301  
302 8 ▶ return $FALSE unless @args == 1;
Branch: 1 branch
BranchTrueFalseTotal
unless @args == 1 0 8 8
303 8 ▶ return $FALSE unless $args[0]->isa('PPI::Structure::List');
Branch: 1 branch
BranchTrueFalseTotal
unless $args[0]->isa("PPI::Structure::List") 7 1 8
304  
305 1 ▶ return $args[0]->schildren ? $FALSE : $TRUE;
Branch: 1 branch
BranchTrueFalseTotal
$args[0]->schildren ? : 0 1 1
306 }
307  
308 # Look for calls to any of @functions anywhere in the document, either as a
309 # plain function call or fully qualified as $module::function.
310 5 ▶sub _document_calls_any {
Subroutine _document_calls_any: called CC=6 SCAR=18.0
311 5 my ( $self, $elem, $module, @functions ) = @_;
312  
313 5 ▶ my $doc = $elem->top or return $FALSE;
Branch: 1 branch
BranchTrueFalseTotal
my $doc = $elem->top or return $Perl::Critic::Policy::PreferredModules::FALSE 5 0 5
314 5 ▶ return $FALSE unless $doc->isa('PPI::Document');
Branch: 1 branch
BranchTrueFalseTotal
unless $doc->isa("PPI::Document") 0 5 5
315  
316 5 my %wanted = map { ( $_ => 1, "${module}::$_" => 1 ) } @functions;
317  
318 5 ▶ my $words = $doc->find( sub { $_[1]->isa('PPI::Token::Word') && $wanted{ $_[1]->content } } ) or return $FALSE;
Subroutine __ANON__: called CC=2 SCAR=6.9
Branch: 2 branches
BranchTrueFalseTotal
my $words = $doc->find(sub { $_[1]->isa("PPI::Token::Word") and $wanted{$_[1]->content}; } ) or return $Perl::Critic::Policy::PreferredModules::FALSE 3 2 5
$_[1]->isa("PPI::Token::Word") and $wanted{$_[1]->content} 29 147 176
319  
320 3 foreach my $word (@$words) {
321 3 ▶ return $TRUE if is_function_call($word);
Branch: 1 branch
BranchTrueFalseTotal
if is_function_call($word) 2 1 3
322 }
323  
324 1 return $FALSE;
325 }
326  
327 # A `for` entry restricts the preference to the listed functions: the module is
328 # only reported when one of them is actually pulled in or called.
329 35 ▶sub _matches_for {
Subroutine _matches_for: called CC=5 SCAR=16.1
330 35 my ( $self, $elem, $module, $setup ) = @_;
331  
332 35 my $functions = $setup->{_for_functions};
333 35 ▶ return $TRUE unless $functions && @$functions;
Branch: 1 branch
BranchTrueFalseTotal
unless $functions and @$functions 25 10 35
Condition66%2/3
$functions and @$functions
!ll&&!rl&&r
25010
MC/DC50%1/2
$functions and @$functions
$functions @$functions
Truth table66%2/3
A and B
A= $functionsB= @$functions
ABresult
0X0
100
111
334  
335 10 my %wanted = map { $_ => 1 } @$functions;
336  
337 10 foreach my $name ( $self->_imported_names($elem) ) {
338 8 ▶ return $TRUE if $wanted{$name};
Branch: 1 branch
BranchTrueFalseTotal
if $wanted{$name} 5 3 8
339 }
340  
341 5 return $self->_document_calls_any( $elem, $module, @$functions );
342 }
343  
344 80 ▶sub _normalize_export_name {
Subroutine _normalize_export_name: called CC=1 SCAR=0.0
345 80 my ($name) = @_;
346  
347 80 $name =~ s{\A&}{};
348  
349 80 return $name;
350 }
351  
352 # What $module exports, as { default => {}, optional => {}, tags => {} }, or
353 # undef when we cannot tell. Answers are cached: resolving means loading or
354 # parsing the module.
355 20 ▶sub _module_exports {
Subroutine _module_exports: called CC=5 SCAR=16.1
356 20 my ( $self, $module ) = @_;
357  
358 20 ▶ my $cache = $self->{_module_exports} //= {};
Condition100%2/2
$self->{"_module_exports"} //= {}
l!l
128
MC/DC100%1/1
$self->{"_module_exports"} //= {}
$self->{"_module_exports"}
Truth table100%2/2
A
A= $self->{"_module_exports"}
Aresult
11
00
359 20 ▶ return $cache->{$module} if exists $cache->{$module};
Branch: 1 branch
BranchTrueFalseTotal
if exists $cache->{$module} 11 9 20
360  
361 9 $cache->{$module} = undef; # guards against a module resolving to itself
362  
363 9 ▶ return undef unless $module =~ m{\A [A-Za-z_]\w* (?: :: \w+ )* \z}x;
Branch: 1 branch
BranchTrueFalseTotal
unless $module =~ /\A [A-Za-z_]\w* (?: :: \w+ )* \z/x 0 9 9
364  
365 9 ▶ return $cache->{$module} = $self->_load_module_exports($module) // $self->_parse_module_exports($module);
Condition100%3/3
$self->_load_module_exports($module) // $self->_parse_module_exports($module)
l!l&&r!l&&!r
333
MC/DC100%2/2
$self->_load_module_exports($module) // $self->_parse_module_exports($module)
$self->_load_module_exports($module) $self->_parse_module_exports($module)
Truth table100%3/3
A // B
A= $self->_load_module_exports($module)B= $self->_parse_module_exports($module)
ABresult
1X1
011
000
366 }
367  
368 # Preferred source: once the module is loaded its symbol table is what Exporter
369 # itself consults, including anything built at load time or inherited.
370 9 ▶sub _load_module_exports {
Subroutine _load_module_exports: called CC=5 SCAR=16.1
371 9 my ( $self, $module ) = @_;
372  
373 9 my $path = _module_path($module);
374  
375 9 ▶ if ( !$INC{$path} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($INC{$path}) 7 2 9
376 7 ▶ local $SIG{__WARN__} = sub { };
Subroutine __ANON__: not called CC=1 SCAR=0.0
377 7 ▶ eval { require $path; 1 } or return undef;
Branch: 1 branch
BranchTrueFalseTotal
eval { do { require $path; 1 } } or return undef 1 6 7
378 }
379  
380 3 my %tags;
381 3 my %declared_tags = _package_hash( $module, 'EXPORT_TAGS' );
382 3 foreach my $tag ( keys %declared_tags ) {
383 2 my $names = $declared_tags{$tag};
384 2 ▶ next unless ref $names eq 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $names eq "ARRAY" 0 2 2
385 2 $tags{$tag} = { map { _normalize_export_name($_) => 1 } @$names };
386 }
387  
388 return {
389 0 default => { map { _normalize_export_name($_) => 1 } _package_array( $module, 'EXPORT' ) },
390 3 optional => { map { _normalize_export_name($_) => 1 } _package_array( $module, 'EXPORT_OK' ) },
391 tags => \%tags,
392 };
393 }
394  
395 6 ▶sub _package_array {
Subroutine _package_array: called CC=1 SCAR=0.0
396 6 my ( $module, $name ) = @_;
397  
398 2 ▶ no strict 'refs'; ## no critic (ProhibitNoStrict)
Subroutine BEGIN: called CC=1 SCAR=0.0
399 6 return @{"${module}::${name}"};
400 }
401  
402 3 ▶sub _package_hash {
Subroutine _package_hash: called CC=1 SCAR=0.0
403 3 my ( $module, $name ) = @_;
404  
405 2 ▶ no strict 'refs'; ## no critic (ProhibitNoStrict)
Subroutine BEGIN: called CC=1 SCAR=0.0
406 3 return %{"${module}::${name}"};
407 }
408  
409 # Fallback for a module that will not load in this process: read the export
410 # lists straight out of its source. Only literal declarations are understood.
411 6 ▶sub _parse_module_exports {
Subroutine _parse_module_exports: called CC=9 SCAR=23.6
412 6 my ( $self, $module ) = @_;
413  
414 6 ▶ my $file = _find_module_file($module) or return undef;
Branch: 1 branch
BranchTrueFalseTotal
my $file = _find_module_file($module) or return undef 3 3 6
415  
416 3 ▶ my $doc = eval { PPI::Document->new($file) } or return undef;
Branch: 1 branch
BranchTrueFalseTotal
my $doc = eval { do { "PPI::Document"->new($file) } } or return undef 3 0 3
417  
418 3 my %exports = ( default => {}, optional => {}, tags => {} );
419  
420 3 ▶ my $symbols = $doc->find( sub { $_[1]->isa('PPI::Token::Symbol') } ) || [];
Subroutine __ANON__: called CC=1 SCAR=0.2
Condition50%1/2
$doc->find(sub { $_[1]->isa('PPI::Token::Symbol'); } ) || []
l!l
30
MC/DC0%0/1
$doc->find(sub { $_[1]->isa('PPI::Token::Symbol'); } ) || []
$doc->find(sub { $_[1]->isa('PPI::Token::Symbol'); } )
Truth table50%1/2
A
A= $doc->find(sub { $_[1]->isa('PPI::Token::Symbol'); } )
Aresult
11
00
421  
422 3 foreach my $symbol (@$symbols) {
423 5 ▶ my $statement = $symbol->statement or next;
Branch: 1 branch
BranchTrueFalseTotal
my $statement = $symbol->statement or next 5 0 5
424  
425 5 my $kind = { '@EXPORT' => 'default', '@EXPORT_OK' => 'optional' }->{ $symbol->symbol };
426  
427 5 ▶ if ($kind) {
Branch: 2 branches
BranchTrueFalseTotal
if ($kind) { } 5 0 5
elsif ($symbol->symbol eq '%EXPORT_TAGS') { } 0 0 0
428 5 $exports{$kind}{$_} = 1 for _exported_names_in($statement);
429 }
430 elsif ( $symbol->symbol eq '%EXPORT_TAGS' ) {
431 0 _parse_export_tags( $statement, $exports{tags} );
432 }
433 }
434  
435 3 return \%exports;
436 }
437  
438 9 ▶sub _module_path {
Subroutine _module_path: called CC=1 SCAR=0.0
439 9 my ($module) = @_;
440  
441 9 ( my $path = "$module.pm" ) =~ s{::}{/}g;
442  
443 9 return $path;
444 }
445  
446 6 ▶sub _find_module_file {
Subroutine _find_module_file: called CC=4 SCAR=13.9
447 6 my ($module) = @_;
448  
449 6 my @parts = split m{::}, "$module.pm";
450  
451 6 foreach my $dir (@INC) {
452 30 ▶ next if ref $dir;
Branch: 1 branch
BranchTrueFalseTotal
if ref $dir 0 30 30
453 30 my $candidate = File::Spec->catfile( $dir, @parts );
454 30 ▶ return $candidate if -f $candidate;
Branch: 1 branch
BranchTrueFalseTotal
if -f $candidate 3 27 30
455 }
456  
457 3 return undef;
458 }
459  
460 5 ▶sub _within_subscript {
Subroutine _within_subscript: called CC=3 SCAR=11.0
461 5 my ($token) = @_;
462  
463 5 my $parent = $token->parent;
464 5 ▶ while ($parent) {
Branch: 1 branch
BranchTrueFalseTotal
if ($parent) 12 5 17
465 12 ▶ return $TRUE if $parent->isa('PPI::Structure::Subscript');
Branch: 1 branch
BranchTrueFalseTotal
if $parent->isa("PPI::Structure::Subscript") 0 12 12
466 12 $parent = $parent->parent;
467 }
468  
469 5 return $FALSE;
470 }
471  
472 # Literal names in an export declaration. Anything under a subscript is skipped,
473 # so `@EXPORT_OK = ( @{ $EXPORT_TAGS{'all'} } )` does not yield 'all'.
474 5 ▶sub _exported_names_in {
Subroutine _exported_names_in: called CC=5 SCAR=16.2
475 5 my ($element) = @_;
476  
477 5 ▶ my $tokens = $element->find( sub { _is_string_token( $_[1] ) } ) || [];
Subroutine __ANON__: called CC=1 SCAR=0.2
Condition50%1/2
$element->find(sub { _is_string_token($_[1]); } ) || []
l!l
50
MC/DC0%0/1
$element->find(sub { _is_string_token($_[1]); } ) || []
$element->find(sub { _is_string_token($_[1]); } )
Truth table50%1/2
A
A= $element->find(sub { _is_string_token($_[1]); } )
Aresult
11
00
478  
479 5 my @names;
480 5 foreach my $token (@$tokens) {
481 5 ▶ next if _within_subscript($token);
Branch: 1 branch
BranchTrueFalseTotal
if _within_subscript($token) 0 5 5
482 5 ▶ push @names, $token->isa('PPI::Token::QuoteLike::Words') ? $token->literal : $token->string;
Branch: 1 branch
BranchTrueFalseTotal
$token->isa('PPI::Token::QuoteLike::Words') ? : 4 1 5
483 }
484  
485 5 return map { _normalize_export_name($_) } @names;
486 }
487  
488 # Pick up `tag => [ ... ]` pairs from a %EXPORT_TAGS declaration.
489 0 ▶sub _parse_export_tags {
Subroutine _parse_export_tags: not called CC=5 SCAR=34.0
490 0 my ( $statement, $tags ) = @_;
491  
492 0 ▶ my $constructors = $statement->find( sub { $_[1]->isa('PPI::Structure::Constructor') } ) || [];
Subroutine __ANON__: not called CC=1 SCAR=6.9
Condition0%0/2
$statement->find(sub { $_[1]->isa('PPI::Structure::Constructor'); } ) || []
l!l
00
MC/DC0%0/1
$statement->find(sub { $_[1]->isa('PPI::Structure::Constructor'); } ) || []
$statement->find(sub { $_[1]->isa('PPI::Structure::Constructor'); } )
Truth table0%0/2
A
A= $statement->find(sub { $_[1]->isa('PPI::Structure::Constructor'); } )
Aresult
11
00
493  
494 0 foreach my $constructor (@$constructors) {
495 0 ▶ my $tag = _tag_name_before($constructor) or next;
Branch: 1 branch
BranchTrueFalseTotal
my $tag = _tag_name_before($constructor) or next 0 0 0
496  
497 0 my %names = map { $_ => 1 } _exported_names_in($constructor);
498 0 ▶ $tags->{$tag} = \%names if %names;
Branch: 1 branch
BranchTrueFalseTotal
if %names 0 0 0
499 }
500  
501 0 return;
502 }
503  
504 0 ▶sub _tag_name_before {
Subroutine _tag_name_before: not called CC=7 SCAR=40.3
505 0 my ($constructor) = @_;
506  
507 0 ▶ my $fat_comma = $constructor->sprevious_sibling or return undef;
Branch: 1 branch
BranchTrueFalseTotal
my $fat_comma = $constructor->sprevious_sibling or return undef 0 0 0
508 0 ▶ return undef unless $fat_comma->isa('PPI::Token::Operator') && $fat_comma->content eq '=>';
Branch: 1 branch
BranchTrueFalseTotal
unless $fat_comma->isa("PPI::Token::Operator") and $fat_comma->content eq "=>" 0 0 0
Condition0%0/3
$fat_comma->isa("PPI::Token::Operator") and $fat_comma->content eq "=>"
!ll&&!rl&&r
000
MC/DC0%0/2
$fat_comma->isa("PPI::Token::Operator") and $fat_comma->content eq "=>"
$fat_comma->isa("PPI::Token::Operator") $fat_comma->content eq "=>"
Truth table0%0/3
A and B
A= $fat_comma->isa("PPI::Token::Operator")B= $fat_comma->content eq "=>"
ABresult
0X0
100
111
509  
510 0 ▶ my $key = $fat_comma->sprevious_sibling or return undef;
Branch: 1 branch
BranchTrueFalseTotal
my $key = $fat_comma->sprevious_sibling or return undef 0 0 0
511  
512 0 ▶ return $key->content if $key->isa('PPI::Token::Word');
Branch: 1 branch
BranchTrueFalseTotal
if $key->isa("PPI::Token::Word") 0 0 0
513 0 ▶ return $key->string if $key->isa('PPI::Token::Quote');
Branch: 1 branch
BranchTrueFalseTotal
if $key->isa("PPI::Token::Quote") 0 0 0
514  
515 0 return undef;
516 }
517  
518 # True when this include provides $function: either it is named in the import
519 # list, or it comes in through a tag or the module's default exports. When the
520 # exports cannot be resolved we assume it is provided, so an unknown module
521 # never turns into a false positive.
522 14 ▶sub _include_provides {
Subroutine _include_provides: called CC=11 SCAR=24.0
523 14 my ( $self, $include, $module, $function ) = @_;
524  
525 14 my $exports = $self->_module_exports($module);
526  
527 14 my @args = $include->arguments;
528  
529 14 ▶ if ( !@args ) { # a bare `use Module` imports whatever it exports by default
Branch: 1 branch
BranchTrueFalseTotal
unless (@args) 6 8 14
530 6 ▶ return $TRUE unless $exports;
Branch: 1 branch
BranchTrueFalseTotal
unless $exports 1 5 6
531 5 ▶ return $exports->{default}{$function} ? $TRUE : $FALSE;
Branch: 1 branch
BranchTrueFalseTotal
$exports->{'default'}{$function} ? : 2 3 5
532 }
533  
534 8 ▶ return $FALSE if $self->_has_empty_import_list($include);
Branch: 1 branch
BranchTrueFalseTotal
if $self->_has_empty_import_list($include) 1 7 8
535  
536 7 foreach my $imported ( $self->_imported_names($include) ) {
537 7 $imported = _normalize_export_name($imported);
538  
539 7 ▶ return $TRUE if $imported eq $function;
Branch: 1 branch
BranchTrueFalseTotal
if $imported eq $function 3 4 7
540  
541 4 ▶ next unless my ($tag) = $imported =~ m{\A:(.+)\z};
Branch: 1 branch
BranchTrueFalseTotal
unless my($tag) = $imported =~ /\A:(.+)\z/ 2 2 4
542 2 ▶ return $TRUE unless $exports;
Branch: 1 branch
BranchTrueFalseTotal
unless $exports 0 2 2
543  
544 2 my $names = $exports->{tags}{$tag};
545 2 ▶ return $TRUE unless $names; # an unknown tag might well cover it
Branch: 1 branch
BranchTrueFalseTotal
unless $names 1 1 2
546 1 ▶ return $TRUE if $names->{$function};
Branch: 1 branch
BranchTrueFalseTotal
if $names->{$function} 1 0 1
547 }
548  
549 2 return $FALSE;
550 }
551  
552 # True when $module is used in a way that provides $function to the current
553 # document, so a call to $function is already the preferred implementation.
554 19 ▶sub _provides_function {
Subroutine _provides_function: called CC=10 SCAR=23.7
555 19 my ( $self, $elem, $module, $function ) = @_;
556  
557 19 ▶ my $doc = $elem->top or return $FALSE;
Branch: 1 branch
BranchTrueFalseTotal
my $doc = $elem->top or return $Perl::Critic::Policy::PreferredModules::FALSE 19 0 19
558 19 ▶ return $FALSE unless $doc->isa('PPI::Document');
Branch: 1 branch
BranchTrueFalseTotal
unless $doc->isa("PPI::Document") 0 19 19
559  
560 19 ▶ my $includes = $doc->find( sub { $_[1]->isa('PPI::Statement::Include') } ) or return $FALSE;
Subroutine __ANON__: called CC=1 SCAR=0.0
Branch: 1 branch
BranchTrueFalseTotal
my $includes = $doc->find(sub { $_[1]->isa("PPI::Statement::Include"); } ) or return $Perl::Critic::Policy::PreferredModules::FALSE 15 4 19
561  
562 15 foreach my $include (@$includes) {
563 15 ▶ next unless ( $include->module // '' ) eq $module;
Branch: 1 branch
BranchTrueFalseTotal
unless ($include->module // "") eq $module 1 14 15
Condition50%1/2
$include->module // ""
l!l
150
MC/DC0%0/1
$include->module // ""
$include->module
Truth table50%1/2
A
A= $include->module
Aresult
11
00
564 14 ▶ next if ( $include->type // '' ) eq 'require';
Branch: 1 branch
BranchTrueFalseTotal
if ($include->type // "") eq "require" 0 14 14
Condition50%1/2
$include->type // ""
l!l
140
MC/DC0%0/1
$include->type // ""
$include->type
Truth table50%1/2
A
A= $include->type
Aresult
11
00
565  
566 14 ▶ return $TRUE if $self->_include_provides( $include, $module, $function );
Branch: 1 branch
BranchTrueFalseTotal
if $self->_include_provides($include, $module, $function) 8 6 14
567 }
568  
569 7 return $FALSE;
570 }
571  
572 # True when the module has the function but does not hand it over by default,
573 # which is the case worth explaining: the config looks satisfied while the
574 # builtin is still what runs.
575 6 ▶sub _exports_on_request {
Subroutine _exports_on_request: called CC=4 SCAR=14.2
576 6 my ( $self, $module, $function ) = @_;
577  
578 6 ▶ my $exports = $self->_module_exports($module) or return $FALSE;
Branch: 1 branch
BranchTrueFalseTotal
my $exports = $self->_module_exports($module) or return $Perl::Critic::Policy::PreferredModules::FALSE 5 1 6
579  
580 5 ▶ return $FALSE if $exports->{default}{$function};
Branch: 1 branch
BranchTrueFalseTotal
if $exports->{"default"}{$function} 0 5 5
581  
582 5 ▶ return $exports->{optional}{$function} ? $TRUE : $FALSE;
Branch: 1 branch
BranchTrueFalseTotal
$exports->{'optional'}{$function} ? : 5 0 5
583 }
584  
585 56 ▶sub _violates_include {
Subroutine _violates_include: called CC=13 SCAR=25.9
586 56 my ( $self, $elem ) = @_;
587  
588 # 'no Module' unloads/disables — not a use violation
589 56 my $type = $elem->type;
590 56 ▶ return () if defined $type && $type eq 'no';
Branch: 1 branch
BranchTrueFalseTotal
if defined $type and $type eq "no" 2 54 56
Condition66%2/3
defined $type and $type eq "no"
!ll&&!rl&&r
0542
MC/DC50%1/2
defined $type and $type eq "no"
defined $type $type eq "no"
Truth table66%2/3
A and B
A= defined $typeB= $type eq "no"
ABresult
0X0
100
111
591  
592 54 my $module = $elem->module;
593  
594 54 ▶ return () unless defined $module;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $module 0 54 54
595  
596 # Handle 'use if CONDITION, Module' by extracting the real module
597 54 ▶ if ( $module eq 'if' && ( $elem->type // '' ) eq 'use' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($module eq "if" and ($elem->type // "") eq "use") 7 47 54
Condition60%3/5
$module eq "if" and ($elem->type // "") eq "use"
!ll&&!rl&&r
4707
$elem->type // ""
l!l
70
MC/DC50%1/2
$module eq "if" and ($elem->type // "") eq "use"
$module eq "if" $elem->type
Truth table66%2/3
A and B
A= $module eq "if"B= $elem->type
ABresult
0X0
100
111
598 7 $module = $self->_extract_use_if_module($elem);
599 7 ▶ return () unless defined $module;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $module 0 7 7
600 }
601  
602 54 my @violations;
603  
604 54 ▶ if ( my $setup = $self->{_cfg_preferred_modules}->{$module} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $setup = $self->{"_cfg_preferred_modules"}{$module}) 29 25 54
605 29 push @violations, $self->_build_violation( $module, $setup, $elem );
606 }
607  
608 # Also check modules passed as arguments to 'use parent' / 'use base'
609 54 ▶ if ( $module eq 'parent' || $module eq 'base' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($module eq "parent" or $module eq "base") 6 48 54
Condition100%3/3
$module eq "parent" or $module eq "base"
l!l&&r!l&&!r
5148
MC/DC100%2/2
$module eq "parent" or $module eq "base"
$module eq "parent" $module eq "base"
Truth table100%3/3
A or B
A= $module eq "parent"B= $module eq "base"
ABresult
1X1
011
000
610 6 for my $parent_mod ( $self->_extract_parent_modules($elem) ) {
611 8 ▶ next unless my $setup = $self->{_cfg_preferred_modules}->{$parent_mod};
Branch: 1 branch
BranchTrueFalseTotal
unless my $setup = $self->{"_cfg_preferred_modules"}{$parent_mod} 2 6 8
612 6 push @violations, $self->_build_violation( $parent_mod, $setup, $elem );
613 }
614 }
615  
616 54 return @violations;
617 }
618  
619 35 ▶sub _build_violation {
Subroutine _build_violation: called CC=6 SCAR=17.9
620 35 my ( $self, $module, $setup, $elem ) = @_;
621  
622 35 ▶ return () unless $self->_matches_for( $elem, $module, $setup );
Branch: 1 branch
BranchTrueFalseTotal
unless $self->_matches_for($elem, $module, $setup) 3 32 35
623  
624 32 my $desc = qq[Using module $module is not recommended];
625 32 ▶ my $expl = $setup->{reason} // $desc;
Condition66%2/3
$setup->{"reason"} // $desc
l!l&&r!l&&!r
22100
MC/DC0%0/2
$setup->{"reason"} // $desc
$setup->{"reason"} $desc
Truth table66%2/3
A // B
A= $setup->{"reason"}B= $desc
ABresult
1X1
011
000
626  
627 32 ▶ if ( defined $setup->{message} ) {
Branch: 2 branches
BranchTrueFalseTotal
if (defined $setup->{'message'}) { } 3 29 32
elsif (my $prefer = $setup->{'prefer'}) { } 23 6 29
628 3 $desc = $setup->{message};
629 }
630 elsif ( my $prefer = $setup->{prefer} ) {
631 23 $desc = "Prefer using module $prefer over $module";
632 }
633  
634 32 ▶ if ( my $functions = $setup->{_for_functions} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $functions = $setup->{"_for_functions"}) 7 25 32
635 7 $desc .= ' for ' . join( ', ', @$functions );
636 }
637  
638 32 return $self->_violation_for( $setup, $desc, $expl, $elem );
639 }
640  
641 291 ▶sub _violates_builtin {
Subroutine _violates_builtin: called CC=8 SCAR=20.8
642 291 my ( $self, $elem ) = @_;
643  
644 291 my $function = $elem->content;
645  
646 291 ▶ return () unless my $setup = $self->{_cfg_preferred_builtins}->{$function};
Branch: 1 branch
BranchTrueFalseTotal
unless my $setup = $self->{"_cfg_preferred_builtins"}{$function} 268 23 291
647 23 ▶ return () unless is_function_call($elem);
Branch: 1 branch
BranchTrueFalseTotal
unless is_function_call($elem) 3 20 23
648  
649 20 my $desc = qq[Using the builtin $function is not recommended];
650 20 ▶ my $expl = $setup->{reason} // $desc;
Condition66%2/3
$setup->{"reason"} // $desc
l!l&&r!l&&!r
8120
MC/DC0%0/2
$setup->{"reason"} // $desc
$setup->{"reason"} $desc
Truth table66%2/3
A // B
A= $setup->{"reason"}B= $desc
ABresult
1X1
011
000
651  
652 20 ▶ if ( my $prefer = $setup->{prefer} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $prefer = $setup->{"prefer"}) 19 1 20
653  
654 # already calling the preferred implementation
655 19 ▶ return () if $self->_provides_function( $elem, $prefer, $function );
Branch: 1 branch
BranchTrueFalseTotal
if $self->_provides_function($elem, $prefer, $function) 8 11 19
656  
657 11 $desc = "Prefer using ${prefer}::${function} over the builtin $function";
658  
659 11 ▶ if ( !defined $setup->{reason} && $self->_exports_on_request( $prefer, $function ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $setup->{"reason"} and $self->_exports_on_request($prefer, $function)) 5 6 11
Condition100%3/3
not defined $setup->{"reason"} and $self->_exports_on_request($prefer, $function)
!ll&&!rl&&r
515
MC/DC100%2/2
not defined $setup->{"reason"} and $self->_exports_on_request($prefer, $function)
not defined $setup->{"reason"} $self->_exports_on_request($prefer, $function)
Truth table100%3/3
not(A) and B
A= not defined $setup->{"reason"}B= $self->_exports_on_request($prefer, $function)
ABresult
0X0
100
111
660 5 $expl =
661 "$prefer exports $function on request only, so this call is still the builtin"
662 . " - import it explicitly with `use $prefer qw{ $function }`";
663 }
664 }
665  
666 12 return $self->_violation_for( $setup, $desc, $expl, $elem );
667 }
668  
669 44 ▶sub _violation_for {
Subroutine _violation_for: called CC=2 SCAR=6.9
670 44 my ( $self, $setup, $desc, $expl, $elem ) = @_;
671  
672 44 ▶ if ( my $sev = $setup->{severity} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $sev = $setup->{"severity"}) 5 39 44
673 5 local $self->{_severity} = $sev;
674 5 return $self->violation( $desc, $expl, $elem );
675 }
676  
677 39 return $self->violation( $desc, $expl, $elem );
678 }
679  
680 6 ▶sub _extract_parent_modules {
Subroutine _extract_parent_modules: called CC=5 SCAR=16.1
681 6 my ( $self, $elem ) = @_;
682  
683 6 my @modules;
684 6 for my $child ( $elem->schildren ) {
685 28 ▶ if ( $child->isa('PPI::Token::Quote') ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($child->isa('PPI::Token::Quote')) { } 6 22 28
elsif ($child->isa('PPI::Token::QuoteLike::Words')) { } 1 21 22
686 6 my $val = $child->string;
687 6 ▶ push @modules, $val if $val =~ /\A[A-Za-z_]\w*(?:::\w+)*\z/;
Branch: 1 branch
BranchTrueFalseTotal
if $val =~ /\A[A-Za-z_]\w*(?:::\w+)*\z/ 6 0 6
688 }
689 elsif ( $child->isa('PPI::Token::QuoteLike::Words') ) {
690 1 push @modules, grep { /\A[A-Za-z_]\w*(?:::\w+)*\z/ } $child->literal;
691 }
692 }
693 6 return @modules;
694 }
695  
696 347 ▶sub violates {
Subroutine violates: called CC=5 SCAR=17.2
Pod documented
697 347 my ( $self, $elem ) = @_;
698  
699 347 ▶ return () unless $self->{_is_enabled};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"_is_enabled"} 0 347 347
700 347 ▶ return () unless $elem;
Branch: 1 branch
BranchTrueFalseTotal
unless $elem 0 347 347
701  
702 347 ▶ return $self->_violates_include($elem) if $elem->isa('PPI::Statement::Include');
Branch: 1 branch
BranchTrueFalseTotal
if $elem->isa("PPI::Statement::Include") 56 291 347
703 291 ▶ return $self->_violates_builtin($elem) if $elem->isa('PPI::Token::Word');
Branch: 1 branch
BranchTrueFalseTotal
if $elem->isa("PPI::Token::Word") 291 0 291
704  
705 0 return ();
706 }
707  
708 2 1;
709  
710 __END__
↑ Summary