File Coverage

blib/lib/Perl/Critic/Policy/Community/StrictWarnings.pm
Criterion Covered Total %
statement 49 50 98.0
branch 17 18 94.4
condition 16 19 84.2
subroutine 12 13 92.3
pod 4 5 80.0
total 98 105 93.3


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