Policy/ValuesAndExpressions/ProhibitBarewordDoubleColon.pm

filter: Statementstmt 98.1% 53 / 54 Branchbran 92.8% 13 / 14 Conditioncond 50.0% 3 / 6 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 100% 14 / 14 Podpod 100% 1 / 1 totaltotal 90.3% 84 / 93 CC 10 SCAR 12.4
CC
10
Cov
93%
_word_is_indirect_call_classname
7.4
violates
4.0
BEGIN
1.0
CRAP 3.5
« Policy/ValuesAndExpressions/UnexpandedSpecialLiteral.pm ↑ Summary Policy/Documentation/ProhibitLinkToSelf.pm »
1 # Copyright 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 package Perl::Critic::Policy::ValuesAndExpressions::ProhibitBarewordDoubleColon;
17 40 ▶use 5.006;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 40 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
19 40 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
20 40 ▶use List::Util;
Subroutine BEGIN: called CC=1 SCAR=0.0
21  
22 40 ▶use base 'Perl::Critic::Policy';
Subroutine BEGIN: called CC=1 SCAR=0.0
23 40 ▶use Perl::Critic::Utils;
Subroutine BEGIN: called CC=1 SCAR=0.0
24  
25 40 ▶use Perl::Critic::Pulp;
Subroutine BEGIN: called CC=1 SCAR=0.0
26 40 ▶use Perl::Critic::Pulp::Utils 'elem_is_comma_operator';
Subroutine BEGIN: called CC=1 SCAR=0.0
27  
28 # uncomment this to run the ### lines
29 #use Smart::Comments;
30  
31 40 our $VERSION = 100;
32  
33 40 ▶use constant supported_parameters =>
Subroutine BEGIN: called CC=1 SCAR=0.0
34 ({ name => 'allow_indirect_syntax',
35 description => 'Whether to allow double-colon in indirect object syntax "new Foo:: arg,arg".',
36 behavior => 'boolean',
37 default_string => '1',
38 40 });
39  
40 40 ▶use constant default_severity => $Perl::Critic::Utils::SEVERITY_LOWEST;
Subroutine BEGIN: called CC=1 SCAR=0.0
41 40 ▶use constant default_themes => qw(pulp cosmetic);
Subroutine BEGIN: called CC=1 SCAR=0.0
42 40 ▶use constant applies_to => 'PPI::Token::Word';
Subroutine BEGIN: called CC=1 SCAR=0.0
43  
44 39 ▶sub violates {
Subroutine violates: called CC=4 SCAR=13.9
Pod documented
45 39 my ($self, $elem, $document) = @_;
46  
47 39 ▶ $elem =~ /::$/ or return;
Branch: 1 branch
BranchTrueFalseTotal
$elem =~ /::$/ or return 13 26 39
48  
49 13 ▶ if ($self->{'_allow_indirect_syntax'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"_allow_indirect_syntax"}) 6 7 13
50 6 ▶ if (_word_is_indirect_call_classname($elem)) {
Branch: 1 branch
BranchTrueFalseTotal
if (_word_is_indirect_call_classname($elem)) 2 4 6
51 2 return;
52 }
53 }
54  
55 11 return $self->violation
56 ('Use plain string instead of Foo:: bareword',
57 '',
58 $elem);
59 }
60  
61 # $elem is a PPI::Token::Word.
62 # Return true if it's the class name in an indirect object syntax method call.
63 #
64 6 ▶sub _word_is_indirect_call_classname {
Subroutine _word_is_indirect_call_classname: called CC=7 SCAR=20.0
65 6 my ($elem) = @_;
66 ### _word_is_indirect_call_classname(): "$elem"
67  
68 6 ▶ my $prev = $elem->sprevious_sibling || do {
Condition66%2/3
$elem->sprevious_sibling || do { return 0 }
l!l&&r!l&&!r
510
MC/DC0%0/2
$elem->sprevious_sibling || do { return 0 }
$elem->sprevious_sibling do { return 0 }
Truth table33%1/3
A || B
A= $elem->sprevious_siblingB= do { return 0 }
ABresult
1X1
011
000
69 ### no method preceding, not an indirect call ...
70 1 return 0;
71 };
72 ### prev: ref $prev, $prev->content
73  
74 5 ▶ if (! $prev->isa('PPI::Token::Word')) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($prev->isa("PPI::Token::Word")) 2 3 5
75 ### not a bareword method name preceding, not an indirect call ...
76 2 return 0;
77 }
78 3 ▶ if ($prev eq 'return') {
Branch: 1 branch
BranchTrueFalseTotal
if ($prev eq "return") 1 2 3
79 ### return is never an indirect call ...
80 1 return 0;
81 }
82  
83 # What about "foo bar Foo::"? Assume its function foo and method bar?
84 #
85 # $prev = $prev->sprevious_sibling;
86 # ### prev-prev: ref $prev, $prev->content
87 # if ($prev && $prev->isa('PPI::Token::Word')) { return 0; }
88  
89 2 ▶ if (my $next = $elem->snext_sibling) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $next = $elem->snext_sibling) 1 1 2
90 1 ▶ if ($next->isa('PPI::Token::Operator') && $next eq '=>') {
Branch: 1 branch
BranchTrueFalseTotal
if ($next->isa("PPI::Token::Operator") and $next eq "=>") 0 1 1
Condition33%1/3
$next->isa("PPI::Token::Operator") and $next eq "=>"
!ll&&!rl&&r
100
MC/DC0%0/2
$next->isa("PPI::Token::Operator") and $next eq "=>"
$next->isa("PPI::Token::Operator") $next eq "=>"
Truth table33%1/3
A and B
A= $next->isa("PPI::Token::Operator")B= $next eq "=>"
ABresult
0X0
100
111
91 # "word1 word2 => ..." is either a function call to word1 or a syntax
92 # error, not an indirect call. But "word1 word2," can be an indirect
93 # call in a comma operator list
94 #
95 ### fat comma not an indirect ...
96 0 return 0;
97 }
98 }
99  
100 2 return 1;
101 }
102  
103 40 1;
104 __END__
« Policy/ValuesAndExpressions/UnexpandedSpecialLiteral.pm ↑ Summary Policy/Documentation/ProhibitLinkToSelf.pm »