File Coverage

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


line stmt bran cond sub pod time code
1             package Perl::Critic::Policy::Modules::RequireEndWithOne;
2              
3 40     40   26414 use 5.010001;
  40         186  
4 40     40   234 use strict;
  40         110  
  40         905  
5 40     40   207 use warnings;
  40         108  
  40         951  
6 40     40   207 use Readonly;
  40         86  
  40         1968  
7              
8 40     40   262 use Perl::Critic::Utils qw{ :severities :classification };
  40         95  
  40         2109  
9 40     40   13318 use parent 'Perl::Critic::Policy';
  40         112  
  40         217  
10              
11             our $VERSION = '1.146';
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 106     106 0 1745 sub supported_parameters { return () }
21 83     83 1 405 sub default_severity { return $SEVERITY_HIGH }
22 92     92 1 397 sub default_themes { return qw( core bugs pbp certrule ) }
23 47     47 1 155 sub applies_to { return 'PPI::Document' }
24              
25             #-----------------------------------------------------------------------------
26              
27             sub prepare_to_scan_document {
28 49     49 1 181 my ( $self, $document ) = @_;
29              
30 49         4191 return $document->is_module(); # Must be a library or module.
31             }
32              
33             sub violates {
34 47     47 1 130 my ( $self, $elem, $doc ) = @_;
35              
36             # Last statement should be just "1;"
37 47         329 my @significant = grep { _is_code($_) } $doc->schildren();
  274         2017  
38 47         207 my $match = $significant[-1];
39 47 100       220 return if !$match;
40 44 100 100     249 return if ((ref $match) eq 'PPI::Statement' &&
41             $match =~ m{\A 1 \s* ; \z}xms );
42              
43             # Must be a violation...
44 9         113 return $self->violation( $DESC, $EXPL, $match );
45             }
46              
47             sub _is_code {
48 274     274   483 my $elem = shift;
49 274   100     1582 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 :