Policy/Compatibility/ConstantPragmaHash.pm

filter: Statementstmt 100% 62 / 62 Branchbran 86.3% 19 / 22 Conditioncond 66.6% 14 / 21 MC/DCmcdc 41.6% 5 / 12 Subroutinesub 100% 13 / 13 Podpod 100% 1 / 1 totaltotal 87.0% 114 / 131 CC 21 SCAR 25.0
CC
21
Cov
90%
violates
20.2
_use_constant_is_multi
3.0
BEGIN
1.0
CRAP 12.2
« Policy/ValuesAndExpressions/ConstantBeforeLt.pm ↑ Summary Policy/Compatibility/ConstantLeadingUnderscore.pm »
1 # Copyright 2008, 2009, 2010, 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::ConstantPragmaHash;
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 version (); # but don't import qv()
Subroutine BEGIN: called CC=1 SCAR=0.0
25  
26 # uncomment this to run the ### lines
27 # use Smart::Comments;
28  
29 40 our $VERSION = 100;
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 compatibility);
Subroutine BEGIN: called CC=1 SCAR=0.0
34 40 ▶use constant applies_to => 'PPI::Document';
Subroutine BEGIN: called CC=1 SCAR=0.0
35  
36 40 my $perl_ok_version = version->new('5.008');
37 40 my $constant_ok_version = version->new('1.03');
38  
39 26 ▶sub violates {
Subroutine violates: called CC=19 SCAR=30.0
Pod documented
40 26 my ($self, $elem, $document) = @_;
41  
42 26 my @violations;
43 my $perlver; # a "version" object
44 26 my $modver; # a "version" object
45  
46 26 ▶ my $aref = $document->find ('PPI::Statement::Include')
Condition100%2/2
$document->find('PPI::Statement::Include') || (return)
l!l
251
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
47 || return; # if no includes at all
48 25 foreach my $inc (@$aref) {
49  
50 39 ▶ $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 37 2 39
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
3252
$inc->type eq "require" and Perl::Critic::Pulp::Utils::elem_in_BEGIN($inc)
!ll&&!rl&&r
025
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
51 || ($inc->type eq 'require'
52 && Perl::Critic::Pulp::Utils::elem_in_BEGIN($inc))
53 || next;
54  
55 37 ▶ if (my $ver = $inc->version) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $ver = $inc->version) 9 28 37
56 # "use 5.008" etc perl version
57 9 $ver = version->new ($ver);
58 9 ▶ if (! defined $perlver || $ver > $perlver) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $perlver or $ver > $perlver) 9 0 9
Condition33%1/3
not defined $perlver or $ver > $perlver
l!l&&r!l&&!r
900
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
59 9 $perlver = $ver;
60  
61 9 ▶ if ($perlver >= $perl_ok_version) {
Branch: 1 branch
BranchTrueFalseTotal
if ($perlver >= $perl_ok_version) 8 1 9
62 # adequate perl version demanded, stop here
63 8 last;
64 }
65 }
66 1 next;
67 }
68  
69 28 ▶ ($inc->module||'') eq 'constant' || next;
Branch: 1 branch
BranchTrueFalseTotal
($inc->module || "") eq "constant" or next 28 0 28
Condition50%1/2
$inc->module || ''
l!l
280
MC/DC0%0/1
$inc->module || ''
$inc->module
Truth table50%1/2
A
A= $inc->module
Aresult
11
00
70  
71 28 ▶ 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)) 9 19 28
72 ### $ver
73 # PPI::Token::Number::Float
74 9 $ver = version->new ($ver->content);
75 9 ▶ if (! defined $modver || $ver > $modver) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $modver or $ver > $modver) 9 0 9
Condition33%1/3
not defined $modver or $ver > $modver
l!l&&r!l&&!r
900
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
76 9 $modver = $ver;
77  
78 9 ▶ if ($modver >= $constant_ok_version) {
Branch: 1 branch
BranchTrueFalseTotal
if ($modver >= $constant_ok_version) 7 2 9
79 # adequate "constant" version demanded, stop here
80 7 last;
81 }
82 }
83 }
84  
85 21 ▶ if (_use_constant_is_multi ($inc)) {
Branch: 1 branch
BranchTrueFalseTotal
if (_use_constant_is_multi($inc)) 15 6 21
86 15 ▶ push @violations, $self->violation
Branch: 2 branches
BranchTrueFalseTotal
defined $perlver ? : 1 14 15
defined $modver ? : 2 13 15
87 ("'use constant' with multi-constant hash requires perl 5.8 or constant 1.03 (at this point have "
88 . (defined $perlver ? "perl $perlver" : "no perl version")
89 . (defined $modver ? ", constant $modver)" : ", no constant version)"),
90 '',
91 $inc);
92 }
93 }
94  
95 25 return @violations;
96 }
97  
98 # $inc is a PPI::Statement::Include with type "use" and module "constant".
99 # Return true if it has a multi-constant hash as its argument like
100 # "use constant { X => 1 };"
101 #
102 # The plain "use constant { x=>1 }" comes out as
103 #
104 # PPI::Statement::Include
105 # PPI::Token::Word 'use'
106 # PPI::Token::Word 'constant'
107 # PPI::Structure::Constructor { ... }
108 # PPI::Statement
109 # PPI::Token::Word 'x'
110 # PPI::Token::Operator '=>'
111 # PPI::Token::Number '1'
112 #
113 # Or as of PPI 1.203 with a version number "use constant 1.03 { x=>1 }" is
114 # different
115 #
116 # PPI::Statement::Include
117 # PPI::Token::Word 'use'
118 # PPI::Token::Word 'constant'
119 # PPI::Token::Number::Float '1.03'
120 # PPI::Structure::Block { ... }
121 # PPI::Statement
122 # PPI::Token::Word 'x'
123 # PPI::Token::Operator '=>'
124 # PPI::Token::Number '1'
125 #
126 49 ▶sub _use_constant_is_multi {
Subroutine _use_constant_is_multi: called CC=3 SCAR=11.0
127 49 my ($inc) = @_;
128 49 ▶ my $arg = Perl::Critic::Pulp::Utils::include_module_first_arg ($inc)
Condition100%2/2
Perl::Critic::Pulp::Utils::include_module_first_arg($inc) || (return 0)
l!l
427
MC/DC100%1/1
Perl::Critic::Pulp::Utils::include_module_first_arg($inc) || (return 0)
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
129 || return 0; # empty "use constant" or version "use constant 1.05"
130 42 ▶ return ($arg->isa('PPI::Structure::Constructor') # without version number
Condition66%2/3
$arg->isa('PPI::Structure::Constructor') || $arg->isa('PPI::Structure::Block')
l!l&&r!l&&!r
23019
MC/DC50%1/2
$arg->isa('PPI::Structure::Constructor') || $arg->isa('PPI::Structure::Block')
$arg->isa('PPI::Structure::Constructor') $arg->isa('PPI::Structure::Block')
Truth table66%2/3
A || B
A= $arg->isa('PPI::Structure::Constructor')B= $arg->isa('PPI::Structure::Block')
ABresult
1X1
011
000
131 || $arg->isa('PPI::Structure::Block')); # with version number
132 }
133  
134  
135 40 1;
136 __END__
« Policy/ValuesAndExpressions/ConstantBeforeLt.pm ↑ Summary Policy/Compatibility/ConstantLeadingUnderscore.pm »