Login
Rename is_document_exempt() to prepare_to_scan_document().
[gknop/Perl-Critic.git] / lib / Perl / Critic / Policy / Modules / RequireEndWithOne.pm
CommitLineData
6036a254 1##############################################################################
a627f28c
JRT
2# $URL$
3# $Date$
4# $Author$
5# $Revision$
6036a254 6##############################################################################
dff08b70 7
bf159007
JRT
8package Perl::Critic::Policy::Modules::RequireEndWithOne;
9
df6dee2b 10use 5.006001;
bf159007
JRT
11use strict;
12use warnings;
c680a9c9
ES
13use Readonly;
14
bbf4108c 15use Perl::Critic::Utils qw{ :severities :classification };
bf159007
JRT
16use base 'Perl::Critic::Policy';
17
173667ce 18our $VERSION = '1.093_01';
bf159007 19
6036a254 20#-----------------------------------------------------------------------------
86bf4aa6 21
c680a9c9
ES
22Readonly::Scalar my $EXPL => q{Must end with a recognizable true value};
23Readonly::Scalar my $DESC => q{Module does not end with "1;"};
bf159007 24
6036a254 25#-----------------------------------------------------------------------------
bf159007 26
c680a9c9
ES
27sub supported_parameters { return () }
28sub default_severity { return $SEVERITY_HIGH }
29sub default_themes { return qw( core bugs pbp ) }
30sub applies_to { return 'PPI::Document' }
86bf4aa6 31
6036a254 32#-----------------------------------------------------------------------------
bf159007 33
78afb6d4 34sub prepare_to_scan_document {
f9d21294
ES
35 my ( $self, $document ) = @_;
36
78afb6d4 37 return not is_script($document); # Must be a library or module.
f9d21294
ES
38}
39
bf159007
JRT
40sub violates {
41 my ( $self, $elem, $doc ) = @_;
bf159007
JRT
42
43 # Last statement should be just "1;"
6bf9b465 44 my @significant = grep { _is_code($_) } $doc->schildren();
6015ad73
CD
45 my $match = $significant[-1];
46 return if !$match;
47 return if ((ref $match) eq 'PPI::Statement' &&
a0dcf06f 48 $match =~ m{\A 1 \s* ; \z}xms );
6bf9b465
JRT
49
50 # Must be a violation...
c680a9c9 51 return $self->violation( $DESC, $EXPL, $match );
6bf9b465
JRT
52}
53
54sub _is_code {
55 my $elem = shift;
56 return ! ( $elem->isa('PPI::Statement::End')
57 || $elem->isa('PPI::Statement::Data'));
bf159007
JRT
58}
59
601;
61
62__END__
63
6036a254 64#-----------------------------------------------------------------------------
86bf4aa6 65
bf159007
JRT
66=pod
67
68=head1 NAME
69
f017d93a 70Perl::Critic::Policy::Modules::RequireEndWithOne - End each module with an explicitly C<1;> instead of some funky expression.
bf159007 71
11f53956 72
af93c316
ES
73=head1 AFFILIATION
74
11f53956
ES
75This Policy is part of the core L<Perl::Critic|Perl::Critic>
76distribution.
af93c316
ES
77
78
bf159007
JRT
79=head1 DESCRIPTION
80
81All files included via C<use> or C<require> must end with a true value
82to indicate to the caller that the include was successful. The
83standard practice is to conclude your .pm files with C<1;>, but some
84authors like to get clever and return some other true value like
11f53956
ES
85C<return "Club sandwich";>. We cannot tolerate such frivolity! OK,
86we can, but we don't recommend it since it confuses the newcomers.
bf159007 87
0cb729f0
ES
88
89=head1 CONFIGURATION
90
49860482 91This Policy is not configurable except for the standard options.
0cb729f0
ES
92
93
bf159007
JRT
94=head1 AUTHOR
95
96Chris Dolan C<cdolan@cpan.org>
97
98Some portions cribbed from
11f53956
ES
99L<Perl::Critic::Policy::Modules::RequireExplicitPackage|Perl::Critic::Policy::Modules::RequireExplicitPackage>.
100
bf159007
JRT
101
102=head1 COPYRIGHT
103
20dfddeb 104Copyright (c) 2005-2008 Chris Dolan and Jeffrey Ryan Thalhammer. All
86bf4aa6 105rights reserved.
bf159007
JRT
106
107This program is free software; you can redistribute it and/or modify
108it under the same terms as Perl itself. The full text of this license
109can be found in the LICENSE file included with this module.
110
111=cut
737d3b65
CD
112
113# Local Variables:
114# mode: cperl
115# cperl-indent-level: 4
116# fill-column: 78
117# indent-tabs-mode: nil
118# c-indentation-style: bsd
119# End:
96fed375 120# ex: set ts=8 sts=4 sw=4 tw=78 ft=perl expandtab shiftround :