blib/lib/Perl/Critic/Policy/ValuesAndExpressions/ProhibitLeadingZeros.pm

filter: Statementstmt 61.7% 63 / 102 Branchbran 25.0% 18 / 72 Conditioncond 33.3% 3 / 9 MC/DCmcdc 0.0% 0 / 5 Subroutinesub 100% 18 / 18 Podpod 80.0% 4 / 5 totaltotal 50.2% 106 / 211 CC 40 SCAR 38.2
CC
40
Cov
46%
_is_fourth_argument_of_sysopen
146.5
_is_third_argument_of_dbmopen
39.0
_is_second_argument_of_mkfifo
12.4
CRAP 45.5
« blib/lib/Perl/Critic/Policy/BuiltinFunctions/ProhibitBooleanGrep.pm ↑ Summary blib/lib/Perl/Critic/Policy/RegularExpressions/ProhibitEnumeratedClasses.pm »
1 package Perl::Critic::Policy::ValuesAndExpressions::ProhibitLeadingZeros;
2  
3 40 ▶use 5.010001;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 40 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 40 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 40 ▶use Readonly;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 40 ▶use Perl::Critic::Utils qw{ :characters :severities hashify };
Subroutine BEGIN: called CC=1 SCAR=0.0
10 40 ▶use parent 'Perl::Critic::Policy';
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 40 our $VERSION = '1.150';
13  
14 #-----------------------------------------------------------------------------
15  
16 40 Readonly::Scalar my $LEADING_RX => qr<\A [+-]? (?: 0+ _* )+ [1-9]>xms;
17 40 Readonly::Scalar my $EXPL => [ 58 ];
18  
19 #-----------------------------------------------------------------------------
20  
21 sub supported_parameters {
22 return (
23 {
24 91 ▶ name => 'strict',
Subroutine supported_parameters: called CC=1 SCAR=0.0
Pod undocumented
25 description =>
26 q<Don't allow any leading zeros at all. Otherwise builtins that deal with Unix permissions, e.g. chmod, don't get flagged.>,
27 default_string => '0',
28 behavior => 'boolean',
29 },
30 );
31 }
32  
33 89 ▶sub default_severity { return $SEVERITY_HIGHEST }
Subroutine default_severity: called CC=1 SCAR=0.0
Pod documented
34 92 ▶sub default_themes { return qw< core pbp bugs certrec > }
Subroutine default_themes: called CC=1 SCAR=0.0
Pod documented
35 37 ▶sub applies_to { return 'PPI::Token::Number::Octal' }
Subroutine applies_to: called CC=1 SCAR=0.0
Pod documented
36  
37 #-----------------------------------------------------------------------------
38  
39 sub violates {
40 14 ▶ my ( $self, $elem, undef ) = @_;
Subroutine violates: called CC=8 SCAR=22.8
Pod documented
41  
42 14 ▶ return if $elem !~ $LEADING_RX;
Branch: 1 branch
BranchTrueFalseTotal
unless $elem =~ /$LEADING_RX/ 0 14 14
43 14 ▶ return $self->_create_violation($elem) if $self->{_strict};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"_strict"} 0 14 14
44 14 ▶ return if $self->_is_first_argument_of_chmod_or_umask($elem);
Branch: 1 branch
BranchTrueFalseTotal
if $self->_is_first_argument_of_chmod_or_umask($elem) 0 14 14
45 14 ▶ return if $self->_is_second_argument_of_mkdir($elem);
Branch: 1 branch
BranchTrueFalseTotal
if $self->_is_second_argument_of_mkdir($elem) 0 14 14
46 14 ▶ return if $self->_is_second_argument_of_mkfifo($elem);
Branch: 1 branch
BranchTrueFalseTotal
if $self->_is_second_argument_of_mkfifo($elem) 0 14 14
47 14 ▶ return if $self->_is_third_argument_of_dbmopen($elem);
Branch: 1 branch
BranchTrueFalseTotal
if $self->_is_third_argument_of_dbmopen($elem) 0 14 14
48 14 ▶ return if $self->_is_fourth_argument_of_sysopen($elem);
Branch: 1 branch
BranchTrueFalseTotal
if $self->_is_fourth_argument_of_sysopen($elem) 0 14 14
49 14 return $self->_create_violation($elem);
50 }
51  
52 sub _create_violation {
53 14 ▶ my ($self, $elem) = @_;
Subroutine _create_violation: called CC=1 SCAR=0.0
54  
55 14 return $self->violation(
56 qq<Integer with leading zeros: "$elem">,
57 $EXPL,
58 $elem
59 );
60 }
61  
62 sub _is_first_argument_of_chmod_or_umask {
63 14 ▶ my ($self, $elem) = @_;
Subroutine _is_first_argument_of_chmod_or_umask: called CC=2 SCAR=7.0
64  
65 14 my $previous_token = _previous_token_that_isnt_a_parenthesis($elem);
66 14 ▶ return if not $previous_token;
Branch: 1 branch
BranchTrueFalseTotal
unless $previous_token 0 14 14
67  
68 14 state $is_chmod_or_umask = { hashify( qw( chmod umask ) ) };
69 14 return $is_chmod_or_umask->{$previous_token->content()};
70 }
71  
72 sub _is_second_argument_of_mkdir {
73 14 ▶ my ($self, $elem) = @_;
Subroutine _is_second_argument_of_mkdir: called CC=5 SCAR=24.7
74  
75 # Preceding comma.
76 14 my $previous_token = _previous_token_that_isnt_a_parenthesis($elem);
77 14 ▶ return if not $previous_token;
Branch: 1 branch
BranchTrueFalseTotal
unless $previous_token 0 14 14
78 14 ▶ return if $previous_token->content() ne $COMMA; # Don't know what it is.
Branch: 1 branch
BranchTrueFalseTotal
if $previous_token->content ne $Perl::Critic::Policy::ValuesAndExpressions::ProhibitLeadingZeros::COMMA 14 0 14
79  
80 # Directory name.
81 0 $previous_token =
82 _previous_token_that_isnt_a_parenthesis($previous_token);
83 0 ▶ return if not $previous_token;
Branch: 1 branch
BranchTrueFalseTotal
unless $previous_token 0 0 0
84  
85 0 $previous_token =
86 _previous_token_that_isnt_a_parenthesis($previous_token);
87 0 ▶ return if not $previous_token;
Branch: 1 branch
BranchTrueFalseTotal
unless $previous_token 0 0 0
88  
89 0 return $previous_token->content() eq 'mkdir';
90 }
91  
92 sub _is_second_argument_of_mkfifo {
93 14 ▶ my ($self, $elem) = @_;
Subroutine _is_second_argument_of_mkfifo: called CC=5 SCAR=25.2
94  
95 # Preceding comma.
96 14 my $previous_token = _previous_token_that_isnt_a_parenthesis($elem);
97 14 ▶ return if not $previous_token;
Branch: 1 branch
BranchTrueFalseTotal
unless $previous_token 0 14 14
98 14 ▶ return if $previous_token->content() ne $COMMA; # Don't know what it is.
Branch: 1 branch
BranchTrueFalseTotal
if $previous_token->content ne $Perl::Critic::Policy::ValuesAndExpressions::ProhibitLeadingZeros::COMMA 14 0 14
99  
100 # FIFO name.
101 0 $previous_token =
102 _previous_token_that_isnt_a_parenthesis($previous_token);
103 0 ▶ return if not $previous_token;
Branch: 1 branch
BranchTrueFalseTotal
unless $previous_token 0 0 0
104  
105 0 $previous_token =
106 _previous_token_that_isnt_a_parenthesis($previous_token);
107 0 ▶ return if not $previous_token;
Branch: 1 branch
BranchTrueFalseTotal
unless $previous_token 0 0 0
108  
109 0 state $is_mkfifo = { hashify( 'mkfifo', 'POSIX::mkfifo' ) };
110 0 return $is_mkfifo->{$previous_token->content()};
111 }
112  
113 sub _is_third_argument_of_dbmopen {
114 14 ▶ my ($self, $elem) = @_;
Subroutine _is_third_argument_of_dbmopen: called CC=8 SCAR=36.6
115  
116 # Preceding comma.
117 14 my $previous_token = _previous_token_that_isnt_a_parenthesis($elem);
118 14 ▶ return if not $previous_token;
Branch: 1 branch
BranchTrueFalseTotal
unless $previous_token 0 14 14
119 14 ▶ return if $previous_token->content() ne $COMMA; # Don't know what it is.
Branch: 1 branch
BranchTrueFalseTotal
if $previous_token->content ne $Perl::Critic::Policy::ValuesAndExpressions::ProhibitLeadingZeros::COMMA 14 0 14
120  
121 # File path.
122 0 $previous_token =
123 _previous_token_that_isnt_a_parenthesis($previous_token);
124 0 ▶ return if not $previous_token;
Branch: 1 branch
BranchTrueFalseTotal
unless $previous_token 0 0 0
125  
126 # Another comma.
127 0 $previous_token =
128 _previous_token_that_isnt_a_parenthesis($previous_token);
129 0 ▶ return if not $previous_token;
Branch: 1 branch
BranchTrueFalseTotal
unless $previous_token 0 0 0
130 0 ▶ return if $previous_token->content() ne $COMMA; # Don't know what it is.
Branch: 1 branch
BranchTrueFalseTotal
if $previous_token->content ne $Perl::Critic::Policy::ValuesAndExpressions::ProhibitLeadingZeros::COMMA 0 0 0
131  
132 # Variable name.
133 0 $previous_token =
134 _previous_token_that_isnt_a_parenthesis($previous_token);
135 0 ▶ return if not $previous_token;
Branch: 1 branch
BranchTrueFalseTotal
unless $previous_token 0 0 0
136  
137 0 $previous_token =
138 _previous_token_that_isnt_a_parenthesis($previous_token);
139 0 ▶ return if not $previous_token;
Branch: 1 branch
BranchTrueFalseTotal
unless $previous_token 0 0 0
140  
141 0 return $previous_token->content() eq 'dbmopen';
142 }
143  
144 sub _is_fourth_argument_of_sysopen {
145 14 ▶ my ($self, $elem) = @_;
Subroutine _is_fourth_argument_of_sysopen: called CC=14 SCAR=49.9
146  
147 # Preceding comma.
148 14 my $previous_token = _previous_token_that_isnt_a_parenthesis($elem);
149 14 ▶ return if not $previous_token;
Branch: 1 branch
BranchTrueFalseTotal
unless $previous_token 0 14 14
150 14 ▶ return if $previous_token->content() ne $COMMA; # Don't know what it is.
Branch: 1 branch
BranchTrueFalseTotal
if $previous_token->content ne $Perl::Critic::Policy::ValuesAndExpressions::ProhibitLeadingZeros::COMMA 14 0 14
151  
152 # Mode.
153 0 $previous_token =
154 _previous_token_that_isnt_a_parenthesis($previous_token);
155 0 ▶ while ($previous_token and $previous_token->content() ne $COMMA) {
Branch: 1 branch
BranchTrueFalseTotal
if ($previous_token and $previous_token->content ne $Perl::Critic::Policy::ValuesAndExpressions::ProhibitLeadingZeros::COMMA) 0 0 0
Condition0%0/3
$previous_token and $previous_token->content ne $Perl::Critic::Policy::ValuesAndExpressions::ProhibitLeadingZeros::COMMA
!ll&&!rl&&r
000
MC/DC0%0/2
$previous_token and $previous_token->content ne $Perl::Critic::Policy::ValuesAndExpressions::ProhibitLeadingZeros::COMMA
$previous_token $previous_token->content ne $Perl::Critic::Policy::ValuesAndExpressions::ProhibitLeadingZeros::COMMA
Truth table0%0/3
A and B
A= $previous_tokenB= $previous_token->content ne $Perl::Critic::Policy::ValuesAndExpressions::ProhibitLeadingZeros::COMMA
ABresult
0X0
100
111
156 0 $previous_token =
157 _previous_token_that_isnt_a_parenthesis($previous_token);
158 }
159 0 ▶ return if not $previous_token;
Branch: 1 branch
BranchTrueFalseTotal
unless $previous_token 0 0 0
160 0 ▶ return if $previous_token->content() ne $COMMA; # Don't know what it is.
Branch: 1 branch
BranchTrueFalseTotal
if $previous_token->content ne $Perl::Critic::Policy::ValuesAndExpressions::ProhibitLeadingZeros::COMMA 0 0 0
161  
162 # File name.
163 0 $previous_token =
164 _previous_token_that_isnt_a_parenthesis($previous_token);
165 0 ▶ return if not $previous_token;
Branch: 1 branch
BranchTrueFalseTotal
unless $previous_token 0 0 0
166  
167 # Yet another comma.
168 0 $previous_token =
169 _previous_token_that_isnt_a_parenthesis($previous_token);
170 0 ▶ return if not $previous_token;
Branch: 1 branch
BranchTrueFalseTotal
unless $previous_token 0 0 0
171 0 ▶ return if $previous_token->content() ne $COMMA; # Don't know what it is.
Branch: 1 branch
BranchTrueFalseTotal
if $previous_token->content ne $Perl::Critic::Policy::ValuesAndExpressions::ProhibitLeadingZeros::COMMA 0 0 0
172  
173 # File handle.
174 0 $previous_token =
175 _previous_token_that_isnt_a_parenthesis($previous_token);
176 0 ▶ return if not $previous_token;
Branch: 1 branch
BranchTrueFalseTotal
unless $previous_token 0 0 0
177  
178 0 $previous_token =
179 _previous_token_that_isnt_a_parenthesis($previous_token);
180 0 ▶ return if not $previous_token;
Branch: 1 branch
BranchTrueFalseTotal
unless $previous_token 0 0 0
181  
182 # GitHub #789
183 0 ▶ if ( $previous_token->content() eq 'my' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($previous_token->content eq "my") 0 0 0
184 0 $previous_token = _previous_token_that_isnt_a_parenthesis(
185 $previous_token );
186 0 ▶ return if not $previous_token;
Branch: 1 branch
BranchTrueFalseTotal
unless $previous_token 0 0 0
187 }
188  
189 0 return $previous_token->content() eq 'sysopen';
190 }
191  
192 sub _previous_token_that_isnt_a_parenthesis {
193 70 ▶ my ($elem) = @_;
Subroutine _previous_token_that_isnt_a_parenthesis: called CC=4 SCAR=14.2
194  
195 70 state $is_paren = { hashify( $LEFT_PAREN, $RIGHT_PAREN ) };
196  
197 70 my $previous_token = $elem->previous_token();
198 70 ▶ while (
Branch: 1 branch
BranchTrueFalseTotal
if ($previous_token and !$previous_token->significant || $$is_paren{$previous_token->content}) 70 70 140
Condition50%3/6
$previous_token and !$previous_token->significant || $$is_paren{$previous_token->content}
!ll&&!rl&&r
01400
not $previous_token->significant or $$is_paren{$previous_token->content}
l!l&&r!l&&!r
70070
MC/DC0%0/3
$previous_token and !$previous_token->significant || $$is_paren{$previous_token->content}
$previous_token not $previous_token->significant $$is_paren{$previous_token->content}
Truth table25%1/4
A and not(B) or C
A= $previous_tokenB= not $previous_token->significantC= $$is_paren{$previous_token->content}
ABCresult
0XX0
1000
11X1
1011
199 $previous_token
200 and (
201 not $previous_token->significant()
202 or $is_paren->{$previous_token->content()}
203 )
204 ) {
205 70 $previous_token = $previous_token->previous_token();
206 }
207  
208 70 return $previous_token;
209 }
210  
211 40 1;
212  
213 __END__
214  
215 #-----------------------------------------------------------------------------
216  
217 =pod
218  
219 =head1 NAME
220  
221 Perl::Critic::Policy::ValuesAndExpressions::ProhibitLeadingZeros - Write C<oct(755)> instead of C<0755>.
222  
223  
224 =head1 AFFILIATION
225  
226 This Policy is part of the core L<Perl::Critic|Perl::Critic>
227 distribution.
228  
229  
230 =head1 DESCRIPTION
231  
232 Perl interprets numbers with leading zeros as octal. If that's what
233 you really want, its better to use C<oct> and make it obvious.
234  
235 $var = 041; # not ok, actually 33
236 $var = oct(41); # ok
237  
238 chmod 0644, $file; # ok by default
239 dbmopen %database, 'foo.db', 0600; # ok by default
240 mkdir $directory, 0755; # ok by default
241 sysopen $filehandle, $filename, O_RDWR, 0666; # ok by default
242 umask 0002; # ok by default
243  
244 use POSIX 'mkfifo';
245 mkfifo $fifo, 0600; # ok by default
246 POSIX::mkfifo $fifo, 0600; # ok by default
247  
248 =head1 CONFIGURATION
249  
250 If you want to ban all leading zeros, set C<strict> to a true value in
251 a F<.perlcriticrc> file.
252  
253 [ValuesAndExpressions::ProhibitLeadingZeros]
254 strict = 1
255  
256  
257 =head1 AUTHOR
258  
259 Jeffrey Ryan Thalhammer <jeff@imaginative-software.com>
260  
261 =head1 COPYRIGHT
262  
263 Copyright (c) 2005-2023 Imaginative Software Systems. All rights reserved.
264  
265 This program is free software; you can redistribute it and/or modify
266 it under the same terms as Perl itself. The full text of this license
267 can be found in the LICENSE file included with this module.
268  
269 =cut
270  
271 # Local Variables:
272 # mode: cperl
273 # cperl-indent-level: 4
274 # fill-column: 78
275 # indent-tabs-mode: nil
276 # c-indentation-style: bsd
277 # End:
278 # ex: set ts=8 sts=4 sw=4 tw=78 ft=perl expandtab shiftround :
« blib/lib/Perl/Critic/Policy/BuiltinFunctions/ProhibitBooleanGrep.pm ↑ Summary blib/lib/Perl/Critic/Policy/RegularExpressions/ProhibitEnumeratedClasses.pm »