blib/lib/Perl/Critic/Policy/ValuesAndExpressions/ProhibitComplexVersion.pm

filter: Statementstmt 85.9% 61 / 71 Branchbran 42.1% 16 / 38 Conditioncond 25.0% 3 / 12 MC/DCmcdc 0.0% 0 / 8 Subroutinesub 100% 15 / 15 Podpod 80.0% 4 / 5 totaltotal 66.4% 99 / 149 CC 27 SCAR 28.9
CC
27
Cov
66%
_validate_fully_qualified_symbols
38.5
violates
17.1
_get_simple_statement
5.9
CRAP 18.0
« blib/lib/Perl/Critic/Utils/PPI.pm ↑ Summary blib/lib/Perl/Critic/Policy/Subroutines/ProtectPrivateSubs.pm »
1 package Perl::Critic::Policy::ValuesAndExpressions::ProhibitComplexVersion;
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  
7 40 ▶use Perl::Critic::Utils qw{ :booleans :characters :severities };
Subroutine BEGIN: called CC=1 SCAR=0.0
8 40 use Perl::Critic::Utils::PPI qw{
9 get_next_element_in_same_simple_statement
10 get_previous_module_used_on_same_line
11 is_ppi_simple_statement
12 40 ▶};
Subroutine BEGIN: called CC=1 SCAR=0.0
13 40 ▶use Readonly;
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 40 ▶use parent 'Perl::Critic::Policy';
Subroutine BEGIN: called CC=1 SCAR=0.0
16  
17 40 our $VERSION = '1.150';
18  
19 #-----------------------------------------------------------------------------
20  
21 40 Readonly::Scalar my $DOLLAR => q<$>;
22 # All uses of the $DOLLAR variable below are to prevent false failures in
23 # xt/93_version.t.
24 40 Readonly::Scalar my $VERSION_MODULE => q<version>;
25 40 Readonly::Scalar my $VERSION_VARIABLE => $DOLLAR . q<VERSION>;
26  
27 40 Readonly::Scalar my $DESC =>
28 $DOLLAR . q<VERSION value should not come from outside module>;
29 40 Readonly::Scalar my $EXPL =>
30 q<If the version comes from outside the module, you can get everything from unexpected version changes to denial-of-service attacks.>;
31  
32 #-----------------------------------------------------------------------------
33  
34 sub supported_parameters { return (
35 {
36 91 ▶ name => 'forbid_use_version',
Subroutine supported_parameters: called CC=1 SCAR=0.0
Pod undocumented
37 description =>
38 qq<Make "use version; our ${DOLLAR}VERSION = qv('1.2.3');" a violation of this policy.>,
39 default_string => $FALSE,
40 behavior => 'boolean',
41 },
42 );
43 }
44 75 ▶sub default_severity { return $SEVERITY_MEDIUM }
Subroutine default_severity: called CC=1 SCAR=0.0
Pod documented
45 74 ▶sub default_themes { return qw( core maintenance ) }
Subroutine default_themes: called CC=1 SCAR=0.0
Pod documented
46 31 ▶sub applies_to { return 'PPI::Token::Symbol' }
Subroutine applies_to: called CC=1 SCAR=0.0
Pod documented
47  
48 #-----------------------------------------------------------------------------
49  
50 sub violates {
51 173 ▶ my ( $self, $elem, $doc ) = @_;
Subroutine violates: called CC=9 SCAR=28.4
Pod documented
52  
53 # Any variable other than $VERSION is ignored.
54 173 ▶ return if $VERSION_VARIABLE ne $elem->content();
Branch: 1 branch
BranchTrueFalseTotal
if $VERSION_VARIABLE ne $elem->content 146 27 173
55  
56 # We are only interested in assignments to $VERSION, but it might be a
57 # list assignment, so if we do not find an assignment, we move up the
58 # parse tree. If we hit a statement (or no parent at all) we do not
59 # understand the code to be an assignment statement, and we simply return.
60 27 my $operator;
61 return if
62 27 ▶ not $operator = get_next_element_in_same_simple_statement( $elem )
Branch: 1 branch
BranchTrueFalseTotal
if not $operator = get_next_element_in_same_simple_statement($elem) or $Perl::Critic::Policy::ValuesAndExpressions::ProhibitComplexVersion::EQUAL ne $operator 0 27 27
Condition33%1/3
not $operator = get_next_element_in_same_simple_statement($elem) or $Perl::Critic::Policy::ValuesAndExpressions::ProhibitComplexVersion::EQUAL ne $operator
l!l&&r!l&&!r
0027
MC/DC0%0/2
not $operator = get_next_element_in_same_simple_statement($elem) or $Perl::Critic::Policy::ValuesAndExpressions::ProhibitComplexVersion::EQUAL ne $operator
not $operator = get_next_element_in_same_simple_statement($elem) $Perl::Critic::Policy::ValuesAndExpressions::ProhibitComplexVersion::EQUAL ne $operator
Truth table33%1/3
not(A) or B
A= not $operator = get_next_element_in_same_simple_statement($elem)B= $Perl::Critic::Policy::ValuesAndExpressions::ProhibitComplexVersion::EQUAL ne $operator
ABresult
1X1
011
000
63 or $EQUAL ne $operator;
64  
65 # Find the simple statement we are in. If we can not find it, abandon the
66 # attempt to analyze the code.
67 27 ▶ my $statement = $self->_get_simple_statement( $elem )
Branch: 1 branch
BranchTrueFalseTotal
my $statement = $self->_get_simple_statement($elem) or return 0 27 27
68 or return;
69  
70 # Check all symbols in the statement for violation.
71 27 my $exception;
72 27 ▶ return $exception if
Branch: 1 branch
BranchTrueFalseTotal
if $exception = $self->_validate_fully_qualified_symbols($elem, $statement, $doc) 0 27 27
73 $exception =
74 $self->_validate_fully_qualified_symbols($elem, $statement, $doc);
75  
76 # At this point we have found no data that is explicitly from outside the
77 # file. If the author wants to use a $VERSION from another module, _and_
78 # wants MM->parse_version to understand it, the other module must be used
79 # on the same line. So we assume no violation unless this has been done.
80 27 ▶ my $module = get_previous_module_used_on_same_line( $elem )
Branch: 1 branch
BranchTrueFalseTotal
my $module = get_previous_module_used_on_same_line($elem) or return 27 0 27
81 or return;
82  
83 # We make an exception for 'use version' unless configured otherwise; so
84 # let it be written, so let it be done.
85 0 ▶ return if $module eq $VERSION_MODULE and not $self->{_forbid_use_version};
Branch: 1 branch
BranchTrueFalseTotal
if $module eq $VERSION_MODULE and not $self->{"_forbid_use_version"} 0 0 0
Condition0%0/3
$module eq $VERSION_MODULE and not $self->{"_forbid_use_version"}
!ll&&!rl&&r
000
MC/DC0%0/2
$module eq $VERSION_MODULE and not $self->{"_forbid_use_version"}
$module eq $VERSION_MODULE not $self->{"_forbid_use_version"}
Truth table0%0/3
A and not(B)
A= $module eq $VERSION_MODULEB= not $self->{"_forbid_use_version"}
ABresult
0X0
100
111
86  
87 # We assume nefarious intent if we have any other module used on the same
88 # line as the $VERSION assignment.
89 0 return $self->violation( $DESC, $EXPL, $elem );
90 }
91  
92 #-----------------------------------------------------------------------------
93  
94 # Return the simple statement that contains our element. The classification
95 # done by is_ppi_simple_statement is not quite good enough in this case -- if
96 # our parent is a PPI::Structure::List, we want to keep looking.
97  
98 sub _get_simple_statement {
99 27 ▶ my ( $self, $elem ) = @_;
Subroutine _get_simple_statement: called CC=5 SCAR=17.8
100  
101 27 my $statement = $elem;
102  
103 27 ▶ while ( $statement) {
Branch: 1 branch
BranchTrueFalseTotal
if ($statement) 54 0 54
104 54 my $parent;
105 54 ▶ if ( is_ppi_simple_statement( $statement ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (is_ppi_simple_statement($statement)) { } 27 27 54
106 27 ▶ return $statement if
Branch: 1 branch
BranchTrueFalseTotal
unless $parent = $statement->parent and $parent->isa("PPI::Structure::List") 27 0 27
Condition33%1/3
$parent = $statement->parent and $parent->isa("PPI::Structure::List")
!ll&&!rl&&r
0270
MC/DC0%0/2
$parent = $statement->parent and $parent->isa("PPI::Structure::List")
$parent = $statement->parent $parent->isa("PPI::Structure::List")
Truth table33%1/3
A and B
A= $parent = $statement->parentB= $parent->isa("PPI::Structure::List")
ABresult
0X0
100
111
107 not $parent = $statement->parent()
108 or not $parent->isa( 'PPI::Structure::List' );
109 0 $statement = $parent;
110 } else {
111 27 $statement = $statement->parent();
112 }
113 }
114  
115 0 return;
116 }
117  
118 #-----------------------------------------------------------------------------
119  
120 sub _validate_fully_qualified_symbols {
121 27 ▶ my ( $self, $elem, $statement, $doc ) = @_;
Subroutine _validate_fully_qualified_symbols: called CC=14 SCAR=36.5
122  
123 # Find the package(s) in this file.
124 my %local_package =
125 27 map { $_->schild( 1 ) => 1 }
126 27 ▶ @{ $doc->find( 'PPI::Statement::Package' ) || [] };
Branch: 1 branch
BranchTrueFalseTotal
$doc->find("PPI::Statement::Package") or [] 0 27 27
127 27 $local_package{main} = 1; # For completeness.
128  
129 # Check all symbols in the statement for violation.
130 27 foreach my $symbol (
131 27 ▶ @{ $statement->find( 'PPI::Token::Symbol' ) || [] }
Branch: 1 branch
BranchTrueFalseTotal
$statement->find("PPI::Token::Symbol") or [] 0 27 27
132 ) {
133 27 ▶ if ( $symbol->canonical() =~ m< \A [@\$%&] ([\w:]*) :: >smx ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($symbol->canonical =~ / \A [\@\$%&] ([\w:]*) :: /msx) 0 27 27
134 0 ▶ $local_package{ $1 }
Branch: 1 branch
BranchTrueFalseTotal
$local_package{$1} or return $self->violation($DESC, $EXPL, $elem) 0 0 0
135 or return $self->violation( $DESC, $EXPL, $elem );
136 }
137 }
138  
139 # Check all interpolatable strings in the statement for violation.
140 # TODO this does not correctly handle "@{[some_expression()]}".
141 27 foreach my $string (
142 @{
143 $statement->find(
144 sub {
145 return
146 216 ▶ $_[1]->isa('PPI::Token::Quote::Double')
Subroutine __ANON__: called CC=2 SCAR=9.2
Condition33%1/3
$_[1]->isa('PPI::Token::Quote::Double') || $_[1]->isa('PPI::Token::Quote::Interpolate')
l!l&&r!l&&!r
00216
MC/DC0%0/2
$_[1]->isa('PPI::Token::Quote::Double') || $_[1]->isa('PPI::Token::Quote::Interpolate')
$_[1]->isa('PPI::Token::Quote::Double') $_[1]->isa('PPI::Token::Quote::Interpolate')
Truth table33%1/3
A || B
A= $_[1]->isa('PPI::Token::Quote::Double')B= $_[1]->isa('PPI::Token::Quote::Interpolate')
ABresult
1X1
011
000
147 || $_[1]->isa('PPI::Token::Quote::Interpolate');
148 }
149 )
150 27 ▶ or []
Branch: 1 branch
BranchTrueFalseTotal
$statement->find(sub { return $_[1]->isa("PPI::Token::Quote::Double") || $_[1]->isa("PPI::Token::Quote::Interpolate"); } ) or [] 27 0 27
151 }
152 ) {
153 0 my $unquoted = $string->string();
154 0 ▶ while (
Branch: 1 branch
BranchTrueFalseTotal
if ($unquoted =~ / (?: \A | [^\\] ) (?: \\{2} )* [\@\$] [{]? ([\w:]*) :: /gmsx) 0 0 0
155 $unquoted =~
156 m<
157 (?: \A | [^\\] )
158 (?: \\{2} )*
159 [@\$]
160 [{]?
161 ([\w:]*)
162 ::
163 >gsmx
164 ) {
165 0 ▶ next if $local_package{ $1 };
Branch: 1 branch
BranchTrueFalseTotal
if $local_package{$1} 0 0 0
166  
167 0 return $self->violation( $DESC, $EXPL, $elem );
168 }
169 }
170  
171 # Check all words in the statement for violation.
172 27 ▶ foreach my $symbol ( @{ $statement->find( 'PPI::Token::Word' ) || [] } ) {
Branch: 1 branch
BranchTrueFalseTotal
$statement->find("PPI::Token::Word") or [] 0 27 27
173 27 ▶ if ( $symbol->content() =~ m/ \A ([\w:]*) :: /smx ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($symbol->content =~ / \A ([\w:]*) :: /msx) 0 27 27
174 return $self->violation( $DESC, $EXPL, $elem )
175 0 ▶ if not $local_package{ $1 };
Branch: 1 branch
BranchTrueFalseTotal
unless $local_package{$1} 0 0 0
176 }
177 }
178  
179 27 return;
180 }
181  
182 40 1;
183  
184 __END__
185  
186 #-----------------------------------------------------------------------------
187  
188 =pod
189  
190 =head1 NAME
191  
192 Perl::Critic::Policy::ValuesAndExpressions::ProhibitComplexVersion - Prohibit version values from outside the module.
193  
194 =head1 AFFILIATION
195  
196 This Policy is part of the core L<Perl::Critic|Perl::Critic>
197 distribution.
198  
199  
200 =head1 DESCRIPTION
201  
202 One tempting way to keep a group of related modules at the same version number
203 is to have all of them import the version number from a designated module. For
204 example, module C<Foo::Master> could be the version master for the C<Foo>
205 package, and all other modules could use its C<$VERSION> by
206  
207 use Foo::Master; our $VERSION = $Foo::Master::VERSION;
208  
209 This turns out not to be a good idea, because all sorts of unintended things
210 can happen - anything from unintended version number changes to
211 denial-of-service attacks (since C<Foo::Master> is executed by the 'use').
212  
213 This policy examines statements that assign to C<$VERSION>, and declares a
214 violation under two circumstances: first, if that statement uses a
215 fully-qualified symbol that did not originate in a package declared in the
216 file; second if there is a C<use> statement on the same line that makes the
217 assignment.
218  
219 By default, an exception is made for C<use version;> because of its
220 recommendation by Perl Best Practices. See the C<forbid_use_version>
221 configuration variable if you do not want an exception made for C<use
222 version;>.
223  
224  
225 =head1 CONFIGURATION
226  
227 The construction
228  
229 use version; our $VERSION = qv('1.2.3');
230  
231 is exempt from this policy by default, because it is recommended by Perl Best
232 Practices. Should you wish to identify C<use version;> as a violation, add the
233 following to your perlcriticrc file:
234  
235 [ValuesAndExpressions::ProhibitComplexVersion]
236 forbid_use_version = 1
237  
238  
239 =head1 CAVEATS
240  
241 This code assumes that the hallmark of a violation is a 'use' on the same line
242 as the C<$VERSION> assignment, because that is the way to have it seen by
243 L<ExtUtils::MakeMaker|ExtUtils::MakeMaker>->parse_version(). Other ways to get
244 a version value from outside the module can be imagined, and this policy is
245 currently oblivious to them.
246  
247  
248 =head1 AUTHOR
249  
250 Thomas R. Wyant, III F<wyant at cpan dot org>
251  
252  
253 =head1 COPYRIGHT
254  
255 Copyright (c) 2009-2023 Tom Wyant
256  
257 This program is free software; you can redistribute it and/or modify
258 it under the same terms as Perl itself. The full text of this license
259 can be found in the LICENSE file included with this module.
260  
261 =cut
262  
263 # Local Variables:
264 # mode: cperl
265 # cperl-indent-level: 4
266 # fill-column: 78
267 # indent-tabs-mode: nil
268 # c-indentation-style: bsd
269 # End:
270 # ex: set ts=8 sts=4 sw=4 tw=78 ft=perl expandtab shiftround :
« blib/lib/Perl/Critic/Utils/PPI.pm ↑ Summary blib/lib/Perl/Critic/Policy/Subroutines/ProtectPrivateSubs.pm »