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

filter: Statementstmt 61.7% 63 / 102 Branchbran 25.0% 18 / 72 Conditioncond 44.4% 4 / 9 MC/DCmcdc 20.0% 1 / 5 Subroutinesub 100% 18 / 18 Podpod 80.0% 4 / 5 totaltotal 51.1% 108 / 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
« lib/Perl/Critic/Policy/BuiltinFunctions/ProhibitBooleanGrep.pm ↑ Summary 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.156';
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 91 ▶sub supported_parameters {
Subroutine supported_parameters: called CC=1 SCAR=0.0
Pod undocumented
22 return (
23 {
24 91 name => 'strict',
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 88 ▶sub default_severity { return $SEVERITY_HIGHEST }
Subroutine default_severity: called CC=1 SCAR=0.0
Pod documented
34 91 ▶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 14 ▶sub violates {
Subroutine violates: called CC=8 SCAR=22.8
Pod documented
40 14 my ( $self, $elem, undef ) = @_;
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 _is_first_argument_of_chmod_or_umask($elem);
Branch: 1 branch
BranchTrueFalseTotal
if _is_first_argument_of_chmod_or_umask($elem) 0 14 14
45 14 ▶ return if _is_second_argument_of_mkdir($elem);
Branch: 1 branch
BranchTrueFalseTotal
if _is_second_argument_of_mkdir($elem) 0 14 14
46 14 ▶ return if _is_second_argument_of_mkfifo($elem);
Branch: 1 branch
BranchTrueFalseTotal
if _is_second_argument_of_mkfifo($elem) 0 14 14
47 14 ▶ return if _is_third_argument_of_dbmopen($elem);
Branch: 1 branch
BranchTrueFalseTotal
if _is_third_argument_of_dbmopen($elem) 0 14 14
48 14 ▶ return if _is_fourth_argument_of_sysopen($elem);
Branch: 1 branch
BranchTrueFalseTotal
if _is_fourth_argument_of_sysopen($elem) 0 14 14
49 14 return $self->_create_violation($elem);
50 }
51  
52 14 ▶sub _create_violation {
Subroutine _create_violation: called CC=1 SCAR=0.0
53 14 my ($self, $elem) = @_;
54  
55 14 return $self->violation(
56 qq<Integer with leading zeros: "$elem">,
57 $EXPL,
58 $elem
59 );
60 }
61  
62 14 ▶sub _is_first_argument_of_chmod_or_umask {
Subroutine _is_first_argument_of_chmod_or_umask: called CC=2 SCAR=7.0
63 14 my ($elem) = @_;
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 14 ▶sub _is_second_argument_of_mkdir {
Subroutine _is_second_argument_of_mkdir: called CC=5 SCAR=24.7
73 14 my ($elem) = @_;
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 14 ▶sub _is_second_argument_of_mkfifo {
Subroutine _is_second_argument_of_mkfifo: called CC=5 SCAR=25.2
93 14 my ($elem) = @_;
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 14 ▶sub _is_third_argument_of_dbmopen {
Subroutine _is_third_argument_of_dbmopen: called CC=8 SCAR=36.6
114 14 my ($elem) = @_;
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 14 ▶sub _is_fourth_argument_of_sysopen {
Subroutine _is_fourth_argument_of_sysopen: called CC=14 SCAR=49.9
145 14 my ($elem) = @_;
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 70 ▶sub _previous_token_that_isnt_a_parenthesis {
Subroutine _previous_token_that_isnt_a_parenthesis: called CC=4 SCAR=14.0
193 70 my ($elem) = @_;
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
Condition66%4/6
$previous_token and !$previous_token->significant || $$is_paren{$previous_token->content}
!ll&&!rl&&r
07070
not $previous_token->significant or $$is_paren{$previous_token->content}
l!l&&r!l&&!r
70070
MC/DC33%1/3
$previous_token and !$previous_token->significant || $$is_paren{$previous_token->content}
$previous_token not $previous_token->significant $$is_paren{$previous_token->content}
Truth table50%2/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__
« lib/Perl/Critic/Policy/BuiltinFunctions/ProhibitBooleanGrep.pm ↑ Summary lib/Perl/Critic/Policy/RegularExpressions/ProhibitEnumeratedClasses.pm »