File Coverage

blib/lib/Perl/Critic/Policy/Modules/RequireEndWithOne.pm
Criterion Covered Total %
statement 32 32 100.0
branch 4 4 100.0
condition 3 6 50.0
subroutine 13 13 100.0
pod 5 6 83.3
total 57 61 93.4


line stmt bran cond sub pod time code
1             package Perl::Critic::Policy::Modules::RequireEndWithOne;
2              
3 40     40   25593 use 5.010001;
  40         168  
4 40     40   253 use strict;
  40         111  
  40         893  
5 40     40   244 use warnings;
  40         91  
  40         982  
6 40     40   205 use Readonly;
  40         340  
  40         5735  
7              
8 40     40   285 use Perl::Critic::Utils qw{ :severities :classification };
  40         87  
  40         2113  
9 40     40   15972 use parent 'Perl::Critic::Policy';
  40         166  
  40         237  
10              
11             our $VERSION = '1.150';
12              
13             #-----------------------------------------------------------------------------
14              
15             Readonly::Scalar my $EXPL => q{Must end with a recognizable true value};
16             Readonly::Scalar my $DESC => q{Module does not end with "1;"};
17              
18             #-----------------------------------------------------------------------------
19              
20 89     89 0 1683 sub supported_parameters { return () }
21 79     79 1 333 sub default_severity { return $SEVERITY_HIGH }
22 92     92 1 379 sub default_themes { return qw( core bugs pbp certrule ) }
23 31     31 1 86 sub applies_to { return 'PPI::Document' }
24              
25             #-----------------------------------------------------------------------------
26              
27             sub prepare_to_scan_document {
28 32     32 1 125 my ( $self, $document ) = @_;
29              
30 32         118 return $document->is_module(); # Must be a library or module.
31             }
32              
33             sub violates {
34 31     31 1 106 my ( $self, $elem, $doc ) = @_;
35              
36             # Last statement should be just "1;"
37 31         238 my @significant = grep { _is_code($_) } $doc->schildren();
  245         1358  
38 31         126 my $match = $significant[-1];
39 31 100       146 return if !$match;
40 29 100 66     149 return if ((ref $match) eq 'PPI::Statement' &&
41             $match =~ m{\A 1 \s* ; \z}xms );
42              
43             # Must be a violation...
44 5         29 return $self->violation( $DESC, $EXPL, $match );
45             }
46              
47             sub _is_code {
48 245     245   324 my $elem = shift;
49 245   33     1156 return ! ( $elem->isa('PPI::Statement::End')
50             || $elem->isa('PPI::Statement::Data'));
51             }
52              
53             1;
54              
55             __END__
56              
57             #-----------------------------------------------------------------------------
58              
59             =pod
60              
61             =head1 NAME
62              
63             Perl::Critic::Policy::Modules::RequireEndWithOne - End each module with an explicitly C<1;> instead of some funky expression.
64              
65              
66             =head1 AFFILIATION
67              
68             This Policy is part of the core L<Perl::Critic|Perl::Critic>
69             distribution.
70              
71              
72             =head1 DESCRIPTION
73              
74             All files included via C<use> or C<require> must end with a true value
75             to indicate to the caller that the include was successful. The
76             standard practice is to conclude your .pm files with C<1;>, but some
77             authors like to get clever and return some other true value like
78             C<return "Club sandwich";>. We cannot tolerate such frivolity! OK,
79             we can, but we don't recommend it since it confuses the newcomers.
80              
81              
82             =head1 CONFIGURATION
83              
84             This Policy is not configurable except for the standard options.
85              
86              
87             =head1 AUTHOR
88              
89             Chris Dolan C<cdolan@cpan.org>
90              
91             Some portions cribbed from
92             L<Perl::Critic::Policy::Modules::RequireExplicitPackage|Perl::Critic::Policy::Modules::RequireExplicitPackage>.
93              
94              
95             =head1 COPYRIGHT
96              
97             Copyright (c) 2005-2011 Chris Dolan and Imaginative Software Systems. All
98             rights reserved.
99              
100             This program is free software; you can redistribute it and/or modify
101             it under the same terms as Perl itself. The full text of this license
102             can be found in the LICENSE file included with this module.
103              
104             =cut
105              
106             # Local Variables:
107             # mode: cperl
108             # cperl-indent-level: 4
109             # fill-column: 78
110             # indent-tabs-mode: nil
111             # c-indentation-style: bsd
112             # End:
113             # ex: set ts=8 sts=4 sw=4 tw=78 ft=perl expandtab shiftround :