Policy/CodeLayout/RequireFinalSemicolon.pm

filter: Statementstmt 92.5% 136 / 147 Branchbran 92.1% 59 / 64 Conditioncond 68.1% 60 / 88 MC/DCmcdc 44.1% 19 / 43 Subroutinesub 92.5% 25 / 27 Podpod 100% 1 / 1 totaltotal 81.0% 300 / 370 CC 64 SCAR 21.4
CC
64
Cov
85%
_elem_skip_whitespace_and_comments
20.0
_block_is_hash_constructor
16.4
violates
13.4
CRAP 8.5
« Policy/Documentation/ProhibitAdjacentLinks.pm ↑ Summary Policy/ValuesAndExpressions/ProhibitEmptyCommas.pm »
1 # Copyright 2010, 2011, 2012, 2013, 2014, 2015, 2016, 2017, 2019, 2021 Kevin Ryde
2  
3 # Perl-Critic-Pulp is free software; you can redistribute it and/or modify
4 # it under the terms of the GNU General Public License as published by the
5 # Free Software Foundation; either version 3, or (at your option) any later
6 # version.
7 #
8 # Perl-Critic-Pulp is distributed in the hope that it will be useful, but
9 # WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
10 # or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
11 # for more details.
12 #
13 # You should have received a copy of the GNU General Public License along
14 # with Perl-Critic-Pulp. If not, see <http://www.gnu.org/licenses/>.
15  
16 package Perl::Critic::Policy::CodeLayout::RequireFinalSemicolon;
17 40 ▶use 5.006;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 40 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
19 40 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
20 40 ▶use List::Util;
Subroutine BEGIN: called CC=1 SCAR=0.0
21  
22 40 ▶use base 'Perl::Critic::Policy';
Subroutine BEGIN: called CC=1 SCAR=0.0
23 40 ▶use Perl::Critic::Utils;
Subroutine BEGIN: called CC=1 SCAR=0.0
24 40 ▶use Perl::Critic::Pulp;
Subroutine BEGIN: called CC=1 SCAR=0.0
25 40 ▶use Perl::Critic::Pulp::Utils;
Subroutine BEGIN: called CC=1 SCAR=0.0
26  
27 # uncomment this to run the ### lines
28 # use Smart::Comments;
29  
30 40 our $VERSION = 100;
31  
32 40 ▶use constant supported_parameters =>
Subroutine BEGIN: called CC=1 SCAR=0.0
33 ({ name => 'except_same_line',
34 description => 'Whether to allow no semicolon at the end of blocks with the } closing brace on the same line as the last statement.',
35 behavior => 'boolean',
36 default_string => '1',
37 },
38 { name => 'except_expression_blocks',
39 description => 'Whether to allow no semicolon at the end of do{} expression blocks.',
40 behavior => 'boolean',
41 default_string => '1',
42 40 });
43 40 ▶use constant default_severity => $Perl::Critic::Utils::SEVERITY_LOW;
Subroutine BEGIN: called CC=1 SCAR=0.0
44 40 ▶use constant default_themes => qw(pulp cosmetic);
Subroutine BEGIN: called CC=1 SCAR=0.0
45 40 ▶use constant applies_to => 'PPI::Structure::Block';
Subroutine BEGIN: called CC=1 SCAR=0.0
46  
47 98 ▶sub violates {
Subroutine violates: called CC=13 SCAR=25.9
Pod documented
48 98 my ($self, $elem, $document) = @_;
49 ### RequireFinalSemicolon elem: $elem->content
50  
51 98 ▶ if (_block_is_hash_constructor($elem) != 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (_block_is_hash_constructor($elem) != 0) 10 88 98
52 ### hash constructor, or likely so, stop ...
53 10 return;
54 }
55  
56 88 ▶ my $block_last = $elem->schild(-1)
Condition100%2/2
$elem->schild(-1) || (return)
l!l
826
MC/DC100%1/1
$elem->schild(-1) || (return)
$elem->schild(-1)
Truth table100%2/2
A
A= $elem->schild(-1)
Aresult
11
00
57 || return; # empty block doesn't need a semi
58  
59 ### block_last: ref($block_last),$block_last->content
60 82 ▶ $block_last->isa('PPI::Statement') || do {
Branch: 1 branch
BranchTrueFalseTotal
$block_last->isa("PPI::Statement") or do { return } 82 0 82
61 ### last in block is not a PPI-Statement ...
62 0 return;
63 };
64 82 ▶ if (_elem_statement_no_need_semicolon($block_last)) {
Branch: 1 branch
BranchTrueFalseTotal
if (_elem_statement_no_need_semicolon($block_last)) 11 71 82
65 11 return;
66 }
67  
68 {
69 71 ▶ my $bstat_last = $block_last->schild(-1)
Condition50%1/2
$block_last->schild(-1) || (return)
l!l
710
MC/DC0%0/1
$block_last->schild(-1) || (return)
$block_last->schild(-1)
Truth table50%1/2
A
A= $block_last->schild(-1)
Aresult
11
00
70 || return; # statement shouldn't be empty, should it?
71 ### bstat_last in statement: ref($bstat_last),$bstat_last->content
72  
73 71 ▶ if (_elem_is_semicolon($bstat_last)) {
Branch: 1 branch
BranchTrueFalseTotal
if (_elem_is_semicolon($bstat_last)) 10 61 71
74 ### has final semicolon, ok ...
75 10 return;
76 }
77 }
78  
79 61 ▶ if ($self->{'_except_expression_blocks'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"_except_expression_blocks"}) 61 0 61
80 61 ▶ if (_block_is_expression($elem)) {
Branch: 1 branch
BranchTrueFalseTotal
if (_block_is_expression($elem)) 13 48 61
81 ### do expression, ok
82 13 return;
83 }
84 ### not a do{} expression
85 }
86  
87 # if don't have final brace then this option doesn't apply as there's no
88 # final brace to be on the same line
89 48 ▶ if ($self->{'_except_same_line'} && $elem->complete) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"_except_same_line"} and $elem->complete) 46 2 48
Condition66%2/3
$self->{"_except_same_line"} and $elem->complete
!ll&&!rl&&r
0246
MC/DC50%1/2
$self->{"_except_same_line"} and $elem->complete
$self->{"_except_same_line"} $elem->complete
Truth table66%2/3
A and B
A= $self->{"_except_same_line"}B= $elem->complete
ABresult
0X0
100
111
90 46 ▶ if (! _newline_in_following_sibling($block_last)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (_newline_in_following_sibling($block_last)) 25 21 46
91 ### no newline before close, ok
92 25 return;
93 }
94 }
95  
96 23 ▶ my $report_at = $block_last->next_sibling || $block_last;
Condition66%2/3
$block_last->next_sibling || $block_last
l!l&&r!l&&!r
2210
MC/DC0%0/2
$block_last->next_sibling || $block_last
$block_last->next_sibling $block_last
Truth table66%2/3
A || B
A= $block_last->next_siblingB= $block_last
ABresult
1X1
011
000
97 23 return $self->violation
98 ('Put semicolon ; on last statement in a block',
99 '',
100 $report_at);
101 }
102  
103 # return true if $elem is a PPI::Statement subclass which doesn't require a
104 # terminating ";"
105 82 ▶sub _elem_statement_no_need_semicolon {
Subroutine _elem_statement_no_need_semicolon: called CC=8 SCAR=21.6
106 82 my ($elem) = @_;
107 82 ▶ return ($elem->isa('PPI::Statement::Compound') # for(){} etc
Condition76%16/21
$elem->isa('PPI::Statement::Compound') || $elem->isa('PPI::Statement::Sub') || $elem->isa('PPI::Statement::Given') || $elem->isa('PPI::Statement::When') || $elem->isa('PPI::Statement::End') || $elem->isa('PPI::Statement::Null') || $elem->isa('PPI::Statement::UnmatchedBrace') || _elem_is_try_block($elem)
l!l&&r!l&&!r
6571
$elem->isa("PPI::Statement::Compound") or $elem->isa("PPI::Statement::Sub") or $elem->isa("PPI::Statement::Given") or $elem->isa("PPI::Statement::When") or $elem->isa("PPI::Statement::End") or $elem->isa("PPI::Statement::Null") or $elem->isa("PPI::Statement::UnmatchedBrace")
l!l&&r!l&&!r
6076
$elem->isa("PPI::Statement::Compound") or $elem->isa("PPI::Statement::Sub") or $elem->isa("PPI::Statement::Given") or $elem->isa("PPI::Statement::When") or $elem->isa("PPI::Statement::End") or $elem->isa("PPI::Statement::Null")
l!l&&r!l&&!r
6076
$elem->isa("PPI::Statement::Compound") or $elem->isa("PPI::Statement::Sub") or $elem->isa("PPI::Statement::Given") or $elem->isa("PPI::Statement::When") or $elem->isa("PPI::Statement::End")
l!l&&r!l&&!r
5176
$elem->isa("PPI::Statement::Compound") or $elem->isa("PPI::Statement::Sub") or $elem->isa("PPI::Statement::Given") or $elem->isa("PPI::Statement::When")
l!l&&r!l&&!r
5077
$elem->isa("PPI::Statement::Compound") or $elem->isa("PPI::Statement::Sub") or $elem->isa("PPI::Statement::Given")
l!l&&r!l&&!r
5077
$elem->isa("PPI::Statement::Compound") or $elem->isa("PPI::Statement::Sub")
l!l&&r!l&&!r
5077
MC/DC37%3/8
$elem->isa('PPI::Statement::Compound') || $elem->isa('PPI::Statement::Sub') || $elem->isa('PPI::Statement::Given') || $elem->isa('PPI::Statement::When') || $elem->isa('PPI::Statement::End') || $elem->isa('PPI::Statement::Null') || $elem->isa('PPI::Statement::UnmatchedBrace') || _elem_is_try_block($elem)
$elem->isa("PPI::Statement::Compound") $elem->isa("PPI::Statement::Sub") $elem->isa("PPI::Statement::Given") $elem->isa("PPI::Statement::When") $elem->isa("PPI::Statement::End") $elem->isa("PPI::Statement::Null") $elem->isa("PPI::Statement::UnmatchedBrace") _elem_is_try_block($elem)
Truth table44%4/9
A or B or C or D or E or F or G || H
A= $elem->isa("PPI::Statement::Compound")B= $elem->isa("PPI::Statement::Sub")C= $elem->isa("PPI::Statement::Given")D= $elem->isa("PPI::Statement::When")E= $elem->isa("PPI::Statement::End")F= $elem->isa("PPI::Statement::Null")G= $elem->isa("PPI::Statement::UnmatchedBrace")H= _elem_is_try_block($elem)
ABCDEFGHresult
1XXXXXXX1
01XXXXXX1
001XXXXX1
0001XXXX1
00001XXX1
000001XX1
0000001X1
000000011
000000000
108 || $elem->isa('PPI::Statement::Sub') # nested named sub
109 || $elem->isa('PPI::Statement::Given')
110 || $elem->isa('PPI::Statement::When')
111 || $elem->isa('PPI::Statement::End') # __END__
112 || $elem->isa('PPI::Statement::Null') # ;
113 || $elem->isa('PPI::Statement::UnmatchedBrace') # stray }
114 || _elem_is_try_block($elem)
115 );
116 }
117  
118 40 my %postfix_loops = (while => 1, until => 1);
119  
120 my %prefix_expressions = (do => 1,
121 map => 1,
122 grep => 1,
123 sort => 1,
124  
125 1640 map { $_ => 1, "List::Util::$_" => 1 }
126 qw(
127 reduce any all none notall first
128 pairfirst pairgrep pairmap
129 ),
130  
131 40 map { $_ => 1, "List::Pairwise::$_" => 1 }
132 qw(
133 mapp map_pairwise grepp grep_pairwise
134 firstp first_pairwise lastp last_pairwise
135 ),
136 );
137  
138 # $elem is a PPI::Structure::Block.
139 # return 1 definitely a hash
140 # 0 definitely a block
141 # -1 not certain
142 #
143 # PPI 1.212 tends to be give PPI::Structure::Block for various things which
144 # are actually anon hash constructors and ought to be
145 # PPI::Structure::Constructor. For example,
146 #
147 # return bless { x => 123 };
148 # return \ { x => 123 };
149 #
150 # _block_is_hash_constructor() tries to recognise some of those blocks which
151 # are actually hash constructors, so as not to apply the final semicolon
152 # rule to hash constructors.
153 #
154 40 my %word_is_block = (sub => 1,
155 do => 1,
156 map => 1,
157 grep => 1,
158 sort => 1,
159  
160 # from Try.pm, TryCatch.pm, Try::Tiny prototypes, etc
161 try => 1,
162 catch => 1,
163 finally => 1,
164  
165 # List::Util first() etc are not of interest to
166 # RequireFinalSemicolon but ProhibitDuplicateHashKeys
167 # shares this code so recognise them for it.
168 %prefix_expressions,
169 );
170 109 ▶sub _block_is_hash_constructor {
Subroutine _block_is_hash_constructor: called CC=16 SCAR=28.0
171 109 my ($elem) = @_;
172 ### _block_is_hash_constructor(): ref($elem), "$elem"
173  
174 # if (_block_starts_semi($elem)) {
175 # ### begins with ";", block is correct ...
176 # return 0;
177 # }
178 109 ▶ if (_block_has_multiple_statements($elem)) {
Branch: 1 branch
BranchTrueFalseTotal
if (_block_has_multiple_statements($elem)) 7 102 109
179 ### contains one or more ";", block is correct ...
180 7 return 0;
181 }
182  
183 102 ▶ if (my $prev = $elem->sprevious_sibling) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $prev = $elem->sprevious_sibling) 95 7 102
184 ### prev: ref($prev), "$prev"
185 95 ▶ if ($prev->isa('PPI::Structure::Condition')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($prev->isa("PPI::Structure::Condition")) 6 89 95
186 ### prev condition, block is correct ...
187 6 return 0;
188 }
189 89 ▶ if ($prev->isa('PPI::Token::Cast')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($prev->isa("PPI::Token::Cast")) 9 80 89
190 9 ▶ if ($prev eq '\\') {
Branch: 1 branch
BranchTrueFalseTotal
if ($prev eq '\\') { } 8 1 9
191 ### ref cast, is a hash ...
192 8 return 1;
193 } else {
194 ### other cast, block is correct (or a variable name) ...
195 1 return 0;
196 }
197 }
198 80 ▶ if ($prev->isa('PPI::Token::Operator')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($prev->isa("PPI::Token::Operator")) 0 80 80
199 ### prev operator, is a hash ...
200 0 return 1;
201 }
202 80 ▶ if (! $prev->isa('PPI::Token::Word')) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($prev->isa("PPI::Token::Word")) 2 78 80
203 ### prev not a word, not sure ...
204 2 return -1;
205 }
206  
207 78 ▶ if ($word_is_block{$prev}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($word_is_block{$prev}) 49 29 78
208 # "sub { ... }"
209 # "do { ... }"
210 ### do/sub/map/grep/sort, block is correct ...
211 49 return 0;
212 }
213  
214 29 ▶ if (! ($prev = $prev->sprevious_sibling)) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($prev = $prev->sprevious_sibling) 3 26 29
215 # "bless { ... }"
216 # "return { ... }" etc
217 # ENHANCE-ME: notice List::Util first{} and other prototyped things
218 ### nothing else preceding, likely a hash ...
219 3 return -1;
220 }
221 ### prev prev: "$prev"
222  
223 26 ▶ if ($prev eq 'sub') {
Branch: 1 branch
BranchTrueFalseTotal
if ($prev eq "sub") 25 1 26
224 # "sub foo {}"
225 ### named sub, block is correct ...
226 25 return 0;
227 }
228 # "word bless { ... }"
229 # "word return { ... }" etc
230 ### other word preceding, likely a hash ...
231 1 return -1;
232 }
233  
234 7 ▶ my $parent = $elem->parent || do {
Condition33%1/3
$elem->parent || do { return 0 }
l!l&&r!l&&!r
700
MC/DC0%0/2
$elem->parent || do { return 0 }
$elem->parent do { return 0 }
Truth table33%1/3
A || B
A= $elem->parentB= do { return 0 }
ABresult
1X1
011
000
235 ### umm, at toplevel, is a block ...
236 0 return 0;
237 };
238  
239 7 ▶ if ($parent->isa('PPI::Statement::Compound')
Branch: 1 branch
BranchTrueFalseTotal
if ($parent->isa("PPI::Statement::Compound") and $parent = $parent->parent and $parent->isa("PPI::Structure::List") || $parent->isa("PPI::Structure::Constructor")) 2 5 7
Condition77%7/9
$parent->isa("PPI::Statement::Compound") and $parent = $parent->parent and $parent->isa("PPI::Structure::List") || $parent->isa("PPI::Structure::Constructor")
!ll&&!rl&&r
142
$parent->isa("PPI::Statement::Compound") and $parent = $parent->parent
!ll&&!rl&&r
106
$parent->isa("PPI::Structure::List") or $parent->isa("PPI::Structure::Constructor")
l!l&&r!l&&!r
024
MC/DC50%2/4
$parent->isa("PPI::Statement::Compound") and $parent = $parent->parent and $parent->isa("PPI::Structure::List") || $parent->isa("PPI::Structure::Constructor")
$parent->isa("PPI::Statement::Compound") $parent = $parent->parent $parent->isa("PPI::Structure::List") $parent->isa("PPI::Structure::Constructor")
Truth table60%3/5
A and B and C or D
A= $parent->isa("PPI::Statement::Compound")B= $parent = $parent->parentC= $parent->isa("PPI::Structure::List")D= $parent->isa("PPI::Structure::Constructor")
ABCDresult
0XXX0
10XX0
11000
111X1
11011
240 && ($parent = $parent->parent)
241 && (
242 $parent->isa('PPI::Structure::List')
243 ||
244 $parent->isa('PPI::Structure::Constructor'))) {
245 ### in a list or arrayref, is a hashref ...
246  
247 # This catches
248 # ppidump "[{%args}]"
249 # which comes out (from PPI 1.270) as
250 #
251 # PPI::Structure::Constructor [ ... ]
252 # PPI::Statement::Compound
253 # PPI::Structure::Block { ... }
254 # PPI::Statement
255 # PPI::Token::Symbol '%args'
256 #
257 # It should be like
258 #
259 # PPI::Structure::Constructor [ ... ]
260 # PPI::Statement
261 # PPI::Structure::Constructor { ... }
262 # PPI::Statement::Expression
263 #
264 # which is what ppidump "[{x=>1}]" gives.
265 #
266 # The PPI::Structure::List is for something like
267 # ppidump "func({ %args })"
268 # which in for example PPI 1.220 was PPI::Structure::Block too.
269 # That one is ok in PPI 1.270 (when ready to demand that version).
270 #
271 # The plan would be to remove the whole of this check for
272 # PPI::Statement::Compound if PPI can do the right thing on arrayrefs
273 # too ...
274  
275 2 return 1;
276 }
277  
278 5 return 0;
279 }
280  
281 # $elem is a PPI::Structure::Block
282 # return true if it contains two or more PPI::Statement
283 #
284 109 ▶sub _block_has_multiple_statements {
Subroutine _block_has_multiple_statements: called CC=3 SCAR=11.0
285 109 my ($elem) = @_;
286 109 my $count = 0;
287 109 foreach my $child ($elem->schildren) {
288 108 $count++;
289 108 ▶ if ($count >= 2) { return 1; }
Branch: 1 branch
BranchTrueFalseTotal
if ($count >= 2) 7 101 108
290 }
291 102 return 0;
292 }
293  
294 # $elem is a PPI::Structure::Block
295 # return true if it starts with a ";"
296 #
297 0 ▶sub _block_starts_semi {
Subroutine _block_starts_semi: not called CC=2 SCAR=17.9
298 0 my ($elem) = @_;
299  
300 # note child() not schild() since an initial ";" is not "significant"
301 0 $elem = $elem->child(0);
302 ### first child: $elem && (ref $elem)." $elem"
303  
304 0 $elem = _elem_skip_whitespace_and_comments($elem);
305 0 ▶ return ($elem && $elem->isa('PPI::Statement::Null'));
Condition0%0/3
$elem && $elem->isa('PPI::Statement::Null')
!ll&&!rl&&r
000
MC/DC0%0/2
$elem && $elem->isa('PPI::Statement::Null')
$elem $elem->isa('PPI::Statement::Null')
Truth table0%0/3
A && B
A= $elemB= $elem->isa('PPI::Statement::Null')
ABresult
0X0
100
111
306 }
307  
308 # $elem is a PPI::Element or undef
309 # return the next non-whitespace and non-comment after it
310 0 ▶sub _elem_skip_whitespace_and_comments {
Subroutine _elem_skip_whitespace_and_comments: not called CC=4 SCAR=30.0
311 0 my ($elem) = @_;
312 0 ▶ while ($elem
Branch: 1 branch
BranchTrueFalseTotal
if ($elem and $elem->isa("PPI::Token::Whitespace") || $elem->isa("PPI::Token::Comment")) 0 0 0
Condition0%0/6
$elem and $elem->isa("PPI::Token::Whitespace") || $elem->isa("PPI::Token::Comment")
!ll&&!rl&&r
000
$elem->isa("PPI::Token::Whitespace") or $elem->isa("PPI::Token::Comment")
l!l&&r!l&&!r
000
MC/DC0%0/3
$elem and $elem->isa("PPI::Token::Whitespace") || $elem->isa("PPI::Token::Comment")
$elem $elem->isa("PPI::Token::Whitespace") $elem->isa("PPI::Token::Comment")
Truth table0%0/4
A and B or C
A= $elemB= $elem->isa("PPI::Token::Whitespace")C= $elem->isa("PPI::Token::Comment")
ABCresult
0XX0
1000
11X1
1011
313 && ($elem->isa('PPI::Token::Whitespace')
314 || $elem->isa ('PPI::Token::Comment'))) {
315 0 $elem = $elem->next_sibling;
316 ### next elem: $elem && (ref $elem)." $elem"
317 }
318 0 return $elem;
319 }
320  
321 71 ▶sub _elem_is_semicolon {
Subroutine _elem_is_semicolon: called CC=2 SCAR=7.1
322 71 my ($elem) = @_;
323 71 ▶ return ($elem->isa('PPI::Token::Structure') && $elem eq ';');
Condition66%2/3
$elem->isa('PPI::Token::Structure') && $elem eq ';'
!ll&&!rl&&r
61010
MC/DC50%1/2
$elem->isa('PPI::Token::Structure') && $elem eq ';'
$elem->isa('PPI::Token::Structure') $elem eq ';'
Truth table66%2/3
A && B
A= $elem->isa('PPI::Token::Structure')B= $elem eq ';'
ABresult
0X0
100
111
324 }
325  
326 # $elem is a PPI::Node
327 # return true if any following sibling (not $elem itself) contains a newline
328 46 ▶sub _newline_in_following_sibling {
Subroutine _newline_in_following_sibling: called CC=3 SCAR=11.0
329 46 my ($elem) = @_;
330 46 ▶ while ($elem = $elem->next_sibling) {
Branch: 1 branch
BranchTrueFalseTotal
if ($elem = $elem->next_sibling) 52 25 77
331 52 ▶ if ($elem =~ /\n/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($elem =~ /\n/) 21 31 52
332 21 return 1;
333 }
334 }
335 25 return 0;
336 }
337  
338 # $block is a PPI::Structure::Block
339 # return true if it's "do{}" expression, and not a "do{}while" or "do{}until"
340 # loop
341 61 ▶sub _block_is_expression {
Subroutine _block_is_expression: called CC=6 SCAR=17.9
342 61 my ($elem) = @_;
343 ### _block_is_expression(): "$elem"
344  
345 61 ▶ if (my $next = $elem->snext_sibling) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $next = $elem->snext_sibling) 25 36 61
346 25 ▶ if ($next->isa('PPI::Token::Word')
Branch: 1 branch
BranchTrueFalseTotal
if ($next->isa("PPI::Token::Word") and $postfix_loops{$next}) 6 19 25
Condition100%3/3
$next->isa("PPI::Token::Word") and $postfix_loops{$next}
!ll&&!rl&&r
9106
MC/DC100%2/2
$next->isa("PPI::Token::Word") and $postfix_loops{$next}
$next->isa("PPI::Token::Word") $postfix_loops{$next}
Truth table100%3/3
A and B
A= $next->isa("PPI::Token::Word")B= $postfix_loops{$next}
ABresult
0X0
100
111
347 && $postfix_loops{$next}) {
348 ### {}while or {}until, not an expression
349 6 return 0;
350 }
351 }
352  
353 ### do, map, grep, sort, etc are expressions ..
354 55 my $prev = $elem->sprevious_sibling;
355 return ($prev
356 && $prev->isa('PPI::Token::Word')
357 55 ▶ && $prefix_expressions{$prev});
Condition83%5/6
$prev && $prev->isa('PPI::Token::Word') && $prefix_expressions{$prev}
!ll&&!rl&&r
63613
$prev and $prev->isa("PPI::Token::Word")
!ll&&!rl&&r
6049
MC/DC66%2/3
$prev && $prev->isa('PPI::Token::Word') && $prefix_expressions{$prev}
$prev $prev->isa("PPI::Token::Word") $prefix_expressions{$prev}
Truth table75%3/4
A and B && C
A= $prevB= $prev->isa("PPI::Token::Word")C= $prefix_expressions{$prev}
ABCresult
0XX0
10X0
1100
1111
358 }
359  
360 # Return true if $elem is a "try" block like
361 # Try.pm try { } catch {}
362 # TryCatch.pm try { } catch ($err) {} ... catch {}
363 # Syntax::Feature::Try try { } catch ($err) {} ... catch {} finally {}
364 # The return is true only for the block type "try"s of these three modules.
365 # "try" forms from Try::Tiny and its friends are plain subroutine calls
366 # rather than blocks.
367 #
368 119 ▶sub _elem_is_try_block {
Subroutine _elem_is_try_block: called CC=5 SCAR=16.6
369 119 my ($elem) = @_;
370 119 ▶ return ($elem->isa('PPI::Statement')
Condition75%9/12
$elem->isa('PPI::Statement') && ($elem = $elem->schild(0)) && $elem->isa('PPI::Token::Word') && $elem->content eq 'try' && _elem_has_preceding_use_trycatch($elem)
!ll&&!rl&&r
103511
$elem->isa("PPI::Statement") and $elem = $elem->schild(0) and $elem->isa("PPI::Token::Word") and $elem->content eq "try"
!ll&&!rl&&r
485516
$elem->isa("PPI::Statement") and $elem = $elem->schild(0) and $elem->isa("PPI::Token::Word")
!ll&&!rl&&r
04871
$elem->isa("PPI::Statement") and $elem = $elem->schild(0)
!ll&&!rl&&r
00119
MC/DC60%3/5
$elem->isa('PPI::Statement') && ($elem = $elem->schild(0)) && $elem->isa('PPI::Token::Word') && $elem->content eq 'try' && _elem_has_preceding_use_trycatch($elem)
$elem->isa("PPI::Statement") $elem = $elem->schild(0) $elem->isa("PPI::Token::Word") $elem->content eq "try" _elem_has_preceding_use_trycatch($elem)
Truth table66%4/6
A and B and C and D && E
A= $elem->isa("PPI::Statement")B= $elem = $elem->schild(0)C= $elem->isa("PPI::Token::Word")D= $elem->content eq "try"E= _elem_has_preceding_use_trycatch($elem)
ABCDEresult
0XXXX0
10XXX0
110XX0
1110X0
111100
111111
371 && ($elem = $elem->schild(0))
372 && $elem->isa('PPI::Token::Word')
373 && $elem->content eq 'try'
374 && _elem_has_preceding_use_trycatch($elem));
375 }
376  
377 # return true if $elem is preceded by any of
378 # use Try
379 # use TryCatch
380 # use syntax 'try'
381 16 ▶sub _elem_has_preceding_use_trycatch {
Subroutine _elem_has_preceding_use_trycatch: called CC=1 SCAR=0.0
382 16 my ($elem) = @_;
383 16 my $ret = 0;
384 16 my $document = $elem->top; # PPI::Document, not Perl::Critic::Document
385 674 ▶ $document->find_first (sub {
Subroutine __ANON__: called CC=3 SCAR=11.0
386 674 my ($doc, $e) = @_;
387 # ### comment: (ref $e)." ".$e->content
388 674 ▶ if ($e == $elem) {
Branch: 1 branch
BranchTrueFalseTotal
if ($e == $elem) 16 658 674
389 ### not found before target elem, stop ...
390 16 return undef;
391 }
392 658 ▶ if (_elem_is_use_try($e)) {
Branch: 1 branch
BranchTrueFalseTotal
if (_elem_is_use_try($e)) 11 647 658
393 ### found "use Try" etc, stop ...
394 11 $ret = 1;
395 11 return undef;
396 }
397 647 return 0; # continue
398 16 });
399 16 return $ret;
400 }
401  
402 658 ▶sub _elem_is_use_try {
Subroutine _elem_is_use_try: called CC=6 SCAR=18.0
403 658 my ($elem) = @_;
404 658 ▶ ($elem->isa('PPI::Statement::Include') && $elem->type eq 'use')
Branch: 1 branch
BranchTrueFalseTotal
$elem->isa("PPI::Statement::Include") and $elem->type eq "use" or return 0 16 642 658
Condition66%2/3
$elem->isa("PPI::Statement::Include") and $elem->type eq "use"
!ll&&!rl&&r
642016
MC/DC50%1/2
$elem->isa("PPI::Statement::Include") and $elem->type eq "use"
$elem->isa("PPI::Statement::Include") $elem->type eq "use"
Truth table66%2/3
A and B
A= $elem->isa("PPI::Statement::Include")B= $elem->type eq "use"
ABresult
0X0
100
111
405 or return 0;
406 16 my $module = $elem->module;
407 16 ▶ return ($module eq 'Try'
Condition88%8/9
$module eq 'Try' || $module eq 'TryCatch' || $module eq 'syntax' && _syntax_has_feature($elem, 'try')
l!l&&r!l&&!r
925
$module eq "Try" or $module eq "TryCatch"
l!l&&r!l&&!r
367
$module eq "syntax" and _syntax_has_feature($elem, "try")
!ll&&!rl&&r
502
MC/DC75%3/4
$module eq 'Try' || $module eq 'TryCatch' || $module eq 'syntax' && _syntax_has_feature($elem, 'try')
$module eq "Try" $module eq "TryCatch" $module eq "syntax" _syntax_has_feature($elem, "try")
Truth table80%4/5
A or B || C and D
A= $module eq "Try"B= $module eq "TryCatch"C= $module eq "syntax"D= _syntax_has_feature($elem, "try")
ABCDresult
1XXX1
01XX1
00111
000X0
00100
408 || $module eq 'TryCatch'
409 || ($module eq 'syntax'
410 && _syntax_has_feature($elem,'try')));
411 }
412  
413 # $elem is a PPI::Statement::Include of "use syntax".
414 # Return true if $feature (a string) is among the feature names it imports.
415 6 ▶sub _syntax_has_feature {
Subroutine _syntax_has_feature: called CC=1 SCAR=0.0
416 6 my ($elem, $feature) = @_;
417 6 return ((grep {$_ eq $feature} _syntax_feature_list($elem)) > 0);
418 }
419  
420 # $elem is a PPI::Statement::Include of "use syntax".
421 # Return a list of the feature names it imports.
422 9 ▶sub _syntax_feature_list {
Subroutine _syntax_feature_list: called CC=5 SCAR=16.1
423 9 my ($elem) = @_;
424 ### _syntax_feature_list(): $elem && ref $elem
425 9 my @ret;
426 9 ▶ for ($elem = $elem->schild(2); $elem; $elem = $elem->snext_sibling) {
Branch: 1 branch
BranchTrueFalseTotal
if ($elem) 33 9 42
427 33 ▶ if ($elem->isa('PPI::Token::Word')) {
Branch: 3 branches
BranchTrueFalseTotal
if ($elem->isa('PPI::Token::Word')) { } 5 28 33
elsif ($elem->isa('PPI::Token::QuoteLike::Words')) { } 3 25 28
elsif ($elem->isa('PPI::Token::Quote')) { } 5 20 25
428 5 push @ret, $elem->content;
429 } elsif ($elem->isa('PPI::Token::QuoteLike::Words')) {
430 3 push @ret, $elem->literal;
431 } elsif ($elem->isa('PPI::Token::Quote')) {
432 5 push @ret, $elem->string;
433 }
434 }
435 9 return @ret;
436 }
437  
438 40 1;
439 __END__
« Policy/Documentation/ProhibitAdjacentLinks.pm ↑ Summary Policy/ValuesAndExpressions/ProhibitEmptyCommas.pm »