File Coverage

blib/lib/Perl/Critic/Policy/Plicease/ProhibitLeadingZeros.pm
Criterion Covered Total %
statement 30 31 96.7
branch 11 12 91.6
condition 5 8 62.5
subroutine 8 9 88.8
pod 4 4 100.0
total 58 64 90.6


line stmt bran cond sub pod time code
1             package Perl::Critic::Policy::Plicease::ProhibitLeadingZeros;
2              
3 5     5   4740 use strict;
  5         17  
  5         222  
4 5     5   31 use warnings;
  5         12  
  5         633  
5 5     5   94 use 5.010001;
  5         20  
6 5     5   27 use Perl::Critic::Utils;
  5         8  
  5         151  
7 5     5   4671 use base qw( Perl::Critic::Policy );
  5         11  
  5         2549  
8              
9             # ABSTRACT: Leading zeroes are okay as the first arg to chmod, and other such reasonableness
10             our $VERSION = '0.10'; # VERSION
11              
12              
13             my $DESCRIPTION = q{Integer with leading zeros outside of chmod, mkpath};
14             my $EXPLANATION = "Only use leading zeros on numbers indicating file modes";
15              
16 1     1 1 19 sub default_severity { $SEVERITY_MEDIUM }
17 0     0 1 0 sub default_themes { () }
18 12     12 1 2170082 sub applies_to { 'PPI::Token::Number' }
19              
20             sub violates
21             {
22 16     16 1 376 my($self, $element, undef) = @_;
23              
24 16 100       65 return unless $element =~ m/\A [+-]? (?: 0+ _* )+ [1-9]/mx;
25 8 100       119 return if $element->sprevious_sibling eq 'chmod';
26 7 100 100     377 return if (eval { $element->sprevious_sibling->sprevious_sibling->sprevious_sibling->sprevious_sibling->sprevious_sibling }||'') eq 'mkpath';
27              
28 6         369 my $working = eval { $element->parent->parent };
  6         28  
29 6 100       71 if($element->parent->isa('PPI::Statement::Expression'))
30             {
31 5         32 my $working = $element->parent->parent;
32 5         24 while(eval { $working->isa('PPI::Structure::List') })
  10         57  
33             {
34 5         67 $working = $working->parent;
35             }
36 5 100 66     65 return if $working and ($working->children)[0] eq 'chmod';
37 3 50 33     61 return if $working and ($working->children)[-3] eq 'mkpath';
38             }
39              
40 1         19 return $self->violation($DESCRIPTION, $EXPLANATION, $element);
41             }
42              
43             1;
44              
45             __END__
46              
47             =pod
48              
49             =encoding UTF-8
50              
51             =head1 NAME
52              
53             Perl::Critic::Policy::Plicease::ProhibitLeadingZeros - Leading zeroes are okay as the first arg to chmod, and other such reasonableness
54              
55             =head1 VERSION
56              
57             version 0.10
58              
59             =head1 SYNOPSIS
60              
61             perlcriticrc:
62              
63             [Plicease::ProhibitLeadingZeros]
64              
65             code:
66              
67             0123; # not ok
68             1234; # ok
69             chmod 0700; # ok
70             mkpath($foo, 0700); # ok
71              
72             =head1 DESCRIPTION
73              
74             Perl interprets numbers with leading zeros as octal. If that's what you really want, its
75             better to use C<oct> and make it obvious. However, some operations are well known as using
76             octal such as C<chmod> and C<mkpath> so this policy disallows mistakes like this:
77              
78             my $x = 1231;
79             my $y = 2345;
80             my $z = 0032;
81              
82             But not non-mistakes like this:
83              
84             chmod 0700, "secret_file.txt";
85              
86             or this:
87              
88             use File::Path qw( mkpath );
89            
90             mkpath("/foo/bar/baz", 1, 0700);
91              
92             or is this:
93              
94             use Path::Class qw( dir );
95            
96             dir()->mkpath(1,0700);
97              
98             =head1 AFFILIATION
99              
100             None.
101              
102             =head1 CONFIGURATION
103              
104             This policy is not configurable except for the standard options.
105              
106             =head1 CAVEATS
107              
108             Because C<mkpath> is not a built in (as C<chmod> is), this policy does not differentiate between the
109             C<mkpath> function provided by L<File::Path> or the C<mkpath> method provided by L<Path::Class::Dir>
110             and arbitrary C<mkpath> function or methods that you or someone else might define. Also, there is
111             no way to really check if the object invocant of a C<mkpath> method is really an instance of
112             L<Path::Class::Dir>.
113              
114             =head1 SEE ALSO
115              
116             This policy is based largely on the existing in-core policy, and one in the lax bundle, but adds a
117             few exceptions that I find useful.
118              
119             =over 4
120              
121             =item L<Perl::Critic::Policy::ValuesAndExpressions::ProhibitLeadingZeros>
122              
123             =item L<Perl::Critic::Policy::Lax::ProhibitLeadingZeros::ExceptChmod>
124              
125             =back
126              
127             =head1 AUTHOR
128              
129             Author: Graham Ollis E<lt>plicease@cpan.orgE<gt>
130              
131             Contributors:
132              
133             Ville Skyttä (SCOP)
134              
135             Yoshikazu Sawa (yoshikazusawa)
136              
137             Christian Walde (wchristian, MITHALDU)
138              
139             =head1 COPYRIGHT AND LICENSE
140              
141             This software is copyright (c) 2019-2024 by Graham Ollis.
142              
143             This is free software; you can redistribute it and/or modify it under
144             the same terms as the Perl 5 programming language system itself.
145              
146             =cut