Policy/ValuesAndExpressions/ConstantBeforeLt.pm

filter: Statementstmt 100% 97 / 97 Branchbran 90.7% 49 / 54 Conditioncond 81.8% 9 / 11 MC/DCmcdc 66.6% 4 / 6 Subroutinesub 100% 15 / 15 Podpod 100% 1 / 1 totaltotal 95.1% 175 / 184 CC 32 SCAR 25.4
CC
32
Cov
96%
_use_constants
21.2
_one_violate
11.0
_elem_and_ssiblings
2.0
CRAP 12.7
« Policy/Miscellanea/TextDomainPlaceholders.pm ↑ Summary Policy/Compatibility/ConstantPragmaHash.pm »
1 # Copyright 2008, 2009, 2010, 2011, 2012, 2013, 2014, 2015, 2016, 2017, 2019, 2021 Kevin Ryde
2  
3 # This file is part of Perl-Critic-Pulp.
4  
5 # Perl-Critic-Pulp is free software; you can redistribute it and/or
6 # modify it under the terms of the GNU General Public License as published
7 # by the Free Software Foundation; either version 3, or (at your option) any
8 # later version.
9 #
10 # Perl-Critic-Pulp is distributed in the hope that it will be
11 # useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
13 # Public License for more details.
14 #
15 # You should have received a copy of the GNU General Public License along
16 # with Perl-Critic-Pulp. If not, see
17 # <http://www.gnu.org/licenses/>.
18  
19  
20 package Perl::Critic::Policy::ValuesAndExpressions::ConstantBeforeLt;
21 40 ▶use 5.006;
Subroutine BEGIN: called CC=1 SCAR=0.0
22 40 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
23 40 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
24 40 ▶use PPI 1.220; # for its incompatible change to PPI::Statement::Sub->prototype
Subroutine BEGIN: called CC=1 SCAR=0.0
25 40 ▶use base 'Perl::Critic::Policy';
Subroutine BEGIN: called CC=1 SCAR=0.0
26 40 ▶use Perl::Critic::Utils qw(is_included_module_name
Subroutine BEGIN: called CC=1 SCAR=0.0
27 is_method_call
28 is_perl_builtin_with_no_arguments
29 40 split_nodes_on_comma);
30  
31 # uncomment this to run the ### lines
32 # use Smart::Comments;
33  
34 40 our $VERSION = 100;
35  
36 #
37 # Incidentally "require Foo < 123" is a similar sort of problem in all Perls
38 # (or at least up to 5.10.0) with "<" being taken to be a "< >". But since
39 # it always provokes a warning when run it doesn't really need perlcritic,
40 # or if it does then leave it to another policy to address.
41 #
42  
43 40 ▶use constant supported_parameters => ();
Subroutine BEGIN: called CC=1 SCAR=0.0
44 40 ▶use constant default_severity => $Perl::Critic::Utils::SEVERITY_MEDIUM;
Subroutine BEGIN: called CC=1 SCAR=0.0
45 40 ▶use constant default_themes => qw(pulp bugs);
Subroutine BEGIN: called CC=1 SCAR=0.0
46 40 ▶use constant applies_to => ('PPI::Document');
Subroutine BEGIN: called CC=1 SCAR=0.0
47  
48 14 ▶sub violates {
Subroutine violates: called CC=1 SCAR=0.0
Pod documented
49 14 my ($self, $document) = @_;
50  
51 14 my @violations;
52 my %constants;
53 14 my $constants = \%constants;
54 $document->find
55 246 ▶ (sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
56 246 my ($document, $elem) = @_;
57 246 @constants{ _use_constants($elem) } = 1; # hash slice
58 246 push @violations, _one_violate ($self, $elem, $constants);
59 246 return 0; # no-match, and continue
60 14 });
61 14 return @violations;
62 }
63  
64 246 ▶sub _one_violate {
Subroutine _one_violate: called CC=11 SCAR=24.0
65 246 my ($self, $elem, $constants) = @_;
66 246 ▶ if (! $elem->isa ('PPI::Token::Word')) { return; }
Branch: 1 branch
BranchTrueFalseTotal
unless ($elem->isa("PPI::Token::Word")) 203 43 246
67  
68 # eg. "use constant FOO => 123; if (FOO < 456) {}" is ok, for a constant
69 # defined at the point in question
70 43 ▶ if (exists $constants->{$elem->content}) { return; }
Branch: 1 branch
BranchTrueFalseTotal
if (exists $$constants{$elem->content}) 11 32 43
71  
72 # eg "time < 123" is ok
73 32 ▶ if (is_perl_builtin_with_no_arguments ($elem)) { return; }
Branch: 1 branch
BranchTrueFalseTotal
if (is_perl_builtin_with_no_arguments($elem)) 1 31 32
74  
75 # eg. "bar" in "$foo->bar < 123" is ok
76 31 ▶ if (is_method_call ($elem)) { return; }
Branch: 1 branch
BranchTrueFalseTotal
if (is_method_call($elem)) 3 28 31
77  
78 # eg. "Foo" in "require Foo" is not a constant
79 28 ▶ if (is_included_module_name ($elem)) { return; }
Branch: 1 branch
BranchTrueFalseTotal
if (is_included_module_name($elem)) 7 21 28
80  
81  
82 # must be followed by "<" like "MYBAREWORD < 123"
83 21 ▶ my $lt = $elem->snext_sibling or return;
Branch: 1 branch
BranchTrueFalseTotal
my $lt = $elem->snext_sibling or return 21 0 21
84 21 ▶ $lt->isa('PPI::Token::Operator') or return;
Branch: 1 branch
BranchTrueFalseTotal
$lt->isa("PPI::Token::Operator") or return 10 11 21
85 10 ▶ $lt->content eq '<' or return;
Branch: 1 branch
BranchTrueFalseTotal
$lt->content eq "<" or return 6 4 10
86  
87 # if a ">" somewhere later like "foo <...>" then it's probably a function
88 # call on a readline or glob
89 #
90 6 my $after = $lt;
91 6 for (;;) {
92 16 ▶ $after = $after->snext_sibling or last;
Branch: 1 branch
BranchTrueFalseTotal
$after = $after->snext_sibling or last 11 5 16
93 11 ▶ if ($after->content eq '>') {
Branch: 1 branch
BranchTrueFalseTotal
if ($after->content eq ">") 1 10 11
94 1 return;
95 }
96 }
97  
98 5 return $self->violation ('Bareword constant before "<"',
99 '', $elem);
100 }
101  
102 # $elem is any element. If it's a "use constants" or a "sub foo () { ...}"
103 # then return the name or names of the constants so created. Otherwise
104 # return an empty list.
105 #
106 # Perl::Critic::StricterSubs::Utils::find_declared_constant_names() does
107 # some similar stuff, but it crunches the whole document at once, instead of
108 # just one statement.
109 #
110 40 my %constant_modules = ('constant' => 1, 'constant::defer' => 1);
111 529 ▶sub _use_constants {
Subroutine _use_constants: called CC=21 SCAR=30.6
112 529 my ($elem) = @_;
113  
114 529 ▶ if ($elem->isa ('PPI::Statement::Sub')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($elem->isa("PPI::Statement::Sub")) 10 519 529
115 10 my $prototype = $elem->prototype;
116 ### $prototype
117 10 ▶ if (defined $prototype && $prototype eq '') { # prototype ()
Branch: 1 branch
BranchTrueFalseTotal
if (defined $prototype and $prototype eq "") 4 6 10
Condition100%3/3
defined $prototype and $prototype eq ""
!ll&&!rl&&r
424
MC/DC100%2/2
defined $prototype and $prototype eq ""
defined $prototype $prototype eq ""
Truth table100%3/3
A and B
A= defined $prototypeB= $prototype eq ""
ABresult
0X0
100
111
118 4 ▶ if (my $name = $elem->name) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $name = $elem->name) 4 0 4
119 4 return $name;
120 }
121 }
122 # anonymous sub or without prototype
123 6 return;
124 }
125  
126 return unless ($elem->isa ('PPI::Statement::Include')
127 && $elem->type eq 'use'
128 519 ▶ && $constant_modules{$elem->module || ''});
Branch: 1 branch
BranchTrueFalseTotal
unless $elem->isa("PPI::Statement::Include") and $elem->type eq "use" and $constant_modules{$elem->module or ""} 489 30 519
Condition75%6/8
$elem->isa("PPI::Statement::Include") and $elem->type eq "use" and $constant_modules{$elem->module or ""}
!ll&&!rl&&r
489030
$elem->isa("PPI::Statement::Include") and $elem->type eq "use"
!ll&&!rl&&r
487230
$elem->module || ''
l!l
300
MC/DC66%2/3
$elem->isa("PPI::Statement::Include") and $elem->type eq "use" and $constant_modules{$elem->module or ""}
$elem->isa("PPI::Statement::Include") $elem->type eq "use" $constant_modules{$elem->module or ""}
MC/DC0%0/1
$elem->module || ''
$elem->module
Truth table75%3/4
A and B and C
A= $elem->isa("PPI::Statement::Include")B= $elem->type eq "use"C= $constant_modules{$elem->module or ""}
ABCresult
0XX0
10X0
1100
1111
Truth table50%1/2
A
A= $elem->module
Aresult
11
00
129  
130 30 ▶ $elem = $elem->schild(2) or return; # could be "use constant" alone
Branch: 1 branch
BranchTrueFalseTotal
$elem = $elem->schild(2) or return 29 1 30
131 ### start at: $elem->content
132  
133 29 my $single = 1;
134 29 ▶ if ($elem->isa ('PPI::Structure::Constructor')) {
Branch: 2 branches
BranchTrueFalseTotal
if ($elem->isa('PPI::Structure::Constructor')) { } 8 21 29
elsif ($elem->isa('PPI::Structure::List')) { } 4 17 21
135 # multi-constant "use constant { FOO => 1, BAR => 2 }"
136 #
137 # PPI::Structure::Constructor { ... }
138 # PPI::Statement
139 # PPI::Token::Word 'foo'
140 #
141 8 $single = 0;
142 # multiple constants
143 8 ▶ $elem = $elem->schild(0)
Branch: 1 branch
BranchTrueFalseTotal
$elem = $elem->schild(0) or return 6 2 8
144 or return; # empty on "use constant {}"
145 6 goto SKIPSTATEMENT;
146  
147 } elsif ($elem->isa ('PPI::Structure::List')) {
148 # single constant in parens "use constant (FOO => 1,2,3)"
149 #
150 # PPI::Structure::List ( ... )
151 # PPI::Statement::Expression
152 # PPI::Token::Word 'Foo'
153 #
154 4 ▶ $elem = $elem->schild(0)
Branch: 1 branch
BranchTrueFalseTotal
$elem = $elem->schild(0) or return 2 2 4
155 or return; # empty on "use constant {}"
156  
157 SKIPSTATEMENT:
158 8 ▶ if ($elem->isa ('PPI::Statement')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($elem->isa("PPI::Statement")) 8 0 8
159 8 ▶ $elem = $elem->schild(0) or return;
Branch: 1 branch
BranchTrueFalseTotal
$elem = $elem->schild(0) or return 8 0 8
160 }
161 }
162  
163 # split_nodes_on_comma() handles oddities like "use constant qw(FOO 1)"
164 #
165 25 my @nodes = _elem_and_ssiblings ($elem);
166 25 my @arefs = split_nodes_on_comma (@nodes);
167  
168 ### @arefs
169  
170 25 ▶ if ($single) {
Branch: 1 branch
BranchTrueFalseTotal
if ($single) 19 6 25
171 19 $#arefs = 0; # first elem only
172 }
173 25 my @constants;
174 25 ▶ for (my $i = 0; $i < @arefs; $i += 2) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @arefs) 30 25 55
175 30 my $aref = $arefs[$i];
176 30 ▶ if (@$aref == 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (@$aref == 1) 30 0 30
177 30 my $name_elem = $aref->[0];
178 30 ▶ if (! $name_elem->isa ('PPI::Token::Structure')) { # not final ";"
Branch: 1 branch
BranchTrueFalseTotal
unless ($name_elem->isa("PPI::Token::Structure")) 29 1 30
179 29 ▶ push @constants, ($name_elem->can('string')
Branch: 1 branch
BranchTrueFalseTotal
$name_elem->can('string') ? : 2 27 29
180 ? $name_elem->string
181 : $name_elem->content);
182 29 next;
183 }
184 }
185 ### ConstantBeforeLt skip non-name constant: $aref
186 }
187 25 return @constants;
188 }
189  
190 78 ▶sub _elem_and_ssiblings {
Subroutine _elem_and_ssiblings: called CC=2 SCAR=6.9
191 78 my ($elem) = @_;
192 78 my @ret;
193 78 ▶ while ($elem) {
Branch: 1 branch
BranchTrueFalseTotal
if ($elem) 550 78 628
194 550 push @ret, $elem;
195 550 $elem = $elem->snext_sibling;
196 }
197 78 return @ret;
198 }
199  
200 40 1;
201 __END__
« Policy/Miscellanea/TextDomainPlaceholders.pm ↑ Summary Policy/Compatibility/ConstantPragmaHash.pm »