File Coverage

blib/lib/Perl/Critic/Policy/Community/StrictWarnings.pm
Criterion Covered Total %
statement 48 49 97.9
branch 15 16 93.7
condition 13 16 81.2
subroutine 12 13 92.3
pod 4 5 80.0
total 92 99 92.9


line stmt bran cond sub pod time code
1             package Perl::Critic::Policy::Community::StrictWarnings;
2              
3 1     1   483 use strict;
  1         3  
  1         31  
4 1     1   6 use warnings;
  1         2  
  1         31  
5              
6 1     1   7 use Perl::Critic::Utils qw(:severities :classification :ppi);
  1         3  
  1         105  
7 1     1   446 use Perl::Critic::Utils::Constants qw(@STRICT_EQUIVALENT_MODULES @WARNINGS_EQUIVALENT_MODULES);
  1         3  
  1         91  
8 1     1   16 use parent 'Perl::Critic::Policy';
  1         5  
  1         7  
9 1     1   542 use version;
  1         2244  
  1         8  
10              
11             our $VERSION = 'v1.0.1';
12              
13 1     1   105 use constant DESC => 'Missing strict or warnings';
  1         3  
  1         94  
14 1     1   19 use constant EXPL => 'The strict and warnings pragmas are important to avoid common pitfalls and deprecated/experimental functionality. Make sure each script or module contains "use strict; use warnings;" or a module that does this for you.';
  1         3  
  1         458  
15              
16             sub supported_parameters {
17             (
18             {
19 13     13 0 44657 name => 'extra_importers',
20             description => 'Non-standard modules to recognize as importing strict and warnings',
21             behavior => 'string list',
22             },
23             )
24             }
25              
26 7     7 1 89 sub default_severity { $SEVERITY_HIGH }
27 0     0 1 0 sub default_themes { 'community' }
28 13     13 1 78961 sub applies_to { 'PPI::Document' }
29              
30             my @incomplete_importers = qw(common::sense sanity);
31              
32             sub violates {
33 13     13 1 149 my ($self, $elem) = @_;
34 13   100     45 my $includes = $elem->find('PPI::Statement::Include') || [];
35            
36             # Add importers from Perl::Critic core
37 13         225 my %strict_importers = map { ($_ => 1) } @STRICT_EQUIVALENT_MODULES;
  364         2196  
38 13         117 my %warnings_importers = map { ($_ => 1) } @WARNINGS_EQUIVALENT_MODULES;
  364         1976  
39            
40             # Remove incomplete importers if added
41 13         119 delete $strict_importers{$_} for @incomplete_importers;
42 13         37 delete $warnings_importers{$_} for @incomplete_importers;
43            
44             # Add extra importers
45 13         24 $strict_importers{$_} = $warnings_importers{$_} = 1 foreach keys %{$self->{_extra_importers}};
  13         47  
46            
47 13         26 my ($has_strict, $has_warnings);
48 13         27 foreach my $include (@$includes) {
49 19 100       74 if ($include->pragma) {
50 10 100       374 $has_strict = 1 if $include->pragma eq 'strict';
51 10 100       324 $has_warnings = 1 if $include->pragma eq 'warnings';
52             }
53 19 50 50     617 if ($include->type//'' eq 'use') {
54 19 100 100     412 $has_strict = 1 if $include->version and version->parse($include->version) >= version->parse('v5.12');
55 19 100 66     670 $has_strict = 1 if defined $include->module and exists $strict_importers{$include->module};
56 19 100 66     827 $has_warnings = 1 if defined $include->module and exists $warnings_importers{$include->module};
57             }
58 19 100 100     956 return () if $has_strict and $has_warnings;
59             }
60            
61 7         47 return $self->violation(DESC, EXPL, $elem);
62             }
63              
64             1;
65              
66             =head1 NAME
67              
68             Perl::Critic::Policy::Community::StrictWarnings - Always use strict and
69             warnings, or a module that imports these
70              
71             =head1 DESCRIPTION
72              
73             The L<strict> and L<warnings> pragmas help avoid many common pitfalls such as
74             misspellings, scoping issues, and performing operations on undefined values.
75             Warnings can also alert you to deprecated or experimental functionality. The
76             pragmas may either be explicitly imported with C<use>, or indirectly through a
77             number of importer modules such as L<Moose> or L<strictures>. L<strict> is also
78             enabled automatically with a C<use> declaration of perl version 5.12 or higher.
79              
80             use strict;
81             use warnings;
82              
83             use Moose;
84              
85             use 5.012;
86             use warnings;
87              
88             This policy is similar to the core policies
89             L<Perl::Critic::Policy::TestingAndDebugging::RequireUseStrict> and
90             L<Perl::Critic::Policy::TestingAndDebugging::RequireUseWarnings>, but combines
91             them into one policy in the C<community> theme. The default modules recognized
92             as importing L<strict> and L<warnings> are defined by the same constants as the
93             core policies, L<Perl::Critic::Utils::Constants/"@STRICT_EQUIVALENT_MODULES">.
94             To define additional modules, see L</"CONFIGURATION">.
95              
96             =head1 AFFILIATION
97              
98             This policy is part of L<Perl::Critic::Community>.
99              
100             =head1 CONFIGURATION
101              
102             This policy can be configured to recognize additional modules as importers of
103             L<strict> and L<warnings>, by putting an entry in a C<.perlcriticrc> file like
104             this:
105              
106             [Community::StrictWarnings]
107             extra_importers = MyApp::Class MyApp::Role
108              
109             =head1 AUTHOR
110              
111             Dan Book, C<dbook@cpan.org>
112              
113             =head1 COPYRIGHT AND LICENSE
114              
115             Copyright 2015, Dan Book.
116              
117             This library is free software; you may redistribute it and/or modify it under
118             the terms of the Artistic License version 2.0.
119              
120             =head1 SEE ALSO
121              
122             L<Perl::Critic>