File Coverage

blib/lib/Perl/Critic/Policy/ProhibitImplicitImport.pm
Criterion Covered Total %
statement 26 26 100.0
branch 2 2 100.0
condition 10 14 71.4
subroutine 10 10 100.0
pod 3 4 75.0
total 51 56 91.0


line stmt bran cond sub pod time code
1             package Perl::Critic::Policy::ProhibitImplicitImport;
2              
3 1     1   178981 use strict;
  1         10  
  1         29  
4 1     1   6 use warnings;
  1         2  
  1         43  
5              
6             our $VERSION = '0.000001';
7              
8 1     1   5 use Perl::Critic::Utils qw($SEVERITY_LOW);
  1         3  
  1         81  
9 1     1   6 use parent 'Perl::Critic::Policy';
  1         2  
  1         8  
10              
11 1     1   34838 use constant DESC => 'Using a module without an explicit import list';
  1         3  
  1         75  
12 1         248 use constant EXPL =>
13 1     1   7 'Using the a module without specifying an import list can result in importing many symbols. Import the symbols you want explicitly, or prevent implicit imports via ().';
  1         2  
14              
15 1     1 1 26455 sub applies_to { 'PPI::Statement::Include' }
16              
17 3     3 1 265426 sub default_severity { $SEVERITY_LOW }
18              
19             sub supported_parameters {
20             return (
21             {
22 2     2 0 303767 name => 'ignored_modules',
23             description => 'Modules which will be ignored by this policy.',
24             behavior => 'string list',
25             list_always_present_values => [
26             qw(
27             Carp::Always
28             Courriel::Builder
29             Data::Dumper
30             Data::Dumper::Concise
31             Data::Printer
32             DDP
33             Devel::Confess
34             Encode::Guess
35             Exporter::Lite
36             File::chdir
37             FindBin
38             Git::Sub
39             HTTP::Message::PSGI
40             Import::Into
41             Mojolicious::Lite
42             Moo
43             Moo::Role
44             Moose
45             Moose::Exporter
46             Moose::Role
47             Moose::Util::TypeConstraints
48             MooseX::Getopt
49             MooseX::LazyRequire
50             MooseX::NonMoose
51             MooseX::Role::Parameterized
52             MooseX::SemiAffordanceAccessor
53             MooX::Options
54             MooX::StrictConstructor
55             Mouse
56             PerlIO::gzip
57             Stepford::Role::Step
58             Test2::V0
59             Test::Class::Moose
60             Test::Class::Moose::Role
61             Test::More
62             Test::Number::Delta
63             Test::XML
64             )
65             ],
66             },
67             );
68             }
69              
70             sub violates {
71 14     14 1 605 my ( $self, $elem ) = @_;
72 14         30 my $ignore = $self->{_ignored_modules};
73              
74 14 100 66     35 if ( !$elem->pragma
      66        
      66        
      50        
      100        
75             && $elem->type
76             && $elem->type eq 'use'
77             && !$elem->arguments
78             && !exists $ignore->{ ( $elem->module // q{} ) } ) {
79 2         252 return $self->violation( DESC, EXPL, $elem );
80             }
81              
82 12         1082 return ();
83             }
84              
85             1;
86              
87             # ABSTRACT: Prefer symbol imports to be explicit
88              
89             __END__
90              
91             =pod
92              
93             =encoding UTF-8
94              
95             =head1 NAME
96              
97             Perl::Critic::Policy::ProhibitImplicitImport - Prefer symbol imports to be explicit
98              
99             =head1 VERSION
100              
101             version 0.000001
102              
103             =head1 DESCRIPTION
104              
105             Some Perl modules can implicitly import many symbols if no imports are
106             specified. To avoid this, and to assist in finding where symbols have been
107             imported from, specify the symbols you want to import explicitly in the C<use>
108             statement. Alternatively, specify an empty import list with C<use Foo ()> to
109             avoid importing any symbols at all, and fully qualify the functions or
110             constants, such as C<Foo::strftime>.
111              
112             use POSIX; # not ok
113             use POSIX (); # ok
114             use POSIX qw(fcntl); # ok
115             use POSIX qw(O_APPEND O_CREAT O_EXCL O_RDONLY O_RDWR O_WRONLY); # ok
116              
117             For modules which inherit from L<Test::Builder::Module>, you may need to use a
118             different import syntax.
119              
120             use Test::JSON; # not ok
121             use Test::JSON import => ['is_json']; # ok
122              
123             =head1 CONFIGURATION
124              
125             By default, this policy ignores many modules (like L<Moo> and L<Moose>) for
126             which implicit imports provide the expected behaviour. See the source of this
127             module for a complete list. If you would like to ignore additional modules,
128             this can be done via configuration:
129              
130             [ProhibitImplicitImport]
131             ignored_modules = Git::Sub Regexp::Common
132              
133             =head1 ACKNOWLEDGEMENTS
134              
135             Much of this code and even some documentation has been inspired by and borrowed
136             directly from L<Perl::Critic::Policy::Freenode::POSIXImports> and
137             L<Perl::Critic::Policy::TooMuchCode>.
138              
139             =head1 AUTHOR
140              
141             Olaf Alders <olaf@wundercounter.com>
142              
143             =head1 COPYRIGHT AND LICENSE
144              
145             This software is copyright (c) 2020 by Olaf Alders.
146              
147             This is free software; you can redistribute it and/or modify it under
148             the same terms as the Perl 5 programming language system itself.
149              
150             =cut