« Policy/Compatibility/ConstantPragmaHash.pm
↑ Summary
Policy/ValuesAndExpressions/RequireNumericVersion.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::Compatibility::ConstantLeadingUnderscore; | ||||||||||||||||||||||||||||||||||||||||||
| 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 base 'Perl::Critic::Policy'; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 22 | 40 | ▶ | use Perl::Critic::Utils; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 23 | 40 | ▶ | use Perl::Critic::Pulp::Utils; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 24 | 40 | ▶ | use Perl::Critic::Policy::Miscellanea::TextDomainPlaceholders; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 25 | 40 | ▶ | use version (); # but don't import qv() | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 26 | |||||||||||||||||||||||||||||||||||||||||||
| 27 | # uncomment this to run the ### lines | ||||||||||||||||||||||||||||||||||||||||||
| 28 | # use Smart::Comments; | ||||||||||||||||||||||||||||||||||||||||||
| 29 | |||||||||||||||||||||||||||||||||||||||||||
| 30 | 40 | our $VERSION = 100; | |||||||||||||||||||||||||||||||||||||||||
| 31 | |||||||||||||||||||||||||||||||||||||||||||
| 32 | 40 | ▶ | use constant supported_parameters => (); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 33 | 40 | ▶ | use constant default_severity => $Perl::Critic::Utils::SEVERITY_MEDIUM; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 34 | 40 | ▶ | use constant default_themes => qw(pulp compatibility); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 35 | 40 | ▶ | use constant applies_to => 'PPI::Document'; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 36 | |||||||||||||||||||||||||||||||||||||||||||
| 37 | 40 | my $perl_ok_version = version->new('5.006'); | |||||||||||||||||||||||||||||||||||||||||
| 38 | 40 | my $constant_ok_version = version->new('1.02'); | |||||||||||||||||||||||||||||||||||||||||
| 39 | |||||||||||||||||||||||||||||||||||||||||||
| 40 | 28 | ▶ | sub violates { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
violates: called CC=20 SCAR=30.5
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 41 | 28 | my ($self, $elem, $document) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 42 | |||||||||||||||||||||||||||||||||||||||||||
| 43 | 28 | my @violations; | |||||||||||||||||||||||||||||||||||||||||
| 44 | my $perlver; # a "version" object | ||||||||||||||||||||||||||||||||||||||||||
| 45 | 28 | my $modver; # a "version" object | |||||||||||||||||||||||||||||||||||||||||
| 46 | |||||||||||||||||||||||||||||||||||||||||||
| 47 | 28 | ▶ | my $aref = $document->find ('PPI::Statement::Include') | ||||||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
$document->find('PPI::Statement::Include') || (return)
MC/DC100%1/1
$document->find('PPI::Statement::Include') || (return)
$document->find('PPI::Statement::Include')
Truth table100%2/2
A
A= $document->find('PPI::Statement::Include')
| |||||||||||||||||||||||||||||||||||||||||||
| 48 | || return; # if no includes at all | ||||||||||||||||||||||||||||||||||||||||||
| 49 | 27 | foreach my $inc (@$aref) { | |||||||||||||||||||||||||||||||||||||||||
| 50 | |||||||||||||||||||||||||||||||||||||||||||
| 51 | 47 | ▶ | $inc->type eq 'use' | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition83%5/6
$inc->type eq "use" or $inc->type eq "require" and Perl::Critic::Pulp::Utils::elem_in_BEGIN($inc)
$inc->type eq "require" and Perl::Critic::Pulp::Utils::elem_in_BEGIN($inc)
MC/DC66%2/3
$inc->type eq "use" or $inc->type eq "require" and Perl::Critic::Pulp::Utils::elem_in_BEGIN($inc)
$inc->type eq "use"
$inc->type eq "require"
Perl::Critic::Pulp::Utils::elem_in_BEGIN($inc)
Truth table75%3/4
A or B and C
A= $inc->type eq "use"B= $inc->type eq "require"C= Perl::Critic::Pulp::Utils::elem_in_BEGIN($inc)
| |||||||||||||||||||||||||||||||||||||||||||
| 52 | || ($inc->type eq 'require' | ||||||||||||||||||||||||||||||||||||||||||
| 53 | && Perl::Critic::Pulp::Utils::elem_in_BEGIN($inc)) | ||||||||||||||||||||||||||||||||||||||||||
| 54 | || next; | ||||||||||||||||||||||||||||||||||||||||||
| 55 | |||||||||||||||||||||||||||||||||||||||||||
| 56 | 46 | ▶ | if (my $ver = $inc->version) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 57 | # "use 5.006" etc perl version | ||||||||||||||||||||||||||||||||||||||||||
| 58 | 10 | $ver = version->new ($ver); | |||||||||||||||||||||||||||||||||||||||||
| 59 | 10 | ▶ | if (! defined $perlver || $ver > $perlver) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
not defined $perlver or $ver > $perlver
MC/DC0%0/2
not defined $perlver or $ver > $perlver
not defined $perlver
$ver > $perlver
Truth table33%1/3
not(A) or B
A= not defined $perlverB= $ver > $perlver
| |||||||||||||||||||||||||||||||||||||||||||
| 60 | 10 | $perlver = $ver; # maximum seen so-far | |||||||||||||||||||||||||||||||||||||||||
| 61 | |||||||||||||||||||||||||||||||||||||||||||
| 62 | 10 | ▶ | if ($perlver >= $perl_ok_version) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 63 | # adequate perl version demanded, stop here | ||||||||||||||||||||||||||||||||||||||||||
| 64 | 7 | last; | |||||||||||||||||||||||||||||||||||||||||
| 65 | } | ||||||||||||||||||||||||||||||||||||||||||
| 66 | } | ||||||||||||||||||||||||||||||||||||||||||
| 67 | 3 | next; | |||||||||||||||||||||||||||||||||||||||||
| 68 | } | ||||||||||||||||||||||||||||||||||||||||||
| 69 | |||||||||||||||||||||||||||||||||||||||||||
| 70 | 36 | ▶ | ($inc->module||'') eq 'constant' || next; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition50%1/2
$inc->module || ''
MC/DC0%0/1
$inc->module || ''
$inc->module
Truth table50%1/2
A
A= $inc->module
| |||||||||||||||||||||||||||||||||||||||||||
| 71 | |||||||||||||||||||||||||||||||||||||||||||
| 72 | 36 | ▶ | if (my $ver = Perl::Critic::Pulp::Utils::include_module_version ($inc)) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 73 | ### $ver | ||||||||||||||||||||||||||||||||||||||||||
| 74 | # PPI::Token::Number::Float | ||||||||||||||||||||||||||||||||||||||||||
| 75 | 11 | $ver = version->new ($ver->content); | |||||||||||||||||||||||||||||||||||||||||
| 76 | 11 | ▶ | if (! defined $modver || $ver > $modver) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
not defined $modver or $ver > $modver
MC/DC0%0/2
not defined $modver or $ver > $modver
not defined $modver
$ver > $modver
Truth table33%1/3
not(A) or B
A= not defined $modverB= $ver > $modver
| |||||||||||||||||||||||||||||||||||||||||||
| 77 | 11 | $modver = $ver; | |||||||||||||||||||||||||||||||||||||||||
| 78 | |||||||||||||||||||||||||||||||||||||||||||
| 79 | 11 | ▶ | if ($modver >= $constant_ok_version) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 80 | # adequate "constant" version demanded, stop here | ||||||||||||||||||||||||||||||||||||||||||
| 81 | 5 | last; | |||||||||||||||||||||||||||||||||||||||||
| 82 | } | ||||||||||||||||||||||||||||||||||||||||||
| 83 | } | ||||||||||||||||||||||||||||||||||||||||||
| 84 | } | ||||||||||||||||||||||||||||||||||||||||||
| 85 | |||||||||||||||||||||||||||||||||||||||||||
| 86 | 31 | my $name = _use_constant_single_name ($inc); | |||||||||||||||||||||||||||||||||||||||||
| 87 | 31 | ▶ | if (defined $name && $name =~ /^_/) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
defined $name and $name =~ /^_/
MC/DC100%2/2
defined $name and $name =~ /^_/
defined $name
$name =~ /^_/
Truth table100%3/3
A and B
A= defined $nameB= $name =~ /^_/
| |||||||||||||||||||||||||||||||||||||||||||
| 88 | 20 | ▶ | push @violations, $self->violation | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||
| 89 | ("'use constant' with leading underscore requires perl 5.6 or constant 1.02 (at this point have " | ||||||||||||||||||||||||||||||||||||||||||
| 90 | . (defined $perlver ? "perl $perlver" : "no perl version") | ||||||||||||||||||||||||||||||||||||||||||
| 91 | . (defined $modver ? ", constant $modver)" : ", no constant version)"), | ||||||||||||||||||||||||||||||||||||||||||
| 92 | '', | ||||||||||||||||||||||||||||||||||||||||||
| 93 | $inc); | ||||||||||||||||||||||||||||||||||||||||||
| 94 | } | ||||||||||||||||||||||||||||||||||||||||||
| 95 | } | ||||||||||||||||||||||||||||||||||||||||||
| 96 | |||||||||||||||||||||||||||||||||||||||||||
| 97 | 27 | return @violations; | |||||||||||||||||||||||||||||||||||||||||
| 98 | } | ||||||||||||||||||||||||||||||||||||||||||
| 99 | |||||||||||||||||||||||||||||||||||||||||||
| 100 | # $inc is a PPI::Statement::Include with type "use" and module "constant". | ||||||||||||||||||||||||||||||||||||||||||
| 101 | # If it's a single-name "use constant foo => ..." then return the name | ||||||||||||||||||||||||||||||||||||||||||
| 102 | # string "foo". If it's a multi-constant or something unrecognised then | ||||||||||||||||||||||||||||||||||||||||||
| 103 | # return undef.. | ||||||||||||||||||||||||||||||||||||||||||
| 104 | # | ||||||||||||||||||||||||||||||||||||||||||
| 105 | 71 | ▶ | sub _use_constant_single_name { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_use_constant_single_name: called CC=8 SCAR=20.8
| |||||||||||||||||||||||||||||||||||||||||||
| 106 | 71 | my ($inc) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 107 | 71 | ▶ | my $arg = Perl::Critic::Pulp::Utils::include_module_first_arg ($inc) | ||||||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
Perl::Critic::Pulp::Utils::include_module_first_arg($inc) || (return undef)
MC/DC100%1/1
Perl::Critic::Pulp::Utils::include_module_first_arg($inc) || (return undef)
Perl::Critic::Pulp::Utils::include_module_first_arg($inc)
Truth table100%2/2
A
A= Perl::Critic::Pulp::Utils::include_module_first_arg($inc)
| |||||||||||||||||||||||||||||||||||||||||||
| 108 | || return undef; # empty "use constant" or version "use constant 1.05" | ||||||||||||||||||||||||||||||||||||||||||
| 109 | |||||||||||||||||||||||||||||||||||||||||||
| 110 | 61 | ▶ | if ($arg->isa('PPI::Token::Word')) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 111 | # use constant FOO ... | ||||||||||||||||||||||||||||||||||||||||||
| 112 | 28 | return $arg->content; | |||||||||||||||||||||||||||||||||||||||||
| 113 | } | ||||||||||||||||||||||||||||||||||||||||||
| 114 | 33 | ▶ | if ($arg->isa('PPI::Token::Quote::Single') | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$arg->isa("PPI::Token::Quote::Single") or $arg->isa("PPI::Token::Quote::Literal")
MC/DC100%2/2
$arg->isa("PPI::Token::Quote::Single") or $arg->isa("PPI::Token::Quote::Literal")
$arg->isa("PPI::Token::Quote::Single")
$arg->isa("PPI::Token::Quote::Literal")
Truth table100%3/3
A or B
A= $arg->isa("PPI::Token::Quote::Single")B= $arg->isa("PPI::Token::Quote::Literal")
| |||||||||||||||||||||||||||||||||||||||||||
| 115 | || $arg->isa('PPI::Token::Quote::Literal')) { | ||||||||||||||||||||||||||||||||||||||||||
| 116 | # use constant 'FOO', ... | ||||||||||||||||||||||||||||||||||||||||||
| 117 | # use constant q{FOO}, ... | ||||||||||||||||||||||||||||||||||||||||||
| 118 | 10 | return $arg->literal; | |||||||||||||||||||||||||||||||||||||||||
| 119 | } | ||||||||||||||||||||||||||||||||||||||||||
| 120 | 23 | ▶ | if ($arg->isa('PPI::Token::Quote::Double') | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$arg->isa("PPI::Token::Quote::Double") or $arg->isa("PPI::Token::Quote::Interpolate")
MC/DC100%2/2
$arg->isa("PPI::Token::Quote::Double") or $arg->isa("PPI::Token::Quote::Interpolate")
$arg->isa("PPI::Token::Quote::Double")
$arg->isa("PPI::Token::Quote::Interpolate")
Truth table100%3/3
A or B
A= $arg->isa("PPI::Token::Quote::Double")B= $arg->isa("PPI::Token::Quote::Interpolate")
| |||||||||||||||||||||||||||||||||||||||||||
| 121 | || $arg->isa('PPI::Token::Quote::Interpolate')) { | ||||||||||||||||||||||||||||||||||||||||||
| 122 | # ENHANCE-ME: use $arg->interpolations() when available also on | ||||||||||||||||||||||||||||||||||||||||||
| 123 | # PPI::Token::Quote::Interpolate | ||||||||||||||||||||||||||||||||||||||||||
| 124 | 9 | my $str = $arg->string; | |||||||||||||||||||||||||||||||||||||||||
| 125 | 9 | ▶ | if (! Perl::Critic::Policy::Miscellanea::TextDomainPlaceholders::_string_any_vars($str)) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 126 | # use constant "FOO", ... | ||||||||||||||||||||||||||||||||||||||||||
| 127 | # use constant qq{FOO}, ... | ||||||||||||||||||||||||||||||||||||||||||
| 128 | # not quite right, but often close enough | ||||||||||||||||||||||||||||||||||||||||||
| 129 | 9 | return $str; | |||||||||||||||||||||||||||||||||||||||||
| 130 | } | ||||||||||||||||||||||||||||||||||||||||||
| 131 | } | ||||||||||||||||||||||||||||||||||||||||||
| 132 | # a hash or an expression or something unrecognised | ||||||||||||||||||||||||||||||||||||||||||
| 133 | 14 | return undef; | |||||||||||||||||||||||||||||||||||||||||
| 134 | } | ||||||||||||||||||||||||||||||||||||||||||
| 135 | |||||||||||||||||||||||||||||||||||||||||||
| 136 | # $str is the contents of a "" or qq{} string | ||||||||||||||||||||||||||||||||||||||||||
| 137 | # return true if it has any $ or @ interpolation forms | ||||||||||||||||||||||||||||||||||||||||||
| 138 | 0 | ▶ | sub _string_any_vars { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_string_any_vars: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||||||
| 139 | 0 | my ($str) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 140 | 0 | return ($str =~ /(^|[^\\])(\\\\)*[\$@]/); | |||||||||||||||||||||||||||||||||||||||||
| 141 | } | ||||||||||||||||||||||||||||||||||||||||||
| 142 | |||||||||||||||||||||||||||||||||||||||||||
| 143 | 40 | 1; | |||||||||||||||||||||||||||||||||||||||||
| 144 | __END__ | ||||||||||||||||||||||||||||||||||||||||||