File Coverage

blib/lib/Perl/Critic/Policy/logicLAB/ModuleBlacklist.pm
Criterion Covered Total %
statement 61 66 92.4
branch 14 20 70.0
condition 5 8 62.5
subroutine 14 14 100.0
pod 3 3 100.0
total 97 111 87.3


line stmt bran cond sub pod time code
1             package Perl::Critic::Policy::logicLAB::ModuleBlacklist;
2              
3 2     2   1533 use strict;
  2         5  
  2         46  
4 2     2   8 use warnings;
  2         4  
  2         54  
5 2     2   46 use 5.008;
  2         5  
6              
7 2     2   9 use base 'Perl::Critic::Policy';
  2         4  
  2         1703  
8 2     2   398510 use Perl::Critic::Utils qw{ $SEVERITY_MEDIUM :booleans};
  2         5  
  2         109  
9 2     2   177 use Carp qw(carp);
  2         4  
  2         85  
10 2     2   883 use Data::Dumper;
  2         6095  
  2         144  
11              
12             our $VERSION = '0.04';
13              
14 2     2   12 use constant supported_parameters => qw(modules debug);
  2         3  
  2         136  
15 2     2   11 use constant default_severity => $SEVERITY_MEDIUM;
  2         3  
  2         86  
16 2     2   8 use constant default_themes => qw(logiclab);
  2         4  
  2         1143  
17              
18             sub applies_to {
19             return (
20 9     9 1 43595 qw(
21             PPI::Statement::Include
22             )
23             );
24             }
25              
26             sub violates {
27 17     17 1 488 my ( $self, $elem ) = @_;
28              
29             #Policy not configured, nothing to assert
30 17 50       51 if ( not $self->{_modules} ) {
31 0         0 return;
32             }
33              
34 17         49 my @children = $elem->children;
35              
36 17 100 100     113 if ( $children[0]->content eq 'use' or $children[0]->content eq 'require' )
    50          
37             {
38              
39 16         126 my $package = $children[2]->content;
40              
41 16 50       70 if ( $self->{debug} ) {
42 0         0 print STDERR "located include: $package\n";
43             }
44              
45 16         19 foreach my $module ( keys %{ $self->{_modules} } ) {
  16         43  
46              
47 42 100       90 if ( $package eq $module ) {
48              
49 8 100       40 if ( defined $self->{_modules}->{$module} ) {
50 4         8 my $recommendation = $self->{_modules}->{$module};
51 4         24 return $self->violation(
52             "Blacklisted: $package is not recommended by required standard",
53             "Use recommended module: $recommendation instead of $package",
54             $elem,
55             );
56              
57             }
58             else {
59              
60 4         24 return $self->violation(
61             "Blacklisted: $package is not recommended by required standard",
62             "Use alternative implementation or module instead of $package",
63             $elem,
64             );
65             }
66             }
67             }
68              
69             #we ignore negative use statements, they are for pragma [issue1]
70             }
71             elsif ( $children[0]->content eq 'no' ) {
72 1 50       21 if ( $self->{debug} ) {
73 0         0 print STDERR "located 'no' use/require statement\n";
74             }
75             }
76             else {
77 0         0 carp 'Unable to locate package keyword';
78             }
79              
80 9         27 return;
81             }
82              
83             sub initialize_if_enabled {
84 1     1 1 1706095 my ( $self, $config ) = @_;
85              
86             #debug - order is significant, since we might need debugging
87 1   33     6 $self->{debug} = $config->get('debug') || $FALSE;
88              
89             #Names:
90             #fetching list of blacklisted modules
91 1         14 my $modules = $config->get('modules');
92              
93 1 50       10 if ( $self->{debug} ) {
94 0         0 carp "Blacklisted modules are: $modules\n";
95             }
96              
97             #parsing blacklisted modules, see also _parse_blacklist
98 1 50       11 if ($modules) {
99 1   50     6 $self->{_modules} = $self->_parse_modules($modules) || q{};
100             }
101              
102 1         3 return $TRUE;
103             }
104              
105             sub _parse_modules {
106 1     1   2 my ( $self, $config_string ) = @_;
107              
108             #first we split on commas
109 1         13 my @parameters = split /\s*,\s*/, $config_string;
110 1         2 my %modules;
111              
112             #then we split on fat commas, to locate recommendations
113 1         3 foreach my $parameter (@parameters) {
114 3 100       14 if ( $parameter =~ m/\s*=>\s*/ ) {
115 2         7 my @p = split /\s*=>\s*/, $parameter;
116              
117 2         7 $modules{ $p[0] } = $p[1];
118             }
119             else {
120 1         3 $modules{$parameter} = undef;
121             }
122             }
123              
124 1         6 return \%modules;
125             }
126              
127             1;
128              
129             __END__
130              
131             =pod
132              
133             =encoding utf8
134              
135             =head1 NAME
136              
137             Perl::Critic::Policy::logicLAB::ModuleBlacklist - blacklist modules you want to prohibit use of
138              
139             =head1 AFFILIATION
140              
141             This policy is a policy in the Perl::Critic::logicLAB distribution. The policy
142             is themed: logiclab.
143              
144             =head1 VERSION
145              
146             This documentation describes version 0.03
147              
148             =head1 DESCRIPTION
149              
150             This policy can be used to specify a list of unwanted modules. Using a blacklisting, so if the
151             modules are used in the evaluated code a violation is triggered.
152              
153             In addition to blacklisting modules it is possible to recommend alternatives to
154             blacklisted modules.
155              
156             =head1 CONFIGURATION AND ENVIRONMENT
157              
158             =head2 modules
159              
160             You can blacklist modules using the configuration parameter B<modules>
161              
162             [logicLAB::ModuleBlacklist]
163             modules = IDNA::Punycode
164              
165             If you want to blacklist multiple modules specify using a comma separated list:
166              
167             [logicLAB::ModuleBlacklist]
168             modules = Try::Tiny, Contextual::Return, IDNA::Punycode
169              
170             If you want to recommend alternatives to, use fat comma in addition
171              
172             [logicLAB::ModuleBlacklist]
173             modules = Try::Tiny => TryCatch, Contextual::Return, IDNA::Punycode => Net::IDN::Encode
174              
175             =head1 DEPENDENCIES AND REQUIREMENTS
176              
177             =over
178              
179             =item * L<Perl> 5.8.0
180              
181             =item * L<Module::Build>
182              
183             =item * L<Perl::Critic>
184              
185             =item * L<Perl::Critic::Utils>
186              
187             =item * L<Perl::Critic::Policy>
188              
189             =item * L<Test::More>
190              
191             =item * L<Test::Perl::Critic>
192              
193             =item * L<Data::Dumper>
194              
195             =item * L<Carp>
196              
197             =back
198              
199             =head1 INCOMPATIBILITIES
200              
201             This distribution has no known incompatibilities.
202              
203             =head1 BUGS AND LIMITATIONS
204              
205             There are no known bugs or limitations
206              
207             =head1 TEST AND QUALITY
208              
209             The following policies have been disabled for this distribution
210              
211             =over
212              
213             =item * L<Perl::Critic::Policy::ValuesAndExpressions::ProhibitConstantPragma>
214              
215             Constants are good, - see the link below.
216              
217             =over
218              
219             =item * L<https://logiclab.jira.com/wiki/display/OPEN/Perl-Critic-Policy-ValuesAndExpressions-ProhibitConstantPragma>
220              
221             =back
222              
223             =item * L<Perl::Critic::Policy::NamingConventions::Capitalization>
224              
225             =back
226              
227             See also F<t/perlcriticrc>
228              
229             =head2 TEST COVERAGE
230              
231             Coverage test executed the following way, the coverage report is based on the
232             version described in this documentation (see L</VERSION>).
233              
234             ./Build testcover
235              
236             ---------------------------- ------ ------ ------ ------ ------ ------ ------
237             File stmt bran cond sub pod time total
238             ---------------------------- ------ ------ ------ ------ ------ ------ ------
239             ...gicLAB/ModuleBlacklist.pm 88.9 63.6 40.0 100.0 100.0 100.0 83.6
240             Total 88.9 63.6 40.0 100.0 100.0 100.0 83.6
241             ---------------------------- ------ ------ ------ ------ ------ ------ ------
242              
243             =head1 BUG REPORTING
244              
245             Please report issues via CPAN RT:
246              
247             http://rt.cpan.org/NoAuth/Bugs.html?Dist=Perl-Critic-Policy-logicLAB-ModuleBlacklist
248              
249             or by sending mail to
250              
251             bug-Perl-Critic-Policy-logicLAB-ModuleBlacklist@rt.cpan.org
252              
253             =head1 SEE ALSO
254              
255             =over
256              
257             =item * L<Perl::Critic>
258              
259             =item * L<http://logiclab.jira.com/wiki/display/PCLL/Home>
260              
261             =back
262              
263             =head1 MOTIVATION
264              
265             I once read an article which compared programming languages to
266             natural languages. Programming languages in themselves are not
267             large as such, but if you also regard the APIs, data structures
268             and components a computer programmer use on a daily basis, the
269             amount is enormous.
270              
271             Where I work We try to keep a more simple code base, the complexity
272             is in our business and that is our primary problem area, so it should
273             not be difficult to understand the code used to model this complexity.
274              
275             So sometimes it is necessary to make a decision on what should be
276             allowed in our code base and what should not. This policy aims to
277             support this coding practice.
278              
279             The practice it basically to prohibit problematic components and
280             recommend alternatives where possible.
281              
282             =head1 RECOMMENDATIONS
283              
284             Here follows some recommendations I have picked up.
285              
286             =over
287              
288             =item * L<Error> should be replaced by L<Class::Exception>, by recommendation
289             the author
290              
291             =item * L<IDNA::Punycode> should be replaced by L<Net::IDN::Encode> by recommendation
292             the author
293              
294             =item * <File::Slurp> should be replaced by either <File::Slurper>, <Path::Tiny> or <IO::All>
295             Ref: L<http://blogs.perl.org/users/leon_timmermans/2015/08/fileslurp-is-broken-and-wrong.html>
296              
297             =item * <File::Stat> should be replaced by <File::stat>
298              
299             =back
300              
301             =head1 AUTHOR
302              
303             =over
304              
305             =item * Jonas B. Nielsen, jonasbn C<< <jonasbn@cpan.org> >>
306              
307             =back
308              
309             =head1 ACKNOWLEDGEMENT
310              
311             =over
312              
313             =item * Jeffrey Ryan Thalhammer (THALJEF) and the Perl::Critic contributors for
314             Perl::Critic
315              
316             =item * Milan Å orm for the first and second bug reports on this policy
317              
318             =back
319              
320             =head1 LICENSE AND COPYRIGHT
321              
322             Copyright (c) 2014-2015 Jonas B. Nielsen, jonasbn. All rights reserved.
323              
324             Perl::Critic::Policy::logicLAB::ModuleBlacklist is released under
325             the Artistic License 2.0
326              
327             The distribution is licensed under the Artistic License 2.0, as specified by
328             the license file included in this distribution.
329              
330             =cut