Policy/Modules/ProhibitUseQuotedVersion.pm

filter: Statementstmt 100% 45 / 45 Branchbran 87.5% 7 / 8 Conditioncond 72.7% 8 / 11 MC/DCmcdc 40.0% 2 / 5 Subroutinesub 100% 13 / 13 Podpod 50.0% 1 / 2 totaltotal 90.4% 76 / 84 SCAR 22.0
CC
9
Cov
94%
violates
6.0
elem_is_last_of_statement
4.1
BEGIN
1.0
CRAP 9.0
« Policy/Documentation/RequireFilenameMarkup.pm ↑ Summary Policy/Documentation/RequireEndBeforeLastPod.pm »
1 # Copyright 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::Modules::ProhibitUseQuotedVersion;
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 40 our $VERSION = 99;
27  
28 40 ▶use constant supported_parameters => ();
Subroutine BEGIN: called CC=1 SCAR=0.0
29 40 ▶use constant default_severity => $Perl::Critic::Utils::SEVERITY_MEDIUM;
Subroutine BEGIN: called CC=1 SCAR=0.0
30 40 ▶use constant default_themes => qw(pulp bugs);
Subroutine BEGIN: called CC=1 SCAR=0.0
31 40 ▶use constant applies_to => 'PPI::Statement::Include';
Subroutine BEGIN: called CC=1 SCAR=0.0
32  
33 sub violates {
34 16 ▶ my ($self, $elem, $document) = @_;
Subroutine violates: called CC=6 SCAR=18.0
Pod documented
35  
36 16 ▶ defined($elem->module) || return; # if a "use 5.005" etc
Branch: 1 branch
BranchTrueFalseTotal
defined $elem->module or return 0 16 16
37 16 ▶ my $arg = $elem->schild(2) || return; # if a "use Foo" with no args
Condition50%1/2
$elem->schild(2) || (return)
l!l
160
MC/DC0%0/1
$elem->schild(2) || (return)
$elem->schild(2)
Truth table50%1/2
A
A= $elem->schild(2)
Aresult
11
00
38 16 ▶ $arg->isa('PPI::Token::Quote') || return;
Branch: 1 branch
BranchTrueFalseTotal
$arg->isa("PPI::Token::Quote") or return 10 6 16
39 6 ▶ elem_is_last_of_statement($arg) || return;
Branch: 1 branch
BranchTrueFalseTotal
elem_is_last_of_statement($arg) or return 1 5 6
40  
41 # This is a strict match of what a module version number is like.
42 # Previously looser forms like '.500' were matched too, but of course
43 # unquoting that doesn't give something that's checked by perl itself.
44 # There don't seem to be forms like .500 used in practice, so don't think
45 # it's important.
46 #
47 5 my $str = $arg->string;
48 5 ▶ $str =~ $Perl::Critic::Pulp::Utils::use_module_version_number_re
Branch: 1 branch
BranchTrueFalseTotal
$str =~ /$Perl::Critic::Pulp::Utils::use_module_version_number_re/ or return 1 4 5
49 or return;
50  
51 4 return $self->violation
52 ("Don't use a quoted string version number in a \"use\" statement",
53 '',
54 $arg);
55 }
56  
57 # return true if $elem is the last thing in its statement, apart from an
58 # optional terminating ";"
59 #
60 sub elem_is_last_of_statement {
61 6 ▶ my ($elem) = @_;
Subroutine elem_is_last_of_statement: called CC=4 SCAR=14.0
Pod undocumented
62 6 my $next = $elem->snext_sibling;
63 6 ▶ return (! $next
Condition77%7/9
!$next || $next->isa('PPI::Token::Structure') && $next eq ';' && !$next->snext_sibling
l!l&&r!l&&!r
321
$next->isa("PPI::Token::Structure") and $next eq ";" and not $next->snext_sibling
!ll&&!rl&&r
102
$next->isa("PPI::Token::Structure") and $next eq ";"
!ll&&!rl&&r
102
MC/DC50%2/4
!$next || $next->isa('PPI::Token::Structure') && $next eq ';' && !$next->snext_sibling
!$next $next->isa("PPI::Token::Structure") $next eq ";" not $next->snext_sibling
Truth table60%3/5
not(A) || B and C and not(D)
A= !$nextB= $next->isa("PPI::Token::Structure")C= $next eq ";"D= not $next->snext_sibling
ABCDresult
1XXX1
01111
00XX0
010X0
01100
64 || ($next->isa('PPI::Token::Structure')
65 && $next eq ';'
66 && ! $next->snext_sibling));
67 }
68  
69 40 1;
70 __END__
71  
72 =for stopwords builtin arg ok Ryde representable
73  
74 =head1 NAME
75  
76 Perl::Critic::Policy::Modules::ProhibitUseQuotedVersion - avoid quoted version number string in a "use" statement
77  
78 =head1 DESCRIPTION
79  
80 This policy is part of the L<C<Perl::Critic::Pulp>|Perl::Critic::Pulp>
81 add-on. It asks you not to quote a version number string as the sole
82 argument to a C<use> or C<no> statement.
83  
84 use Foo::Bar '1.50'; # bad
85 use Foo::Bar 1.50; # ok
86  
87 no Abc::Def '2.000_010'; # bad
88 no Abc::Def 2.000_010; # ok
89  
90 The unquoted form uses Perl's builtin module version check (Perl 5.004 up)
91 and is always enforced. The quoted form is passed to the module's
92 C<import()> and relies on it to do the check. If there's no C<import()>
93 then the quoted form is silently ignored.
94  
95 L<C<Exporter>|Exporter> as used by many modules provides an C<import()>
96 which checks a version number arg, so those modules are fine. But the idea
97 of this policy is to do what works always and on that basis is under the
98 "bugs" theme (see L<Perl::Critic/POLICY THEMES>).
99  
100 The builtin module version check is new in Perl 5.004. For earlier versions
101 both forms behave the same, with the string or number going through to the
102 module C<import> and so may or may not be checked. But even in code
103 supporting older Perl it's good to write the unquoted number so later Perl
104 will be certain to enforce it.
105  
106 The policy only applies to a single number string argument, anything else is
107 taken to be a module parameters.
108  
109 no Abc::Def '123', 'ABC'; # ok
110 use lib '..'; # ok
111  
112 If you're a bit nervous about unquoting because floating point version
113 numbers are often not exactly representable in binary, well, yes, that's
114 true, but in practice it works, either by converting the same way everywhere
115 in the program or by treated as a string to the C<version.pm> module anyway.
116  
117 =head2 Disabling
118  
119 If you're confident about the C<import()> in modules you use and prefer the
120 string form you can always disable C<ProhibitUseQuotedVersion> from your
121 F<.perlcriticrc> in the usual way (see L<Perl::Critic/CONFIGURATION>),
122  
123 [-Modules::ProhibitUseQuotedVersion]
124  
125 =head1 SEE ALSO
126  
127 L<Perl::Critic::Pulp>, L<Perl::Critic>
128  
129 L<Perl::Critic::Policy::ValuesAndExpressions::ProhibitVersionStrings>
130  
131 =head1 HOME PAGE
132  
133 L<http://user42.tuxfamily.org/perl-critic-pulp/index.html>
134  
135 =head1 COPYRIGHT
136  
137 Copyright 2009, 2010, 2011, 2012, 2013, 2014, 2015, 2016, 2017, 2019, 2021 Kevin Ryde
138  
139 Perl-Critic-Pulp is free software; you can redistribute it and/or modify it
140 under the terms of the GNU General Public License as published by the Free
141 Software Foundation; either version 3, or (at your option) any later
142 version.
143  
144 Perl-Critic-Pulp is distributed in the hope that it will be useful, but
145 WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
146 or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
147 more details.
148  
149 You should have received a copy of the GNU General Public License along with
150 Perl-Critic-Pulp. If not, see <http://www.gnu.org/licenses/>.
151  
152 =cut
« Policy/Documentation/RequireFilenameMarkup.pm ↑ Summary Policy/Documentation/RequireEndBeforeLastPod.pm »