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 SCAR 30.6
CC
21
Cov
90%
violates
20.2
_use_constant_is_multi
3.0
BEGIN
1.0
CRAP 21.4
« Policy/Documentation/ProhibitDuplicateHeadings.pm ↑ Summary Policy/Documentation/ProhibitAdjacentLinks.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 = 99;
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 sub violates {
40 26 ▶ my ($self, $elem, $document) = @_;
Subroutine violates: called CC=19 SCAR=30.0
Pod documented
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 2 37 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 0 28 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 sub _use_constant_is_multi {
127 49 ▶ my ($inc) = @_;
Subroutine _use_constant_is_multi: called CC=3 SCAR=11.0
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__
137  
138 =for stopwords multi-constant CPAN perl ok ConstantPragmaHash backports prereqs Ryde
139  
140 =head1 NAME
141  
142 Perl::Critic::Policy::Compatibility::ConstantPragmaHash - new enough "constant" module for multiple constants
143  
144 =head1 DESCRIPTION
145  
146 This policy is part of the L<C<Perl::Critic::Pulp>|Perl::Critic::Pulp>
147 add-on. It requires that when you use the hash style multiple constants of
148 C<use constant> that you explicitly declare either Perl 5.8 or C<constant>
149 1.03 or higher.
150  
151 use constant { AA => 1, BB => 2 }; # bad
152  
153 use 5.008;
154 use constant { CC => 1, DD => 2 }; # ok
155  
156 use constant 1.03;
157 use constant { EE => 1, FF => 2 }; # ok
158  
159 use constant 1.03 { GG => 1, HH => 2 }; # ok
160  
161 The idea is to keep you from using the multi-constant feature in code which
162 might run on Perl 5.6, or might in principle still run there. On that basis
163 this policy is under the "compatibility" theme (see L<Perl::Critic/POLICY
164 THEMES>).
165  
166 If you declare C<constant 1.03> then the code can still run on Perl 5.6 and
167 perhaps earlier if the user gets a suitably newer C<constant> module from
168 CPAN. Or of course for past compatibility just don't use the hash style at
169 all!
170  
171 =head2 Details
172  
173 A version declaration must be before the first multi-constant, so it's
174 checked before the multi-constant is attempted and gives an obscure error.
175  
176 use constant { X => 1, Y => 2 }; # bad
177 use 5.008;
178  
179 A C<require> for the perl version is not enough since C<use constant> is at
180 C<BEGIN> time, before plain code.
181  
182 require 5.008; # doesn't run early enough
183 use constant { X => 1, Y => 2 }; # bad
184  
185 But a C<require> within a C<BEGIN> block is ok (a past style, still found
186 occasionally).
187  
188 BEGIN { require 5.008 }
189 use constant { X => 1, Y => 2 }; # ok
190  
191 BEGIN {
192 require 5.008;
193 and_other_setups ...;
194 }
195 use constant { X => 1, Y => 2 }; # ok
196  
197 Currently C<ConstantPragmaHash> pays no attention to any conditionals within
198 the C<BEGIN>, it assumes any C<require> there always runs. It could be
199 tricked by some obscure tests but hopefully anything like that is rare or
200 does the right thing anyway.
201  
202 A quoted version number like
203  
204 use constant '1.03'; # no good
205  
206 is no good, only a bare number is recognised by C<use> and acted on by
207 ConstantPragmaHash. A string like that goes through to C<constant> as if a
208 name to define (which you'll see it objects to as soon as you try run it).
209  
210 =head2 Drawbacks
211  
212 Explicitly adding required version numbers in the code can be irritating,
213 especially if other things you're doing only run on 5.8 up anyway. But
214 declaring what code needs is accurate, it allows maybe for backports of
215 modules, and explicit versions can be grepped out to create or check
216 F<Makefile.PL> or F<Build.PL> prereqs.
217  
218 As always if you don't care about this or if you only ever use Perl 5.8
219 anyway then you can disable C<ConstantPragmaHash> from your F<.perlcriticrc>
220 in the usual way (see L<Perl::Critic/CONFIGURATION>),
221  
222 [-Compatibility::ConstantPragmaHash]
223  
224 =head1 SEE ALSO
225  
226 L<Perl::Critic::Pulp>,
227 L<Perl::Critic>,
228 L<Perl::Critic::Policy::Compatibility::ConstantLeadingUnderscore>,
229 L<Perl::Critic::Policy::ValuesAndExpressions::ProhibitConstantPragma>,
230 L<Perl::Critic::Policy::Modules::RequirePerlVersion>
231  
232 L<constant>,
233 L<perlsub/Constant Functions>
234  
235 =head1 HOME PAGE
236  
237 L<http://user42.tuxfamily.org/perl-critic-pulp/index.html>
238  
239 =head1 COPYRIGHT
240  
241 Copyright 2008, 2009, 2010, 2011, 2012, 2013, 2014, 2015, 2016, 2017, 2019, 2021 Kevin Ryde
242  
243 Perl-Critic-Pulp is free software; you can redistribute it and/or modify it
244 under the terms of the GNU General Public License as published by the Free
245 Software Foundation; either version 3, or (at your option) any later
246 version.
247  
248 Perl-Critic-Pulp is distributed in the hope that it will be useful, but
249 WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
250 or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
251 more details.
252  
253 You should have received a copy of the GNU General Public License along with
254 Perl-Critic-Pulp. If not, see <http://www.gnu.org/licenses/>.
255  
256 =cut
« Policy/Documentation/ProhibitDuplicateHeadings.pm ↑ Summary Policy/Documentation/ProhibitAdjacentLinks.pm »