Perl/Lint/Policy/InputOutput/RequireBriefOpen.pm

filter: Statementstmt 13.9% 16 / 115 Branchbran 0.0% 0 / 76 Conditioncond 0.0% 0 / 69 MC/DCmcdc 0.0% 0 / 33 Subroutinesub 83.3% 5 / 6 Podpod 0.0% 0 / 1 totaltotal 7.0% 21 / 300 CC 70 SCAR 84.4
CC
70
Cov
6%
evaluate
4970.0
BEGIN
1.0
BEGIN
1.0
CRAP 4638.7
« Perl/Lint/Policy/ErrorHandling/RequireCheckingReturnValueOfEval.pm ↑ Summary Perl/Lint/Policy/InputOutput/RequireCheckedOpen.pm »
1 package Perl::Lint::Policy::InputOutput::RequireBriefOpen;
2 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 2 ▶use Perl::Lint::Constants::Type;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 2 ▶use parent "Perl::Lint::Policy";
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 2 ▶use constant {
Subroutine BEGIN: called CC=1 SCAR=0.0
8 2 DESC => 'Close filehandles as soon as possible after opening them..',
9 EXPL => [209],
10 2 };
11  
12 0 ▶sub evaluate {
Subroutine evaluate: not called CC=70 SCAR=85.1
Pod undocumented
13 0 my ($class, $file, $tokens, $src, $args) = @_;
14  
15 0 my @violations;
16  
17 0 my $line_gap = 9;
18  
19 0 ▶ if (my $this_policies_arg = $args->{require_brief_open}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $this_policies_arg = $args->{"require_brief_open"}) 0 0 0
20 0 ▶ $line_gap = $this_policies_arg->{lines} // 9;
Condition0%0/2
$this_policies_arg->{"lines"} // 9
l!l
00
MC/DC0%0/1
$this_policies_arg->{"lines"} // 9
$this_policies_arg->{"lines"}
Truth table0%0/2
A
A= $this_policies_arg->{"lines"}
Aresult
11
00
21 }
22  
23 0 my $depth = 0;
24  
25 0 my @opened_file_handlers_for_each_depth = ([]);
26  
27 0 my %opened_file_globs_for_each_depth;
28 my %closed_file_globs_for_each_depth;
29  
30 0 my %function_declared_depth;
31  
32 0 my @not_closed_file_handlers;
33  
34 0 ▶ for (my $i = 0, my $token_type, my $token_data; my $token = $tokens->[$i]; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $token = $tokens->[$i]) 0 0 0
35 0 $token_type = $token->{type};
36 0 $token_data = $token->{data};
37  
38 0 ▶ if ($token_type == LEFT_BRACE) {
Branch: 1 branch
BranchTrueFalseTotal
if ($token_type == 109) 0 0 0
39 0 $depth++;
40 0 next;
41 }
42  
43 0 ▶ if ($token_type == RIGHT_BRACE) {
Branch: 1 branch
BranchTrueFalseTotal
if ($token_type == 110) 0 0 0
44 0 ▶ my %not_closed_file_handlers = %{$opened_file_handlers_for_each_depth[-1]->[$depth] || {}};
Condition0%0/2
$opened_file_handlers_for_each_depth[-1][$depth] || {}
l!l
00
MC/DC0%0/1
$opened_file_handlers_for_each_depth[-1][$depth] || {}
$opened_file_handlers_for_each_depth[-1][$depth]
Truth table0%0/2
A
A= $opened_file_handlers_for_each_depth[-1][$depth]
Aresult
11
00
45 0 for my $not_closed_fh_name (keys %not_closed_file_handlers) {
46 0 push @not_closed_file_handlers, $not_closed_file_handlers{$not_closed_fh_name};
47 }
48  
49 0 $depth--;
50  
51 0 ▶ if ($function_declared_depth{$depth}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($function_declared_depth{$depth}) 0 0 0
52 0 pop @opened_file_handlers_for_each_depth;
53 0 ▶ if (!@opened_file_handlers_for_each_depth) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@opened_file_handlers_for_each_depth) 0 0 0
54 0 @opened_file_handlers_for_each_depth = ([]);
55 }
56 }
57  
58 0 next;
59 }
60  
61 # to support CORE(::GLOBAL)::open or close
62 0 my $is_core_func = 0;
63 0 ▶ if ($token_type == NAMESPACE && $token_data eq 'CORE') {
Branch: 1 branch
BranchTrueFalseTotal
if ($token_type == 127 and $token_data eq "CORE") 0 0 0
Condition0%0/3
$token_type == 127 and $token_data eq "CORE"
!ll&&!rl&&r
000
MC/DC0%0/2
$token_type == 127 and $token_data eq "CORE"
$token_type == 127 $token_data eq "CORE"
Truth table0%0/3
A and B
A= $token_type == 127B= $token_data eq "CORE"
ABresult
0X0
100
111
64 0 ▶ $token = $tokens->[++$i] or last;
Branch: 1 branch
BranchTrueFalseTotal
$token = $$tokens[++$i] or last 0 0 0
65 0 ▶ if ($token->{type} == NAMESPACE_RESOLVER) {
Branch: 1 branch
BranchTrueFalseTotal
if ($token->{"type"} == 126) 0 0 0
66 0 ▶ $token = $tokens->[++$i] or last;
Branch: 1 branch
BranchTrueFalseTotal
$token = $$tokens[++$i] or last 0 0 0
67 0 ▶ if ($token->{type} == NAMESPACE && $token->{data} eq 'GLOBAL') {
Branch: 1 branch
BranchTrueFalseTotal
if ($token->{"type"} == 127 and $token->{"data"} eq "GLOBAL") 0 0 0
Condition0%0/3
$token->{"type"} == 127 and $token->{"data"} eq "GLOBAL"
!ll&&!rl&&r
000
MC/DC0%0/2
$token->{"type"} == 127 and $token->{"data"} eq "GLOBAL"
$token->{"type"} == 127 $token->{"data"} eq "GLOBAL"
Truth table0%0/3
A and B
A= $token->{"type"} == 127B= $token->{"data"} eq "GLOBAL"
ABresult
0X0
100
111
68 0 ▶ $token = $tokens->[++$i] or last;
Branch: 1 branch
BranchTrueFalseTotal
$token = $$tokens[++$i] or last 0 0 0
69 0 ▶ if ($token->{type} == NAMESPACE_RESOLVER) {
Branch: 1 branch
BranchTrueFalseTotal
if ($token->{"type"} == 126) 0 0 0
70 0 ▶ $token = $tokens->[++$i] or last;
Branch: 1 branch
BranchTrueFalseTotal
$token = $$tokens[++$i] or last 0 0 0
71 }
72 }
73 0 $is_core_func = 1;
74 0 $token_type = $token->{type};
75 0 $token_data = $token->{data};
76  
77 }
78  
79 # fall through
80 }
81  
82 # for open()
83 0 ▶ if (
Branch: 1 branch
BranchTrueFalseTotal
if ($token_type == 70 and $token_data eq "open" or $is_core_func and $token_type == 127 and $token_data eq "open") 0 0 0
Condition0%0/12
$token_type == 70 and $token_data eq "open" or $is_core_func and $token_type == 127 and $token_data eq "open"
l!l&&r!l&&!r
000
$token_type == 70 and $token_data eq "open"
!ll&&!rl&&r
000
$is_core_func and $token_type == 127 and $token_data eq "open"
!ll&&!rl&&r
000
$is_core_func and $token_type == 127
!ll&&!rl&&r
000
MC/DC0%0/5
$token_type == 70 and $token_data eq "open" or $is_core_func and $token_type == 127 and $token_data eq "open"
$token_type == 70 $token_data eq "open" $is_core_func $token_type == 127 $token_data eq "open"
Truth table0%0/9
A and B or C and D and E
A= $token_type == 70B= $token_data eq "open"C= $is_core_funcD= $token_type == 127E= $token_data eq "open"
ABCDEresult
11XXX1
0X1111
101111
0X0XX0
0X10X0
0X1100
100XX0
1010X0
101100
84 ($token_type == BUILTIN_FUNC && $token_data eq 'open') ||
85 ($is_core_func && $token_type == NAMESPACE && $token_data eq 'open')
86 ) {
87 0 my $lbnum = 0;
88 0 ▶ for ($i++; $token = $tokens->[$i]; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($token = $tokens->[$i]) 0 0 0
89 0 $token_type = $token->{type};
90 0 $token_data = $token->{data};
91 0 ▶ if (
Branch: 4 branches
BranchTrueFalseTotal
if ($token_type == 194 or $token_type == 191 or $token_type == 166) { } 0 0 0
elsif ($token_type == 122) { } 0 0 0
elsif ($token_type == 109) { } 0 0 0
elsif ($token_type == 79 or $token_type == 80 or $token_type == 81 or $token_type == 104) { } 0 0 0
Condition0%0/6
$token_type == 194 or $token_type == 191 or $token_type == 166
l!l&&r!l&&!r
000
$token_type == 194 or $token_type == 191
l!l&&r!l&&!r
000
MC/DC0%0/3
$token_type == 194 or $token_type == 191 or $token_type == 166
$token_type == 194 $token_type == 191 $token_type == 166
Truth table0%0/4
A or B or C
A= $token_type == 194B= $token_type == 191C= $token_type == 166
ABCresult
1XX1
01X1
0011
0000
92 $token_type == GLOBAL_VAR ||
93 $token_type == LOCAL_VAR ||
94 $token_type == VAR
95 ) {
96 0 $opened_file_handlers_for_each_depth[-1]->[$depth]->{$token_data} = $token;
97 0 last;
98 }
99 elsif ($token_type == KEY) {
100 0 ▶ if ($token_data =~ /\A[A-Z0-9_]+\z/) { # check UPPER_CASE or not
Branch: 1 branch
BranchTrueFalseTotal
if ($token_data =~ /\A[A-Z0-9_]+\z/) 0 0 0
101 0 $opened_file_globs_for_each_depth{$token_data} = $token;
102 }
103 0 last;
104 }
105 elsif ($token_type == LEFT_BRACE) {
106 0 $lbnum++;
107 }
108 ▶ elsif (
Condition0%0/9
$token_type == 79 or $token_type == 80 or $token_type == 81 or $token_type == 104
l!l&&r!l&&!r
000
$token_type == 79 or $token_type == 80 or $token_type == 81
l!l&&r!l&&!r
000
$token_type == 79 or $token_type == 80
l!l&&r!l&&!r
000
MC/DC0%0/4
$token_type == 79 or $token_type == 80 or $token_type == 81 or $token_type == 104
$token_type == 79 $token_type == 80 $token_type == 81 $token_type == 104
Truth table0%0/5
A or B or C or D
A= $token_type == 79B= $token_type == 80C= $token_type == 81D= $token_type == 104
ABCDresult
1XXX1
01XX1
001X1
00011
00000
109 $token_type == TYPE_STDIN ||
110 $token_type == TYPE_STDOUT ||
111 $token_type == TYPE_STDERR ||
112 $token_type == COMMA # <= fail safe
113 ) {
114 0 last;
115 }
116 }
117  
118 0 ▶ if ($lbnum) {
Branch: 1 branch
BranchTrueFalseTotal
if ($lbnum) 0 0 0
119 0 ▶ for ($i++; $token = $tokens->[$i]; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($token = $tokens->[$i]) 0 0 0
120 0 $token_type = $token->{type};
121 0 ▶ if ($token_type == RIGHT_BRACE) {
Branch: 1 branch
BranchTrueFalseTotal
if ($token_type == 110) 0 0 0
122 0 ▶ last if --$lbnum <= 0;
Branch: 1 branch
BranchTrueFalseTotal
if --$lbnum <= 0 0 0 0
123 }
124 }
125 }
126  
127 0 next;
128 }
129  
130 # for close() or return
131 # If file handler is returned by function, it is equivalent to be closed.
132 0 ▶ if (
Branch: 1 branch
BranchTrueFalseTotal
if ($token_type == 0 or $token_type == 70 and $token_data eq "close" or $is_core_func and $token_type == 127 and $token_data eq "close") 0 0 0
Condition0%0/15
$token_type == 0 or $token_type == 70 and $token_data eq "close" or $is_core_func and $token_type == 127 and $token_data eq "close"
l!l&&r!l&&!r
000
$token_type == 0 or $token_type == 70 and $token_data eq "close"
l!l&&r!l&&!r
000
$token_type == 70 and $token_data eq "close"
!ll&&!rl&&r
000
$is_core_func and $token_type == 127 and $token_data eq "close"
!ll&&!rl&&r
000
$is_core_func and $token_type == 127
!ll&&!rl&&r
000
MC/DC0%0/6
$token_type == 0 or $token_type == 70 and $token_data eq "close" or $is_core_func and $token_type == 127 and $token_data eq "close"
$token_type == 0 $token_type == 70 $token_data eq "close" $is_core_func $token_type == 127 $token_data eq "close"
Truth table0%0/10
A or B and C or D and E and F
A= $token_type == 0B= $token_type == 70C= $token_data eq "close"D= $is_core_funcE= $token_type == 127F= $token_data eq "close"
ABCDEFresult
1XXXXX1
011XXX1
00X1111
0101111
00X0XX0
00X10X0
00X1100
0100XX0
01010X0
0101100
133 $token_type == RETURN || ($token_type == BUILTIN_FUNC && $token_data eq 'close') ||
134 ($is_core_func && $token_type == NAMESPACE && $token_data eq 'close')
135 ) {
136 0 ▶ $token = $tokens->[++$i] or last;
Branch: 1 branch
BranchTrueFalseTotal
$token = $$tokens[++$i] or last 0 0 0
137 0 $token_type = $token->{type};
138 0 ▶ if ($token_type == LEFT_PAREN) {
Branch: 1 branch
BranchTrueFalseTotal
if ($token_type == 107) 0 0 0
139 0 ▶ $token = $tokens->[++$i] or last;
Branch: 1 branch
BranchTrueFalseTotal
$token = $$tokens[++$i] or last 0 0 0
140 0 $token_type = $token->{type};
141 }
142  
143 0 $token_data = $token->{data};
144  
145 0 ▶ if (
Branch: 2 branches
BranchTrueFalseTotal
if ($token_type == 194 or $token_type == 191 or $token_type == 166) { } 0 0 0
elsif ($token_type == 122) { } 0 0 0
Condition0%0/6
$token_type == 194 or $token_type == 191 or $token_type == 166
l!l&&r!l&&!r
000
$token_type == 194 or $token_type == 191
l!l&&r!l&&!r
000
MC/DC0%0/3
$token_type == 194 or $token_type == 191 or $token_type == 166
$token_type == 194 $token_type == 191 $token_type == 166
Truth table0%0/4
A or B or C
A= $token_type == 194B= $token_type == 191C= $token_type == 166
ABCresult
1XX1
01X1
0011
0000
146 $token_type == GLOBAL_VAR ||
147 $token_type == LOCAL_VAR ||
148 $token_type == VAR
149 ) {
150 0 for my $d (reverse 0 .. $depth) {
151 0 my $hit = $opened_file_handlers_for_each_depth[-1]->[$d]->{$token_data};
152  
153 0 ▶ if (defined $hit) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $hit) 0 0 0
154 0 ▶ if ($token->{line} - $hit->{line} <= $line_gap) {
Branch: 1 branch
BranchTrueFalseTotal
if ($token->{"line"} - $hit->{"line"} <= $line_gap) 0 0 0
155 0 delete $opened_file_handlers_for_each_depth[-1]->[$d]->{$token_data};
156 }
157 0 last;
158 }
159 }
160 }
161 elsif ($token_type == KEY) {
162 0 $closed_file_globs_for_each_depth{$token_data} = 1;
163 }
164 }
165  
166 # for close method (OOP style)
167 0 ▶ if ($token_type == METHOD && $token_data eq 'close') {
Branch: 1 branch
BranchTrueFalseTotal
if ($token_type == 64 and $token_data eq "close") 0 0 0
Condition0%0/3
$token_type == 64 and $token_data eq "close"
!ll&&!rl&&r
000
MC/DC0%0/2
$token_type == 64 and $token_data eq "close"
$token_type == 64 $token_data eq "close"
Truth table0%0/3
A and B
A= $token_type == 64B= $token_data eq "close"
ABresult
0X0
100
111
168 0 my $var_token = $tokens->[$i-2];
169 0 my $var_type = $var_token->{type};
170 0 my $var_name = $var_token->{data};
171  
172 0 ▶ if (
Branch: 1 branch
BranchTrueFalseTotal
if ($var_type == 194 or $var_type == 191 or $var_type == 166) 0 0 0
Condition0%0/6
$var_type == 194 or $var_type == 191 or $var_type == 166
l!l&&r!l&&!r
000
$var_type == 194 or $var_type == 191
l!l&&r!l&&!r
000
MC/DC0%0/3
$var_type == 194 or $var_type == 191 or $var_type == 166
$var_type == 194 $var_type == 191 $var_type == 166
Truth table0%0/4
A or B or C
A= $var_type == 194B= $var_type == 191C= $var_type == 166
ABCresult
1XX1
01X1
0011
0000
173 $var_type == GLOBAL_VAR ||
174 $var_type == LOCAL_VAR ||
175 $var_type == VAR
176 ) {
177 0 for my $d (reverse 0 .. $depth) {
178 0 my $hit = $opened_file_handlers_for_each_depth[-1]->[$d]->{$var_name};
179 0 ▶ if (defined $hit) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $hit) 0 0 0
180 0 ▶ if ($var_token->{line} - $hit->{line} <= $line_gap) {
Branch: 1 branch
BranchTrueFalseTotal
if ($var_token->{"line"} - $hit->{"line"} <= $line_gap) 0 0 0
181 0 delete $opened_file_handlers_for_each_depth[-1]->[$d]->{$var_name};
182 }
183 0 last;
184 }
185 }
186 }
187 }
188  
189 # to separate scope by function inside and outside
190 0 ▶ if ($token_type == FUNCTION_DECL) {
Branch: 1 branch
BranchTrueFalseTotal
if ($token_type == 63) 0 0 0
191 0 $function_declared_depth{$depth} = 1;
192  
193 0 push @opened_file_handlers_for_each_depth, [];
194 0 next;
195 }
196 }
197  
198 # for file handlers (variable)
199 0 ▶ my %not_closed_file_handlers = %{$opened_file_handlers_for_each_depth[-1]->[0] || {}};
Condition0%0/2
$opened_file_handlers_for_each_depth[-1][0] || {}
l!l
00
MC/DC0%0/1
$opened_file_handlers_for_each_depth[-1][0] || {}
$opened_file_handlers_for_each_depth[-1][0]
Truth table0%0/2
A
A= $opened_file_handlers_for_each_depth[-1][0]
Aresult
11
00
200 0 for my $not_closed_fh_name (keys %not_closed_file_handlers) {
201 0 push @not_closed_file_handlers, $not_closed_file_handlers{$not_closed_fh_name};
202 }
203  
204 0 for my $not_closed_file_handler (@not_closed_file_handlers) {
205 push @violations, {
206 filename => $file,
207 line => $not_closed_file_handler->{line},
208 0 description => DESC,
209 explanation => EXPL,
210 policy => __PACKAGE__,
211 };
212 }
213  
214 # If glob is used as file handler, it beyonds the scope
215 0 for my $not_closed_file_glob (keys %closed_file_globs_for_each_depth) {
216 0 delete $opened_file_globs_for_each_depth{$not_closed_file_glob};
217 }
218  
219 0 for my $not_closed_file_glob (keys %opened_file_globs_for_each_depth) {
220 push @violations, {
221 filename => $file,
222 line => $opened_file_globs_for_each_depth{$not_closed_file_glob}->{line},
223 0 description => DESC,
224 explanation => EXPL,
225 policy => __PACKAGE__,
226 };
227 }
228  
229 0 return \@violations;
230 }
231  
232 2 1;
233  
« Perl/Lint/Policy/ErrorHandling/RequireCheckingReturnValueOfEval.pm ↑ Summary Perl/Lint/Policy/InputOutput/RequireCheckedOpen.pm »