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 3     3   1170644 use strict;
  3         20  
  3         74  
4 3     3   15 use warnings;
  3         6  
  3         57  
5 3     3   42 use 5.008001;
  3         10  
6 3     3   14 use Perl::Critic::Utils;
  3         7  
  3         50  
7 3     3   2228 use base qw( Perl::Critic::Policy );
  3         12  
  3         1308  
8              
9             # ABSTRACT: Leading zeroes are okay as the first arg to chmod, and other such reasonableness
10             our $VERSION = '0.03'; # 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 17 sub default_severity { $SEVERITY_MEDIUM }
17 0     0 1 0 sub default_themes { () }
18 12     12 1 99208 sub applies_to { 'PPI::Token::Number' }
19              
20             sub violates
21             {
22 16     16 1 250 my($self, $element, undef) = @_;
23              
24 16 100       42 return unless $element =~ m/\A [+-]? (?: 0+ _* )+ [1-9]/mx;
25 8 100       78 return if $element->sprevious_sibling eq 'chmod';
26 7 100 100     259 return if (eval { $element->sprevious_sibling->sprevious_sibling->sprevious_sibling->sprevious_sibling->sprevious_sibling }||'') eq 'mkpath';
27              
28 6         311 my $working = eval { $element->parent->parent };
  6         31  
29 6 100       47 if($element->parent->isa('PPI::Statement::Expression'))
30             {
31 5         28 my $working = $element->parent->parent;
32 5         23 while(eval { $working->isa('PPI::Structure::List') })
  10         40  
33             {
34 5         37 $working = $working->parent;
35             }
36 5 100 66     30 return if $working and ($working->children)[0] eq 'chmod';
37 3 50 33     58 return if $working and ($working->children)[-3] eq 'mkpath';
38             }
39              
40 1         14 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.03
58              
59             =head1 DESCRIPTION
60              
61             This is a stupid mistake:
62              
63             my $x = 1231;
64             my $y = 2345;
65             my $z = 0032;
66              
67             This is not:
68              
69             chmod 0700, "secret_file.txt";
70              
71             Neither is this:
72              
73             use File::Path qw( mkpath );
74            
75             mkpath("/foo/bar/baz", 1, 0700);
76              
77             Nor is this:
78              
79             use Path::Class qw( dir );
80            
81             dir()->mkpath(1,0700);
82              
83             =head1 CAVEATS
84              
85             Because C<mkpath> is not a built in (as C<chmod> is), this policy does not differentiate between the
86             C<mkpath> function provided by L<File::Path> or the C<mkpath> method provided by L<Path::Class::Dir>
87             and arbitrary C<mkpath> function or methods that you or someone else might define. Also, there is
88             no way to really check if the object invocant of a C<mkpath> method is really an instance of
89             L<Path::Class::Dir>.
90              
91             =head1 SEE ALSO
92              
93             This policy is based largely on the existing in-core policy, and one in the lax bundle, but adds a
94             few exceptions that I find useful.
95              
96             =over 4
97              
98             =item L<Perl::Critic::Policy::ValuesAndExpressions::ProhibitLeadingZeros>
99              
100             =item L<Perl::Critic::Policy::Lax::ProhibitLeadingZeros::ExceptChmod>
101              
102             =back
103              
104             =head1 AUTHOR
105              
106             Graham Ollis <plicease@cpan.org>
107              
108             =head1 COPYRIGHT AND LICENSE
109              
110             This software is copyright (c) 2019 by Graham Ollis.
111              
112             This is free software; you can redistribute it and/or modify it under
113             the same terms as the Perl 5 programming language system itself.
114              
115             =cut