« 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
| |||||||||||||||||||||||||||||
| 48 | |||||||||||||||||||||||||||||
| 49 | 13 | ▶ | if ($self->{'_allow_indirect_syntax'}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 50 | 6 | ▶ | if (_word_is_indirect_call_classname($elem)) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
}
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
}
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 90 | 1 | ▶ | if ($next->isa('PPI::Token::Operator') && $next eq '=>') { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$next->isa("PPI::Token::Operator") and $next eq "=>"
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 "=>"
| |||||||||||||||||||||||||||||
| 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__ | ||||||||||||||||||||||||||||