lib/Perl/Critic/Policy/Modules/ProhibitEvilModules.pm

filter: Statementstmt 69.7% 69 / 99 Branchbran 35.2% 12 / 34 Conditioncond 46.6% 7 / 15 MC/DCmcdc 22.2% 2 / 9 Subroutinesub 94.4% 17 / 18 Podpod 83.3% 5 / 6 totaltotal 61.8% 112 / 181 CC 25 SCAR 22.7
CC
25
Cov
59%
_parse_modules_file
26.8
_handle_module_specification
13.1
_handle_module_specification_on_line
12.0
CRAP 9.7
« lib/Perl/Critic/Policy/BuiltinFunctions/ProhibitSleepViaSelect.pm ↑ Summary lib/Perl/Critic/Policy/Variables/RequireLocalizedPunctuationVars.pm »
1 package Perl::Critic::Policy::Modules::ProhibitEvilModules;
2  
3 40 ▶use 5.010001;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 40 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 40 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 40 ▶use English qw(-no_match_vars);
Subroutine BEGIN: called CC=1 SCAR=0.0
8 40 ▶use Readonly;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 use Perl::Critic::Exception::Configuration::Option::Policy::ParameterValue
11 40 ▶ qw{ throw_policy_value };
Subroutine BEGIN: called CC=1 SCAR=0.0
12 40 ▶use Perl::Critic::Utils qw( :characters :severities :data_conversion );
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 40 ▶use parent 'Perl::Critic::Policy';
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 40 our $VERSION = '1.156';
17  
18 #-----------------------------------------------------------------------------
19  
20 40 Readonly::Scalar my $EXPL => q{Find an alternative module};
21  
22 40 Readonly::Scalar my $MODULE_NAME_REGEX =>
23 qr<
24 \b
25 [[:alpha:]_]
26 (?:
27 (?: \w | :: )*
28 \w
29 )?
30 \b
31 >xms;
32 40 Readonly::Scalar my $REGULAR_EXPRESSION_REGEX => qr< [/] ( [^/]+ ) [/] >xms;
33 40 Readonly::Scalar my $DESCRIPTION_REGEX => qr< [{] ( [^}]+ ) [}] >xms;
34  
35 # It's kind of unfortunate that I had to put capturing parentheses in the
36 # component regexes above, because they're not visible here and so make
37 # figuring out the positions of captures hard. Too bad we can't make the
38 # minimum perl version 5.10. :]
39 40 Readonly::Scalar my $MODULES_REGEX =>
40 qr<
41 \A
42 \s*
43 (?:
44 ( $MODULE_NAME_REGEX )
45 | $REGULAR_EXPRESSION_REGEX
46 )
47 (?: \s* $DESCRIPTION_REGEX )?
48 \s*
49 >xms;
50  
51 40 Readonly::Scalar my $MODULES_FILE_LINE_REGEX =>
52 qr<
53 \A
54 \s*
55 (?:
56 ( $MODULE_NAME_REGEX )
57 | $REGULAR_EXPRESSION_REGEX
58 )
59 \s*
60 ( \S (?: .* \S )? )?
61 \s*
62 \z
63 >xms;
64  
65 Readonly::Scalar my $DEFAULT_MODULES =>
66 join
67 $SPACE,
68 40 map { "$_ {Found use of $_. This module is deprecated by the Perl 5 Porters.}" }
69 qw< Class::ISA Pod::Plainer Shell Switch >;
70  
71 # Indexes in the arrays of regexes for the "modules" option.
72 40 Readonly::Scalar my $INDEX_REGEX => 0;
73 40 Readonly::Scalar my $INDEX_DESCRIPTION => 1;
74  
75 #-----------------------------------------------------------------------------
76  
77 94 ▶sub supported_parameters {
Subroutine supported_parameters: called CC=1 SCAR=0.0
Pod undocumented
78 return (
79 {
80 94 name => 'modules',
81 description => 'The names of or patterns for modules to forbid.',
82 default_string => $DEFAULT_MODULES,
83 parser => \&_parse_modules,
84 },
85 {
86 name => 'modules_file',
87 description => 'A file containing names of or patterns for modules to forbid.',
88 default_string => $EMPTY,
89 parser => \&_parse_modules_file,
90 },
91 );
92 }
93  
94 74 ▶sub default_severity { return $SEVERITY_HIGHEST }
Subroutine default_severity: called CC=1 SCAR=0.0
Pod documented
95 73 ▶sub default_themes { return qw( core bugs certrule ) }
Subroutine default_themes: called CC=1 SCAR=0.0
Pod documented
96 37 ▶sub applies_to { return 'PPI::Statement::Include' }
Subroutine applies_to: called CC=1 SCAR=0.0
Pod documented
97  
98 #-----------------------------------------------------------------------------
99  
100 92 ▶sub _parse_modules {
Subroutine _parse_modules: called CC=6 SCAR=18.3
101 92 my ($self, $parameter, $config_string) = @_;
102  
103 92 ▶ my $module_specifications = $config_string // $parameter->get_default_string();
Condition66%2/3
$config_string // $parameter->get_default_string
l!l&&r!l&&!r
3890
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
104  
105 92 ▶ return if not $module_specifications;
Branch: 1 branch
BranchTrueFalseTotal
unless $module_specifications 1 91 92
106 91 ▶ return if $module_specifications =~ m< \A \s* \z >xms;
Branch: 1 branch
BranchTrueFalseTotal
if $module_specifications =~ / \A \s* \z /msx 0 91 91
107  
108 91 ▶ while ( $module_specifications =~ s< $MODULES_REGEX ><>xms ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($module_specifications =~ s/ $MODULES_REGEX //msx) 364 91 455
109 364 my ($module, $regex_string, $description) = ($1, $2, $3);
110  
111 364 $self->_handle_module_specification(
112 module => $module,
113 regex_string => $regex_string,
114 description => $description,
115 option_name => 'modules',
116 option_value => $config_string,
117 );
118 }
119  
120 91 ▶ if ($module_specifications) {
Branch: 1 branch
BranchTrueFalseTotal
if ($module_specifications) 0 91 91
121 0 throw_policy_value
122 policy => $self->get_short_name(),
123 option_name => 'modules',
124 option_value => $config_string,
125 message_suffix =>
126 qq{contains unparseable data: "$module_specifications"};
127 }
128  
129 91 return;
130 }
131  
132 92 ▶sub _parse_modules_file {
Subroutine _parse_modules_file: called CC=6 SCAR=32.9
133 92 my ($self, undef, $config_string) = @_;
134  
135 92 ▶ return if not $config_string;
Branch: 1 branch
BranchTrueFalseTotal
unless $config_string 92 0 92
136 0 ▶ return if $config_string =~ m< \A \s* \z >xms;
Branch: 1 branch
BranchTrueFalseTotal
if $config_string =~ / \A \s* \z /msx 0 0 0
137  
138 0 ▶ open my $handle, '<', $config_string
Branch: 1 branch
BranchTrueFalseTotal
open my $handle, "<", $config_string or throw_policy_value("policy", $self->get_short_name, "option_name", "modules_file", "option_value", $config_string, "message_suffix", "refers to a file that could not be opened: $Perl::Critic::Policy::Modules::ProhibitEvilModules::OS_ERROR") 0 0 0
139 or throw_policy_value
140 policy => $self->get_short_name(),
141 option_name => 'modules_file',
142 option_value => $config_string,
143 message_suffix =>
144 qq<refers to a file that could not be opened: $OS_ERROR>;
145 0 ▶ while ( my $line = <$handle> ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $line = readline $handle)) 0 0 0
146 0 $self->_handle_module_specification_on_line($line, $config_string);
147 }
148 0 ▶ close $handle or warn qq<Could not close "$config_string": $OS_ERROR\n>;
Branch: 1 branch
BranchTrueFalseTotal
close $handle or warn qq[Could not close "$config_string": $Perl::Critic::Policy::Modules::ProhibitEvilModules::OS_ERROR\n] 0 0 0
149  
150 0 return;
151 }
152  
153 0 ▶sub _handle_module_specification_on_line {
Subroutine _handle_module_specification_on_line: not called CC=3 SCAR=24.8
154 0 my ($self, $line, $config_string) = @_;
155  
156 0 $line =~ s< [#] .* \z ><>xms;
157 0 $line =~ s< \s+ \z ><>xms;
158 0 $line =~ s< \A \s+ ><>xms;
159  
160 0 ▶ return if not $line;
Branch: 1 branch
BranchTrueFalseTotal
unless $line 0 0 0
161  
162 0 ▶ if ( $line =~ s< $MODULES_FILE_LINE_REGEX ><>xms ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($line =~ s/ $MODULES_FILE_LINE_REGEX //msx) { } 0 0 0
163 0 my ($module, $regex_string, $description) = ($1, $2, $3);
164  
165 0 $self->_handle_module_specification(
166 module => $module,
167 regex_string => $regex_string,
168 description => $description,
169 option_name => 'modules_file',
170 option_value => $config_string,
171 );
172 }
173 else {
174 0 throw_policy_value
175 policy => $self->get_short_name(),
176 option_name => 'modules_file',
177 option_value => $config_string,
178 message_suffix =>
179 qq{contains unparseable data: "$line"};
180 }
181  
182 0 return;
183 }
184  
185 364 ▶sub _handle_module_specification {
Subroutine _handle_module_specification: called CC=6 SCAR=25.8
186 364 my ($self, %arguments) = @_;
187  
188 364 ▶ my $description = $arguments{description} || $EMPTY;
Condition33%1/3
$arguments{'description'} || $Perl::Critic::Policy::Modules::ProhibitEvilModules::EMPTY
l!l&&r!l&&!r
36400
MC/DC0%0/2
$arguments{'description'} || $Perl::Critic::Policy::Modules::ProhibitEvilModules::EMPTY
$arguments{'description'} $Perl::Critic::Policy::Modules::ProhibitEvilModules::EMPTY
Truth table33%1/3
A || B
A= $arguments{'description'}B= $Perl::Critic::Policy::Modules::ProhibitEvilModules::EMPTY
ABresult
1X1
011
000
189  
190 364 ▶ if ( my $regex_string = $arguments{regex_string} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $regex_string = $arguments{'regex_string'}) { } 0 364 364
191 # These are module name patterns (e.g. /Acme/)
192 0 my $actual_regex;
193  
194 0 eval { $actual_regex = qr/$regex_string/; 1 } ## no critic (ExtendedFormatting, LineBoundaryMatching, DotMatchAnything)
195 or throw_policy_value
196 policy => $self->get_short_name(),
197 option_name => $arguments{option_name},
198 option_value => $arguments{option_value},
199 0 ▶ 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"]) 0 0 0
200 qq{contains an invalid regular expression: "$regex_string"};
201  
202 # Can't use a hash due to stringification, so this is an AoA.
203 0 ▶ $self->{_evil_modules_regexes} ||= [];
Condition0%0/2
$self->{"_evil_modules_regexes"} ||= []
l!l
00
MC/DC0%0/1
$self->{"_evil_modules_regexes"} ||= []
$self->{"_evil_modules_regexes"}
Truth table0%0/2
A
A= $self->{"_evil_modules_regexes"}
Aresult
11
00
204  
205 push
206 0 @{ $self->{_evil_modules_regexes} },
207 [ $actual_regex, $description ];
208 }
209 else {
210 # These are literal module names (e.g. Acme::Foo)
211 364 ▶ $self->{_evil_modules} ||= {};
Condition100%2/2
$self->{"_evil_modules"} ||= {}
l!l
27391
MC/DC100%1/1
$self->{"_evil_modules"} ||= {}
$self->{"_evil_modules"}
Truth table100%2/2
A
A= $self->{"_evil_modules"}
Aresult
11
00
212 364 $self->{_evil_modules}{ $arguments{module} } = $description;
213 }
214  
215 364 return;
216 }
217  
218 #-----------------------------------------------------------------------------
219  
220 73 ▶sub initialize_if_enabled {
Subroutine initialize_if_enabled: called CC=2 SCAR=7.1
Pod documented
221 73 my ($self, undef) = @_;
222  
223 # Disable if no modules are specified; there's no point in running if
224 # there aren't any.
225 return
226 exists $self->{_evil_modules}
227 73 ▶ || exists $self->{_evil_modules_regexes};
Condition66%2/3
exists $self->{'_evil_modules'} || exists $self->{'_evil_modules_regexes'}
l!l&&r!l&&!r
7201
MC/DC50%1/2
exists $self->{'_evil_modules'} || exists $self->{'_evil_modules_regexes'}
exists $self->{'_evil_modules'} exists $self->{'_evil_modules_regexes'}
Truth table66%2/3
A || B
A= exists $self->{'_evil_modules'}B= exists $self->{'_evil_modules_regexes'}
ABresult
1X1
011
000
228 }
229  
230 #-----------------------------------------------------------------------------
231  
232 63 ▶sub violates {
Subroutine violates: called CC=7 SCAR=24.2
Pod documented
233 63 my ( $self, $elem, undef ) = @_;
234  
235 63 my $module = $elem->module();
236 63 ▶ return if not $module;
Branch: 1 branch
BranchTrueFalseTotal
unless $module 2 61 63
237  
238 61 my $evil_modules = $self->{_evil_modules};
239 61 my $evil_modules_regexes = $self->{_evil_modules_regexes};
240 61 my $description;
241  
242 61 ▶ if ( exists $evil_modules->{$module} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $evil_modules->{$module}) { } 0 61 61
243 0 $description = $evil_modules->{ $module };
244 }
245 else {
246 REGEX:
247 61 foreach my $regex ( @{$evil_modules_regexes} ) {
248 0 ▶ if ( $module =~ $regex->[$INDEX_REGEX] ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($module =~ /$regex->[$INDEX_REGEX]/) 0 0 0
249 0 $description = $regex->[$INDEX_DESCRIPTION];
250 0 last REGEX;
251 }
252 }
253 }
254  
255 61 ▶ if (defined $description) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $description) 0 61 61
256 0 ▶ $description ||= qq<Prohibited module "$module" used>;
Condition0%0/2
$description ||= qq[Prohibited module "$module" used]
l!l
00
MC/DC0%0/1
$description ||= qq[Prohibited module "$module" used]
$description
Truth table0%0/2
A
A= $description
Aresult
11
00
257  
258 0 return $self->violation( $description, $EXPL, $elem );
259 }
260  
261 61 return; # ok!
262 }
263  
264 40 1;
265  
266 __END__
« lib/Perl/Critic/Policy/BuiltinFunctions/ProhibitSleepViaSelect.pm ↑ Summary lib/Perl/Critic/Policy/Variables/RequireLocalizedPunctuationVars.pm »