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