File Coverage

blib/lib/Perl/Lint/Policy/Modules/ProhibitEvilModules.pm
Criterion Covered Total %
statement 51 51 100.0
branch 10 10 100.0
condition 11 11 100.0
subroutine 9 9 100.0
pod 0 1 0.0
total 81 82 98.7


line stmt bran cond sub pod time code
1             package Perl::Lint::Policy::Modules::ProhibitEvilModules;
2 133     133   105004 use strict;
  133         261  
  133         4961  
3 133     133   599 use warnings;
  133         201  
  133         3879  
4 133     133   599 use List::Util qw/any/;
  133         203  
  133         8787  
5 133     133   1038 use Perl::Lint::Constants::Type;
  133         234  
  133         83257  
6 133     133   1072 use parent "Perl::Lint::Policy";
  133         233  
  133         822  
7              
8             use constant {
9 133         9304 DESC => 'The names of or patterns for modules to forbid',
10             EXPL => 'Find an alternative module',
11 133     133   9138 };
  133         240  
12              
13             # TODO Should use Module::Adviser?
14 133         66445 use constant EVILS => [qw/
15             Class::ISA
16             Pod::Plainer
17             Shell
18             Switch
19 133     133   689 /];
  133         224  
20              
21             sub evaluate {
22 15     15 0 49 my ($class, $file, $tokens, $src, $args) = @_;
23              
24 15   100     94 my $modules_arg = $args->{prohibit_evil_modules}->{modules} || '';
25 15         48 $modules_arg =~ s/{.*?}//g;
26 15         55 my @evils = split(/ /, $modules_arg);
27              
28 15         43 my $modules_file = $args->{prohibit_evil_modules}->{modules_file};
29 15 100       47 if ($modules_file) {
30 2         112 open my $fh, '<', $modules_file;
31 2         6 my $content = do { local $/; <$fh> };
  2         8  
  2         49  
32 2         166 push @evils, ($content =~ /^\s*?([^ \n\r\f\t#]+)/gm);
33             }
34              
35 15         49 my @evils_re = map {m!/(.+?)/!; $1} @evils;
  29         66  
  29         83  
36              
37 15         19 my @violations;
38 15         118 for (my $i = 0; my $token = $tokens->[$i]; $i++) {
39 70         81 my $token_type = $token->{type};
40 70 100       143 if ($token_type == USE_DECL) {
41 31         41 my $used_name = '';
42 31         78 for ($i++; my $token = $tokens->[$i]; $i++) {
43 115         111 my $token_type = $token->{type};
44 115 100 100     396 if (
      100        
45             $token_type != NAMESPACE &&
46             $token_type != NAMESPACE_RESOLVER &&
47             $token_type != USED_NAME
48             ) {
49 30         50 last;
50             }
51 85         184 $used_name .= $token->{data};
52             }
53              
54 31 100 100     137 if (
55 188 100   188   255 any {$used_name eq $_} (@{+EVILS}, @evils) or
  31         184  
  37         392  
56             any {$_ && $used_name =~ /$_/} @evils_re
57             ) {
58 30         271 push @violations, {
59             filename => $file,
60             line => $token->{line},
61             description => DESC,
62             explanation => EXPL,
63             policy => __PACKAGE__,
64             };
65             }
66             }
67             }
68              
69 15         87 return \@violations;
70             }
71              
72             1;
73