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