Modified source files and compiled any and armel versions of packages
[pkg-perl] / deb-src / libperl-critic-perl / libperl-critic-perl-1.088 / lib / Perl / Critic / Policy / TestingAndDebugging / RequireUseWarnings.pm
1 ##############################################################################
2 #      $URL: http://perlcritic.tigris.org/svn/perlcritic/trunk/Perl-Critic/lib/Perl/Critic/Policy/TestingAndDebugging/RequireUseWarnings.pm $
3 #     $Date: 2008-07-03 10:19:10 -0500 (Thu, 03 Jul 2008) $
4 #   $Author: clonezone $
5 # $Revision: 2489 $
6 ##############################################################################
7
8 package Perl::Critic::Policy::TestingAndDebugging::RequireUseWarnings;
9
10 use 5.006001;
11 use strict;
12 use warnings;
13 use Readonly;
14
15 use List::Util qw(first);
16 use version ();
17
18 use Perl::Critic::Utils qw{ :severities };
19 use base 'Perl::Critic::Policy';
20
21 our $VERSION = '1.088';
22
23 #-----------------------------------------------------------------------------
24
25 Readonly::Scalar my $DESC => q{Code before warnings are enabled};
26 Readonly::Scalar my $EXPL => [431];
27
28 Readonly::Scalar my $MINIMUM_VERSION => version->new(5.006);
29
30 #-----------------------------------------------------------------------------
31
32 sub supported_parameters { return ()                  }
33 sub default_severity     { return $SEVERITY_HIGH      }
34 sub default_themes       { return qw( core bugs pbp ) }
35 sub applies_to           { return 'PPI::Document'     }
36
37 sub default_maximum_violations_per_document { return 1; }
38
39 #-----------------------------------------------------------------------------
40
41 sub violates {
42     my ( $self, $elem, $document ) = @_;
43
44     my $version = $document->highest_explicit_perl_version();
45     return if $version and $version < $MINIMUM_VERSION;
46
47     # Find the first 'use warnings' statement
48     my $warn_stmnt = $document->find_first( \&_is_use_warnings );
49     my $warn_line  = $warn_stmnt ? $warn_stmnt->location()->[0] : undef;
50
51     # Find all statements that aren't 'use', 'require', or 'package'
52     my $stmnts_ref = $document->find( \&_isnt_include_or_package );
53     return if !$stmnts_ref;
54
55     # If the 'use warnings' statement is not defined, or the other
56     # statement appears before the 'use warnings', then it violates.
57
58     my @viols = ();
59     for my $stmnt ( @{ $stmnts_ref } ) {
60         last if $stmnt->isa('PPI::Statement::End');
61         last if $stmnt->isa('PPI::Statement::Data');
62
63         my $stmnt_line = $stmnt->location()->[0];
64         if ( (! defined $warn_line) || ($stmnt_line < $warn_line) ) {
65             push @viols, $self->violation( $DESC, $EXPL, $stmnt );
66         }
67     }
68     return @viols;
69 }
70
71 sub _is_use_warnings {
72     my (undef, $elem) = @_;
73
74     return 0 if !$elem->isa('PPI::Statement::Include');
75     return 0 if $elem->type() ne 'use';
76
77     if (
78             $elem->pragma() ne 'warnings'
79         and $elem->module() ne 'Moose'
80         and $elem->module() ne 'Moose::Role'
81     ) {
82         return 0;
83     }
84
85     return 1;
86 }
87
88 sub _isnt_include_or_package {
89     my (undef, $elem) = @_;
90     return 0 if ! $elem->isa('PPI::Statement');
91     return 0 if $elem->isa('PPI::Statement::Package');
92     return 0 if $elem->isa('PPI::Statement::Include');
93     return 1;
94 }
95
96 1;
97
98 __END__
99
100 #-----------------------------------------------------------------------------
101
102 =pod
103
104 =head1 NAME
105
106 Perl::Critic::Policy::TestingAndDebugging::RequireUseWarnings - Always C<use warnings>.
107
108
109 =head1 AFFILIATION
110
111 This Policy is part of the core L<Perl::Critic> distribution.
112
113
114 =head1 DESCRIPTION
115
116 Using warnings, and paying attention to what they say, is probably the
117 single most effective way to improve the quality of your code.  This
118 policy requires that the C<'use warnings'> statement must come before
119 any other statements except C<package>, C<require>, and other C<use>
120 statements.  Thus, all the code in the entire package will be
121 affected.
122
123 There are special exemptions for L<Moose> and L<Moose::Role> because
124 they enforces strictness; e.g. C<'use Moose'> is treated as equivalent
125 to C<'use warnings'>.
126
127 This policy will not complain if the file explicitly states that it is
128 compatible with a version of perl prior to 5.6 via an include
129 statement, e.g. by having C<require 5.005> in it.
130
131 The maximum number of violations per document for this policy defaults
132 to 1.
133
134
135 =head1 CONFIGURATION
136
137 This Policy is not configurable except for the standard options.
138
139
140 =head1 SEE ALSO
141
142 L<Perl::Critic::Policy::TestingAndDebugging::ProhibitNoWarnings>
143
144
145 =head1 AUTHOR
146
147 Jeffrey Ryan Thalhammer <thaljef@cpan.org>
148
149
150 =head1 COPYRIGHT
151
152 Copyright (c) 2005-2008 Jeffrey Ryan Thalhammer.  All rights reserved.
153
154 This program is free software; you can redistribute it and/or modify
155 it under the same terms as Perl itself.  The full text of this license
156 can be found in the LICENSE file included with this module
157
158 =cut
159
160 ##############################################################################
161 # Local Variables:
162 #   mode: cperl
163 #   cperl-indent-level: 4
164 #   fill-column: 78
165 #   indent-tabs-mode: nil
166 #   c-indentation-style: bsd
167 # End:
168 # ex: set ts=8 sts=4 sw=4 tw=78 ft=perl expandtab shiftround :