File Coverage

blib/lib/Perl/Critic/Policy/TooMuchCode/ProhibitExtraStricture.pm
Criterion Covered Total %
statement 42 43 97.6
branch 9 10 90.0
condition n/a
subroutine 10 11 90.9
pod 3 4 75.0
total 64 68 94.1


line stmt bran cond sub pod time code
1             package Perl::Critic::Policy::TooMuchCode::ProhibitExtraStricture;
2 4     4   2670 use strict;
  4         10  
  4         123  
3 4     4   77 use warnings;
  4         10  
  4         132  
4 4     4   1656 use version 0.77;
  4         7714  
  4         31  
5 4     4   348 use Perl::Critic::Utils ':booleans';
  4         11  
  4         315  
6 4     4   376 use Perl::Critic::Utils::Constants qw(@STRICT_EQUIVALENT_MODULES);
  4         10  
  4         322  
7 4     4   28 use parent 'Perl::Critic::Policy';
  4         10  
  4         62  
8              
9 0     0 1 0 sub default_themes { return qw( maintenance ) }
10 8     8 1 63183 sub applies_to { return 'PPI::Document' }
11              
12             sub supported_parameters {
13             return (
14             {
15 11     11 0 54298 name => 'stricture_modules',
16             description => 'Modules which enables strictures.',
17             behavior => 'string list',
18             list_always_present_values => [
19             @STRICT_EQUIVALENT_MODULES,
20             'Test2::V0',
21             ],
22             }
23             );
24             }
25              
26             #---------------------------------------------------------------------------
27              
28             sub violates {
29 8     8 1 122 my ( $self, $elem, $doc ) = @_;
30              
31 8         21 my @violations;
32 8 50       23 my @includes = grep { $_->type eq "use" } @{ $doc->find('PPI::Statement::Include') ||[] };
  13         357  
  8         39  
33 8         201 my @st_strict_pragma = grep { $_->pragma eq "strict" } @includes;
  13         214  
34 8     14   311 my $version_statement = $doc->find_first( sub { $_[1]->version } );
  14         642  
35              
36 8 100       152 if (@st_strict_pragma == 1) {
37 6         18 my %is_stricture_module = %{$self->{_stricture_modules}};
  6         148  
38              
39 6         27 my @st_strict_module = grep { $is_stricture_module{ $_ } } map { $_->module } @includes;
  11         152  
  11         136  
40              
41 6 100       24 if ($version_statement) {
42 2         10 my $version = version->parse( $version_statement->version );
43              
44 2 100       72 if ( $version >= qv('v5.11.0') ) {
45 1         19 push @st_strict_module, $version;
46             }
47             }
48              
49 6 100       43 if (@st_strict_module) {
50 4         46 push @violations, $self->violation(
51             "This `use strict` is redundant since ". $st_strict_module[0] . " also in place",
52             "stricture is implied when using " . $st_strict_module[0] . ". Therefore there is no need to `use strict` in the same scope.",
53             $st_strict_pragma[0],
54             )
55             }
56             }
57              
58 8         1039 return @violations;
59             }
60              
61              
62             1;
63              
64             =encoding utf-8
65              
66             =head1 NAME
67              
68             TooMuchCode::ProhibitExtraStricture -- Find unnecessary 'use strict'
69              
70             =head1 DESCRIPTION
71              
72             Code stricture is good but that does not mean you always need to put
73             C<use strict> in your code. Several other modules enable code
74             stricture in the current scope, effectively the same having C<use strict>
75              
76             Here's a list of those modules:
77              
78             Moose
79             Mouse
80             Moo
81             Mo
82             Moose::Role
83             Mouse::Role
84             Moo::Role
85             Test2::V0
86              
87             When one of these modules are used, C<use strict> is considered redundant
88             and is marked as violation by this policy.
89              
90             =head2 Configuration
91              
92             The builtin list of stricture modules is obviously not
93             comprehensive. You could extend the list by setting the
94             C<stricture_modules> in the config. For example, with the following
95             setting, two modules, C<Foo> and C<Bar>, are appended to the list of
96             stricture modules.
97              
98             [TooMuchCode::ProhibitExtraStricture]
99             stricture_modules = Foo Bar
100              
101              
102             =cut
103              
104             1;