Policy/Compatibility/ProhibitUnixDevNull.pm

filter: Statementstmt 100% 48 / 48 Branchbran 100% 8 / 8 Conditioncond 83.3% 5 / 6 MC/DCmcdc 66.6% 2 / 3 Subroutinesub 100% 14 / 14 Podpod 100% 1 / 1 totaltotal 97.5% 78 / 80 SCAR 20.8
CC
8
Cov
98%
violates
8.0
BEGIN
1.0
BEGIN
1.0
CRAP 8.0
« Policy/Documentation/RequireEndBeforeLastPod.pm ↑ Summary Policy/Documentation/ProhibitDuplicateSeeAlso.pm »
1 # Copyright 2009, 2010, 2011, 2012, 2013, 2014, 2015, 2016, 2017, 2019, 2021 Kevin Ryde
2  
3 # Perl-Critic-Pulp is free software; you can redistribute it and/or modify
4 # it under the terms of the GNU General Public License as published by the
5 # Free Software Foundation; either version 3, or (at your option) any later
6 # version.
7 #
8 # Perl-Critic-Pulp is distributed in the hope that it will be useful, but
9 # WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
10 # or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
11 # for more details.
12 #
13 # You should have received a copy of the GNU General Public License along
14 # with Perl-Critic-Pulp. If not, see <http://www.gnu.org/licenses/>.
15  
16  
17 package Perl::Critic::Policy::Compatibility::ProhibitUnixDevNull;
18 41 ▶use 5.006;
Subroutine BEGIN: called CC=1 SCAR=0.0
19 41 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
20 41 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
21 41 ▶use List::Util;
Subroutine BEGIN: called CC=1 SCAR=0.0
22 41 ▶use base 'Perl::Critic::Policy';
Subroutine BEGIN: called CC=1 SCAR=0.0
23 41 ▶use Perl::Critic::Utils;
Subroutine BEGIN: called CC=1 SCAR=0.0
24 41 ▶use Perl::Critic::Pulp;
Subroutine BEGIN: called CC=1 SCAR=0.0
25  
26 41 our $VERSION = 99;
27  
28 41 ▶use constant supported_parameters => ();
Subroutine BEGIN: called CC=1 SCAR=0.0
29 41 ▶use constant default_severity => $Perl::Critic::Utils::SEVERITY_LOW;
Subroutine BEGIN: called CC=1 SCAR=0.0
30 41 ▶use constant default_themes => qw(pulp bugs);
Subroutine BEGIN: called CC=1 SCAR=0.0
31 41 use constant applies_to => qw(PPI::Token::Quote
32 41 ▶ PPI::Token::QuoteLike::Words);
Subroutine BEGIN: called CC=1 SCAR=0.0
33  
34 # See Perl_do_openn() for IsSPACE allowed leading, after mode and trailing.
35 # No layers in a two-arg open, only < > >> etc.
36 #
37 41 use constant _DEV_NULL_RE => qr{^\s*
38 (\+?(<|>>?)\s*)?
39 /dev/null
40 \s*$
41 41 ▶ }sxo;
Subroutine BEGIN: called CC=1 SCAR=0.0
42  
43 41 my %equality_operators = (eq => 1, ne => 1);
44  
45 sub violates {
46 10 ▶ my ($self, $elem, $document) = @_;
Subroutine violates: called CC=8 SCAR=20.8
Pod documented
47  
48 10 ▶ if ($elem->isa('PPI::Token::QuoteLike::Words')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($elem->isa('PPI::Token::QuoteLike::Words')) { } 2 8 10
49 2 ▶ return unless List::Util::first {$_ eq '/dev/null'} $elem->literal;
Subroutine __ANON__: called CC=1 SCAR=0.0
Branch: 1 branch
BranchTrueFalseTotal
unless &List::Util::first(sub { $_ eq "/dev/null"; } , $elem->literal) 1 1 2
50  
51 } else { # PPI::Token::Quote
52 8 my $str = $elem->string;
53 8 ▶ return unless $str =~ _DEV_NULL_RE;
Branch: 1 branch
BranchTrueFalseTotal
unless $str =~ m[^\s* (\+?(<|>>?)\s*)? /dev/null \s*$ ]sx 2 6 8
54  
55 # Allow ... eq 'dev/null' or 'dev/null' eq ...
56 #
57 # Could think about the filetest operators too. -e '/dev/null' is
58 # probably a portability check, but believe still better to have
59 # File::Spec->devnull there.
60 #
61 6 foreach my $adj ($elem->sprevious_sibling, $elem->snext_sibling) {
62 11 ▶ if ($adj
Branch: 1 branch
BranchTrueFalseTotal
if ($adj and $adj->isa("PPI::Token::Operator") and $equality_operators{$adj}) 2 9 11
Condition83%5/6
$adj and $adj->isa("PPI::Token::Operator") and $equality_operators{$adj}
!ll&&!rl&&r
542
$adj and $adj->isa("PPI::Token::Operator")
!ll&&!rl&&r
506
MC/DC66%2/3
$adj and $adj->isa("PPI::Token::Operator") and $equality_operators{$adj}
$adj $adj->isa("PPI::Token::Operator") $equality_operators{$adj}
Truth table75%3/4
A and B and C
A= $adjB= $adj->isa("PPI::Token::Operator")C= $equality_operators{$adj}
ABCresult
0XX0
10X0
1100
1111
63 && $adj->isa('PPI::Token::Operator')
64 && $equality_operators{$adj}) {
65 2 return;
66 }
67 }
68 }
69  
70 5 return $self->violation
71 ('For maximum portability use File::Spec->devnull instead of "/dev/null"',
72 '',
73 $elem);
74 }
75  
76 41 1;
77 __END__
78  
79 =for stopwords filename backticks Ryde
80  
81 =head1 NAME
82  
83 Perl::Critic::Policy::Compatibility::ProhibitUnixDevNull - don't use explicit /dev/null
84  
85 =head1 DESCRIPTION
86  
87 This policy is part of the L<C<Perl::Critic::Pulp>|Perl::Critic::Pulp>
88 add-on. It ask you to not to use filename
89  
90 =over
91  
92 F</dev/null>
93  
94 =back
95  
96 explicitly, but instead C<File::Spec-E<gt>devnull()> for maximum portability
97 across operating systems.
98  
99 This policy is under the C<maintenance> theme (see
100 L<Perl::Critic/POLICY THEMES>) on the basis that even if you're on a Unix
101 system now you never know where your code might travel in the future.
102  
103 C<devnull()> is new in C<File::Spec> version 0.8, so you should require that
104 version (it's included in Perl 5.6.0 and up).
105  
106 The checks for F</dev/null> are unsophisticated. A violation is reported
107 for any string C</dev/null>, possibly with an C<open> style mode part, and
108 any C<qw> containing C</dev/null>.
109  
110 open my $fh, '< /dev/null'; # bad
111 do_something ("/dev/null"); # bad
112 foreach my $file (qw(/dev/null /etc/passwd)) # bad
113  
114 String comparisons are allowed because they're not uses of F</dev/null> as
115 such but likely some sort of cross-platform check.
116  
117 if ($f eq '/dev/null') { ... } # ok
118 return ($f ne '>/dev/null'); # ok
119  
120 F</dev/null> as just part of a string is allowed, including things like
121 backticks and C<system>.
122  
123 print "Flames to /dev/null please\n" # ok
124 system ('rmdir /foo/bar >/dev/null 2>&1'); # ok
125 $hi = `echo hi </dev/null`; # ok
126  
127 Whether F</dev/null> is a good idea in such command strings depends what
128 sort of shell you reach with that command and how much of Unix it might
129 emulate on a non-Unix system.
130  
131 =head2 Disabling
132  
133 If you only ever use a system with F</dev/null> or if everything else you
134 write is hopelessly wedded to Unix anyway then you can disable
135 C<ProhibitUnixDevNull> from your F<.perlcriticrc> in the usual way (see
136 L<Perl::Critic/CONFIGURATION>),
137  
138 [-Compatibility::ProhibitUnixDevNull]
139  
140 =head1 SEE ALSO
141  
142 L<Perl::Critic::Pulp>, L<Perl::Critic>, L<File::Spec>
143  
144 =head1 HOME PAGE
145  
146 L<http://user42.tuxfamily.org/perl-critic-pulp/index.html>
147  
148 =head1 COPYRIGHT
149  
150 Copyright 2009, 2010, 2011, 2012, 2013, 2014, 2015, 2016, 2017, 2019, 2021 Kevin Ryde
151  
152 Perl-Critic-Pulp is free software; you can redistribute it and/or modify it
153 under the terms of the GNU General Public License as published by the Free
154 Software Foundation; either version 3, or (at your option) any later
155 version.
156  
157 Perl-Critic-Pulp is distributed in the hope that it will be useful, but
158 WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
159 or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
160 more details.
161  
162 You should have received a copy of the GNU General Public License along with
163 Perl-Critic-Pulp. If not, see <http://www.gnu.org/licenses/>.
164  
165 =cut
« Policy/Documentation/RequireEndBeforeLastPod.pm ↑ Summary Policy/Documentation/ProhibitDuplicateSeeAlso.pm »