Policy/Modules/ProhibitPOSIXimport.pm

filter: Statementstmt 100% 125 / 125 Branchbran 80.7% 42 / 52 Conditioncond 77.4% 24 / 31 MC/DCmcdc 62.5% 10 / 16 Subroutinesub 100% 27 / 27 Podpod 100% 2 / 2 totaltotal 90.9% 230 / 253 SCAR 36.8
CC
39
Cov
92%
_parse_args
9.2
_inc_exporter_imports_type
8.0
violates
6.1
CRAP 39.8
« Policy/ValuesAndExpressions/RequireNumericVersion.pm ↑ Summary Policy/CodeLayout/RequireTrailingCommaAtNewline.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::Modules::ProhibitPOSIXimport;
18 40 ▶use 5.006;
Subroutine BEGIN: called CC=1 SCAR=0.0
19 40 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
20 40 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
21 40 ▶use List::MoreUtils;
Subroutine BEGIN: called CC=1 SCAR=0.0
22 40 ▶use POSIX ('abort'); # must import something to initialize @POSIX::EXPORT
Subroutine BEGIN: called CC=1 SCAR=0.0
23 40 ▶use Scalar::Util;
Subroutine BEGIN: called CC=1 SCAR=0.0
24  
25 40 ▶use base 'Perl::Critic::Policy';
Subroutine BEGIN: called CC=1 SCAR=0.0
26 40 use Perl::Critic::Utils qw(is_function_call
27 40 ▶ split_nodes_on_comma);
Subroutine BEGIN: called CC=1 SCAR=0.0
28 40 ▶use Perl::Critic::Utils::PPI qw(is_ppi_expression_or_generic_statement);
Subroutine BEGIN: called CC=1 SCAR=0.0
29 40 ▶use Perl::Critic::Pulp::Utils;
Subroutine BEGIN: called CC=1 SCAR=0.0
30  
31 # uncomment this to run the ### lines
32 # use Smart::Comments;
33  
34 40 our $VERSION = 99;
35  
36 40 ▶use constant _ALLOWED_CALL_COUNT => 15;
Subroutine BEGIN: called CC=1 SCAR=0.0
37  
38 40 ▶use constant supported_parameters => ();
Subroutine BEGIN: called CC=1 SCAR=0.0
39 40 ▶use constant default_severity => $Perl::Critic::Utils::SEVERITY_LOW;
Subroutine BEGIN: called CC=1 SCAR=0.0
40 40 ▶use constant default_themes => qw(pulp efficiency);
Subroutine BEGIN: called CC=1 SCAR=0.0
41 40 ▶use constant applies_to => ('PPI::Statement::Include');
Subroutine BEGIN: called CC=1 SCAR=0.0
42  
43 40 my %posix_function;
44  
45 sub initialize_if_enabled {
46 1 ▶ my ($self, $config) = @_;
Subroutine initialize_if_enabled: called CC=1 SCAR=0.0
Pod documented
47 1 @posix_function{@POSIX::EXPORT} = (); # hash slice
48 ### POSIX EXPORT count: scalar(keys %posix_function)
49 1 return 1;
50 }
51  
52  
53 sub violates {
54 68 ▶ my ($self, $elem, $document) = @_;
Subroutine violates: called CC=6 SCAR=18.0
Pod documented
55  
56 68 ▶ return unless ($elem->module||'') eq 'POSIX'; # "use POSIX"
Branch: 1 branch
BranchTrueFalseTotal
unless ($elem->module || "") eq "POSIX" 0 68 68
Condition50%1/2
$elem->module || ''
l!l
680
MC/DC0%0/1
$elem->module || ''
$elem->module
Truth table50%1/2
A
A= $elem->module
Aresult
11
00
57 68 ▶ return unless (_inc_exporter_imports_type($elem) eq 'default');
Branch: 1 branch
BranchTrueFalseTotal
unless _inc_exporter_imports_type($elem) eq "default" 34 34 68
58 34 ▶ return if _elem_is_in_package_main($elem); # within main ok
Branch: 1 branch
BranchTrueFalseTotal
if _elem_is_in_package_main($elem) 4 30 34
59 30 ▶ return if _count_posix_calls($document) >= _ALLOWED_CALL_COUNT;
Branch: 1 branch
BranchTrueFalseTotal
if _count_posix_calls($document) >= 15 6 24 30
60  
61 24 return $self->violation
62 ("Don't import the whole of POSIX into a module",
63 '',
64 $elem);
65 }
66  
67 # $inc is a PPI::Statement::Include of a module using Exporter.pm.
68 # Return 'no_import' -- $inc doesn't call import() at all
69 # 'default' -- $inc gets Exporter's default imports
70 # 'explicit' -- $inc chooses certain imports explicitly
71 #
72 sub _inc_exporter_imports_type {
73 128 ▶ my ($inc) = @_;
Subroutine _inc_exporter_imports_type: called CC=8 SCAR=20.8
74  
75 128 ▶ $inc->type eq 'use'
Branch: 1 branch
BranchTrueFalseTotal
$inc->type eq "use" or return "no_import" 4 124 128
76 or return 'no_import'; # "require Foo" or "no Foo" don't import
77  
78 124 ▶ my $mfirst = Perl::Critic::Pulp::Utils::include_module_first_arg ($inc)
Condition100%2/2
Perl::Critic::Pulp::Utils::include_module_first_arg($inc) || (return 'default')
l!l
9628
MC/DC100%1/1
Perl::Critic::Pulp::Utils::include_module_first_arg($inc) || (return 'default')
Perl::Critic::Pulp::Utils::include_module_first_arg($inc)
Truth table100%2/2
A
A= Perl::Critic::Pulp::Utils::include_module_first_arg($inc)
Aresult
11
00
79 || return 'default'; # no args or only a version check
80  
81 96 my @elems = _elem_and_snext_siblings ($mfirst);
82 96 _chomp_trailing_semi (\@elems);
83 ### elems count: scalar(@elems)
84 ### elems: "@elems"
85 96 ▶ if (@elems == 1 && _elem_is_empty_list($elems[0])) {
Branch: 1 branch
BranchTrueFalseTotal
if (@elems == 1 and _elem_is_empty_list($elems[0])) 16 80 96
Condition100%3/3
@elems == 1 and _elem_is_empty_list($elems[0])
!ll&&!rl&&r
404016
MC/DC100%2/2
@elems == 1 and _elem_is_empty_list($elems[0])
@elems == 1 _elem_is_empty_list($elems[0])
Truth table100%3/3
A and B
A= @elems == 1B= _elem_is_empty_list($elems[0])
ABresult
0X0
100
111
86 16 return 'no_import'; # "use Foo ()" doesn't call import() at all
87 }
88  
89 80 my @args = _parse_args (@elems);
90 80 ▶ if (@args >= 1 && _arg_is_number($args[0])) {
Branch: 1 branch
BranchTrueFalseTotal
if (@args >= 1 and _arg_is_number($args[0])) 48 32 80
Condition100%3/3
@args >= 1 and _arg_is_number($args[0])
!ll&&!rl&&r
82448
MC/DC100%2/2
@args >= 1 and _arg_is_number($args[0])
@args >= 1 _arg_is_number($args[0])
Truth table100%3/3
A and B
A= @args >= 1B= _arg_is_number($args[0])
ABresult
0X0
100
111
91 48 shift @args; # use Foo '123',... Exporter skips version number
92 }
93 80 ▶ return (@args ? 'explicit' : 'default');
Branch: 1 branch
BranchTrueFalseTotal
@args ? : 52 28 80
94 }
95  
96 # return true if PPI $elem is within the "package main", either an explicit
97 # "package main" or main as the default when no "package" statement at all
98 #
99 sub _elem_is_in_package_main {
100 34 ▶ my ($elem) = @_;
Subroutine _elem_is_in_package_main: called CC=2 SCAR=6.9
101 34 ▶ my $package = Perl::Critic::Pulp::Utils::elem_package($elem)
Condition100%2/2
Perl::Critic::Pulp::Utils::elem_package($elem) || (return 1)
l!l
322
MC/DC100%1/1
Perl::Critic::Pulp::Utils::elem_package($elem) || (return 1)
Perl::Critic::Pulp::Utils::elem_package($elem)
Truth table100%2/2
A
A= Perl::Critic::Pulp::Utils::elem_package($elem)
Aresult
11
00
102 || return 1; # no package statement
103 ### within_package: "$package"
104 32 return ($package->namespace eq 'main'); # explicit "package main"
105 }
106  
107 sub _parse_args {
108 80 ▶ my @first = split_nodes_on_comma (@_);
Subroutine _parse_args: called CC=9 SCAR=22.2
109 ### first split: scalar(@first)
110 ### @first
111  
112 # if (DEBUG) {
113 # require PPI::Dumper;
114 # foreach my $aref (@first) {
115 # print " aref:\n";
116 # foreach my $elem (@$aref) {
117 # PPI::Dumper->new($elem)->print;
118 # }
119 # }
120 # }
121  
122 80 my @ret;
123 80 ▶ while (@first) {
Branch: 1 branch
BranchTrueFalseTotal
if (@first) 184 80 264
124 184 my $aref = shift @first;
125 184 ▶ next unless defined $aref;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $aref 0 184 184
126 184 ▶ if (@$aref == 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (@$aref == 1) 184 0 184
127 184 my $elem = $aref->[0];
128 184 ▶ if ($elem->isa('PPI::Structure::List')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($elem->isa("PPI::Structure::List")) 84 100 184
129 84 my @children = $elem->schildren;
130 84 ▶ if (@children == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (@children == 0) 36 48 84
131 36 next; # empty list elided
132 }
133 48 ▶ if (@children == 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (@children == 1) 48 0 48
134 48 $elem = $children[0];
135 48 ▶ if ($elem->isa('PPI::Statement')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($elem->isa("PPI::Statement")) 48 0 48
136 48 @children = $elem->schildren;
137 48 ▶ if (@children) {
Branch: 1 branch
BranchTrueFalseTotal
if (@children) 48 0 48
138 48 unshift @first, split_nodes_on_comma (@children);
139 48 next;
140 }
141 }
142 }
143 }
144 }
145 100 push @ret, $aref;
146 }
147  
148 ### final ret: scalar(@ret)
149 # if (DEBUG) {
150 # require PPI::Dumper;
151 # foreach my $aref (@ret) {
152 # print " aref:\n";
153 # foreach my $elem (@$aref) {
154 # PPI::Dumper->new($elem)->print;
155 # }
156 # }
157 # }
158  
159 80 return @ret;
160 }
161  
162 sub _chomp_trailing_semi {
163 96 ▶ my ($aref) = @_;
Subroutine _chomp_trailing_semi: called CC=4 SCAR=14.1
164 96 ▶ while (@$aref
Branch: 1 branch
BranchTrueFalseTotal
if (@$aref and $aref->[-1]->isa("PPI::Token::Structure") and $aref->[-1]->content eq ";") 48 96 144
Condition66%4/6
@$aref and $aref->[-1]->isa("PPI::Token::Structure") and $aref->[-1]->content eq ";"
!ll&&!rl&&r
96048
@$aref and $aref->[-1]->isa("PPI::Token::Structure")
!ll&&!rl&&r
09648
MC/DC33%1/3
@$aref and $aref->[-1]->isa("PPI::Token::Structure") and $aref->[-1]->content eq ";"
@$aref $aref->[-1]->isa("PPI::Token::Structure") $aref->[-1]->content eq ";"
Truth table50%2/4
A and B and C
A= @$arefB= $aref->[-1]->isa("PPI::Token::Structure")C= $aref->[-1]->content eq ";"
ABCresult
0XX0
10X0
1100
1111
165 && $aref->[-1]->isa('PPI::Token::Structure')
166 && $aref->[-1]->content eq ';') {
167 48 pop @$aref;
168 }
169 }
170  
171 sub _elem_and_snext_siblings {
172 96 ▶ my ($elem) = @_;
Subroutine _elem_and_snext_siblings: called CC=2 SCAR=6.9
173 96 my @ret = ($elem);
174 96 ▶ while ($elem = $elem->snext_sibling) {
Branch: 1 branch
BranchTrueFalseTotal
if ($elem = $elem->snext_sibling) 140 96 236
175 140 push @ret, $elem;
176 }
177 96 return @ret;
178 }
179  
180 sub _elem_is_empty_list {
181 56 ▶ my ($elem) = @_;
Subroutine _elem_is_empty_list: called CC=6 SCAR=18.0
182 56 for (;;) {
183 90 ▶ $elem->isa('PPI::Structure::List') || return 0;
Branch: 1 branch
BranchTrueFalseTotal
$elem->isa("PPI::Structure::List") or return 0 34 56 90
184 56 my @children = $elem->schildren;
185 56 ▶ if (@children == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (@children == 0) 16 40 56
186 16 return 1; # empty list
187 }
188 40 ▶ if (@children == 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (@children == 1) 40 0 40
189 40 $elem = $children[0];
190 40 ▶ if ($elem->isa('PPI::Statement')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($elem->isa("PPI::Statement")) 40 0 40
191 40 @children = $elem->schildren;
192 40 ▶ if (@children == 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (@children == 1) 34 6 40
193 34 $elem = $children[0];
194 34 next;
195 }
196 }
197 }
198 6 return 0;
199 }
200 }
201  
202 # $aref is an arrayref of PPI elements which are function arguments.
203 # Return true if the argument is a number, including a numeric string.
204 #
205 # ENHANCE-ME: Do some folding of constant concats or numeric calculations.
206 #
207 sub _arg_is_number {
208 72 ▶ my ($aref) = @_;
Subroutine _arg_is_number: called CC=5 SCAR=17.0
209  
210 72 ▶ @$aref == 1 || return 0; # only single elements for now
Branch: 1 branch
BranchTrueFalseTotal
@$aref == 1 or return 0 0 72 72
211 72 my $arg = $aref->[0];
212 72 ▶ return ($arg->isa('PPI::Token::Number')
Condition66%6/9
$arg->isa('PPI::Token::Number') || ($arg->isa('PPI::Token::Quote::Single') || $arg->isa('PPI::Token::Quote::Literal')) && &Scalar::Util::looks_like_number(scalar $arg->literal)
l!l&&r!l&&!r
282024
$arg->isa("PPI::Token::Quote::Single") || $arg->isa("PPI::Token::Quote::Literal") and &Scalar::Util::looks_like_number(scalar $arg->literal)
!ll&&!rl&&r
02420
$arg->isa("PPI::Token::Quote::Single") or $arg->isa("PPI::Token::Quote::Literal")
l!l&&r!l&&!r
4400
MC/DC50%2/4
$arg->isa('PPI::Token::Number') || ($arg->isa('PPI::Token::Quote::Single') || $arg->isa('PPI::Token::Quote::Literal')) && &Scalar::Util::looks_like_number(scalar $arg->literal)
$arg->isa('PPI::Token::Number') $arg->isa("PPI::Token::Quote::Single") $arg->isa("PPI::Token::Quote::Literal") &Scalar::Util::looks_like_number(scalar $arg->literal)
Truth table50%3/6
A || B or C and D
A= $arg->isa('PPI::Token::Number')B= $arg->isa("PPI::Token::Quote::Single")C= $arg->isa("PPI::Token::Quote::Literal")D= &Scalar::Util::looks_like_number(scalar $arg->literal)
ABCDresult
1XXX1
01X11
00111
000X0
01X00
00100
213  
214 || (($arg->isa('PPI::Token::Quote::Single')
215 || $arg->isa('PPI::Token::Quote::Literal'))
216 && Scalar::Util::looks_like_number ($arg->literal)));
217 }
218  
219  
220 # return a count of calls to POSIX module functions within $document
221 sub _count_posix_calls {
222 30 ▶ my ($document) = @_;
Subroutine _count_posix_calls: called CC=3 SCAR=11.0
223  
224 # function calls like "dup()", with is_function_call() used to exclude
225 # method calls like $x->dup on unrelated objects or classes
226 30 ▶ my $aref = $document->find ('PPI::Token::Word') || [];
Condition50%1/2
$document->find('PPI::Token::Word') || []
l!l
300
MC/DC0%0/1
$document->find('PPI::Token::Word') || []
$document->find('PPI::Token::Word')
Truth table50%1/2
A
A= $document->find('PPI::Token::Word')
Aresult
11
00
227 my $count = List::MoreUtils::true
228 208 ▶ { exists $posix_function{$_->content} && is_function_call($_)
Subroutine __ANON__: called CC=2 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
exists $posix_function{$_->content} and is_function_call($_) 44 164 208
229 30 } @$aref;
230 ### count func calls: $count
231  
232 # symbol references \&dup or calls &dup(6)
233 30 ▶ $aref = $document->find ('PPI::Token::Symbol') || [];
Condition100%2/2
$document->find('PPI::Token::Symbol') || []
l!l
822
MC/DC100%1/1
$document->find('PPI::Token::Symbol') || []
$document->find('PPI::Token::Symbol')
Truth table100%2/2
A
A= $document->find('PPI::Token::Symbol')
Aresult
11
00
234 $count += List::MoreUtils::true
235 88 ▶ { my $symbol = $_->symbol;
Subroutine __ANON__: called CC=2 SCAR=7.2
236 88 ▶ $symbol =~ /^&/ && exists $posix_function{substr($symbol,1)}
Branch: 1 branch
BranchTrueFalseTotal
$symbol =~ /^&/ and exists $posix_function{substr $symbol, 1} 88 0 88
237 30 } @$aref;
238 ### plus symbols gives: $count
239  
240 30 return $count;
241 }
242  
243 40 1;
244 __END__
245  
246 =for stopwords POSIX kbytes Ryde
247  
248 =head1 NAME
249  
250 Perl::Critic::Policy::Modules::ProhibitPOSIXimport - don't import the whole of POSIX into a module
251  
252 =head1 DESCRIPTION
253  
254 This policy is part of the L<C<Perl::Critic::Pulp>|Perl::Critic::Pulp>
255 add-on. It asks you not to C<use POSIX> with an import of all the symbols
256 from that module if you're only using a few things.
257  
258 package Foo;
259 use POSIX; # bad
260  
261 The aim is to save some memory, and maybe run a bit faster. A full C<POSIX>
262 import adds about 550 symbols to your module and that's about 30 to 40
263 kbytes in Perl 5.10 on a 32-bit system, or about 115 kbytes in Perl 5.8. If
264 lots of modules do this then it adds up.
265  
266 As noted in the C<POSIX> module docs, the way it exports everything by
267 default is an historical accident, not something to encourage.
268  
269 =head2 Allowed Forms
270  
271 A full import is allowed in C<package main>, which is the top-level of a
272 script etc, since in a script you want convenience rather than a bit of
273 memory, at least initially.
274  
275 #!/usr/bin/perl
276 use POSIX; # ok
277  
278 An import of no symbols is allowed and you then add a C<POSIX::> qualifier
279 to each call or constant. Qualifiers like this can make it clear where the
280 function is coming from.
281  
282 package Foo;
283 use POSIX (); # ok
284  
285 my $fd = POSIX::dup(0);
286 if ($! == POSIX::ENOENT())
287  
288 An import of an explicit set of functions and constants is allowed. This
289 allows short names without the memory penalty of a full import. However it
290 can be error-prone to update the imports with what you actually use (see
291 C<ProhibitCallsToUndeclaredSubs> for some checking).
292  
293 package Foo;
294 use POSIX qw(dup ENOENT); # ok
295 ...
296 my $fd = dup(0);
297  
298 A full import is allowed in a module if there's 15 or more calls to C<POSIX>
299 module functions. This rule might change or be configurable in the future,
300 but the intention is that a module making heavy use of C<POSIX> shouldn't be
301 burdened by a C<POSIX::> on every call or by maintaining a list of explicit
302 imports.
303  
304 package Foo;
305 use POSIX; # ok
306 ...
307 tzset(); dup(1)... # 15 or more calls to POSIX stuff
308  
309 =head2 Disabling
310  
311 If you don't care this sort of thing you can always disable
312 C<ProhibitPOSIXimport> from your F<.perlcriticrc> in the usual way (see
313 L<Perl::Critic/CONFIGURATION>),
314  
315 [-Modules::ProhibitPOSIXimport]
316  
317 =head1 SEE ALSO
318  
319 L<POSIX>,
320 L<Perl::Critic::Pulp>,
321 L<Perl::Critic>,
322 L<Perl::Critic::Policy::Subroutines::ProhibitCallsToUndeclaredSubs>
323  
324 =head1 HOME PAGE
325  
326 L<http://user42.tuxfamily.org/perl-critic-pulp/index.html>
327  
328 =head1 COPYRIGHT
329  
330 Copyright 2009, 2010, 2011, 2012, 2013, 2014, 2015, 2016, 2017, 2019, 2021 Kevin Ryde
331  
332 Perl-Critic-Pulp is free software; you can redistribute it and/or modify it
333 under the terms of the GNU General Public License as published by the Free
334 Software Foundation; either version 3, or (at your option) any later
335 version.
336  
337 Perl-Critic-Pulp is distributed in the hope that it will be useful, but
338 WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
339 or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
340 more details.
341  
342 You should have received a copy of the GNU General Public License along with
343 Perl-Critic-Pulp. If not, see <http://www.gnu.org/licenses/>.
344  
345 =cut
« Policy/ValuesAndExpressions/RequireNumericVersion.pm ↑ Summary Policy/CodeLayout/RequireTrailingCommaAtNewline.pm »