Policy/Compatibility/ConstantLeadingUnderscore.pm

filter: Statementstmt 97.3% 74 / 76 Branchbran 86.6% 26 / 30 Conditioncond 77.7% 21 / 27 MC/DCmcdc 62.5% 10 / 16 Subroutinesub 93.3% 14 / 15 Podpod 100% 1 / 1 totaltotal 88.4% 146 / 165 CC 27 SCAR 25.0
CC
27
Cov
91%
violates
21.1
_use_constant_single_name
8.0
_string_any_vars
2.0
CRAP 12.2
« 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)
l!l
271
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')
Aresult
11
00
48 || return; # if no includes at all
49 27 foreach my $inc (@$aref) {
50  
51 47 ▶ $inc->type eq 'use'
Branch: 1 branch
BranchTrueFalseTotal
$inc->type eq "use" or $inc->type eq "require" and Perl::Critic::Pulp::Utils::elem_in_BEGIN($inc) or next 46 1 47
Condition83%5/6
$inc->type eq "use" or $inc->type eq "require" and Perl::Critic::Pulp::Utils::elem_in_BEGIN($inc)
l!l&&r!l&&!r
4241
$inc->type eq "require" and Perl::Critic::Pulp::Utils::elem_in_BEGIN($inc)
!ll&&!rl&&r
014
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)
ABCresult
1XX1
0111
00X0
0100
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
BranchTrueFalseTotal
if (my $ver = $inc->version) 10 36 46
57 # "use 5.006" etc perl version
58 10 $ver = version->new ($ver);
59 10 ▶ if (! defined $perlver || $ver > $perlver) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $perlver or $ver > $perlver) 10 0 10
Condition33%1/3
not defined $perlver or $ver > $perlver
l!l&&r!l&&!r
1000
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
ABresult
1X1
011
000
60 10 $perlver = $ver; # maximum seen so-far
61  
62 10 ▶ if ($perlver >= $perl_ok_version) {
Branch: 1 branch
BranchTrueFalseTotal
if ($perlver >= $perl_ok_version) 7 3 10
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
BranchTrueFalseTotal
($inc->module || "") eq "constant" or next 36 0 36
Condition50%1/2
$inc->module || ''
l!l
360
MC/DC0%0/1
$inc->module || ''
$inc->module
Truth table50%1/2
A
A= $inc->module
Aresult
11
00
71  
72 36 ▶ if (my $ver = Perl::Critic::Pulp::Utils::include_module_version ($inc)) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $ver = Perl::Critic::Pulp::Utils::include_module_version($inc)) 11 25 36
73 ### $ver
74 # PPI::Token::Number::Float
75 11 $ver = version->new ($ver->content);
76 11 ▶ if (! defined $modver || $ver > $modver) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $modver or $ver > $modver) 11 0 11
Condition33%1/3
not defined $modver or $ver > $modver
l!l&&r!l&&!r
1100
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
ABresult
1X1
011
000
77 11 $modver = $ver;
78  
79 11 ▶ if ($modver >= $constant_ok_version) {
Branch: 1 branch
BranchTrueFalseTotal
if ($modver >= $constant_ok_version) 5 6 11
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
BranchTrueFalseTotal
if (defined $name and $name =~ /^_/) 20 11 31
Condition100%3/3
defined $name and $name =~ /^_/
!ll&&!rl&&r
4720
MC/DC100%2/2
defined $name and $name =~ /^_/
defined $name $name =~ /^_/
Truth table100%3/3
A and B
A= defined $nameB= $name =~ /^_/
ABresult
0X0
100
111
88 20 ▶ push @violations, $self->violation
Branch: 2 branches
BranchTrueFalseTotal
defined $perlver ? : 2 18 20
defined $modver ? : 3 17 20
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)
l!l
6110
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)
Aresult
11
00
108 || return undef; # empty "use constant" or version "use constant 1.05"
109  
110 61 ▶ if ($arg->isa('PPI::Token::Word')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($arg->isa("PPI::Token::Word")) 28 33 61
111 # use constant FOO ...
112 28 return $arg->content;
113 }
114 33 ▶ if ($arg->isa('PPI::Token::Quote::Single')
Branch: 1 branch
BranchTrueFalseTotal
if ($arg->isa("PPI::Token::Quote::Single") or $arg->isa("PPI::Token::Quote::Literal")) 10 23 33
Condition100%3/3
$arg->isa("PPI::Token::Quote::Single") or $arg->isa("PPI::Token::Quote::Literal")
l!l&&r!l&&!r
6423
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")
ABresult
1X1
011
000
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
BranchTrueFalseTotal
if ($arg->isa("PPI::Token::Quote::Double") or $arg->isa("PPI::Token::Quote::Interpolate")) 9 14 23
Condition100%3/3
$arg->isa("PPI::Token::Quote::Double") or $arg->isa("PPI::Token::Quote::Interpolate")
l!l&&r!l&&!r
5414
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")
ABresult
1X1
011
000
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
BranchTrueFalseTotal
unless (Perl::Critic::Policy::Miscellanea::TextDomainPlaceholders::_string_any_vars($str)) 9 0 9
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__
« Policy/Compatibility/ConstantPragmaHash.pm ↑ Summary Policy/ValuesAndExpressions/RequireNumericVersion.pm »