blib/lib/Perl/Lint/Policy/RegularExpressions/ProhibitCaptureWithoutTest.pm

filter: Statementstmt 83.3% 10 / 12 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 4 / 4 Podpod n/a totaltotal 87.5% 14 / 16 SCAR 0.0
CC
1
Cov
83%
BEGIN
1.3
BEGIN
1.0
BEGIN
1.0
CRAP 1.0
« blib/lib/Perl/Lint/Policy/NamingConventions/ProhibitAmbiguousNames.pm ↑ Summary blib/lib/Perl/Lint/Policy/RegularExpressions/ProhibitEnumeratedClasses.pm »
1 package Perl::Lint::Policy::RegularExpressions::ProhibitCaptureWithoutTest;
2 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use List::Util qw/none/;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use Perl::Lint::Constants::Type;
Subroutine BEGIN: called CC=1 SCAR=2.6
6 use parent "Perl::Lint::Policy";
7  
8 use constant {
9 DESC => 'Capture variable used outside conditional',
10 EXPL => [253],
11 };
12  
13 my %transfer_of_control_stmt_token_types = (
14 &NEXT => 1,
15 &LAST => 1,
16 &REDO => 1,
17 &GOTO => 1,
18 &RETURN => 1,
19 );
20  
21 my %control_stmt_token_types = (
22 &IF_STATEMENT => 1,
23 &ELSIF_STATEMENT => 1,
24 &UNLESS_STATEMENT => 1,
25 &WHILE_STATEMENT => 1,
26 );
27  
28 sub evaluate {
29 my ($class, $file, $tokens, $src, $args) = @_;
30  
31 my %exceptions = (
32 die => 1,
33 croak => 1,
34 confess => 1,
35 );
36  
37 if (my $this_policies_arg = $args->{prohibit_capture_without_test}) {
38 for my $exception (split(/\s+/, $this_policies_arg->{exception_source} || '')) {
39 $exceptions{$exception} = 1;
40 };
41 }
42  
43 my @violations;
44 my @is_tested_by_depth;
45 my $is_in_context_to_assign = 0;
46 my $depth = 0;
47 # use Data::Dumper::Concise; warn Dumper($tokens); # TODO remove
48 for (my $i = 0, my $token_type, my $token_data; my $token = $tokens->[$i]; $i++) {
49 $token_type = $token->{type};
50 $token_data = $token->{data};
51  
52 if ($token_type == REG_QUOTE || $token_type == REG_DOUBLE_QUOTE) {
53 # skip reg quotes (because it is recognized as regexp)
54 $i += 2;
55 next;
56 }
57  
58 if ($token_type == REG_EXP || $token_type == REG_REPLACE_FROM) {
59 # XXX
60 if ($depth < 0 && scalar @is_tested_by_depth < -$depth) {
61 next;
62 }
63 $is_tested_by_depth[$depth] = $is_in_context_to_assign ? 1 : 0;
64  
65 for ($i++; $token = $tokens->[$i]; $i++) {
66 $token_type = $token->{type};
67 if ($token_type == SEMI_COLON) {
68 goto END_OF_STATEMENT;
69 }
70 elsif (
71 $token_type == THREE_TERM_OP
72 ) {
73 $is_tested_by_depth[$depth] = 1;
74 last;
75 }
76 elsif (
77 $token_type == OR ||
78 $token_type == ALPHABET_OR
79 ) {
80 $token = $tokens->[++$i] or last;
81 $token_type = $token->{type};
82 $token_data = $token->{data};
83  
84 if (
85 ($exceptions{$token_data} && $token_type == KEY || $token_type == BUILTIN_FUNC) ||
86 $transfer_of_control_stmt_token_types{$token_type}
87 ) {
88 $is_tested_by_depth[$depth] = 1;
89 last;
90 }
91  
92 $token = $tokens->[++$i] or last;
93 if ($token->{type} == POINTER) {
94 $token = $tokens->[++$i] or last;
95 $token_type = $token->{type};
96 $token_data = $token->{data};
97 if ($exceptions{$token_data} && $token_type == METHOD) {
98 $is_tested_by_depth[$depth] = 1;
99 }
100 last;
101 }
102  
103 last;
104 }
105 }
106  
107 next;
108 }
109  
110 if ($token_type == SPECIFIC_VALUE && $token_data =~ /\A\$[1-9][0-9]*\Z/) {
111 if (none {$_} @is_tested_by_depth) {
112 push @violations, {
113 filename => $file,
114 line => $token->{line},
115 description => DESC,
116 explanation => EXPL,
117 policy => __PACKAGE__,
118 };
119 }
120  
121 next;
122 }
123  
124 if ($control_stmt_token_types{$token_type}) {
125 $token = $tokens->[++$i];
126 $token_type = $token->{type};
127 if ($token_type == LEFT_PAREN) {
128 my $lpnum = 1;
129 for ($i++; $token = $tokens->[$i]; $i++) {
130 $token_type = $token->{type};
131 if ($token_type == LEFT_PAREN) {
132 $lpnum++;
133 }
134 elsif ($token_type == RIGHT_PAREN) {
135 last if --$lpnum <= 0;
136 }
137 elsif ($token_type == REG_EXP || $token_type == REG_REPLACE_FROM) {
138 # XXX
139 if ($depth + 1 < 0 && scalar @is_tested_by_depth < -$depth + 1) {
140 next;
141 }
142 $is_tested_by_depth[$depth + 1] = 1;
143 }
144 }
145  
146 next;
147 }
148  
149 # for postfix
150 for ($i++; $token = $tokens->[$i]; $i++) {
151 $token_type = $token->{type};
152 if ($token_type == REG_EXP || $token_type == REG_REPLACE_FROM) {
153 $is_tested_by_depth[$depth + 1] = 1;
154 }
155 elsif ($token_type == SEMI_COLON) {
156 last;
157 }
158 }
159 next;
160 }
161  
162 if ($token_type == LEFT_BRACE) {
163 $depth++;
164  
165 # XXX
166 if ($depth < 0 && scalar @is_tested_by_depth < -$depth) {
167 next;
168 }
169  
170 $is_tested_by_depth[$depth] ||= 0;
171 next;
172 }
173  
174 if ($token_type == RIGHT_BRACE) {
175 pop @is_tested_by_depth;
176 $depth--;
177 next;
178 }
179  
180 if ($token_type == ASSIGN) {
181 $is_in_context_to_assign = 1;
182 next;
183 }
184  
185 END_OF_STATEMENT:
186 if ($token_type == SEMI_COLON) {
187 $is_in_context_to_assign = 0;
188 next;
189 }
190 }
191  
192 return \@violations;
193 }
194  
195 1;
196  
« blib/lib/Perl/Lint/Policy/NamingConventions/ProhibitAmbiguousNames.pm ↑ Summary blib/lib/Perl/Lint/Policy/RegularExpressions/ProhibitEnumeratedClasses.pm »