File Coverage

blib/lib/Perl/Critic/Policy/OTRS/RequireTrueReturnValueForModules.pm
Criterion Covered Total %
statement 34 34 100.0
branch 6 8 75.0
condition n/a
subroutine 12 12 100.0
pod 5 5 100.0
total 57 59 96.6


line stmt bran cond sub pod time code
1             package Perl::Critic::Policy::OTRS::RequireTrueReturnValueForModules;
2              
3             # ABSTRACT: Check if modules have a "true" return value
4              
5 24     24   14731 use strict;
  24         66  
  24         658  
6 24     24   118 use warnings;
  24         68  
  24         661  
7              
8 24     24   133 use Perl::Critic::Utils qw{ :severities :classification :ppi };
  24         49  
  24         1211  
9 24     24   8042 use base 'Perl::Critic::Policy';
  24         55  
  24         2650  
10              
11 24     24   170 use Readonly;
  24         54  
  24         8564  
12              
13             our $VERSION = '0.04';
14              
15             Readonly::Scalar my $DESC => q{Modules have to return a true value ("1;")};
16             Readonly::Scalar my $EXPL => q{Use "1;" as the last statement of the module};
17              
18 12     12 1 28723 sub supported_parameters { return; }
19 3     3 1 43 sub default_severity { return $SEVERITY_HIGHEST; }
20 1     1 1 581 sub default_themes { return qw( otrs otrs_lt_3_3 ) }
21 6     6 1 241854 sub applies_to { return 'PPI::Document' }
22              
23             sub violates {
24 5     5 1 65 my ( $self, $elem ) = @_;
25              
26 5 50       41 return if $self->_is_script( $elem );
27 5 100       23 return if $self->_returns_1( $elem );
28 2         17 return $self->violation( $DESC, $EXPL, $elem );
29             }
30              
31             sub _returns_1 {
32 5     5   17 my ( $self, $elem ) = @_;
33              
34 5         59 my $last_statement = ( grep{ ref $_ eq 'PPI::Statement' }$elem->schildren )[-1];
  15         207  
35 5 50       25 return 0 if !$last_statement;
36 5 100       46 return 1 if $last_statement eq '1;';
37 2         67 return 0;
38             }
39              
40             sub _is_script {
41 5     5   17 my ( $self, $elem ) = @_;
42              
43 5         40 my $document = $elem->document;
44 5         190 my $filename = $document->logical_filename;
45              
46 5         186 my $is_module = $filename =~ m{ \.pm \z }xms;
47              
48 5         24 return !$is_module;
49             }
50              
51             1;
52              
53             __END__
54              
55             =pod
56              
57             =encoding UTF-8
58              
59             =head1 NAME
60              
61             Perl::Critic::Policy::OTRS::RequireTrueReturnValueForModules - Check if modules have a "true" return value
62              
63             =head1 VERSION
64              
65             version 0.09
66              
67             =head1 METHODS
68              
69             =head2 supported_parameters
70              
71             There are no supported parameters.
72              
73             =head1 AUTHOR
74              
75             Renee Baecker <info@perl-services.de>
76              
77             =head1 COPYRIGHT AND LICENSE
78              
79             This software is Copyright (c) 2013 by Renee Baecker.
80              
81             This is free software, licensed under:
82              
83             The Artistic License 2.0 (GPL Compatible)
84              
85             =cut