Policy/ValuesAndExpressions/RequireNumericVersion.pm

filter: Statementstmt 91.7% 89 / 97 Branchbran 67.3% 31 / 46 Conditioncond 76.9% 20 / 26 MC/DCmcdc 57.1% 8 / 14 Subroutinesub 100% 16 / 16 Podpod 100% 1 / 1 totaltotal 82.5% 165 / 200 CC 37 SCAR 24.1
CC
37
Cov
83%
violates
15.7
_any_eval_VERSION
15.6
_following_expression
15.1
CRAP 11.2
« Policy/Compatibility/ConstantLeadingUnderscore.pm ↑ Summary Policy/CodeLayout/RequireTrailingCommaAtNewline.pm »
1 # Copyright 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::ValuesAndExpressions::RequireNumericVersion;
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 Scalar::Util;
Subroutine BEGIN: called CC=1 SCAR=0.0
22 40 ▶use version (); # but don't import qv()
Subroutine BEGIN: called CC=1 SCAR=0.0
23  
24 40 ▶use base 'Perl::Critic::Policy';
Subroutine BEGIN: called CC=1 SCAR=0.0
25 40 ▶use Perl::Critic::Utils 'precedence_of';
Subroutine BEGIN: called CC=1 SCAR=0.0
26 40 ▶use Perl::Critic::Pulp::Utils;
Subroutine BEGIN: called CC=1 SCAR=0.0
27  
28 # uncomment this to run the ### lines
29 #use Smart::Comments;
30  
31 40 ▶use constant supported_parameters => ();
Subroutine BEGIN: called CC=1 SCAR=0.0
32 40 ▶use constant default_severity => $Perl::Critic::Utils::SEVERITY_MEDIUM;
Subroutine BEGIN: called CC=1 SCAR=0.0
33 40 ▶use constant default_themes => qw(pulp bugs);
Subroutine BEGIN: called CC=1 SCAR=0.0
34 40 ▶use constant applies_to => ('PPI::Token::Symbol');
Subroutine BEGIN: called CC=1 SCAR=0.0
35  
36 40 my $perl_510 = version->new('5.10.0');
37 40 my $assignment_precedence = precedence_of('=');
38  
39 40 our $VERSION = 100;
40  
41 36 ▶sub violates {
Subroutine violates: called CC=15 SCAR=27.6
Pod documented
42 36 my ($self, $elem, $document) = @_;
43 ### NumericVersion violates()
44  
45 ### canonical: $elem->canonical
46 36 ▶ my $package = _symbol_is_mod_VERSION($elem)
Condition100%2/2
_symbol_is_mod_VERSION($elem) || (return)
l!l
324
MC/DC100%1/1
_symbol_is_mod_VERSION($elem) || (return)
_symbol_is_mod_VERSION($elem)
Truth table100%2/2
A
A= _symbol_is_mod_VERSION($elem)
Aresult
11
00
47 || return;
48  
49 32 ▶ my $assign = $elem->snext_sibling || return;
Condition100%2/2
$elem->snext_sibling || (return)
l!l
275
MC/DC100%1/1
$elem->snext_sibling || (return)
$elem->snext_sibling
Truth table100%2/2
A
A= $elem->snext_sibling
Aresult
11
00
50 ### assign: "$assign"
51 27 ▶ $assign eq '=' or return;
Branch: 1 branch
BranchTrueFalseTotal
$assign eq "=" or return 27 0 27
52  
53 27 ▶ my $value = $assign->snext_sibling || return;
Condition50%1/2
$assign->snext_sibling || (return)
l!l
270
MC/DC0%0/1
$assign->snext_sibling || (return)
$assign->snext_sibling
Truth table50%1/2
A
A= $assign->snext_sibling
Aresult
11
00
54 ### value: "$value"
55  
56 27 ▶ if (! $value->isa('PPI::Token::Quote')) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($value->isa("PPI::Token::Quote")) 8 19 27
57 ### an expression, or a number, not a string, so ok ...
58 8 return;
59 }
60 19 ▶ if (_following_expression ($value)) {
Branch: 1 branch
BranchTrueFalseTotal
if (_following_expression($value)) 0 19 19
61 ### can't check an expression (though it starts with a string) ...
62 0 return;
63 }
64  
65 19 my $str = $value->string;
66 19 ▶ if ($value->isa ('PPI::Token::Quote::Double')
Branch: 1 branch
BranchTrueFalseTotal
if ($value->isa("PPI::Token::Quote::Double") or $value->isa("PPI::Token::Quote::Interpolate")) 16 3 19
Condition100%3/3
$value->isa("PPI::Token::Quote::Double") or $value->isa("PPI::Token::Quote::Interpolate")
l!l&&r!l&&!r
1333
MC/DC100%2/2
$value->isa("PPI::Token::Quote::Double") or $value->isa("PPI::Token::Quote::Interpolate")
$value->isa("PPI::Token::Quote::Double") $value->isa("PPI::Token::Quote::Interpolate")
Truth table100%3/3
A or B
A= $value->isa("PPI::Token::Quote::Double")B= $value->isa("PPI::Token::Quote::Interpolate")
ABresult
1X1
011
000
67 || $value->isa ('PPI::Token::Quote::Interpolate')) {
68 ### double quote, check only up to an interpolation
69 16 $str =~ s/[\$\@].*//;
70 }
71  
72 19 ▶ if (_any_eval_VERSION ($document, $package)) {
Branch: 1 branch
BranchTrueFalseTotal
if (_any_eval_VERSION($document, $package)) 3 16 19
73 3 return;
74 }
75  
76 16 ▶ if (! defined(Perl::Critic::Pulp::Utils::version_if_valid($str))) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined Perl::Critic::Pulp::Utils::version_if_valid($str)) 7 9 16
77 7 return $self->violation
78 ('Non-numeric VERSION string (not recognised by version.pm)',
79 '',
80 $value);
81 }
82  
83 # Float number strings like "1e6" are usually rejected by version.pm, but
84 # have seen perl 5.10 and version.pm 0.88 with pure-perl "version::vpp"
85 # accept them. Not sure why that's so, but explicitly reject to be sure.
86 # Such a string form in fact works in perl 5.8.x but not in 5.10.x.
87 #
88 9 ▶ if ($str =~ /e/i) {
Branch: 1 branch
BranchTrueFalseTotal
if ($str =~ /e/i) 0 9 9
89 0 return $self->violation
90 ('Non-numeric VERSION string (exponential string like "1e6" no good in perl 5.10 and up)',
91 '',
92 $value);
93 }
94  
95 9 my $got_perl = $document->highest_explicit_perl_version;
96 9 ▶ if (defined $got_perl && $got_perl >= $perl_510) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $got_perl and $got_perl >= $perl_510) 2 7 9
Condition100%3/3
defined $got_perl and $got_perl >= $perl_510
!ll&&!rl&&r
522
MC/DC100%2/2
defined $got_perl and $got_perl >= $perl_510
defined $got_perl $got_perl >= $perl_510
Truth table100%3/3
A and B
A= defined $got_perlB= $got_perl >= $perl_510
ABresult
0X0
100
111
97 # for 5.10 up only need to satisfy version.pm
98 2 return;
99 }
100  
101 # for 5.8 or unspecified version must be plain number, not "1.2.3" etc
102 7 ▶ if (! Scalar::Util::looks_like_number($str)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (&Scalar::Util::looks_like_number($str)) 7 0 7
103 7 return $self->violation ('Non-numeric VERSION string',
104 '',
105 $value);
106 }
107 0 return;
108 }
109  
110 19 ▶sub _following_expression {
Subroutine _following_expression: called CC=6 SCAR=27.1
111 19 my ($elem) = @_;
112 19 ▶ my $after = $elem->snext_sibling
Branch: 1 branch
BranchTrueFalseTotal
my $after = $elem->snext_sibling or return 0 6 13 19
113 or return 0;
114  
115 6 ▶ if ($after->isa('PPI::Token::Structure')) {
Branch: 2 branches
BranchTrueFalseTotal
if ($after->isa('PPI::Token::Structure')) { } 6 0 6
elsif ($after->isa('PPI::Token::Operator')) { } 0 0 0
116 6 return 0;
117 } elsif ($after->isa('PPI::Token::Operator')) {
118 0 ▶ if (precedence_of($after) >= $assignment_precedence) {
Branch: 1 branch
BranchTrueFalseTotal
if (precedence_of($after) >= $assignment_precedence) 0 0 0
119 0 return 0;
120 }
121 0 ▶ if ($after eq '.') {
Branch: 1 branch
BranchTrueFalseTotal
if ($after eq ".") 0 0 0
122 0 return 0;
123 }
124 }
125 0 return 1;
126 }
127  
128 # $elem is a PPI::Token::Word
129 # return its module, such as "Foo::Bar"
130 # or if it's in "main" then return undef
131 #
132 66 ▶sub _symbol_is_mod_VERSION {
Subroutine _symbol_is_mod_VERSION: called CC=5 SCAR=16.1
133 66 my ($elem) = @_;
134  
135 # canonical() turns $::VERSION into $main::VERSION
136 66 ▶ $elem->canonical =~ /^\$((\w+::)*)VERSION$/
Branch: 1 branch
BranchTrueFalseTotal
$elem->canonical =~ /^\$((\w+::)*)VERSION$/ or return undef 66 0 66
137 or return undef; # not $VERSION or $Foo::VERSION
138 66 my $package = substr($1,0,-2);
139  
140 66 ▶ if ($package eq '') {
Branch: 1 branch
BranchTrueFalseTotal
if ($package eq "") 54 12 66
141 # $elem is an unqualified symbol, find containing "package Foo"
142 54 ▶ my $pelem = Perl::Critic::Pulp::Utils::elem_package($elem)
Condition100%2/2
Perl::Critic::Pulp::Utils::elem_package($elem) || (return undef)
l!l
531
MC/DC100%1/1
Perl::Critic::Pulp::Utils::elem_package($elem) || (return undef)
Perl::Critic::Pulp::Utils::elem_package($elem)
Truth table100%2/2
A
A= Perl::Critic::Pulp::Utils::elem_package($elem)
Aresult
11
00
143 || return undef; # not in a package, not a module $VERSION
144 53 $package = $pelem->namespace;
145 }
146  
147 65 ▶ if ($package eq 'main') {
Branch: 1 branch
BranchTrueFalseTotal
if ($package eq "main") 3 62 65
148 3 return undef; # "package main" or "$main::VERSION", not a module
149 }
150 62 return $package;
151 }
152  
153 # return true if there's a "$VERSION = eval $VERSION" somewhere in
154 # $document, acting on the "$VERSION" of $want_package
155 #
156 19 ▶sub _any_eval_VERSION {
Subroutine _any_eval_VERSION: called CC=14 SCAR=27.5
157 19 my ($document, $want_package) = @_;
158  
159 19 ▶ my $aref = $document->find('PPI::Token::Symbol') || return 0;
Condition50%1/2
$document->find('PPI::Token::Symbol') || (return 0)
l!l
190
MC/DC0%0/1
$document->find('PPI::Token::Symbol') || (return 0)
$document->find('PPI::Token::Symbol')
Truth table50%1/2
A
A= $document->find('PPI::Token::Symbol')
Aresult
11
00
160 19 foreach my $elem (@$aref) {
161 27 ▶ my $got_package = _symbol_is_mod_VERSION($elem) || next;
Condition50%1/2
_symbol_is_mod_VERSION($elem) || (next)
l!l
270
MC/DC0%0/1
_symbol_is_mod_VERSION($elem) || (next)
_symbol_is_mod_VERSION($elem)
Truth table50%1/2
A
A= _symbol_is_mod_VERSION($elem)
Aresult
11
00
162 27 ▶ $got_package eq $want_package || next;
Branch: 1 branch
BranchTrueFalseTotal
$got_package eq $want_package or next 25 2 27
163  
164 25 ▶ my $assign = $elem->snext_sibling || next;
Condition100%2/2
$elem->snext_sibling || (next)
l!l
241
MC/DC100%1/1
$elem->snext_sibling || (next)
$elem->snext_sibling
Truth table100%2/2
A
A= $elem->snext_sibling
Aresult
11
00
165 24 ▶ $assign eq '=' or next;
Branch: 1 branch
BranchTrueFalseTotal
$assign eq "=" or next 24 0 24
166  
167 24 ▶ my $value = $assign->snext_sibling || next;
Condition50%1/2
$assign->snext_sibling || (next)
l!l
240
MC/DC0%0/1
$assign->snext_sibling || (next)
$assign->snext_sibling
Truth table50%1/2
A
A= $assign->snext_sibling
Aresult
11
00
168 24 ▶ $value->isa('PPI::Token::Word') || next;
Branch: 1 branch
BranchTrueFalseTotal
$value->isa("PPI::Token::Word") or next 4 20 24
169 4 ▶ $value eq 'eval' or next;
Branch: 1 branch
BranchTrueFalseTotal
$value eq "eval" or next 4 0 4
170  
171 4 ▶ $value = $value->snext_sibling || next;
Condition50%1/2
$value->snext_sibling || (next)
l!l
40
MC/DC0%0/1
$value->snext_sibling || (next)
$value->snext_sibling
Truth table50%1/2
A
A= $value->snext_sibling
Aresult
11
00
172 4 ▶ $value->isa('PPI::Token::Symbol') || next;
Branch: 1 branch
BranchTrueFalseTotal
$value->isa("PPI::Token::Symbol") or next 3 1 4
173 3 ▶ $got_package = _symbol_is_mod_VERSION($value) || next;
Condition50%1/2
_symbol_is_mod_VERSION($value) || (next)
l!l
30
MC/DC0%0/1
_symbol_is_mod_VERSION($value) || (next)
_symbol_is_mod_VERSION($value)
Truth table50%1/2
A
A= _symbol_is_mod_VERSION($value)
Aresult
11
00
174 3 ▶ $got_package eq $want_package || next;
Branch: 1 branch
BranchTrueFalseTotal
$got_package eq $want_package or next 3 0 3
175  
176 3 return 1;
177 }
178 16 return 0;
179 }
180  
181 40 1;
182 __END__
« Policy/Compatibility/ConstantLeadingUnderscore.pm ↑ Summary Policy/CodeLayout/RequireTrailingCommaAtNewline.pm »