Filename | /Users/timbo/perl5/perlbrew/perls/perl-5.18.2/lib/site_perl/5.18.2/Perl/Critic/Policy/Variables/RequireNegativeIndices.pm |
Statements | Executed 19 statements in 856µs |
Calls | P | F | Exclusive Time |
Inclusive Time |
Subroutine |
---|---|---|---|---|---|
1 | 1 | 1 | 16µs | 16µs | BEGIN@10 | Perl::Critic::Policy::Variables::RequireNegativeIndices::
1 | 1 | 1 | 8µs | 60µs | BEGIN@16 | Perl::Critic::Policy::Variables::RequireNegativeIndices::
1 | 1 | 1 | 8µs | 11µs | BEGIN@12 | Perl::Critic::Policy::Variables::RequireNegativeIndices::
1 | 1 | 1 | 7µs | 27µs | BEGIN@13 | Perl::Critic::Policy::Variables::RequireNegativeIndices::
1 | 1 | 1 | 7µs | 152µs | BEGIN@15 | Perl::Critic::Policy::Variables::RequireNegativeIndices::
1 | 1 | 1 | 7µs | 18µs | BEGIN@11 | Perl::Critic::Policy::Variables::RequireNegativeIndices::
1 | 1 | 1 | 5µs | 6µs | default_severity | Perl::Critic::Policy::Variables::RequireNegativeIndices::
1 | 1 | 1 | 1µs | 1µs | supported_parameters | Perl::Critic::Policy::Variables::RequireNegativeIndices::
0 | 0 | 0 | 0s | 0s | _arrayindex | Perl::Critic::Policy::Variables::RequireNegativeIndices::
0 | 0 | 0 | 0s | 0s | _cast | Perl::Critic::Policy::Variables::RequireNegativeIndices::
0 | 0 | 0 | 0s | 0s | _is_array_name | Perl::Critic::Policy::Variables::RequireNegativeIndices::
0 | 0 | 0 | 0s | 0s | _is_bad_index | Perl::Critic::Policy::Variables::RequireNegativeIndices::
0 | 0 | 0 | 0s | 0s | _is_bad_var_in_index | Perl::Critic::Policy::Variables::RequireNegativeIndices::
0 | 0 | 0 | 0s | 0s | _is_dereferencer | Perl::Critic::Policy::Variables::RequireNegativeIndices::
0 | 0 | 0 | 0s | 0s | _is_minus_number | Perl::Critic::Policy::Variables::RequireNegativeIndices::
0 | 0 | 0 | 0s | 0s | _symbol | Perl::Critic::Policy::Variables::RequireNegativeIndices::
0 | 0 | 0 | 0s | 0s | applies_to | Perl::Critic::Policy::Variables::RequireNegativeIndices::
0 | 0 | 0 | 0s | 0s | default_themes | Perl::Critic::Policy::Variables::RequireNegativeIndices::
0 | 0 | 0 | 0s | 0s | violates | Perl::Critic::Policy::Variables::RequireNegativeIndices::
Line | State ments |
Time on line |
Calls | Time in subs |
Code |
---|---|---|---|---|---|
1 | ############################################################################## | ||||
2 | # $URL$ | ||||
3 | # $Date$ | ||||
4 | # $Author$ | ||||
5 | # $Revision$ | ||||
6 | ############################################################################## | ||||
7 | |||||
8 | package Perl::Critic::Policy::Variables::RequireNegativeIndices; | ||||
9 | |||||
10 | 2 | 42µs | 1 | 16µs | # spent 16µs within Perl::Critic::Policy::Variables::RequireNegativeIndices::BEGIN@10 which was called:
# once (16µs+0s) by Module::Pluggable::Object::_require at line 10 # spent 16µs making 1 call to Perl::Critic::Policy::Variables::RequireNegativeIndices::BEGIN@10 |
11 | 2 | 20µs | 2 | 29µs | # spent 18µs (7+11) within Perl::Critic::Policy::Variables::RequireNegativeIndices::BEGIN@11 which was called:
# once (7µs+11µs) by Module::Pluggable::Object::_require at line 11 # spent 18µs making 1 call to Perl::Critic::Policy::Variables::RequireNegativeIndices::BEGIN@11
# spent 11µs making 1 call to strict::import |
12 | 2 | 18µs | 2 | 15µs | # spent 11µs (8+4) within Perl::Critic::Policy::Variables::RequireNegativeIndices::BEGIN@12 which was called:
# once (8µs+4µs) by Module::Pluggable::Object::_require at line 12 # spent 11µs making 1 call to Perl::Critic::Policy::Variables::RequireNegativeIndices::BEGIN@12
# spent 4µs making 1 call to warnings::import |
13 | 2 | 22µs | 2 | 48µs | # spent 27µs (7+20) within Perl::Critic::Policy::Variables::RequireNegativeIndices::BEGIN@13 which was called:
# once (7µs+20µs) by Module::Pluggable::Object::_require at line 13 # spent 27µs making 1 call to Perl::Critic::Policy::Variables::RequireNegativeIndices::BEGIN@13
# spent 20µs making 1 call to Exporter::import |
14 | |||||
15 | 2 | 24µs | 2 | 297µs | # spent 152µs (7+145) within Perl::Critic::Policy::Variables::RequireNegativeIndices::BEGIN@15 which was called:
# once (7µs+145µs) by Module::Pluggable::Object::_require at line 15 # spent 152µs making 1 call to Perl::Critic::Policy::Variables::RequireNegativeIndices::BEGIN@15
# spent 145µs making 1 call to Exporter::import |
16 | 2 | 717µs | 2 | 113µs | # spent 60µs (8+53) within Perl::Critic::Policy::Variables::RequireNegativeIndices::BEGIN@16 which was called:
# once (8µs+53µs) by Module::Pluggable::Object::_require at line 16 # spent 60µs making 1 call to Perl::Critic::Policy::Variables::RequireNegativeIndices::BEGIN@16
# spent 53µs making 1 call to base::import |
17 | |||||
18 | 1 | 600ns | our $VERSION = '1.121'; | ||
19 | |||||
20 | #----------------------------------------------------------------------------- | ||||
21 | |||||
22 | 1 | 2µs | 1 | 29µs | Readonly::Scalar my $DESC => q{Negative array index should be used}; # spent 29µs making 1 call to Readonly::Scalar |
23 | 1 | 2µs | 1 | 43µs | Readonly::Scalar my $EXPL => [ 88 ]; # spent 43µs making 1 call to Readonly::Scalar |
24 | |||||
25 | #----------------------------------------------------------------------------- | ||||
26 | |||||
27 | 1 | 4µs | # spent 1µs within Perl::Critic::Policy::Variables::RequireNegativeIndices::supported_parameters which was called:
# once (1µs+0s) by Perl::Critic::Policy::new at line 88 of Perl/Critic/Policy.pm | ||
28 | 1 | 1µs | # spent 6µs (5+1) within Perl::Critic::Policy::Variables::RequireNegativeIndices::default_severity which was called:
# once (5µs+1µs) by Perl::Critic::Policy::get_severity at line 331 of Perl/Critic/Policy.pm | ||
29 | sub default_themes { return qw( core maintenance pbp ) } | ||||
30 | sub applies_to { return 'PPI::Structure::Subscript' } | ||||
31 | |||||
32 | #----------------------------------------------------------------------------- | ||||
33 | |||||
34 | sub violates { | ||||
35 | my ( $self, $elem, $doc ) = @_; | ||||
36 | |||||
37 | return if $elem->braces ne '[]'; | ||||
38 | my ($name, $isref) = _is_bad_index( $elem ); | ||||
39 | return if ( !$name ); | ||||
40 | return if !_is_array_name( $elem, $name, $isref ); | ||||
41 | return $self->violation( $DESC, $EXPL, $elem ); | ||||
42 | } | ||||
43 | |||||
44 | 1 | 1µs | 1 | 20µs | Readonly::Scalar my $MAX_EXPRESSION_COMPLEXETY => 4; # spent 20µs making 1 call to Readonly::Scalar |
45 | |||||
46 | sub _is_bad_index { | ||||
47 | # return (varname, 0|1) if this could be a violation | ||||
48 | my ( $elem ) = @_; | ||||
49 | |||||
50 | my @children = $elem->schildren(); | ||||
51 | return if @children != 1; # too complex | ||||
52 | return if !$children[0]->isa( 'PPI::Statement::Expression'); # too complex | ||||
53 | |||||
54 | # This is the expression elements that compose the array indexing | ||||
55 | my @expr = $children[0]->schildren(); | ||||
56 | return if !@expr || @expr > $MAX_EXPRESSION_COMPLEXETY; | ||||
57 | my ($name, $isref, $isindex) = _is_bad_var_in_index(\@expr); | ||||
58 | return if !$name; | ||||
59 | return $name, $isref if !@expr && $isindex; | ||||
60 | return if !_is_minus_number(@expr); | ||||
61 | return $name, $isref; | ||||
62 | } | ||||
63 | |||||
64 | sub _is_bad_var_in_index { | ||||
65 | # return (varname, isref=0|1, isindex=0|1) if this could be a violation | ||||
66 | my ( $expr ) = @_; | ||||
67 | |||||
68 | if ( $expr->[0]->isa('PPI::Token::ArrayIndex') ) { | ||||
69 | # [$#arr] | ||||
70 | return _arrayindex($expr); | ||||
71 | } | ||||
72 | elsif ( $expr->[0]->isa('PPI::Token::Cast') ) { | ||||
73 | # [$#{$arr} ...] or [$#$arr ...] or [@{$arr} ...] or [@$arr ...] | ||||
74 | return _cast($expr); | ||||
75 | } | ||||
76 | elsif ($expr->[0]->isa('PPI::Token::Symbol')) { | ||||
77 | # [@arr ...] | ||||
78 | return _symbol($expr); | ||||
79 | } | ||||
80 | |||||
81 | return; | ||||
82 | } | ||||
83 | |||||
84 | sub _arrayindex { | ||||
85 | # return (varname, isref=0|1, isindex=0|1) if this could be a violation | ||||
86 | my ( $expr ) = @_; | ||||
87 | my $arrindex = shift @{$expr}; | ||||
88 | if ($arrindex->content =~ m/\A \$[#] (.*) \z /xms) { # What else could it be??? | ||||
89 | return $1, 0, 1; | ||||
90 | } | ||||
91 | return; | ||||
92 | } | ||||
93 | |||||
94 | sub _cast { | ||||
95 | # return (varname, isref=0|1, isindex=0|1) if this could be a violation | ||||
96 | my ( $expr ) = @_; | ||||
97 | my $cast = shift @{$expr}; | ||||
98 | if ( $cast eq q{$#} || $cast eq q{@} ) { ## no critic(RequireInterpolationOfMetachars) | ||||
99 | my $isindex = $cast eq q{$#} ? 1 : 0; ## no critic(RequireInterpolationOfMetachars) | ||||
100 | my $arrvar = shift @{$expr}; | ||||
101 | if ($arrvar->isa('PPI::Structure::Block')) { | ||||
102 | # look for [$#{$arr} ...] or [@{$arr} ...] | ||||
103 | my @blockchildren = $arrvar->schildren(); | ||||
104 | return if @blockchildren != 1; | ||||
105 | return if !$blockchildren[0]->isa('PPI::Statement'); | ||||
106 | my @ggg = $blockchildren[0]->schildren; | ||||
107 | return if @ggg != 1; | ||||
108 | return if !$ggg[0]->isa('PPI::Token::Symbol'); | ||||
109 | if ($ggg[0] =~ m/\A \$ (.*) \z/xms) { | ||||
110 | return $1, 1, $isindex; | ||||
111 | } | ||||
112 | } | ||||
113 | elsif ( $arrvar->isa('PPI::Token::Symbol') ) { | ||||
114 | # look for [$#$arr ...] or [@$arr ...] | ||||
115 | if ($arrvar =~ m/\A \$ (.*) \z/xms) { | ||||
116 | return $1, 1, $isindex; | ||||
117 | } | ||||
118 | } | ||||
119 | } | ||||
120 | return; | ||||
121 | } | ||||
122 | |||||
123 | sub _symbol { | ||||
124 | # return (varname, isref=0|1, isindex=0|1) if this could be a violation | ||||
125 | my ( $expr ) = @_; | ||||
126 | my $arrvar = shift @{$expr}; | ||||
127 | if ($arrvar =~ m/\A \@ (.*) \z/xms) { | ||||
128 | return $1, 0, 0; | ||||
129 | } | ||||
130 | return; | ||||
131 | } | ||||
132 | |||||
133 | sub _is_minus_number { # return true if @expr looks like "- n" | ||||
134 | my @expr = @_; | ||||
135 | |||||
136 | return if !@expr; | ||||
137 | |||||
138 | return if @expr != 2; | ||||
139 | |||||
140 | my $op = shift @expr; | ||||
141 | return if !$op->isa('PPI::Token::Operator'); | ||||
142 | return if $op ne q{-}; | ||||
143 | |||||
144 | my $number = shift @expr; | ||||
145 | return if !$number->isa('PPI::Token::Number'); | ||||
146 | |||||
147 | return 1; | ||||
148 | } | ||||
149 | |||||
150 | sub _is_array_name { # return true if name and isref matches | ||||
151 | my ( $elem, $name, $isref ) = @_; | ||||
152 | |||||
153 | my $sib = $elem->sprevious_sibling; | ||||
154 | return if !$sib; | ||||
155 | |||||
156 | if ($sib->isa('PPI::Token::Operator') && $sib eq '->') { | ||||
157 | return if ( !$isref ); | ||||
158 | $isref = 0; | ||||
159 | $sib = $sib->sprevious_sibling; | ||||
160 | return if !$sib; | ||||
161 | } | ||||
162 | |||||
163 | return if !$sib->isa('PPI::Token::Symbol'); | ||||
164 | return if $sib !~ m/\A \$ \Q$name\E \z/xms; | ||||
165 | |||||
166 | my $cousin = $sib->sprevious_sibling; | ||||
167 | return if $isref ^ _is_dereferencer( $cousin ); | ||||
168 | return if $isref && _is_dereferencer( $cousin->sprevious_sibling ); | ||||
169 | |||||
170 | return $elem; | ||||
171 | } | ||||
172 | |||||
173 | sub _is_dereferencer { # must return 0 or 1, not undef | ||||
174 | my $elem = shift; | ||||
175 | |||||
176 | return 0 if !$elem; | ||||
177 | return 1 if $elem->isa('PPI::Token::Operator') && $elem eq '->'; | ||||
178 | return 1 if $elem->isa('PPI::Token::Cast'); | ||||
179 | return 0; | ||||
180 | } | ||||
181 | |||||
182 | 1 | 4µs | 1; | ||
183 | |||||
184 | #----------------------------------------------------------------------------- | ||||
185 | |||||
186 | __END__ |