Login
Fix false positive in ProhibitSingleCharAlternation
[gknop/Perl-Critic.git] / lib / Perl / Critic / Policy / Modules / ProhibitEvilModules.pm
CommitLineData
6036a254 1##############################################################################
800f036a
CD
2# $URL$
3# $Date$
4# $Author$
5# $Revision$
6036a254 6##############################################################################
d59eec4b
JRT
7package Perl::Critic::Policy::Modules::ProhibitEvilModules;
8
df6dee2b 9use 5.006001;
d59eec4b
JRT
10use strict;
11use warnings;
7cc385e4 12use English qw(-no_match_vars);
c680a9c9
ES
13use Readonly;
14
26babf22 15use List::MoreUtils qw(any);
c680a9c9 16
6ed4d974
ES
17use Perl::Critic::Exception::Configuration::Option::Policy::ParameterValue
18 qw{ throw_policy_value };
005d2cb9
ES
19use Perl::Critic::Utils qw{
20 :booleans :characters :severities :data_conversion
21};
c680a9c9 22
d59eec4b
JRT
23use base 'Perl::Critic::Policy';
24
a8da49fd 25our $VERSION = '1.088';
d59eec4b 26
fd5bd7b5
JRT
27#-----------------------------------------------------------------------------
28
c680a9c9
ES
29Readonly::Scalar my $EXPL => q{Find an alternative module};
30Readonly::Scalar my $DESC => q{Prohibited module used};
d59eec4b 31
6036a254 32#-----------------------------------------------------------------------------
d59eec4b 33
6ed4d974
ES
34sub supported_parameters {
35 return (
36 {
37 name => 'modules',
38 description => 'The names of or patterns for modules to forbid.',
39 default_string => $EMPTY,
40 behavior => 'string list',
41 },
42 );
43}
44
fd5bd7b5
JRT
45sub default_severity { return $SEVERITY_HIGHEST }
46sub default_themes { return qw( core bugs ) }
47sub applies_to { return 'PPI::Statement::Include' }
2c943bed 48
6036a254 49#-----------------------------------------------------------------------------
2c943bed 50
005d2cb9
ES
51sub initialize_if_enabled {
52 my ($self, $config) = @_;
d59eec4b 53
26babf22
JRT
54 $self->{_evil_modules} = {}; #Hash
55 $self->{_evil_modules_rx} = []; #Array
56
d59eec4b 57 #Set config, if defined
6ed4d974
ES
58 if ( defined $self->{_modules} ) {
59 my @modules = sort keys %{ $self->{_modules} };
60 foreach my $module ( @modules ) {
7cc385e4 61 if ( $module =~ m{ \A [/] (.+) [/] \z }mx ) {
95ed7a74 62
702bf3be 63 # These are module name patterns (e.g. /Acme/)
a168820a 64 my $re = $1; # Untainting
8b5a642f 65 my $pattern = eval { qr/$re/ }; ##no critic (RegularExpressions::.*)
95ed7a74 66
6ed4d974
ES
67 if ( $EVAL_ERROR ) {
68 throw_policy_value
69 policy => $self->get_short_name(),
70 option_name => 'modules',
71 option_value => ( join q{", "}, @modules ),
72 message_suffix =>
73 qq{contains an invalid regular expression: "$module"};
74 }
95ed7a74
JRT
75
76 push @{ $self->{_evil_modules_rx} }, $pattern;
26babf22
JRT
77 }
78 else {
702bf3be 79 # These are literal module names (e.g. Acme::Foo)
26babf22
JRT
80 $self->{_evil_modules}->{$module} = 1;
81 }
d59eec4b
JRT
82 }
83 }
005d2cb9
ES
84
85 return $TRUE;
d59eec4b
JRT
86}
87
6036a254 88#-----------------------------------------------------------------------------
d59eec4b
JRT
89
90sub violates {
e5f6c18d 91 my ( $self, $elem, undef ) = @_;
6015ad73
CD
92 my $module = $elem->module();
93 return if !$module;
26babf22
JRT
94
95 if ( exists $self->{_evil_modules}->{ $module } ||
96 any { $module =~ $_ } @{ $self->{_evil_modules_rx} } ) {
97
c680a9c9 98 return $self->violation( $DESC, $EXPL, $elem );
d59eec4b
JRT
99 }
100 return; #ok!
101}
102
1031;
104
105__END__
106
6036a254 107#-----------------------------------------------------------------------------
2c943bed 108
d59eec4b
JRT
109=pod
110
111=head1 NAME
112
f017d93a 113Perl::Critic::Policy::Modules::ProhibitEvilModules - Ban modules that aren't blessed by your shop.
d59eec4b 114
af93c316
ES
115=head1 AFFILIATION
116
117This Policy is part of the core L<Perl::Critic> distribution.
118
119
d59eec4b
JRT
120=head1 DESCRIPTION
121
122Use this policy if you wish to prohibit the use of specific modules.
123These may be modules that you feel are deprecated, buggy, unsupported,
124insecure, or just don't like.
125
8a25c8a0 126=head1 CONFIGURATION
d59eec4b 127
8a25c8a0
ES
128The set of prohibited modules is configurable via the C<modules> option. The
129value of C<modules> should be a string of space-delimited, fully qualified
130module names and/or regular expressions. An example of prohibiting two
131specific modules in a F<.perlcriticrc> file:
d59eec4b 132
8a25c8a0
ES
133 [Modules::ProhibitEvilModules]
134 modules = Getopt::Std Autoload
d59eec4b 135
8a25c8a0
ES
136Regular expressions are identified by values beginning and ending with slashes.
137Any module with a name that matches C<m/pattern/> will be forbidden. For
138example:
26babf22
JRT
139
140 [Modules::ProhibitEvilModules]
141 modules = /Acme::/
142
8a25c8a0
ES
143would cause all modules that match C<m/Acme::/> to be forbidden. You can add
144any of the C<imxs> switches to the end of a pattern, but be aware that patterns
145cannot contain whitespace because the configuration file parser uses it to
146delimit the module names and patterns.
26babf22 147
8a25c8a0
ES
148By default, there are no prohibited modules (although I can think of a few that
149should be).
d59eec4b
JRT
150
151=head1 NOTES
152
153Note that this policy doesn't apply to pragmas. Future versions may
154allow you to specify an alternative for each prohibited module, which
155can be suggested by L<Perl::Critic>.
156
157=head1 AUTHOR
158
159Jeffrey Ryan Thalhammer <thaljef@cpan.org>
160
161=head1 COPYRIGHT
162
20dfddeb 163Copyright (c) 2005-2008 Jeffrey Ryan Thalhammer. All rights reserved.
d59eec4b
JRT
164
165This program is free software; you can redistribute it and/or modify
166it under the same terms as Perl itself. The full text of this license
167can be found in the LICENSE file included with this module.
168
169=cut
737d3b65
CD
170
171# Local Variables:
172# mode: cperl
173# cperl-indent-level: 4
174# fill-column: 78
175# indent-tabs-mode: nil
176# c-indentation-style: bsd
177# End:
96fed375 178# ex: set ts=8 sts=4 sw=4 tw=78 ft=perl expandtab shiftround :