« 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)
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)
| |||||||||||||||||||||||||||||
| 47 | || return; | ||||||||||||||||||||||||||||
| 48 | |||||||||||||||||||||||||||||
| 49 | 32 | ▶ | my $assign = $elem->snext_sibling || return; | ||||||||||||||||||||||||||
|
Condition100%2/2
$elem->snext_sibling || (return)
MC/DC100%1/1
$elem->snext_sibling || (return)
$elem->snext_sibling
Truth table100%2/2
A
A= $elem->snext_sibling
| |||||||||||||||||||||||||||||
| 50 | ### assign: "$assign" | ||||||||||||||||||||||||||||
| 51 | 27 | ▶ | $assign eq '=' or return; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 52 | |||||||||||||||||||||||||||||
| 53 | 27 | ▶ | my $value = $assign->snext_sibling || return; | ||||||||||||||||||||||||||
|
Condition50%1/2
$assign->snext_sibling || (return)
MC/DC0%0/1
$assign->snext_sibling || (return)
$assign->snext_sibling
Truth table50%1/2
A
A= $assign->snext_sibling
| |||||||||||||||||||||||||||||
| 54 | ### value: "$value" | ||||||||||||||||||||||||||||
| 55 | |||||||||||||||||||||||||||||
| 56 | 27 | ▶ | if (! $value->isa('PPI::Token::Quote')) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 57 | ### an expression, or a number, not a string, so ok ... | ||||||||||||||||||||||||||||
| 58 | 8 | return; | |||||||||||||||||||||||||||
| 59 | } | ||||||||||||||||||||||||||||
| 60 | 19 | ▶ | if (_following_expression ($value)) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
Condition100%3/3
$value->isa("PPI::Token::Quote::Double") or $value->isa("PPI::Token::Quote::Interpolate")
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")
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 73 | 3 | return; | |||||||||||||||||||||||||||
| 74 | } | ||||||||||||||||||||||||||||
| 75 | |||||||||||||||||||||||||||||
| 76 | 16 | ▶ | if (! defined(Perl::Critic::Pulp::Utils::version_if_valid($str))) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
Condition100%3/3
defined $got_perl and $got_perl >= $perl_510
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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 113 | or return 0; | ||||||||||||||||||||||||||||
| 114 | |||||||||||||||||||||||||||||
| 115 | 6 | ▶ | if ($after->isa('PPI::Token::Structure')) { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 116 | 6 | return 0; | |||||||||||||||||||||||||||
| 117 | } elsif ($after->isa('PPI::Token::Operator')) { | ||||||||||||||||||||||||||||
| 118 | 0 | ▶ | if (precedence_of($after) >= $assignment_precedence) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 119 | 0 | return 0; | |||||||||||||||||||||||||||
| 120 | } | ||||||||||||||||||||||||||||
| 121 | 0 | ▶ | if ($after eq '.') { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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)
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)
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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)
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')
| |||||||||||||||||||||||||||||
| 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)
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)
| |||||||||||||||||||||||||||||
| 162 | 27 | ▶ | $got_package eq $want_package || next; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 163 | |||||||||||||||||||||||||||||
| 164 | 25 | ▶ | my $assign = $elem->snext_sibling || next; | ||||||||||||||||||||||||||
|
Condition100%2/2
$elem->snext_sibling || (next)
MC/DC100%1/1
$elem->snext_sibling || (next)
$elem->snext_sibling
Truth table100%2/2
A
A= $elem->snext_sibling
| |||||||||||||||||||||||||||||
| 165 | 24 | ▶ | $assign eq '=' or next; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 166 | |||||||||||||||||||||||||||||
| 167 | 24 | ▶ | my $value = $assign->snext_sibling || next; | ||||||||||||||||||||||||||
|
Condition50%1/2
$assign->snext_sibling || (next)
MC/DC0%0/1
$assign->snext_sibling || (next)
$assign->snext_sibling
Truth table50%1/2
A
A= $assign->snext_sibling
| |||||||||||||||||||||||||||||
| 168 | 24 | ▶ | $value->isa('PPI::Token::Word') || next; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 169 | 4 | ▶ | $value eq 'eval' or next; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 170 | |||||||||||||||||||||||||||||
| 171 | 4 | ▶ | $value = $value->snext_sibling || next; | ||||||||||||||||||||||||||
|
Condition50%1/2
$value->snext_sibling || (next)
MC/DC0%0/1
$value->snext_sibling || (next)
$value->snext_sibling
Truth table50%1/2
A
A= $value->snext_sibling
| |||||||||||||||||||||||||||||
| 172 | 4 | ▶ | $value->isa('PPI::Token::Symbol') || next; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 173 | 3 | ▶ | $got_package = _symbol_is_mod_VERSION($value) || next; | ||||||||||||||||||||||||||
|
Condition50%1/2
_symbol_is_mod_VERSION($value) || (next)
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)
| |||||||||||||||||||||||||||||
| 174 | 3 | ▶ | $got_package eq $want_package || next; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 175 | |||||||||||||||||||||||||||||
| 176 | 3 | return 1; | |||||||||||||||||||||||||||
| 177 | } | ||||||||||||||||||||||||||||
| 178 | 16 | return 0; | |||||||||||||||||||||||||||
| 179 | } | ||||||||||||||||||||||||||||
| 180 | |||||||||||||||||||||||||||||
| 181 | 40 | 1; | |||||||||||||||||||||||||||
| 182 | __END__ | ||||||||||||||||||||||||||||