« lib/Perl/Critic/Policy/BuiltinFunctions/ProhibitReverseSortBlock.pm
↑ Summary
lib/Perl/Critic/Command.pm »
| 1 | package Perl::Critic::Policy::Variables::RequireNegativeIndices; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | 40 | ▶ | use 5.010001; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 40 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | 40 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | 40 | ▶ | use Readonly; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||
| 8 | 40 | ▶ | use Perl::Critic::Utils qw{ :severities }; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | 40 | ▶ | use parent 'Perl::Critic::Policy'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||
| 11 | 40 | our $VERSION = '1.156'; | |||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||
| 13 | #----------------------------------------------------------------------------- | ||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||
| 15 | 40 | Readonly::Scalar my $DESC => q{Negative array index should be used}; | |||||||||||||||||||||||||||
| 16 | 40 | Readonly::Scalar my $EXPL => [ 88 ]; | |||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||
| 18 | #----------------------------------------------------------------------------- | ||||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||
| 20 | 90 | ▶ | sub supported_parameters { return () } | ||||||||||||||||||||||||||
|
Subroutine
supported_parameters: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 21 | 74 | ▶ | sub default_severity { return $SEVERITY_HIGH } | ||||||||||||||||||||||||||
|
Subroutine
default_severity: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 22 | 85 | ▶ | sub default_themes { return qw( core maintenance pbp ) } | ||||||||||||||||||||||||||
|
Subroutine
default_themes: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 23 | 33 | ▶ | sub applies_to { return 'PPI::Structure::Subscript' } | ||||||||||||||||||||||||||
|
Subroutine
applies_to: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||
| 25 | #----------------------------------------------------------------------------- | ||||||||||||||||||||||||||||
| 26 | |||||||||||||||||||||||||||||
| 27 | 0 | ▶ | sub violates { | ||||||||||||||||||||||||||
|
Subroutine
violates: not called CC=4 SCAR=30.0
Pod
documented
| |||||||||||||||||||||||||||||
| 28 | 0 | my ( $self, $elem, undef ) = @_; | |||||||||||||||||||||||||||
| 29 | |||||||||||||||||||||||||||||
| 30 | 0 | ▶ | return if $elem->braces ne '[]'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 31 | 0 | my ($name, $isref) = _is_bad_index( $elem ); | |||||||||||||||||||||||||||
| 32 | 0 | ▶ | return if ( !$name ); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 33 | 0 | ▶ | return if !_is_array_name( $elem, $name, $isref ); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 34 | 0 | return $self->violation( $DESC, $EXPL, $elem ); | |||||||||||||||||||||||||||
| 35 | } | ||||||||||||||||||||||||||||
| 36 | |||||||||||||||||||||||||||||
| 37 | 40 | Readonly::Scalar my $MAX_EXPRESSION_COMPLEXITY => 4; | |||||||||||||||||||||||||||
| 38 | |||||||||||||||||||||||||||||
| 39 | 0 | ▶ | sub _is_bad_index { | ||||||||||||||||||||||||||
|
Subroutine
_is_bad_index: not called CC=9 SCAR=45.0
| |||||||||||||||||||||||||||||
| 40 | # return (varname, 0|1) if this could be a violation | ||||||||||||||||||||||||||||
| 41 | 0 | my ( $elem ) = @_; | |||||||||||||||||||||||||||
| 42 | |||||||||||||||||||||||||||||
| 43 | 0 | my @children = $elem->schildren(); | |||||||||||||||||||||||||||
| 44 | 0 | ▶ | return if @children != 1; # too complex | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 45 | 0 | ▶ | return if !$children[0]->isa( 'PPI::Statement::Expression'); # too complex | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 46 | |||||||||||||||||||||||||||||
| 47 | # This is the expression elements that compose the array indexing | ||||||||||||||||||||||||||||
| 48 | 0 | my @expr = $children[0]->schildren(); | |||||||||||||||||||||||||||
| 49 | 0 | ▶ | return if !@expr || @expr > $MAX_EXPRESSION_COMPLEXITY; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
not @expr or @expr > $MAX_EXPRESSION_COMPLEXITY
MC/DC0%0/2
not @expr or @expr > $MAX_EXPRESSION_COMPLEXITY
not @expr
@expr > $MAX_EXPRESSION_COMPLEXITY
Truth table0%0/3
not(A) or B
A= not @exprB= @expr > $MAX_EXPRESSION_COMPLEXITY
| |||||||||||||||||||||||||||||
| 50 | 0 | my ($name, $isref, $isindex) = _is_bad_var_in_index(\@expr); | |||||||||||||||||||||||||||
| 51 | 0 | ▶ | return if !$name; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 52 | 0 | ▶ | return $name, $isref if !@expr && $isindex; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
not @expr and $isindex
MC/DC0%0/2
not @expr and $isindex
not @expr
$isindex
Truth table0%0/3
not(A) and B
A= not @exprB= $isindex
| |||||||||||||||||||||||||||||
| 53 | 0 | ▶ | return if !_is_minus_number(@expr); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 54 | 0 | return $name, $isref; | |||||||||||||||||||||||||||
| 55 | } | ||||||||||||||||||||||||||||
| 56 | |||||||||||||||||||||||||||||
| 57 | 0 | ▶ | sub _is_bad_var_in_index { | ||||||||||||||||||||||||||
|
Subroutine
_is_bad_var_in_index: not called CC=4 SCAR=30.0
| |||||||||||||||||||||||||||||
| 58 | # return (varname, isref=0|1, isindex=0|1) if this could be a violation | ||||||||||||||||||||||||||||
| 59 | 0 | my ( $expr ) = @_; | |||||||||||||||||||||||||||
| 60 | |||||||||||||||||||||||||||||
| 61 | 0 | ▶ | if ( $expr->[0]->isa('PPI::Token::ArrayIndex') ) { | ||||||||||||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||||||||||||
| 62 | # [$#arr] | ||||||||||||||||||||||||||||
| 63 | 0 | return _arrayindex($expr); | |||||||||||||||||||||||||||
| 64 | } | ||||||||||||||||||||||||||||
| 65 | elsif ( $expr->[0]->isa('PPI::Token::Cast') ) { | ||||||||||||||||||||||||||||
| 66 | # [$#{$arr} ...] or [$#$arr ...] or [@{$arr} ...] or [@$arr ...] | ||||||||||||||||||||||||||||
| 67 | 0 | return _cast($expr); | |||||||||||||||||||||||||||
| 68 | } | ||||||||||||||||||||||||||||
| 69 | elsif ($expr->[0]->isa('PPI::Token::Symbol')) { | ||||||||||||||||||||||||||||
| 70 | # [@arr ...] | ||||||||||||||||||||||||||||
| 71 | 0 | return _symbol($expr); | |||||||||||||||||||||||||||
| 72 | } | ||||||||||||||||||||||||||||
| 73 | |||||||||||||||||||||||||||||
| 74 | 0 | return; | |||||||||||||||||||||||||||
| 75 | } | ||||||||||||||||||||||||||||
| 76 | |||||||||||||||||||||||||||||
| 77 | 0 | ▶ | sub _arrayindex { | ||||||||||||||||||||||||||
|
Subroutine
_arrayindex: not called CC=2 SCAR=17.9
| |||||||||||||||||||||||||||||
| 78 | # return (varname, isref=0|1, isindex=0|1) if this could be a violation | ||||||||||||||||||||||||||||
| 79 | 0 | my ( $expr ) = @_; | |||||||||||||||||||||||||||
| 80 | 0 | my $arrindex = shift @{$expr}; | |||||||||||||||||||||||||||
| 81 | 0 | ▶ | if ($arrindex->content =~ m/\A \$[#] (.*) \z /xms) { # What else could it be??? | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 82 | 0 | return $1, 0, 1; | |||||||||||||||||||||||||||
| 83 | } | ||||||||||||||||||||||||||||
| 84 | 0 | return; | |||||||||||||||||||||||||||
| 85 | } | ||||||||||||||||||||||||||||
| 86 | |||||||||||||||||||||||||||||
| 87 | 0 | ▶ | sub _cast { | ||||||||||||||||||||||||||
|
Subroutine
_cast: not called CC=12 SCAR=50.5
| |||||||||||||||||||||||||||||
| 88 | # return (varname, isref=0|1, isindex=0|1) if this could be a violation | ||||||||||||||||||||||||||||
| 89 | 0 | my ( $expr ) = @_; | |||||||||||||||||||||||||||
| 90 | 0 | my $cast = shift @{$expr}; | |||||||||||||||||||||||||||
| 91 | 0 | ▶ | if ( $cast eq q{$#} || $cast eq q{@} ) { ## no critic(RequireInterpolationOfMetachars) | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$cast eq "\$#" or $cast eq "\@"
MC/DC0%0/2
$cast eq "\$#" or $cast eq "\@"
$cast eq "\$#"
$cast eq "\@"
Truth table0%0/3
A or B
A= $cast eq "\$#"B= $cast eq "\@"
| |||||||||||||||||||||||||||||
| 92 | 0 | ▶ | my $isindex = $cast eq q{$#} ? 1 : 0; ## no critic(RequireInterpolationOfMetachars) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 93 | 0 | my $arrvar = shift @{$expr}; | |||||||||||||||||||||||||||
| 94 | 0 | ▶ | if ($arrvar->isa('PPI::Structure::Block')) { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 95 | # look for [$#{$arr} ...] or [@{$arr} ...] | ||||||||||||||||||||||||||||
| 96 | 0 | my @blockchildren = $arrvar->schildren(); | |||||||||||||||||||||||||||
| 97 | 0 | ▶ | return if @blockchildren != 1; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 98 | 0 | ▶ | return if !$blockchildren[0]->isa('PPI::Statement'); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 99 | 0 | my @ggg = $blockchildren[0]->schildren; | |||||||||||||||||||||||||||
| 100 | 0 | ▶ | return if @ggg != 1; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 101 | 0 | ▶ | return if !$ggg[0]->isa('PPI::Token::Symbol'); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 102 | 0 | ▶ | if ($ggg[0] =~ m/\A \$ (.*) \z/xms) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 103 | 0 | return $1, 1, $isindex; | |||||||||||||||||||||||||||
| 104 | } | ||||||||||||||||||||||||||||
| 105 | } | ||||||||||||||||||||||||||||
| 106 | elsif ( $arrvar->isa('PPI::Token::Symbol') ) { | ||||||||||||||||||||||||||||
| 107 | # look for [$#$arr ...] or [@$arr ...] | ||||||||||||||||||||||||||||
| 108 | 0 | ▶ | if ($arrvar =~ m/\A \$ (.*) \z/xms) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 109 | 0 | return $1, 1, $isindex; | |||||||||||||||||||||||||||
| 110 | } | ||||||||||||||||||||||||||||
| 111 | } | ||||||||||||||||||||||||||||
| 112 | } | ||||||||||||||||||||||||||||
| 113 | 0 | return; | |||||||||||||||||||||||||||
| 114 | } | ||||||||||||||||||||||||||||
| 115 | |||||||||||||||||||||||||||||
| 116 | 0 | ▶ | sub _symbol { | ||||||||||||||||||||||||||
|
Subroutine
_symbol: not called CC=2 SCAR=17.9
| |||||||||||||||||||||||||||||
| 117 | # return (varname, isref=0|1, isindex=0|1) if this could be a violation | ||||||||||||||||||||||||||||
| 118 | 0 | my ( $expr ) = @_; | |||||||||||||||||||||||||||
| 119 | 0 | my $arrvar = shift @{$expr}; | |||||||||||||||||||||||||||
| 120 | 0 | ▶ | if ($arrvar =~ m/\A \@ (.*) \z/xms) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 121 | 0 | return $1, 0, 0; | |||||||||||||||||||||||||||
| 122 | } | ||||||||||||||||||||||||||||
| 123 | 0 | return; | |||||||||||||||||||||||||||
| 124 | } | ||||||||||||||||||||||||||||
| 125 | |||||||||||||||||||||||||||||
| 126 | 0 | ▶ | sub _is_minus_number { # return true if @expr looks like "- n" | ||||||||||||||||||||||||||
|
Subroutine
_is_minus_number: not called CC=6 SCAR=37.4
| |||||||||||||||||||||||||||||
| 127 | 0 | my @expr = @_; | |||||||||||||||||||||||||||
| 128 | |||||||||||||||||||||||||||||
| 129 | 0 | ▶ | return if !@expr; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 130 | |||||||||||||||||||||||||||||
| 131 | 0 | ▶ | return if @expr != 2; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 132 | |||||||||||||||||||||||||||||
| 133 | 0 | my $op = shift @expr; | |||||||||||||||||||||||||||
| 134 | 0 | ▶ | return if !$op->isa('PPI::Token::Operator'); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 135 | 0 | ▶ | return if $op ne q{-}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 136 | |||||||||||||||||||||||||||||
| 137 | 0 | my $number = shift @expr; | |||||||||||||||||||||||||||
| 138 | 0 | ▶ | return if !$number->isa('PPI::Token::Number'); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 139 | |||||||||||||||||||||||||||||
| 140 | 0 | return 1; | |||||||||||||||||||||||||||
| 141 | } | ||||||||||||||||||||||||||||
| 142 | |||||||||||||||||||||||||||||
| 143 | 0 | ▶ | sub _is_array_name { # return true if name and isref matches | ||||||||||||||||||||||||||
|
Subroutine
_is_array_name: not called CC=11 SCAR=48.8
| |||||||||||||||||||||||||||||
| 144 | 0 | my ( $elem, $name, $isref ) = @_; | |||||||||||||||||||||||||||
| 145 | |||||||||||||||||||||||||||||
| 146 | 0 | my $sib = $elem->sprevious_sibling; | |||||||||||||||||||||||||||
| 147 | 0 | ▶ | return if !$sib; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 148 | |||||||||||||||||||||||||||||
| 149 | 0 | ▶ | if ($sib->isa('PPI::Token::Operator') && $sib eq '->') { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$sib->isa("PPI::Token::Operator") and $sib eq "->"
MC/DC0%0/2
$sib->isa("PPI::Token::Operator") and $sib eq "->"
$sib->isa("PPI::Token::Operator")
$sib eq "->"
Truth table0%0/3
A and B
A= $sib->isa("PPI::Token::Operator")B= $sib eq "->"
| |||||||||||||||||||||||||||||
| 150 | 0 | ▶ | return if ( !$isref ); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 151 | 0 | $isref = 0; | |||||||||||||||||||||||||||
| 152 | 0 | $sib = $sib->sprevious_sibling; | |||||||||||||||||||||||||||
| 153 | 0 | ▶ | return if !$sib; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 154 | } | ||||||||||||||||||||||||||||
| 155 | |||||||||||||||||||||||||||||
| 156 | 0 | ▶ | return if !$sib->isa('PPI::Token::Symbol'); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 157 | 0 | ▶ | return if $sib !~ m/\A \$ \Q$name\E \z/xms; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 158 | |||||||||||||||||||||||||||||
| 159 | 0 | my $cousin = $sib->sprevious_sibling; | |||||||||||||||||||||||||||
| 160 | 0 | ▶ | return if $isref ^ _is_dereferencer( $cousin ); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 161 | 0 | ▶ | return if $isref && _is_dereferencer( $cousin->sprevious_sibling ); | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$isref and _is_dereferencer($cousin->sprevious_sibling)
MC/DC0%0/2
$isref and _is_dereferencer($cousin->sprevious_sibling)
$isref
_is_dereferencer($cousin->sprevious_sibling)
Truth table0%0/3
A and B
A= $isrefB= _is_dereferencer($cousin->sprevious_sibling)
| |||||||||||||||||||||||||||||
| 162 | |||||||||||||||||||||||||||||
| 163 | 0 | return $elem; | |||||||||||||||||||||||||||
| 164 | } | ||||||||||||||||||||||||||||
| 165 | |||||||||||||||||||||||||||||
| 166 | 0 | ▶ | sub _is_dereferencer { # must return 0 or 1, not undef | ||||||||||||||||||||||||||
|
Subroutine
_is_dereferencer: not called CC=5 SCAR=34.0
| |||||||||||||||||||||||||||||
| 167 | 0 | my $elem = shift; | |||||||||||||||||||||||||||
| 168 | |||||||||||||||||||||||||||||
| 169 | 0 | ▶ | return 0 if !$elem; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 170 | 0 | ▶ | return 1 if $elem->isa('PPI::Token::Operator') && $elem eq '->'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$elem->isa("PPI::Token::Operator") and $elem eq "->"
MC/DC0%0/2
$elem->isa("PPI::Token::Operator") and $elem eq "->"
$elem->isa("PPI::Token::Operator")
$elem eq "->"
Truth table0%0/3
A and B
A= $elem->isa("PPI::Token::Operator")B= $elem eq "->"
| |||||||||||||||||||||||||||||
| 171 | 0 | ▶ | return 1 if $elem->isa('PPI::Token::Cast'); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 172 | 0 | return 0; | |||||||||||||||||||||||||||
| 173 | } | ||||||||||||||||||||||||||||
| 174 | |||||||||||||||||||||||||||||
| 175 | 40 | 1; | |||||||||||||||||||||||||||
| 176 | |||||||||||||||||||||||||||||
| 177 | #----------------------------------------------------------------------------- | ||||||||||||||||||||||||||||
| 178 | |||||||||||||||||||||||||||||
| 179 | __END__ | ||||||||||||||||||||||||||||