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