File Coverage

blib/lib/Perl/Critic/Policy/Mardem/ProhibitReturnBooleanAsInt.pm
Criterion Covered Total %
statement 36 40 90.0
branch 5 8 62.5
condition 1 3 33.3
subroutine 11 12 91.6
pod 4 5 80.0
total 57 68 83.8


line stmt bran cond sub pod time code
1              
2             use utf8;
3 8     8   4591  
  8         18  
  8         34  
4             use 5.010;
5 8     8   264  
  8         24  
6             use strict;
7 8     8   35 use warnings;
  8         16  
  8         182  
8 8     8   47  
  8         14  
  8         371  
9             our $VERSION = '0.03';
10              
11             use Readonly;
12 8     8   89 use Perl::Critic::Utils qw( is_hash_key $SEVERITY_MEDIUM );
  8         21  
  8         460  
13 8     8   86  
  8         112  
  8         670  
14             use base 'Perl::Critic::Policy';
15 8     8   49  
  8         14  
  8         2549  
16             ## no critic (RequireInterpolationOfMetachars)
17             Readonly::Scalar my $EXPL => q{Consider using some $false, $true or other available module implementation};
18              
19             Readonly::Scalar my $DESC => q{"return" statement with explicit "0/1"};
20              
21             {
22             return $SEVERITY_MEDIUM;
23             }
24 11     11 1 112  
25             {
26             return qw(complexity maintenance);
27             }
28              
29 0     0 1 0 {
30             return 'PPI::Statement::Break';
31             }
32              
33             {
34 22     22 1 99119 return;
35             }
36              
37             {
38             my ( $self, $elem, undef ) = @_;
39 22     22 0 68037  
40             my $return_keyword = $elem->schild(); # not next element - need first child
41             if ( !$return_keyword ) {
42             return;
43             }
44 18     18 1 342  
45             if ( 'return' ne $return_keyword->content() || is_hash_key( $return_keyword ) ) {
46 18         46 return;
47 18 50       241 }
48 0         0  
49             my $return_line_content = $elem->content();
50             if ( !$return_line_content ) {
51 18 50 33     46 return;
52 0         0 }
53              
54             # fast regex violation check - eg. "return 1"; - "return (1); # comment" - "return 1 if ..."
55 18         964 my $return = q{return};
56 18 50       596 my $value = q{[(]?\s*[01]\s*[)]?};
57 0         0 my $opt_condition = q{(?:(?:if|unless)\s*[(]?\s*.+\s*[)]?\s*)?};
58              
59             # regex /aa requires Perl 5.14
60             my $regex = qr/^\s*$return\s*$value\s*$opt_condition\s*;/ixmso; ## no critic (RegularExpressions::RequireDefault)
61 18         25  
62 18         25 if ( $return_line_content !~ $regex ) {
63 18         25 return;
64             }
65              
66 18         84 return $self->violation( $DESC, $EXPL, $elem );
67             }
68 18 100       180  
69 7         26 1;
70              
71              
72 11         40 #-----------------------------------------------------------------------------
73              
74             =pod
75              
76             =encoding utf8
77              
78             =head1 NAME
79              
80             Perl::Critic::Policy::Mardem::ProhibitReturnBooleanAsInt
81              
82             =head1 DESCRIPTION
83              
84             This Policy searches for C<return 1> and C<return 0> statements,
85             which are mainly used for boolean meaning, but are less expressiv
86             than direct use of some boolean eg. C<return $true>.
87              
88             There are many different modules available for true, false - use it!
89              
90             =head1 CONFIGURATION
91              
92             No Configuration
93              
94             =head1 AFFILIATION
95              
96             This policy is part of L<Perl::Critic::Mardem>.
97              
98             =head1 AUTHOR
99              
100             Markus Demml, mardem@cpan.com
101              
102             =head1 LICENSE AND COPYRIGHT
103              
104             Copyright (c) 2022, Markus Demml
105              
106             This library is free software; you can redistribute it and/or modify it
107             under the same terms as the Perl 5 programming language system itself.
108             The full text of this license can be found in the LICENSE file included
109             with this module.
110              
111             =cut