blib/lib/Perl/Critic/Policy/Modules/ProhibitAutomaticExportation.pm

filter: Statementstmt 67.2% 37 / 55 Branchbran 16.6% 3 / 18 Conditioncond 0.0% 0 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 70.0% 14 / 20 Podpod 80.0% 4 / 5 totaltotal 56.3% 58 / 103 CC 11 SCAR 16.3
CC
11
Cov
53%
__ANON__
12.0
_our_export
12.0
_vars_export
12.0
CRAP 5.1
« blib/lib/Perl/Critic/Statistics.pm ↑ Summary blib/lib/Perl/Critic/Policy/Subroutines/ProhibitNestedSubs.pm »
1 package Perl::Critic::Policy::Modules::ProhibitAutomaticExportation;
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 40 ▶use Readonly;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 40 ▶use Perl::Critic::Utils qw{ :severities };
Subroutine BEGIN: called CC=1 SCAR=0.0
9 40 ▶use List::SomeUtils qw(any);
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.150';
13  
14 #-----------------------------------------------------------------------------
15  
16 40 Readonly::Scalar my $DESC => q{Symbols are exported by default};
17 40 Readonly::Scalar my $EXPL => q{Use '@EXPORT_OK' or '%EXPORT_TAGS' instead}; ## no critic (RequireInterpolation)
18  
19 #-----------------------------------------------------------------------------
20  
21 90 ▶sub supported_parameters { return () }
Subroutine supported_parameters: called CC=1 SCAR=0.0
Pod undocumented
22 75 ▶sub default_severity { return $SEVERITY_HIGH }
Subroutine default_severity: called CC=1 SCAR=0.0
Pod documented
23 74 ▶sub default_themes { return qw( core bugs ) }
Subroutine default_themes: called CC=1 SCAR=0.0
Pod documented
24 33 ▶sub applies_to { return 'PPI::Document' }
Subroutine applies_to: called CC=1 SCAR=0.0
Pod documented
25  
26 #-----------------------------------------------------------------------------
27  
28 sub violates {
29 33 ▶ my ( $self, $elem, $doc ) = @_;
Subroutine violates: called CC=3 SCAR=15.1
Pod documented
30  
31 33 ▶ if ( _uses_exporter($doc) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (_uses_exporter($doc)) 0 33 33
32 0 ▶ if ( my $exp = _has_exports($doc) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $exp = _has_exports($doc)) 0 0 0
33 0 return $self->violation( $DESC, $EXPL, $exp );
34 }
35 }
36 33 return; #ok
37 }
38  
39 #-----------------------------------------------------------------------------
40  
41 sub _uses_exporter {
42 33 ▶ my ($doc) = @_;
Subroutine _uses_exporter: called CC=2 SCAR=6.9
43  
44 33 my $includes_ref = $doc->find('PPI::Statement::Include');
45 33 ▶ return if not $includes_ref;
Branch: 1 branch
BranchTrueFalseTotal
unless $includes_ref 3 30 33
46  
47 # This covers both C<use Exporter;> and C<use parent 'Exporter';>
48 30 ▶ return any { m/ \b Exporter \b/xms } @{ $includes_ref };
Subroutine __ANON__: called CC=1 SCAR=0.0
49 }
50  
51 #------------------
52  
53 sub _has_exports {
54 0 ▶ my ($doc) = @_;
Subroutine _has_exports: not called CC=1 SCAR=6.9
55  
56 my $wanted =
57 0 ▶ sub { _our_export(@_) or _vars_export(@_) or _package_export(@_) };
Subroutine __ANON__: not called CC=3 SCAR=24.8
Branch: 1 branch
BranchTrueFalseTotal
_our_export(@_) or _vars_export(@_) or _package_export(@_) 0 0 0
Condition0%0/3
_our_export(@_) or _vars_export(@_)
l!l&&r!l&&!r
000
MC/DC0%0/2
_our_export(@_) or _vars_export(@_)
_our_export(@_) _vars_export(@_)
Truth table0%0/3
A or B
A= _our_export(@_)B= _vars_export(@_)
ABresult
1X1
011
000
58  
59 0 return $doc->find_first( $wanted );
60 }
61  
62 #------------------
63  
64 sub _our_export {
65 0 ▶ my (undef, $elem) = @_;
Subroutine _our_export: not called CC=3 SCAR=24.8
66  
67 0 ▶ $elem->isa('PPI::Statement::Variable') or return 0;
Branch: 1 branch
BranchTrueFalseTotal
$elem->isa("PPI::Statement::Variable") or return 0 0 0 0
68 0 ▶ $elem->type() eq 'our' or return 0;
Branch: 1 branch
BranchTrueFalseTotal
$elem->type eq "our" or return 0 0 0 0
69  
70 0 ▶ return any { $_ eq '@EXPORT' } $elem->variables(); ## no critic(RequireInterpolationOfMetachars)
Subroutine __ANON__: not called CC=1 SCAR=6.9
71 }
72  
73 #------------------
74  
75 sub _vars_export {
76 0 ▶ my (undef, $elem) = @_;
Subroutine _vars_export: not called CC=3 SCAR=24.8
77  
78 0 ▶ $elem->isa('PPI::Statement::Include') or return 0;
Branch: 1 branch
BranchTrueFalseTotal
$elem->isa("PPI::Statement::Include") or return 0 0 0 0
79 0 ▶ $elem->pragma() eq 'vars' or return 0;
Branch: 1 branch
BranchTrueFalseTotal
$elem->pragma eq "vars" or return 0 0 0 0
80  
81 0 return $elem =~ m{ \@EXPORT \b }xms; #Crude, but usually works
82 }
83  
84 #------------------
85  
86 sub _package_export {
87 0 ▶ my (undef, $elem) = @_;
Subroutine _package_export: not called CC=2 SCAR=17.9
88  
89 0 ▶ $elem->isa('PPI::Token::Symbol') or return 0;
Branch: 1 branch
BranchTrueFalseTotal
$elem->isa("PPI::Token::Symbol") or return 0 0 0 0
90  
91 0 return $elem =~ m{ \A \@ \S+ ::EXPORT \z }xms;
92 #TODO: ensure that it is in _this_ package!
93 }
94  
95 40 1;
96  
97 __END__
98  
99 #-----------------------------------------------------------------------------
100  
101 =pod
102  
103 =head1 NAME
104  
105 Perl::Critic::Policy::Modules::ProhibitAutomaticExportation - Export symbols via C<@EXPORT_OK> or C<%EXPORT_TAGS> instead of C<@EXPORT>.
106  
107  
108 =head1 AFFILIATION
109  
110 This Policy is part of the core L<Perl::Critic|Perl::Critic>
111 distribution.
112  
113  
114 =head1 DESCRIPTION
115  
116 When using L<Exporter|Exporter>, symbols placed in the C<@EXPORT>
117 variable are automatically exported into the caller's namespace.
118 Although convenient, this practice is not polite, and may cause
119 serious problems if the caller declares the same symbols. The best
120 practice is to place your symbols in C<@EXPORT_OK> or C<%EXPORT_TAGS>
121 and let the caller choose exactly which symbols to export.
122  
123 package Foo;
124  
125 use Exporter 'import';
126 our @EXPORT = qw(foo $bar @baz); # not ok
127 our @EXPORT_OK = qw(foo $bar @baz); # ok
128 our %EXPORT_TAGS = ( all => [ qw(foo $bar @baz) ] ); # ok
129  
130  
131 =head1 CONFIGURATION
132  
133 This Policy is not configurable except for the standard options.
134  
135  
136 =head1 AUTHOR
137  
138 Jeffrey Ryan Thalhammer <jeff@imaginative-software.com>
139  
140  
141 =head1 COPYRIGHT
142  
143 Copyright (c) 2005-2023 Imaginative Software Systems. All rights reserved.
144  
145 This program is free software; you can redistribute it and/or modify
146 it under the same terms as Perl itself. The full text of this license
147 can be found in the LICENSE file included with this module.
148  
149 =cut
150  
151 # Local Variables:
152 # mode: cperl
153 # cperl-indent-level: 4
154 # fill-column: 78
155 # indent-tabs-mode: nil
156 # c-indentation-style: bsd
157 # End:
158 # ex: set ts=8 sts=4 sw=4 tw=78 ft=perl expandtab shiftround :
« blib/lib/Perl/Critic/Statistics.pm ↑ Summary blib/lib/Perl/Critic/Policy/Subroutines/ProhibitNestedSubs.pm »