Policy/CodeLayout/RequireKRBracing.pm

filter: Statementstmt 96.8% 62 / 64 Branchbran 81.2% 26 / 32 Conditioncond 66.6% 14 / 21 MC/DCmcdc 38.4% 5 / 13 Subroutinesub 91.6% 11 / 12 Podpod 100% 4 / 4 totaltotal 83.5% 122 / 146 CC 24 SCAR 24.0
CC
24
Cov
87%
_affected_construct
18.3
violates
10.0
default_themes
2.0
CRAP 11.0
↑ Summary Policy/ControlStructures/ProhibitMultipleSubscripts.pm »
1 package Perl::Critic::Policy::CodeLayout::RequireKRBracing;
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 parent qw[ Perl::Critic::Policy ];
Subroutine BEGIN: called CC=1 SCAR=0.0
5 2 ▶use Perl::Critic::Utils qw[ :severities ];
Subroutine BEGIN: called CC=1 SCAR=0.0
6 2 ▶use List::MoreUtils qw[ any ];
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 2 ▶use constant PBP_PAGE => 9;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 2 my %affected_conditional = map { $_ => 1 } qw( if while unless until );
11 2 my %affected_list_loop = map { $_ => 1 } qw( for foreach );
12 2 my %affected_followup = map { $_ => 1 } qw( else elsif continue );
13  
14 10 ▶sub default_severity { return $SEVERITY_LOW }
Subroutine default_severity: called CC=1 SCAR=0.0
Pod documented
15 0 ▶sub default_themes { return qw[ cosmetic pbp ] }
Subroutine default_themes: not called CC=1 SCAR=6.9
Pod documented
16 13 ▶sub applies_to { return 'PPI::Structure::Block' }
Subroutine applies_to: called CC=1 SCAR=0.0
Pod documented
17  
18 20 ▶sub violates {
Subroutine violates: called CC=10 SCAR=23.0
Pod documented
19 20 my ($self, $elem, $doc) = @_;
20  
21 20 my ($keyword, $parens) = _affected_construct($elem);
22 20 ▶ return if any { not defined } $keyword, $parens; # no affected construct detected
Subroutine __ANON__: called CC=1 SCAR=0.0
Branch: 1 branch
BranchTrueFalseTotal
if &any(sub { not defined $_; } , $keyword, $parens) 6 14 20
23  
24 14 my $inter_whitespace = $elem->previous_sibling();
25 14 ▶ if (not $inter_whitespace->isa('PPI::Token::Whitespace')) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($inter_whitespace->isa("PPI::Token::Whitespace")) 2 12 14
26 2 return $self->violation("No whitespace between closing '$keyword' parenthesis and opening brace",
27 PBP_PAGE, $elem);
28 }
29  
30 12 my $pre_whitespace = $parens->previous_sibling();
31 12 ▶ if (not $pre_whitespace->isa('PPI::Token::Whitespace')) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($pre_whitespace->isa("PPI::Token::Whitespace")) 3 9 12
32 3 return $self->violation("No whitespace before opening '$keyword' parenthesis", PBP_PAGE, $parens);
33 }
34  
35 9 ▶ if (index($inter_whitespace->content, "\n") != -1) {
Branch: 1 branch
BranchTrueFalseTotal
if (index($inter_whitespace->content, "\n") != -1) 2 7 9
36 2 return $self->violation("Opening brace of '$keyword' block is not on the same line", PBP_PAGE, $elem);
37 }
38  
39 7 my $next_keyword = $elem->snext_sibling(); # else / elsif / continue
40 7 ▶ if ( ref $next_keyword
Branch: 1 branch
BranchTrueFalseTotal
if (ref $next_keyword and $next_keyword->isa("PPI::Token::Word") and $affected_followup{$next_keyword}) 5 2 7
Condition66%4/6
ref $next_keyword and $next_keyword->isa("PPI::Token::Word") and $affected_followup{$next_keyword}
!ll&&!rl&&r
205
ref $next_keyword and $next_keyword->isa("PPI::Token::Word")
!ll&&!rl&&r
205
MC/DC33%1/3
ref $next_keyword and $next_keyword->isa("PPI::Token::Word") and $affected_followup{$next_keyword}
ref $next_keyword $next_keyword->isa("PPI::Token::Word") $affected_followup{$next_keyword}
Truth table50%2/4
A and B and C
A= ref $next_keywordB= $next_keyword->isa("PPI::Token::Word")C= $affected_followup{$next_keyword}
ABCresult
0XX0
10X0
1100
1111
41 and $next_keyword->isa('PPI::Token::Word')
42 and $affected_followup{$next_keyword})
43 {
44 5 my $follow_whitespace = $elem->next_sibling();
45  
46 5 my $no_whitespace = "No whitespace between closing brace and '$next_keyword'";
47 5 ▶ return $self->violation($no_whitespace, PBP_PAGE, $next_keyword)
Branch: 1 branch
BranchTrueFalseTotal
unless $follow_whitespace->isa("PPI::Token::Whitespace") 1 4 5
48 if not $follow_whitespace->isa('PPI::Token::Whitespace');
49  
50 4 my $no_newline = "'$next_keyword' is on the same line as closing brace";
51 4 ▶ return $self->violation($no_newline, PBP_PAGE, $next_keyword)
Branch: 1 branch
BranchTrueFalseTotal
if index($follow_whitespace->content, "\n") == -1 2 2 4
52 if index($follow_whitespace->content, "\n") == -1;
53 }
54  
55 4 return;
56 }
57  
58 20 ▶sub _affected_construct {
Subroutine _affected_construct: called CC=15 SCAR=29.1
59 20 my ($elem) = @_;
60  
61 20 my $parens = $elem->sprevious_sibling();
62 20 ▶ return if not ref $parens;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $parens 0 20 20
63  
64 20 my $is_conditional = $parens->isa('PPI::Structure::Condition'); # if / while / unless / until
65 20 my $is_list_loop = $parens->isa('PPI::Structure::List'); # for / foreach
66 20 ▶ return if not($is_conditional or $is_list_loop);
Branch: 1 branch
BranchTrueFalseTotal
unless $is_conditional or $is_list_loop 5 15 20
Condition100%3/3
$is_conditional or $is_list_loop
l!l&&r!l&&!r
965
MC/DC100%2/2
$is_conditional or $is_list_loop
$is_conditional $is_list_loop
Truth table100%3/3
A or B
A= $is_conditionalB= $is_list_loop
ABresult
1X1
011
000
67  
68 15 my $keyword = $parens->sprevious_sibling();
69 15 ▶ if ($keyword->isa('PPI::Token::Word')) { # a conditional or foreach without an explicit iterator
Branch: 2 branches
BranchTrueFalseTotal
if ($keyword->isa('PPI::Token::Word')) { } 13 2 15
elsif ($is_list_loop and $keyword->isa('PPI::Token::Symbol')) { } 2 0 2
70 13 ▶ return if $is_conditional and not $affected_conditional{$keyword};
Branch: 1 branch
BranchTrueFalseTotal
if $is_conditional and not $affected_conditional{$keyword} 1 12 13
Condition100%3/3
$is_conditional and not $affected_conditional{$keyword}
!ll&&!rl&&r
481
MC/DC100%2/2
$is_conditional and not $affected_conditional{$keyword}
$is_conditional not $affected_conditional{$keyword}
Truth table100%3/3
A and not(B)
A= $is_conditionalB= not $affected_conditional{$keyword}
ABresult
0X0
100
111
71 12 ▶ return if $is_list_loop and not $affected_list_loop{$keyword};
Branch: 1 branch
BranchTrueFalseTotal
if $is_list_loop and not $affected_list_loop{$keyword} 0 12 12
Condition66%2/3
$is_list_loop and not $affected_list_loop{$keyword}
!ll&&!rl&&r
840
MC/DC0%0/2
$is_list_loop and not $affected_list_loop{$keyword}
$is_list_loop not $affected_list_loop{$keyword}
Truth table66%2/3
A and not(B)
A= $is_list_loopB= not $affected_list_loop{$keyword}
ABresult
0X0
100
111
72 }
73 ▶ elsif ($is_list_loop and $keyword->isa('PPI::Token::Symbol')) { # foreach with an explicit iterator
Condition33%1/3
$is_list_loop and $keyword->isa("PPI::Token::Symbol")
!ll&&!rl&&r
002
MC/DC0%0/2
$is_list_loop and $keyword->isa("PPI::Token::Symbol")
$is_list_loop $keyword->isa("PPI::Token::Symbol")
Truth table33%1/3
A and B
A= $is_list_loopB= $keyword->isa("PPI::Token::Symbol")
ABresult
0X0
100
111
74 2 $keyword = $keyword->sprevious_sibling(); # go back to either 'my' or 'for'/'foreach'
75 2 ▶ $keyword = $keyword->sprevious_sibling # go back one more token in case of 'my'
Branch: 1 branch
BranchTrueFalseTotal
if $keyword->isa("PPI::Token::Word") and $keyword->content eq "my" 2 0 2
Condition33%1/3
$keyword->isa("PPI::Token::Word") and $keyword->content eq "my"
!ll&&!rl&&r
002
MC/DC0%0/2
$keyword->isa("PPI::Token::Word") and $keyword->content eq "my"
$keyword->isa("PPI::Token::Word") $keyword->content eq "my"
Truth table33%1/3
A and B
A= $keyword->isa("PPI::Token::Word")B= $keyword->content eq "my"
ABresult
0X0
100
111
76 if $keyword->isa('PPI::Token::Word')
77 and $keyword->content eq 'my';
78  
79 # Should have found the actual keyword by now
80 2 ▶ return if not $keyword->isa('PPI::Token::Word');
Branch: 1 branch
BranchTrueFalseTotal
unless $keyword->isa("PPI::Token::Word") 0 2 2
81 2 ▶ return if not $affected_list_loop{$keyword};
Branch: 1 branch
BranchTrueFalseTotal
unless $affected_list_loop{$keyword} 0 2 2
82 }
83 else {
84 0 return;
85 }
86  
87 14 return ($keyword, $parens);
88 }
89  
90 2 1;
91 __END__
↑ Summary Policy/ControlStructures/ProhibitMultipleSubscripts.pm »