lib/Perl/Critic/Policy/Variables/RequireNegativeIndices.pm

filter: Statementstmt 24.0% 26 / 108 Branchbran 0.0% 0 / 80 Conditioncond 0.0% 0 / 18 MC/DCmcdc 0.0% 0 / 12 Subroutinesub 52.6% 10 / 19 Podpod 80.0% 4 / 5 totaltotal 16.5% 40 / 242 CC 47 SCAR 42.9
CC
47
Cov
13%
_cast
156.0
_is_array_name
132.0
_is_bad_index
90.0
CRAP 72.8
« 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
BranchTrueFalseTotal
if $elem->braces ne "[]" 0 0 0
31 0 my ($name, $isref) = _is_bad_index( $elem );
32 0 ▶ return if ( !$name );
Branch: 1 branch
BranchTrueFalseTotal
unless $name 0 0 0
33 0 ▶ return if !_is_array_name( $elem, $name, $isref );
Branch: 1 branch
BranchTrueFalseTotal
unless _is_array_name($elem, $name, $isref) 0 0 0
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
BranchTrueFalseTotal
if @children != 1 0 0 0
45 0 ▶ return if !$children[0]->isa( 'PPI::Statement::Expression'); # too complex
Branch: 1 branch
BranchTrueFalseTotal
unless $children[0]->isa("PPI::Statement::Expression") 0 0 0
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
BranchTrueFalseTotal
if not @expr or @expr > $MAX_EXPRESSION_COMPLEXITY 0 0 0
Condition0%0/3
not @expr or @expr > $MAX_EXPRESSION_COMPLEXITY
l!l&&r!l&&!r
000
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
ABresult
1X1
011
000
50 0 my ($name, $isref, $isindex) = _is_bad_var_in_index(\@expr);
51 0 ▶ return if !$name;
Branch: 1 branch
BranchTrueFalseTotal
unless $name 0 0 0
52 0 ▶ return $name, $isref if !@expr && $isindex;
Branch: 1 branch
BranchTrueFalseTotal
if not @expr and $isindex 0 0 0
Condition0%0/3
not @expr and $isindex
!ll&&!rl&&r
000
MC/DC0%0/2
not @expr and $isindex
not @expr $isindex
Truth table0%0/3
not(A) and B
A= not @exprB= $isindex
ABresult
0X0
100
111
53 0 ▶ return if !_is_minus_number(@expr);
Branch: 1 branch
BranchTrueFalseTotal
unless _is_minus_number(@expr) 0 0 0
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
BranchTrueFalseTotal
if ($expr->[0]->isa('PPI::Token::ArrayIndex')) { } 0 0 0
elsif ($expr->[0]->isa('PPI::Token::Cast')) { } 0 0 0
elsif ($expr->[0]->isa('PPI::Token::Symbol')) { } 0 0 0
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
BranchTrueFalseTotal
if ($arrindex->content =~ /\A \$[#] (.*) \z /msx) 0 0 0
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
BranchTrueFalseTotal
if ($cast eq "\$#" or $cast eq "\@") 0 0 0
Condition0%0/3
$cast eq "\$#" or $cast eq "\@"
l!l&&r!l&&!r
000
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 "\@"
ABresult
1X1
011
000
92 0 ▶ my $isindex = $cast eq q{$#} ? 1 : 0; ## no critic(RequireInterpolationOfMetachars)
Branch: 1 branch
BranchTrueFalseTotal
$cast eq '$#' ? : 0 0 0
93 0 my $arrvar = shift @{$expr};
94 0 ▶ if ($arrvar->isa('PPI::Structure::Block')) {
Branch: 2 branches
BranchTrueFalseTotal
if ($arrvar->isa('PPI::Structure::Block')) { } 0 0 0
elsif ($arrvar->isa('PPI::Token::Symbol')) { } 0 0 0
95 # look for [$#{$arr} ...] or [@{$arr} ...]
96 0 my @blockchildren = $arrvar->schildren();
97 0 ▶ return if @blockchildren != 1;
Branch: 1 branch
BranchTrueFalseTotal
if @blockchildren != 1 0 0 0
98 0 ▶ return if !$blockchildren[0]->isa('PPI::Statement');
Branch: 1 branch
BranchTrueFalseTotal
unless $blockchildren[0]->isa("PPI::Statement") 0 0 0
99 0 my @ggg = $blockchildren[0]->schildren;
100 0 ▶ return if @ggg != 1;
Branch: 1 branch
BranchTrueFalseTotal
if @ggg != 1 0 0 0
101 0 ▶ return if !$ggg[0]->isa('PPI::Token::Symbol');
Branch: 1 branch
BranchTrueFalseTotal
unless $ggg[0]->isa("PPI::Token::Symbol") 0 0 0
102 0 ▶ if ($ggg[0] =~ m/\A \$ (.*) \z/xms) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ggg[0] =~ /\A \$ (.*) \z/msx) 0 0 0
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
BranchTrueFalseTotal
if ($arrvar =~ /\A \$ (.*) \z/msx) 0 0 0
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
BranchTrueFalseTotal
if ($arrvar =~ /\A \@ (.*) \z/msx) 0 0 0
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
BranchTrueFalseTotal
unless @expr 0 0 0
130  
131 0 ▶ return if @expr != 2;
Branch: 1 branch
BranchTrueFalseTotal
if @expr != 2 0 0 0
132  
133 0 my $op = shift @expr;
134 0 ▶ return if !$op->isa('PPI::Token::Operator');
Branch: 1 branch
BranchTrueFalseTotal
unless $op->isa("PPI::Token::Operator") 0 0 0
135 0 ▶ return if $op ne q{-};
Branch: 1 branch
BranchTrueFalseTotal
if $op ne "-" 0 0 0
136  
137 0 my $number = shift @expr;
138 0 ▶ return if !$number->isa('PPI::Token::Number');
Branch: 1 branch
BranchTrueFalseTotal
unless $number->isa("PPI::Token::Number") 0 0 0
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
BranchTrueFalseTotal
unless $sib 0 0 0
148  
149 0 ▶ if ($sib->isa('PPI::Token::Operator') && $sib eq '->') {
Branch: 1 branch
BranchTrueFalseTotal
if ($sib->isa("PPI::Token::Operator") and $sib eq "->") 0 0 0
Condition0%0/3
$sib->isa("PPI::Token::Operator") and $sib eq "->"
!ll&&!rl&&r
000
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 "->"
ABresult
0X0
100
111
150 0 ▶ return if ( !$isref );
Branch: 1 branch
BranchTrueFalseTotal
unless $isref 0 0 0
151 0 $isref = 0;
152 0 $sib = $sib->sprevious_sibling;
153 0 ▶ return if !$sib;
Branch: 1 branch
BranchTrueFalseTotal
unless $sib 0 0 0
154 }
155  
156 0 ▶ return if !$sib->isa('PPI::Token::Symbol');
Branch: 1 branch
BranchTrueFalseTotal
unless $sib->isa("PPI::Token::Symbol") 0 0 0
157 0 ▶ return if $sib !~ m/\A \$ \Q$name\E \z/xms;
Branch: 1 branch
BranchTrueFalseTotal
unless $sib =~ /\A \$ \Q$name\E \z/msx 0 0 0
158  
159 0 my $cousin = $sib->sprevious_sibling;
160 0 ▶ return if $isref ^ _is_dereferencer( $cousin );
Branch: 1 branch
BranchTrueFalseTotal
if $isref ^ _is_dereferencer($cousin) 0 0 0
161 0 ▶ return if $isref && _is_dereferencer( $cousin->sprevious_sibling );
Branch: 1 branch
BranchTrueFalseTotal
if $isref and _is_dereferencer($cousin->sprevious_sibling) 0 0 0
Condition0%0/3
$isref and _is_dereferencer($cousin->sprevious_sibling)
!ll&&!rl&&r
000
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)
ABresult
0X0
100
111
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
BranchTrueFalseTotal
unless $elem 0 0 0
170 0 ▶ return 1 if $elem->isa('PPI::Token::Operator') && $elem eq '->';
Branch: 1 branch
BranchTrueFalseTotal
if $elem->isa("PPI::Token::Operator") and $elem eq "->" 0 0 0
Condition0%0/3
$elem->isa("PPI::Token::Operator") and $elem eq "->"
!ll&&!rl&&r
000
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 "->"
ABresult
0X0
100
111
171 0 ▶ return 1 if $elem->isa('PPI::Token::Cast');
Branch: 1 branch
BranchTrueFalseTotal
if $elem->isa("PPI::Token::Cast") 0 0 0
172 0 return 0;
173 }
174  
175 40 1;
176  
177 #-----------------------------------------------------------------------------
178  
179 __END__
« lib/Perl/Critic/Policy/BuiltinFunctions/ProhibitReverseSortBlock.pm ↑ Summary lib/Perl/Critic/Command.pm »