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   2583 use strict;
  4         11  
  4         115  
3 4     4   24 use warnings;
  4         9  
  4         126  
4 4     4   1708 use version 0.77;
  4         7620  
  4         30  
5 4     4   318 use Perl::Critic::Utils ':booleans';
  4         9  
  4         246  
6 4     4   381 use Perl::Critic::Utils::Constants qw(@STRICT_EQUIVALENT_MODULES);
  4         10  
  4         315  
7 4     4   25 use parent 'Perl::Critic::Policy';
  4         9  
  4         27  
8              
9 0     0 1 0 sub default_themes { return qw( maintenance ) }
10 8     8 1 62435 sub applies_to { return 'PPI::Document' }
11              
12             sub supported_parameters {
13             return (
14             {
15 11     11 0 53409 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 123 my ( $self, $elem, $doc ) = @_;
30              
31 8         19 my @violations;
32 8 50       20 my @includes = grep { $_->type eq "use" } @{ $doc->find('PPI::Statement::Include') ||[] };
  13         295  
  8         29  
33 8         199 my @st_strict_pragma = grep { $_->pragma eq "strict" } @includes;
  13         225  
34 8     14   337 my $version_statement = $doc->find_first( sub { $_[1]->version } );
  14         639  
35              
36 8 100       146 if (@st_strict_pragma == 1) {
37 6         17 my %is_stricture_module = %{$self->{_stricture_modules}};
  6         131  
38              
39 6         31 my @st_strict_module = grep { $is_stricture_module{ $_ } } map { $_->module } @includes;
  11         155  
  11         137  
40              
41 6 100       24 if ($version_statement) {
42 2         9 my $version = version->parse( $version_statement->version );
43              
44 2 100       70 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         1062 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;